File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -509,8 +509,25 @@ impl PyBaseObject {
509509 if both_mutable || both_module {
510510 let has_dict =
511511 |typ : & Py < PyType > | typ. slots . flags . has_feature ( PyTypeFlags :: HAS_DICT ) ;
512+ // Compare slots tuples
513+ let slots_equal = match (
514+ current_cls
515+ . heaptype_ext
516+ . as_ref ( )
517+ . and_then ( |e| e. slots . as_ref ( ) ) ,
518+ cls. heaptype_ext . as_ref ( ) . and_then ( |e| e. slots . as_ref ( ) ) ,
519+ ) {
520+ ( Some ( a) , Some ( b) ) => {
521+ a. len ( ) == b. len ( )
522+ && a. iter ( )
523+ . zip ( b. iter ( ) )
524+ . all ( |( x, y) | x. as_str ( ) == y. as_str ( ) )
525+ }
526+ ( None , None ) => true ,
527+ _ => false ,
528+ } ;
512529 if current_cls. slots . basicsize != cls. slots . basicsize
513- || current_cls . slots . member_count != cls . slots . member_count
530+ || !slots_equal
514531 || has_dict ( current_cls) != has_dict ( & cls)
515532 {
516533 return Err ( vm. new_type_error ( format ! (
Original file line number Diff line number Diff line change @@ -260,6 +260,36 @@ def __init__(self):
260260)
261261
262262
263+ # Test: same slot count but different slot names should fail
264+ class SlotX :
265+ __slots__ = ("x" ,)
266+
267+
268+ class SlotY :
269+ __slots__ = ("y" ,)
270+
271+
272+ slot_obj = SlotX ()
273+ with assert_raises (TypeError ) as cm :
274+ slot_obj .__class__ = SlotY
275+ assert "__class__ assignment: 'SlotY' object layout differs from 'SlotX'" in str (
276+ cm .exception
277+ )
278+
279+
280+ # Test: same slots should succeed
281+ class SlotA :
282+ __slots__ = ("a" ,)
283+
284+
285+ class SlotA2 :
286+ __slots__ = ("a" ,)
287+
288+
289+ slot_a = SlotA ()
290+ slot_a .__class__ = SlotA2 # Should work
291+
292+
263293assert type (Exception .args ).__name__ == "getset_descriptor"
264294assert type (None ).__bool__ (None ) is False
265295
You can’t perform that action at this time.
0 commit comments