Skip to content

Commit be42c2b

Browse files
committed
feat(vm/slot): implement Py_TPFLAGS_MANAGED_DICT for class objects
1 parent 8b6c78c commit be42c2b

File tree

7 files changed

+21
-15
lines changed

7 files changed

+21
-15
lines changed

Lib/test/test_array.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1285,8 +1285,6 @@ def check_overflow(self, lower, upper):
12851285
self.assertRaises(OverflowError, array.array, self.typecode, [upper+1])
12861286
self.assertRaises(OverflowError, a.__setitem__, 0, upper+1)
12871287

1288-
# TODO: RUSTPYTHON
1289-
@unittest.expectedFailure
12901288
def test_subclassing(self):
12911289
typecode = self.typecode
12921290
class ExaggeratingArray(array.array):

Lib/test/test_class.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -851,7 +851,11 @@ def __init__(self, obj):
851851
except ImportError:
852852
has_inline_values = None
853853

854-
Py_TPFLAGS_MANAGED_DICT = (1 << 2)
854+
855+
Py_TPFLAGS_MANAGED_DICT = (1 << 4)
856+
857+
class NoManagedDict:
858+
__slots__ = ('a',)
855859

856860
class Plain:
857861
pass
@@ -865,11 +869,18 @@ def __init__(self):
865869
self.c = 3
866870
self.d = 4
867871

868-
869-
class TestInlineValues(unittest.TestCase):
872+
class TestNoManagedValues(unittest.TestCase):
873+
def test_flags(self):
874+
self.assertEqual(NoManagedDict.__flags__ & Py_TPFLAGS_MANAGED_DICT, 0)
870875

871876
# TODO: RUSTPYTHON
872877
@unittest.expectedFailure
878+
def test_no_inline_values_for_slots_class(self):
879+
c = NoManagedDict()
880+
self.assertFalse(has_inline_values(c))
881+
882+
class TestInlineValues(unittest.TestCase):
883+
873884
def test_flags(self):
874885
self.assertEqual(Plain.__flags__ & Py_TPFLAGS_MANAGED_DICT, Py_TPFLAGS_MANAGED_DICT)
875886
self.assertEqual(WithAttrs.__flags__ & Py_TPFLAGS_MANAGED_DICT, Py_TPFLAGS_MANAGED_DICT)

Lib/test/test_importlib/test_metadata_api.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,6 @@ def test_entry_points_missing_name(self):
139139
def test_entry_points_missing_group(self):
140140
assert entry_points(group='missing') == ()
141141

142-
# TODO: RUSTPYTHON
143-
@unittest.expectedFailure
144142
def test_entry_points_allows_no_attributes(self):
145143
ep = entry_points().select(group='entries', name='main')
146144
with self.assertRaises(AttributeError):

Lib/test/test_property.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,8 +242,6 @@ class PropertySubSlots(property):
242242

243243
class PropertySubclassTests(unittest.TestCase):
244244

245-
# TODO: RUSTPYTHON
246-
@unittest.expectedFailure
247245
def test_slots_docstring_copy_exception(self):
248246
try:
249247
class Foo(object):

Lib/test/test_weakref.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,8 +1068,6 @@ class MyRef(weakref.ref):
10681068
self.assertIn(r1, refs)
10691069
self.assertIn(r2, refs)
10701070

1071-
# TODO: RUSTPYTHON
1072-
@unittest.expectedFailure
10731071
def test_subclass_refs_with_slots(self):
10741072
class MyRef(weakref.ref):
10751073
__slots__ = "slot1", "slot2"

vm/src/builtins/type.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1019,9 +1019,6 @@ impl Constructor for PyType {
10191019
.into()
10201020
});
10211021

1022-
// TODO: Flags is currently initialized with HAS_DICT. Should be
1023-
// updated when __slots__ are supported (toggling the flag off if
1024-
// a class has __slots__ defined).
10251022
let heaptype_slots: Option<PyTupleTyped<PyStrRef>> =
10261023
if let Some(x) = attributes.get(identifier!(vm, __slots__)) {
10271024
Some(if x.to_owned().class().is(vm.ctx.types.str_type) {
@@ -1053,7 +1050,12 @@ impl Constructor for PyType {
10531050
let heaptype_member_count = heaptype_slots.as_ref().map(|x| x.len()).unwrap_or(0);
10541051
let member_count: usize = base_member_count + heaptype_member_count;
10551052

1056-
let flags = PyTypeFlags::heap_type_flags() | PyTypeFlags::HAS_DICT;
1053+
let mut flags = PyTypeFlags::heap_type_flags();
1054+
// Only add HAS_DICT and MANAGED_DICT if __slots__ is not defined.
1055+
if heaptype_slots.is_none() {
1056+
flags |= PyTypeFlags::HAS_DICT | PyTypeFlags::MANAGED_DICT;
1057+
}
1058+
10571059
let (slots, heaptype_ext) = {
10581060
let slots = PyTypeSlots {
10591061
flags,

vm/src/types/slot.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ bitflags! {
122122
#[derive(Copy, Clone, Debug, PartialEq)]
123123
#[non_exhaustive]
124124
pub struct PyTypeFlags: u64 {
125+
const MANAGED_DICT = 1 << 4;
125126
const IMMUTABLETYPE = 1 << 8;
126127
const HEAPTYPE = 1 << 9;
127128
const BASETYPE = 1 << 10;

0 commit comments

Comments
 (0)