Debian Codegraph @

Lib/test/test_capi/test_sys.py

raw
1 import unittest
2 import contextlib
3 import sys
4 from test import support
5 from test.support import import_helper
6
7 try:
8 import _testlimitedcapi
9 except ImportError:
10 _testlimitedcapi = None
11
12 NULL = None
13
14 class CAPITest(unittest.TestCase):
15 # TODO: Test the following functions:
16 #
17 # PySys_Audit()
18 # PySys_AuditTuple()
19
20 maxDiff = None
21
22 @support.cpython_only
23 @unittest.skipIf(_testlimitedcapi is None, 'need _testlimitedcapi module')
24 def test_sys_getobject(self):
25 # Test PySys_GetObject()
26 getobject = _testlimitedcapi.sys_getobject
27
28 self.assertIs(getobject(b'stdout'), sys.stdout)
29 with support.swap_attr(sys, '\U0001f40d', 42):
30 self.assertEqual(getobject('\U0001f40d'.encode()), 42)
31
32 self.assertIs(getobject(b'nonexisting'), AttributeError)
33 with support.catch_unraisable_exception() as cm:
34 self.assertIs(getobject(b'\xff'), AttributeError)
35 self.assertEqual(cm.unraisable.exc_type, UnicodeDecodeError)
36 self.assertRegex(str(cm.unraisable.exc_value),
37 "'utf-8' codec can't decode")
38 # CRASHES getobject(NULL)
39
40 @support.cpython_only
41 @unittest.skipIf(_testlimitedcapi is None, 'need _testlimitedcapi module')
42 def test_sys_setobject(self):
43 # Test PySys_SetObject()
44 setobject = _testlimitedcapi.sys_setobject
45
46 value = ['value']
47 value2 = ['value2']
48 try:
49 self.assertEqual(setobject(b'newattr', value), 0)
50 self.assertIs(sys.newattr, value)
51 self.assertEqual(setobject(b'newattr', value2), 0)
52 self.assertIs(sys.newattr, value2)
53 self.assertEqual(setobject(b'newattr', NULL), 0)
54 self.assertNotHasAttr(sys, 'newattr')
55 self.assertEqual(setobject(b'newattr', NULL), 0)
56 finally:
57 with contextlib.suppress(AttributeError):
58 del sys.newattr
59 try:
60 self.assertEqual(setobject('\U0001f40d'.encode(), value), 0)
61 self.assertIs(getattr(sys, '\U0001f40d'), value)
62 self.assertEqual(setobject('\U0001f40d'.encode(), NULL), 0)
63 self.assertNotHasAttr(sys, '\U0001f40d')
64 finally:
65 with contextlib.suppress(AttributeError):
66 delattr(sys, '\U0001f40d')
67
68 with self.assertRaises(UnicodeDecodeError):
69 setobject(b'\xff', value)
70 # CRASHES setobject(NULL, value)
71
72 @support.cpython_only
73 @unittest.skipIf(_testlimitedcapi is None, 'need _testlimitedcapi module')
74 def test_sys_getxoptions(self):
75 # Test PySys_GetXOptions()
76 getxoptions = _testlimitedcapi.sys_getxoptions
77
78 self.assertIs(getxoptions(), sys._xoptions)
79
80 xoptions = sys._xoptions
81 try:
82 sys._xoptions = 'non-dict'
83 self.assertEqual(getxoptions(), {})
84 self.assertIs(getxoptions(), sys._xoptions)
85
86 del sys._xoptions
87 self.assertEqual(getxoptions(), {})
88 self.assertIs(getxoptions(), sys._xoptions)
89 finally:
90 sys._xoptions = xoptions
91 self.assertIs(getxoptions(), sys._xoptions)
92
93 def _test_sys_formatstream(self, funname, streamname):
94 import_helper.import_module('ctypes')
95 from ctypes import pythonapi, c_char_p, py_object
96 func = getattr(pythonapi, funname)
97 func.argtypes = (c_char_p,)
98
99 # Supports plain C types.
100 with support.captured_output(streamname) as stream:
101 func(b'Hello, %s!', c_char_p(b'world'))
102 self.assertEqual(stream.getvalue(), 'Hello, world!')
103
104 # Supports Python objects.
105 with support.captured_output(streamname) as stream:
106 func(b'Hello, %R!', py_object('world'))
107 self.assertEqual(stream.getvalue(), "Hello, 'world'!")
108
109 # The total length is not limited.
110 with support.captured_output(streamname) as stream:
111 func(b'Hello, %s!', c_char_p(b'world'*200))
112 self.assertEqual(stream.getvalue(), 'Hello, ' + 'world'*200 + '!')
113
114 def test_sys_formatstdout(self):
115 # Test PySys_FormatStdout()
116 self._test_sys_formatstream('PySys_FormatStdout', 'stdout')
117
118 def test_sys_formatstderr(self):
119 # Test PySys_FormatStderr()
120 self._test_sys_formatstream('PySys_FormatStderr', 'stderr')
121
122 def _test_sys_writestream(self, funname, streamname):
123 import_helper.import_module('ctypes')
124 from ctypes import pythonapi, c_char_p
125 func = getattr(pythonapi, funname)
126 func.argtypes = (c_char_p,)
127
128 # Supports plain C types.
129 with support.captured_output(streamname) as stream:
130 func(b'Hello, %s!', c_char_p(b'world'))
131 self.assertEqual(stream.getvalue(), 'Hello, world!')
132
133 # There is a limit on the total length.
134 with support.captured_output(streamname) as stream:
135 func(b'Hello, %s!', c_char_p(b'world'*100))
136 self.assertEqual(stream.getvalue(), 'Hello, ' + 'world'*100 + '!')
137 with support.captured_output(streamname) as stream:
138 func(b'Hello, %s!', c_char_p(b'world'*200))
139 out = stream.getvalue()
140 self.assertEqual(out[:20], 'Hello, worldworldwor')
141 self.assertEqual(out[-13:], '... truncated')
142 self.assertGreater(len(out), 1000)
143
144 def test_sys_writestdout(self):
145 # Test PySys_WriteStdout()
146 self._test_sys_writestream('PySys_WriteStdout', 'stdout')
147
148 def test_sys_writestderr(self):
149 # Test PySys_WriteStderr()
150 self._test_sys_writestream('PySys_WriteStderr', 'stderr')
151
152
153 if __name__ == "__main__":
154 unittest.main()