Debian Codegraph @

Lib/test/test_capi/test_object.py

raw
1 import enum
2 import unittest
3 from test.support import import_helper
4 from test.support import os_helper
5
6 _testlimitedcapi = import_helper.import_module('_testlimitedcapi')
7 _testcapi = import_helper.import_module('_testcapi')
8
9
10 class Constant(enum.IntEnum):
11 Py_CONSTANT_NONE = 0
12 Py_CONSTANT_FALSE = 1
13 Py_CONSTANT_TRUE = 2
14 Py_CONSTANT_ELLIPSIS = 3
15 Py_CONSTANT_NOT_IMPLEMENTED = 4
16 Py_CONSTANT_ZERO = 5
17 Py_CONSTANT_ONE = 6
18 Py_CONSTANT_EMPTY_STR = 7
19 Py_CONSTANT_EMPTY_BYTES = 8
20 Py_CONSTANT_EMPTY_TUPLE = 9
21
22 INVALID_CONSTANT = Py_CONSTANT_EMPTY_TUPLE + 1
23
24
25 class GetConstantTest(unittest.TestCase):
26 def check_get_constant(self, get_constant):
27 self.assertIs(get_constant(Constant.Py_CONSTANT_NONE), None)
28 self.assertIs(get_constant(Constant.Py_CONSTANT_FALSE), False)
29 self.assertIs(get_constant(Constant.Py_CONSTANT_TRUE), True)
30 self.assertIs(get_constant(Constant.Py_CONSTANT_ELLIPSIS), Ellipsis)
31 self.assertIs(get_constant(Constant.Py_CONSTANT_NOT_IMPLEMENTED), NotImplemented)
32
33 for constant_id, constant_type, value in (
34 (Constant.Py_CONSTANT_ZERO, int, 0),
35 (Constant.Py_CONSTANT_ONE, int, 1),
36 (Constant.Py_CONSTANT_EMPTY_STR, str, ""),
37 (Constant.Py_CONSTANT_EMPTY_BYTES, bytes, b""),
38 (Constant.Py_CONSTANT_EMPTY_TUPLE, tuple, ()),
39 ):
40 with self.subTest(constant_id=constant_id):
41 obj = get_constant(constant_id)
42 self.assertEqual(type(obj), constant_type, obj)
43 self.assertEqual(obj, value)
44
45 with self.assertRaises(SystemError):
46 get_constant(Constant.INVALID_CONSTANT)
47
48 def test_get_constant(self):
49 self.check_get_constant(_testlimitedcapi.get_constant)
50
51 def test_get_constant_borrowed(self):
52 self.check_get_constant(_testlimitedcapi.get_constant_borrowed)
53
54
55 class PrintTest(unittest.TestCase):
56 def testPyObjectPrintObject(self):
57
58 class PrintableObject:
59
60 def __repr__(self):
61 return "spam spam spam"
62
63 def __str__(self):
64 return "egg egg egg"
65
66 obj = PrintableObject()
67 output_filename = os_helper.TESTFN
68 self.addCleanup(os_helper.unlink, output_filename)
69
70 # Test repr printing
71 _testcapi.call_pyobject_print(obj, output_filename, False)
72 with open(output_filename, 'r') as output_file:
73 self.assertEqual(output_file.read(), repr(obj))
74
75 # Test str printing
76 _testcapi.call_pyobject_print(obj, output_filename, True)
77 with open(output_filename, 'r') as output_file:
78 self.assertEqual(output_file.read(), str(obj))
79
80 def testPyObjectPrintNULL(self):
81 output_filename = os_helper.TESTFN
82 self.addCleanup(os_helper.unlink, output_filename)
83
84 # Test repr printing
85 _testcapi.pyobject_print_null(output_filename)
86 with open(output_filename, 'r') as output_file:
87 self.assertEqual(output_file.read(), '<nil>')
88
89 def testPyObjectPrintNoRefObject(self):
90 output_filename = os_helper.TESTFN
91 self.addCleanup(os_helper.unlink, output_filename)
92
93 # Test repr printing
94 correct_output = _testcapi.pyobject_print_noref_object(output_filename)
95 with open(output_filename, 'r') as output_file:
96 self.assertEqual(output_file.read(), correct_output)
97
98 def testPyObjectPrintOSError(self):
99 output_filename = os_helper.TESTFN
100 self.addCleanup(os_helper.unlink, output_filename)
101
102 open(output_filename, "w+").close()
103 with self.assertRaises(OSError):
104 _testcapi.pyobject_print_os_error(output_filename)
105
106
107 class ClearWeakRefsNoCallbacksTest(unittest.TestCase):
108 """Test PyUnstable_Object_ClearWeakRefsNoCallbacks"""
109 def test_ClearWeakRefsNoCallbacks(self):
110 """Ensure PyUnstable_Object_ClearWeakRefsNoCallbacks works"""
111 import weakref
112 import gc
113 class C:
114 pass
115 obj = C()
116 messages = []
117 ref = weakref.ref(obj, lambda: messages.append("don't add this"))
118 self.assertIs(ref(), obj)
119 self.assertFalse(messages)
120 _testcapi.pyobject_clear_weakrefs_no_callbacks(obj)
121 self.assertIsNone(ref())
122 gc.collect()
123 self.assertFalse(messages)
124
125 def test_ClearWeakRefsNoCallbacks_no_weakref_support(self):
126 """Don't fail on objects that don't support weakrefs"""
127 import weakref
128 obj = object()
129 with self.assertRaises(TypeError):
130 ref = weakref.ref(obj)
131 _testcapi.pyobject_clear_weakrefs_no_callbacks(obj)
132
133
134 if __name__ == "__main__":
135 unittest.main()