|
| 1 | +import enum |
| 2 | +import unittest |
| 3 | +from test.support import import_helper |
| 4 | + |
| 5 | +_testlimitedcapi = import_helper.import_module('_testlimitedcapi') |
| 6 | + |
| 7 | + |
| 8 | +class Constant(enum.IntEnum): |
| 9 | + Py_CONSTANT_NONE = 0 |
| 10 | + Py_CONSTANT_FALSE = 1 |
| 11 | + Py_CONSTANT_TRUE = 2 |
| 12 | + Py_CONSTANT_ELLIPSIS = 3 |
| 13 | + Py_CONSTANT_NOT_IMPLEMENTED = 4 |
| 14 | + Py_CONSTANT_ZERO = 5 |
| 15 | + Py_CONSTANT_ONE = 6 |
| 16 | + Py_CONSTANT_EMPTY_STR = 7 |
| 17 | + Py_CONSTANT_EMPTY_BYTES = 8 |
| 18 | + Py_CONSTANT_EMPTY_TUPLE = 9 |
| 19 | + |
| 20 | + INVALID_CONSTANT = Py_CONSTANT_EMPTY_TUPLE + 1 |
| 21 | + |
| 22 | + |
| 23 | +class CAPITest(unittest.TestCase): |
| 24 | + def check_get_constant(self, get_constant): |
| 25 | + self.assertIs(get_constant(Constant.Py_CONSTANT_NONE), None) |
| 26 | + self.assertIs(get_constant(Constant.Py_CONSTANT_FALSE), False) |
| 27 | + self.assertIs(get_constant(Constant.Py_CONSTANT_TRUE), True) |
| 28 | + self.assertIs(get_constant(Constant.Py_CONSTANT_ELLIPSIS), Ellipsis) |
| 29 | + self.assertIs(get_constant(Constant.Py_CONSTANT_NOT_IMPLEMENTED), NotImplemented) |
| 30 | + |
| 31 | + for constant_id, constant_type, value in ( |
| 32 | + (Constant.Py_CONSTANT_ZERO, int, 0), |
| 33 | + (Constant.Py_CONSTANT_ONE, int, 1), |
| 34 | + (Constant.Py_CONSTANT_EMPTY_STR, str, ""), |
| 35 | + (Constant.Py_CONSTANT_EMPTY_BYTES, bytes, b""), |
| 36 | + (Constant.Py_CONSTANT_EMPTY_TUPLE, tuple, ()), |
| 37 | + ): |
| 38 | + with self.subTest(constant_id=constant_id): |
| 39 | + obj = get_constant(constant_id) |
| 40 | + self.assertEqual(type(obj), constant_type, obj) |
| 41 | + self.assertEqual(obj, value) |
| 42 | + |
| 43 | + with self.assertRaises(SystemError): |
| 44 | + get_constant(Constant.INVALID_CONSTANT) |
| 45 | + |
| 46 | + def test_get_constant(self): |
| 47 | + self.check_get_constant(_testlimitedcapi.get_constant) |
| 48 | + |
| 49 | + def test_get_constant_borrowed(self): |
| 50 | + self.check_get_constant(_testlimitedcapi.get_constant_borrowed) |
| 51 | + |
| 52 | + |
| 53 | +if __name__ == "__main__": |
| 54 | + unittest.main() |
0 commit comments