◐ Shell
clean mode source ↗

impl basicsize by youknowone · Pull Request #6557 · RustPython/RustPython

1341-1352: LGTM! Correct gating logic for dict addition.

The may_add_dict guard properly prevents adding a dict when the primary base class already has one (checking !base.slots.flags.has_feature(PyTypeFlags::HAS_DICT)). The updated condition on line 1350 correctly combines:

  1. No __slots__ defined AND base doesn't have dict (automatic dict), OR
  2. __dict__ explicitly in __slots__

This matches CPython's behavior of checking tp_dictoffset == 0 on the base.


1354-1360: LGTM! Proper itemsize inheritance from base type.

Line 1358 correctly propagates itemsize from the base type during heap type creation. This is essential for correctly handling subclasses of variable-length types (e.g., tuple, list, bytes) where itemsize determines the per-element size.


2019-2022: LGTM! Clean helper for layout comparison.

The shape_differs function encapsulates layout comparison logic, checking both __basicsize__() (instance size) and itemsize (per-element size for variable-length types). This improves code readability and maintainability.


2024-2032: LGTM! Refactored to use shape_differs helper.

Line 2031 now uses the shape_differs helper for clearer intent. The logic remains equivalent: if the derived type has a different layout than its base, it becomes the solid base; otherwise, continue up the chain.


785-787: Remove unnecessary concern about const removal—the change is correct and has no breaking callers.

The removal of const from __basicsize__ is correct. The only usage in the codebase (line 2021 in shape_differs) is a normal runtime call. The #[pygetset] decorator indicates this is a Python property getter, which cannot be const. No existing code depends on this being a const fn.