Check whether two types differ in shape. Mirrors the shape_differs() function in typeobject.c in CPython.
(t1: type[object], t2: type[object])
| 515 | |
| 516 | |
| 517 | def _shape_differs(t1: type[object], t2: type[object]) -> bool: |
| 518 | """Check whether two types differ in shape. |
| 519 | |
| 520 | Mirrors the shape_differs() function in typeobject.c in CPython.""" |
| 521 | if sys.version_info >= (3, 12): |
| 522 | return t1.__basicsize__ != t2.__basicsize__ or t1.__itemsize__ != t2.__itemsize__ |
| 523 | else: |
| 524 | # CPython had more complicated logic before 3.12: |
| 525 | # https://github.com/python/cpython/blob/f3c6f882cddc8dc30320d2e73edf019e201394fc/Objects/typeobject.c#L2224 |
| 526 | # We attempt to mirror it here well enough to support the most common cases. |
| 527 | if t1.__itemsize__ or t2.__itemsize__: |
| 528 | return t1.__basicsize__ != t2.__basicsize__ or t1.__itemsize__ != t2.__itemsize__ |
| 529 | t_size = t1.__basicsize__ |
| 530 | if not t2.__weakrefoffset__ and t1.__weakrefoffset__ + SIZEOF_PYOBJECT == t_size: |
| 531 | t_size -= SIZEOF_PYOBJECT |
| 532 | if not t2.__dictoffset__ and t1.__dictoffset__ + SIZEOF_PYOBJECT == t_size: |
| 533 | t_size -= SIZEOF_PYOBJECT |
| 534 | if not t2.__weakrefoffset__ and t2.__weakrefoffset__ == t_size: |
| 535 | t_size -= SIZEOF_PYOBJECT |
| 536 | return t_size != t2.__basicsize__ |
| 537 | |
| 538 | |
| 539 | def _is_disjoint_base(typ: type[object]) -> bool: |
no outgoing calls
no test coverage detected
searching dependent graphs…