|
1 | 1 | import unittest |
2 | 2 | import gc |
| 3 | +from sys import getrefcount |
3 | 4 | from test.support import import_helper |
4 | 5 |
|
5 | 6 | _testcapi = import_helper.import_module('_testcapi') |
6 | 7 | _testlimitedcapi = import_helper.import_module('_testlimitedcapi') |
| 8 | +_testinternalcapi = import_helper.import_module('_testinternalcapi') |
7 | 9 |
|
8 | 10 | NULL = None |
9 | 11 | PY_SSIZE_T_MIN = _testcapi.PY_SSIZE_T_MIN |
@@ -118,6 +120,41 @@ def test_tuple_pack(self): |
118 | 120 | # CRASHES pack(1, NULL) |
119 | 121 | # CRASHES pack(2, [1]) |
120 | 122 |
|
| 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 | + |
| 148 | + def test_tuple_from_pair(self): |
| 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) |
| 157 | + |
121 | 158 | def test_tuple_size(self): |
122 | 159 | # Test PyTuple_Size() |
123 | 160 | size = _testlimitedcapi.tuple_size |
|
0 commit comments