@@ -120,36 +120,40 @@ def test_tuple_pack(self):
120120 # CRASHES pack(1, NULL)
121121 # CRASHES pack(2, [1])
122122
123+ def check_tuple_from_pair (self , from_pair ):
124+ self .assertEqual (type (from_pair (1 , 2 )), tuple )
125+ self .assertEqual (from_pair (1 , 145325 ), (1 , 145325 ))
126+ self .assertEqual (from_pair (None , None ), (None , None ))
127+ self .assertEqual (from_pair (True , False ), (True , False ))
128+
129+ # user class supports gc
130+ class Temp :
131+ pass
132+ temp = Temp ()
133+ temp_rc = getrefcount (temp )
134+ self .assertEqual (from_pair (temp , temp ), (temp , temp ))
135+ self .assertEqual (getrefcount (temp ), temp_rc )
136+
137+ self ._not_tracked (from_pair (1 , 2 ))
138+ self ._not_tracked (from_pair (None , None ))
139+ self ._not_tracked (from_pair (True , False ))
140+ self ._tracked (from_pair (temp , (1 , 2 )))
141+ self ._tracked (from_pair (temp , 1 ))
142+ self ._tracked (from_pair ([], {}))
143+
144+ self .assertRaises (TypeError , from_pair , 1 , 2 , 3 )
145+ self .assertRaises (TypeError , from_pair , 1 )
146+ self .assertRaises (TypeError , from_pair )
147+
123148 def test_tuple_from_pair (self ):
124- # Test _PyTuple_FromPair, _PyTuple_FromPairSteal
125- ctors = (("_PyTuple_FromPair" , _testinternalcapi ._tuple_from_pair ),
126- ("_PyTuple_FromPairSteal" , _testinternalcapi ._tuple_from_pair_steal ))
127-
128- for name , ctor in ctors :
129- with self .subTest (name ):
130- self .assertEqual (type (ctor (1 , 2 )), tuple )
131- self .assertEqual (ctor (1 , 145325 ), (1 , 145325 ))
132- self .assertEqual (ctor (None , None ), (None , None ))
133- self .assertEqual (ctor (True , False ), (True , False ))
134-
135- # user class supports gc
136- class Temp :
137- pass
138- temp = Temp ()
139- temp_rc = getrefcount (temp )
140- self .assertEqual (ctor (temp , temp ), (temp , temp ))
141- self .assertEqual (getrefcount (temp ), temp_rc )
142-
143- self .assertRaises (TypeError , ctor , 1 , 2 , 3 )
144- self .assertRaises (TypeError , ctor , 1 )
145- self .assertRaises (TypeError , ctor )
146-
147- self .assertFalse (gc .is_tracked (ctor (1 , 2 )))
148- self .assertFalse (gc .is_tracked (ctor (None , None )))
149- self .assertFalse (gc .is_tracked (ctor (True , False )))
150- self .assertTrue (gc .is_tracked (ctor (temp , (1 , 2 ))))
151- self .assertTrue (gc .is_tracked (ctor (temp , 1 )))
152- self .assertTrue (gc .is_tracked (ctor ([], {})))
149+ # Test _PyTuple_FromPair()
150+ from_pair = _testinternalcapi .tuple_from_pair
151+ self .check_tuple_from_pair (from_pair )
152+
153+ def test_tuple_from_pair_steal (self ):
154+ # Test _PyTuple_FromPairSteal()
155+ from_pair = _testinternalcapi .tuple_from_pair_steal
156+ self .check_tuple_from_pair (from_pair )
153157
154158 def test_tuple_size (self ):
155159 # Test PyTuple_Size()
0 commit comments