@@ -1329,18 +1329,17 @@ class D(object):
13291329 self .assertNotHasAttr (a , "__weakref__" )
13301330 a .foo = 42
13311331 self .assertEqual (a .__dict__ , {"foo" : 42 })
1332+ with self .assertRaises (TypeError ):
1333+ weakref .ref (a )
13321334
13331335 class W (object ):
13341336 __slots__ = ["__weakref__" ]
13351337 a = W ()
13361338 self .assertHasAttr (a , "__weakref__" )
13371339 self .assertNotHasAttr (a , "__dict__" )
1338- try :
1340+ with self . assertRaises ( AttributeError ) :
13391341 a .foo = 42
1340- except AttributeError :
1341- pass
1342- else :
1343- self .fail ("shouldn't be allowed to set a.foo" )
1342+ self .assertIs (weakref .ref (a )(), a )
13441343
13451344 class C1 (W , D ):
13461345 __slots__ = []
@@ -1349,6 +1348,7 @@ class C1(W, D):
13491348 self .assertHasAttr (a , "__weakref__" )
13501349 a .foo = 42
13511350 self .assertEqual (a .__dict__ , {"foo" : 42 })
1351+ self .assertIs (weakref .ref (a )(), a )
13521352
13531353 class C2 (D , W ):
13541354 __slots__ = []
@@ -1357,6 +1357,77 @@ class C2(D, W):
13571357 self .assertHasAttr (a , "__weakref__" )
13581358 a .foo = 42
13591359 self .assertEqual (a .__dict__ , {"foo" : 42 })
1360+ self .assertIs (weakref .ref (a )(), a )
1361+
1362+ @unittest .skipIf (_testcapi is None , 'need the _testcapi module' )
1363+ def test_slots_special_before_items (self ):
1364+ class D (_testcapi .HeapCCollection ):
1365+ __slots__ = ["__dict__" ]
1366+ a = D (1 , 2 , 3 )
1367+ self .assertHasAttr (a , "__dict__" )
1368+ self .assertNotHasAttr (a , "__weakref__" )
1369+ a .foo = 42
1370+ self .assertEqual (a .__dict__ , {"foo" : 42 })
1371+ with self .assertRaises (TypeError ):
1372+ weakref .ref (a )
1373+ del a .__dict__
1374+ self .assertNotHasAttr (a , "foo" )
1375+ self .assertEqual (a .__dict__ , {})
1376+ self .assertEqual (list (a ), [1 , 2 , 3 ])
1377+
1378+ class W (_testcapi .HeapCCollection ):
1379+ __slots__ = ["__weakref__" ]
1380+ a = W (1 , 2 , 3 )
1381+ self .assertHasAttr (a , "__weakref__" )
1382+ self .assertNotHasAttr (a , "__dict__" )
1383+ with self .assertRaises (AttributeError ):
1384+ a .foo = 42
1385+ self .assertIs (weakref .ref (a )(), a )
1386+
1387+ with self .assertRaises (TypeError ):
1388+ class X (_testcapi .HeapCCollection ):
1389+ __slots__ = ['x' ]
1390+
1391+ with self .assertRaises (TypeError ):
1392+ class X (_testcapi .HeapCCollection ):
1393+ __slots__ = ['__dict__' , 'x' ]
1394+
1395+ @support .subTests (('base' , 'arg' ), [
1396+ (tuple , (1 , 2 , 3 )),
1397+ (int , 9876543210 ** 2 ),
1398+ (bytes , b'ab' ),
1399+ ])
1400+ def test_slots_special_after_items (self , base , arg ):
1401+ class D (base ):
1402+ __slots__ = ["__dict__" ]
1403+ a = D (arg )
1404+ self .assertHasAttr (a , "__dict__" )
1405+ self .assertNotHasAttr (a , "__weakref__" )
1406+ a .foo = 42
1407+ self .assertEqual (a .__dict__ , {"foo" : 42 })
1408+ with self .assertRaises (TypeError ):
1409+ weakref .ref (a )
1410+ del a .__dict__
1411+ self .assertNotHasAttr (a , "foo" )
1412+ self .assertEqual (a .__dict__ , {})
1413+ self .assertEqual (a , base (arg ))
1414+
1415+ class W (base ):
1416+ __slots__ = ["__weakref__" ]
1417+ a = W (arg )
1418+ self .assertHasAttr (a , "__weakref__" )
1419+ self .assertNotHasAttr (a , "__dict__" )
1420+ with self .assertRaises (AttributeError ):
1421+ a .foo = 42
1422+ self .assertIs (weakref .ref (a )(), a )
1423+ self .assertEqual (a , base (arg ))
1424+
1425+ with self .assertRaises (TypeError ):
1426+ class X (base ):
1427+ __slots__ = ['x' ]
1428+ with self .assertRaises (TypeError ):
1429+ class X (base ):
1430+ __slots__ = ['__dict__' , 'x' ]
13601431
13611432 def test_slots_special2 (self ):
13621433 # Testing __qualname__ and __classcell__ in __slots__
0 commit comments