@@ -14,6 +14,12 @@ class TupleSubclass(tuple):
1414
1515
1616class CAPITest (unittest .TestCase ):
17+ def _not_tracked (self , t ):
18+ self .assertFalse (gc .is_tracked (t ), t )
19+
20+ def _tracked (self , t ):
21+ self .assertTrue (gc .is_tracked (t ), t )
22+
1723 def test_check (self ):
1824 # Test PyTuple_Check()
1925 check = _testlimitedcapi .tuple_check
@@ -52,16 +58,47 @@ def test_tuple_new(self):
5258 self .assertEqual (tup1 , ())
5359 self .assertEqual (size (tup1 ), 0 )
5460 self .assertIs (type (tup1 ), tuple )
61+ self ._not_tracked (tup1 )
62+
5563 tup2 = tuple_new (1 )
5664 self .assertIs (type (tup2 ), tuple )
5765 self .assertEqual (size (tup2 ), 1 )
5866 self .assertIsNot (tup2 , tup1 )
5967 self .assertTrue (checknull (tup2 , 0 ))
68+ self ._tracked (tup2 )
6069
6170 self .assertRaises (SystemError , tuple_new , - 1 )
6271 self .assertRaises (SystemError , tuple_new , PY_SSIZE_T_MIN )
6372 self .assertRaises (MemoryError , tuple_new , PY_SSIZE_T_MAX )
6473
74+ def test_tuple_fromarray (self ):
75+ # Test PyTuple_FromArray()
76+ tuple_fromarray = _testcapi .tuple_fromarray
77+
78+ tup = tuple ([i ] for i in range (5 ))
79+ copy = tuple_fromarray (tup )
80+ self .assertEqual (copy , tup )
81+ self ._tracked (copy )
82+
83+ tup = tuple (42 ** i for i in range (5 ))
84+ copy = tuple_fromarray (tup )
85+ self .assertEqual (copy , tup )
86+ self ._not_tracked (copy )
87+
88+ tup = ()
89+ copy = tuple_fromarray (tup )
90+ self .assertIs (copy , tup )
91+
92+ copy = tuple_fromarray (NULL , 0 )
93+ self .assertIs (copy , ())
94+
95+ with self .assertRaises (SystemError ):
96+ tuple_fromarray (NULL , - 1 )
97+ with self .assertRaises (SystemError ):
98+ tuple_fromarray (NULL , PY_SSIZE_T_MIN )
99+ with self .assertRaises (MemoryError ):
100+ tuple_fromarray (NULL , PY_SSIZE_T_MAX )
101+
65102 def test_tuple_pack (self ):
66103 # Test PyTuple_Pack()
67104 pack = _testlimitedcapi .tuple_pack
@@ -70,6 +107,10 @@ def test_tuple_pack(self):
70107 self .assertEqual (pack (1 , [1 ]), ([1 ],))
71108 self .assertEqual (pack (2 , [1 ], [2 ]), ([1 ], [2 ]))
72109
110+ self ._tracked (pack (1 , [1 ]))
111+ self ._tracked (pack (2 , [1 ], b'abc' ))
112+ self ._not_tracked (pack (2 , 42 , b'abc' ))
113+
73114 self .assertRaises (SystemError , pack , PY_SSIZE_T_MIN )
74115 self .assertRaises (SystemError , pack , - 1 )
75116 self .assertRaises (MemoryError , pack , PY_SSIZE_T_MAX )
0 commit comments