1 | n/a | import unittest, test.support |
---|
2 | n/a | from test.support.script_helper import assert_python_ok, assert_python_failure |
---|
3 | n/a | import sys, io, os |
---|
4 | n/a | import struct |
---|
5 | n/a | import subprocess |
---|
6 | n/a | import textwrap |
---|
7 | n/a | import warnings |
---|
8 | n/a | import operator |
---|
9 | n/a | import codecs |
---|
10 | n/a | import gc |
---|
11 | n/a | import sysconfig |
---|
12 | n/a | import platform |
---|
13 | n/a | import locale |
---|
14 | n/a | |
---|
15 | n/a | # count the number of test runs, used to create unique |
---|
16 | n/a | # strings to intern in test_intern() |
---|
17 | n/a | numruns = 0 |
---|
18 | n/a | |
---|
19 | n/a | try: |
---|
20 | n/a | import threading |
---|
21 | n/a | except ImportError: |
---|
22 | n/a | threading = None |
---|
23 | n/a | |
---|
24 | n/a | class SysModuleTest(unittest.TestCase): |
---|
25 | n/a | |
---|
26 | n/a | def setUp(self): |
---|
27 | n/a | self.orig_stdout = sys.stdout |
---|
28 | n/a | self.orig_stderr = sys.stderr |
---|
29 | n/a | self.orig_displayhook = sys.displayhook |
---|
30 | n/a | |
---|
31 | n/a | def tearDown(self): |
---|
32 | n/a | sys.stdout = self.orig_stdout |
---|
33 | n/a | sys.stderr = self.orig_stderr |
---|
34 | n/a | sys.displayhook = self.orig_displayhook |
---|
35 | n/a | test.support.reap_children() |
---|
36 | n/a | |
---|
37 | n/a | def test_original_displayhook(self): |
---|
38 | n/a | import builtins |
---|
39 | n/a | out = io.StringIO() |
---|
40 | n/a | sys.stdout = out |
---|
41 | n/a | |
---|
42 | n/a | dh = sys.__displayhook__ |
---|
43 | n/a | |
---|
44 | n/a | self.assertRaises(TypeError, dh) |
---|
45 | n/a | if hasattr(builtins, "_"): |
---|
46 | n/a | del builtins._ |
---|
47 | n/a | |
---|
48 | n/a | dh(None) |
---|
49 | n/a | self.assertEqual(out.getvalue(), "") |
---|
50 | n/a | self.assertTrue(not hasattr(builtins, "_")) |
---|
51 | n/a | dh(42) |
---|
52 | n/a | self.assertEqual(out.getvalue(), "42\n") |
---|
53 | n/a | self.assertEqual(builtins._, 42) |
---|
54 | n/a | |
---|
55 | n/a | del sys.stdout |
---|
56 | n/a | self.assertRaises(RuntimeError, dh, 42) |
---|
57 | n/a | |
---|
58 | n/a | def test_lost_displayhook(self): |
---|
59 | n/a | del sys.displayhook |
---|
60 | n/a | code = compile("42", "<string>", "single") |
---|
61 | n/a | self.assertRaises(RuntimeError, eval, code) |
---|
62 | n/a | |
---|
63 | n/a | def test_custom_displayhook(self): |
---|
64 | n/a | def baddisplayhook(obj): |
---|
65 | n/a | raise ValueError |
---|
66 | n/a | sys.displayhook = baddisplayhook |
---|
67 | n/a | code = compile("42", "<string>", "single") |
---|
68 | n/a | self.assertRaises(ValueError, eval, code) |
---|
69 | n/a | |
---|
70 | n/a | def test_original_excepthook(self): |
---|
71 | n/a | err = io.StringIO() |
---|
72 | n/a | sys.stderr = err |
---|
73 | n/a | |
---|
74 | n/a | eh = sys.__excepthook__ |
---|
75 | n/a | |
---|
76 | n/a | self.assertRaises(TypeError, eh) |
---|
77 | n/a | try: |
---|
78 | n/a | raise ValueError(42) |
---|
79 | n/a | except ValueError as exc: |
---|
80 | n/a | eh(*sys.exc_info()) |
---|
81 | n/a | |
---|
82 | n/a | self.assertTrue(err.getvalue().endswith("ValueError: 42\n")) |
---|
83 | n/a | |
---|
84 | n/a | def test_excepthook(self): |
---|
85 | n/a | with test.support.captured_output("stderr") as stderr: |
---|
86 | n/a | sys.excepthook(1, '1', 1) |
---|
87 | n/a | self.assertTrue("TypeError: print_exception(): Exception expected for " \ |
---|
88 | n/a | "value, str found" in stderr.getvalue()) |
---|
89 | n/a | |
---|
90 | n/a | # FIXME: testing the code for a lost or replaced excepthook in |
---|
91 | n/a | # Python/pythonrun.c::PyErr_PrintEx() is tricky. |
---|
92 | n/a | |
---|
93 | n/a | def test_exit(self): |
---|
94 | n/a | # call with two arguments |
---|
95 | n/a | self.assertRaises(TypeError, sys.exit, 42, 42) |
---|
96 | n/a | |
---|
97 | n/a | # call without argument |
---|
98 | n/a | with self.assertRaises(SystemExit) as cm: |
---|
99 | n/a | sys.exit() |
---|
100 | n/a | self.assertIsNone(cm.exception.code) |
---|
101 | n/a | |
---|
102 | n/a | rc, out, err = assert_python_ok('-c', 'import sys; sys.exit()') |
---|
103 | n/a | self.assertEqual(rc, 0) |
---|
104 | n/a | self.assertEqual(out, b'') |
---|
105 | n/a | self.assertEqual(err, b'') |
---|
106 | n/a | |
---|
107 | n/a | # call with integer argument |
---|
108 | n/a | with self.assertRaises(SystemExit) as cm: |
---|
109 | n/a | sys.exit(42) |
---|
110 | n/a | self.assertEqual(cm.exception.code, 42) |
---|
111 | n/a | |
---|
112 | n/a | # call with tuple argument with one entry |
---|
113 | n/a | # entry will be unpacked |
---|
114 | n/a | with self.assertRaises(SystemExit) as cm: |
---|
115 | n/a | sys.exit((42,)) |
---|
116 | n/a | self.assertEqual(cm.exception.code, 42) |
---|
117 | n/a | |
---|
118 | n/a | # call with string argument |
---|
119 | n/a | with self.assertRaises(SystemExit) as cm: |
---|
120 | n/a | sys.exit("exit") |
---|
121 | n/a | self.assertEqual(cm.exception.code, "exit") |
---|
122 | n/a | |
---|
123 | n/a | # call with tuple argument with two entries |
---|
124 | n/a | with self.assertRaises(SystemExit) as cm: |
---|
125 | n/a | sys.exit((17, 23)) |
---|
126 | n/a | self.assertEqual(cm.exception.code, (17, 23)) |
---|
127 | n/a | |
---|
128 | n/a | # test that the exit machinery handles SystemExits properly |
---|
129 | n/a | rc, out, err = assert_python_failure('-c', 'raise SystemExit(47)') |
---|
130 | n/a | self.assertEqual(rc, 47) |
---|
131 | n/a | self.assertEqual(out, b'') |
---|
132 | n/a | self.assertEqual(err, b'') |
---|
133 | n/a | |
---|
134 | n/a | def check_exit_message(code, expected, **env_vars): |
---|
135 | n/a | rc, out, err = assert_python_failure('-c', code, **env_vars) |
---|
136 | n/a | self.assertEqual(rc, 1) |
---|
137 | n/a | self.assertEqual(out, b'') |
---|
138 | n/a | self.assertTrue(err.startswith(expected), |
---|
139 | n/a | "%s doesn't start with %s" % (ascii(err), ascii(expected))) |
---|
140 | n/a | |
---|
141 | n/a | # test that stderr buffer is flushed before the exit message is written |
---|
142 | n/a | # into stderr |
---|
143 | n/a | check_exit_message( |
---|
144 | n/a | r'import sys; sys.stderr.write("unflushed,"); sys.exit("message")', |
---|
145 | n/a | b"unflushed,message") |
---|
146 | n/a | |
---|
147 | n/a | # test that the exit message is written with backslashreplace error |
---|
148 | n/a | # handler to stderr |
---|
149 | n/a | check_exit_message( |
---|
150 | n/a | r'import sys; sys.exit("surrogates:\uDCFF")', |
---|
151 | n/a | b"surrogates:\\udcff") |
---|
152 | n/a | |
---|
153 | n/a | # test that the unicode message is encoded to the stderr encoding |
---|
154 | n/a | # instead of the default encoding (utf8) |
---|
155 | n/a | check_exit_message( |
---|
156 | n/a | r'import sys; sys.exit("h\xe9")', |
---|
157 | n/a | b"h\xe9", PYTHONIOENCODING='latin-1') |
---|
158 | n/a | |
---|
159 | n/a | def test_getdefaultencoding(self): |
---|
160 | n/a | self.assertRaises(TypeError, sys.getdefaultencoding, 42) |
---|
161 | n/a | # can't check more than the type, as the user might have changed it |
---|
162 | n/a | self.assertIsInstance(sys.getdefaultencoding(), str) |
---|
163 | n/a | |
---|
164 | n/a | # testing sys.settrace() is done in test_sys_settrace.py |
---|
165 | n/a | # testing sys.setprofile() is done in test_sys_setprofile.py |
---|
166 | n/a | |
---|
167 | n/a | def test_setcheckinterval(self): |
---|
168 | n/a | with warnings.catch_warnings(): |
---|
169 | n/a | warnings.simplefilter("ignore") |
---|
170 | n/a | self.assertRaises(TypeError, sys.setcheckinterval) |
---|
171 | n/a | orig = sys.getcheckinterval() |
---|
172 | n/a | for n in 0, 100, 120, orig: # orig last to restore starting state |
---|
173 | n/a | sys.setcheckinterval(n) |
---|
174 | n/a | self.assertEqual(sys.getcheckinterval(), n) |
---|
175 | n/a | |
---|
176 | n/a | @unittest.skipUnless(threading, 'Threading required for this test.') |
---|
177 | n/a | def test_switchinterval(self): |
---|
178 | n/a | self.assertRaises(TypeError, sys.setswitchinterval) |
---|
179 | n/a | self.assertRaises(TypeError, sys.setswitchinterval, "a") |
---|
180 | n/a | self.assertRaises(ValueError, sys.setswitchinterval, -1.0) |
---|
181 | n/a | self.assertRaises(ValueError, sys.setswitchinterval, 0.0) |
---|
182 | n/a | orig = sys.getswitchinterval() |
---|
183 | n/a | # sanity check |
---|
184 | n/a | self.assertTrue(orig < 0.5, orig) |
---|
185 | n/a | try: |
---|
186 | n/a | for n in 0.00001, 0.05, 3.0, orig: |
---|
187 | n/a | sys.setswitchinterval(n) |
---|
188 | n/a | self.assertAlmostEqual(sys.getswitchinterval(), n) |
---|
189 | n/a | finally: |
---|
190 | n/a | sys.setswitchinterval(orig) |
---|
191 | n/a | |
---|
192 | n/a | def test_recursionlimit(self): |
---|
193 | n/a | self.assertRaises(TypeError, sys.getrecursionlimit, 42) |
---|
194 | n/a | oldlimit = sys.getrecursionlimit() |
---|
195 | n/a | self.assertRaises(TypeError, sys.setrecursionlimit) |
---|
196 | n/a | self.assertRaises(ValueError, sys.setrecursionlimit, -42) |
---|
197 | n/a | sys.setrecursionlimit(10000) |
---|
198 | n/a | self.assertEqual(sys.getrecursionlimit(), 10000) |
---|
199 | n/a | sys.setrecursionlimit(oldlimit) |
---|
200 | n/a | |
---|
201 | n/a | def test_recursionlimit_recovery(self): |
---|
202 | n/a | if hasattr(sys, 'gettrace') and sys.gettrace(): |
---|
203 | n/a | self.skipTest('fatal error if run with a trace function') |
---|
204 | n/a | |
---|
205 | n/a | oldlimit = sys.getrecursionlimit() |
---|
206 | n/a | def f(): |
---|
207 | n/a | f() |
---|
208 | n/a | try: |
---|
209 | n/a | for depth in (10, 25, 50, 75, 100, 250, 1000): |
---|
210 | n/a | try: |
---|
211 | n/a | sys.setrecursionlimit(depth) |
---|
212 | n/a | except RecursionError: |
---|
213 | n/a | # Issue #25274: The recursion limit is too low at the |
---|
214 | n/a | # current recursion depth |
---|
215 | n/a | continue |
---|
216 | n/a | |
---|
217 | n/a | # Issue #5392: test stack overflow after hitting recursion |
---|
218 | n/a | # limit twice |
---|
219 | n/a | self.assertRaises(RecursionError, f) |
---|
220 | n/a | self.assertRaises(RecursionError, f) |
---|
221 | n/a | finally: |
---|
222 | n/a | sys.setrecursionlimit(oldlimit) |
---|
223 | n/a | |
---|
224 | n/a | @test.support.cpython_only |
---|
225 | n/a | def test_setrecursionlimit_recursion_depth(self): |
---|
226 | n/a | # Issue #25274: Setting a low recursion limit must be blocked if the |
---|
227 | n/a | # current recursion depth is already higher than the "lower-water |
---|
228 | n/a | # mark". Otherwise, it may not be possible anymore to |
---|
229 | n/a | # reset the overflowed flag to 0. |
---|
230 | n/a | |
---|
231 | n/a | from _testcapi import get_recursion_depth |
---|
232 | n/a | |
---|
233 | n/a | def set_recursion_limit_at_depth(depth, limit): |
---|
234 | n/a | recursion_depth = get_recursion_depth() |
---|
235 | n/a | if recursion_depth >= depth: |
---|
236 | n/a | with self.assertRaises(RecursionError) as cm: |
---|
237 | n/a | sys.setrecursionlimit(limit) |
---|
238 | n/a | self.assertRegex(str(cm.exception), |
---|
239 | n/a | "cannot set the recursion limit to [0-9]+ " |
---|
240 | n/a | "at the recursion depth [0-9]+: " |
---|
241 | n/a | "the limit is too low") |
---|
242 | n/a | else: |
---|
243 | n/a | set_recursion_limit_at_depth(depth, limit) |
---|
244 | n/a | |
---|
245 | n/a | oldlimit = sys.getrecursionlimit() |
---|
246 | n/a | try: |
---|
247 | n/a | sys.setrecursionlimit(1000) |
---|
248 | n/a | |
---|
249 | n/a | for limit in (10, 25, 50, 75, 100, 150, 200): |
---|
250 | n/a | # formula extracted from _Py_RecursionLimitLowerWaterMark() |
---|
251 | n/a | if limit > 200: |
---|
252 | n/a | depth = limit - 50 |
---|
253 | n/a | else: |
---|
254 | n/a | depth = limit * 3 // 4 |
---|
255 | n/a | set_recursion_limit_at_depth(depth, limit) |
---|
256 | n/a | finally: |
---|
257 | n/a | sys.setrecursionlimit(oldlimit) |
---|
258 | n/a | |
---|
259 | n/a | def test_recursionlimit_fatalerror(self): |
---|
260 | n/a | # A fatal error occurs if a second recursion limit is hit when recovering |
---|
261 | n/a | # from a first one. |
---|
262 | n/a | code = textwrap.dedent(""" |
---|
263 | n/a | import sys |
---|
264 | n/a | |
---|
265 | n/a | def f(): |
---|
266 | n/a | try: |
---|
267 | n/a | f() |
---|
268 | n/a | except RecursionError: |
---|
269 | n/a | f() |
---|
270 | n/a | |
---|
271 | n/a | sys.setrecursionlimit(%d) |
---|
272 | n/a | f()""") |
---|
273 | n/a | with test.support.SuppressCrashReport(): |
---|
274 | n/a | for i in (50, 1000): |
---|
275 | n/a | sub = subprocess.Popen([sys.executable, '-c', code % i], |
---|
276 | n/a | stderr=subprocess.PIPE) |
---|
277 | n/a | err = sub.communicate()[1] |
---|
278 | n/a | self.assertTrue(sub.returncode, sub.returncode) |
---|
279 | n/a | self.assertIn( |
---|
280 | n/a | b"Fatal Python error: Cannot recover from stack overflow", |
---|
281 | n/a | err) |
---|
282 | n/a | |
---|
283 | n/a | def test_getwindowsversion(self): |
---|
284 | n/a | # Raise SkipTest if sys doesn't have getwindowsversion attribute |
---|
285 | n/a | test.support.get_attribute(sys, "getwindowsversion") |
---|
286 | n/a | v = sys.getwindowsversion() |
---|
287 | n/a | self.assertEqual(len(v), 5) |
---|
288 | n/a | self.assertIsInstance(v[0], int) |
---|
289 | n/a | self.assertIsInstance(v[1], int) |
---|
290 | n/a | self.assertIsInstance(v[2], int) |
---|
291 | n/a | self.assertIsInstance(v[3], int) |
---|
292 | n/a | self.assertIsInstance(v[4], str) |
---|
293 | n/a | self.assertRaises(IndexError, operator.getitem, v, 5) |
---|
294 | n/a | self.assertIsInstance(v.major, int) |
---|
295 | n/a | self.assertIsInstance(v.minor, int) |
---|
296 | n/a | self.assertIsInstance(v.build, int) |
---|
297 | n/a | self.assertIsInstance(v.platform, int) |
---|
298 | n/a | self.assertIsInstance(v.service_pack, str) |
---|
299 | n/a | self.assertIsInstance(v.service_pack_minor, int) |
---|
300 | n/a | self.assertIsInstance(v.service_pack_major, int) |
---|
301 | n/a | self.assertIsInstance(v.suite_mask, int) |
---|
302 | n/a | self.assertIsInstance(v.product_type, int) |
---|
303 | n/a | self.assertEqual(v[0], v.major) |
---|
304 | n/a | self.assertEqual(v[1], v.minor) |
---|
305 | n/a | self.assertEqual(v[2], v.build) |
---|
306 | n/a | self.assertEqual(v[3], v.platform) |
---|
307 | n/a | self.assertEqual(v[4], v.service_pack) |
---|
308 | n/a | |
---|
309 | n/a | # This is how platform.py calls it. Make sure tuple |
---|
310 | n/a | # still has 5 elements |
---|
311 | n/a | maj, min, buildno, plat, csd = sys.getwindowsversion() |
---|
312 | n/a | |
---|
313 | n/a | def test_call_tracing(self): |
---|
314 | n/a | self.assertRaises(TypeError, sys.call_tracing, type, 2) |
---|
315 | n/a | |
---|
316 | n/a | @unittest.skipUnless(hasattr(sys, "setdlopenflags"), |
---|
317 | n/a | 'test needs sys.setdlopenflags()') |
---|
318 | n/a | def test_dlopenflags(self): |
---|
319 | n/a | self.assertTrue(hasattr(sys, "getdlopenflags")) |
---|
320 | n/a | self.assertRaises(TypeError, sys.getdlopenflags, 42) |
---|
321 | n/a | oldflags = sys.getdlopenflags() |
---|
322 | n/a | self.assertRaises(TypeError, sys.setdlopenflags) |
---|
323 | n/a | sys.setdlopenflags(oldflags+1) |
---|
324 | n/a | self.assertEqual(sys.getdlopenflags(), oldflags+1) |
---|
325 | n/a | sys.setdlopenflags(oldflags) |
---|
326 | n/a | |
---|
327 | n/a | @test.support.refcount_test |
---|
328 | n/a | def test_refcount(self): |
---|
329 | n/a | # n here must be a global in order for this test to pass while |
---|
330 | n/a | # tracing with a python function. Tracing calls PyFrame_FastToLocals |
---|
331 | n/a | # which will add a copy of any locals to the frame object, causing |
---|
332 | n/a | # the reference count to increase by 2 instead of 1. |
---|
333 | n/a | global n |
---|
334 | n/a | self.assertRaises(TypeError, sys.getrefcount) |
---|
335 | n/a | c = sys.getrefcount(None) |
---|
336 | n/a | n = None |
---|
337 | n/a | self.assertEqual(sys.getrefcount(None), c+1) |
---|
338 | n/a | del n |
---|
339 | n/a | self.assertEqual(sys.getrefcount(None), c) |
---|
340 | n/a | if hasattr(sys, "gettotalrefcount"): |
---|
341 | n/a | self.assertIsInstance(sys.gettotalrefcount(), int) |
---|
342 | n/a | |
---|
343 | n/a | def test_getframe(self): |
---|
344 | n/a | self.assertRaises(TypeError, sys._getframe, 42, 42) |
---|
345 | n/a | self.assertRaises(ValueError, sys._getframe, 2000000000) |
---|
346 | n/a | self.assertTrue( |
---|
347 | n/a | SysModuleTest.test_getframe.__code__ \ |
---|
348 | n/a | is sys._getframe().f_code |
---|
349 | n/a | ) |
---|
350 | n/a | |
---|
351 | n/a | # sys._current_frames() is a CPython-only gimmick. |
---|
352 | n/a | def test_current_frames(self): |
---|
353 | n/a | have_threads = True |
---|
354 | n/a | try: |
---|
355 | n/a | import _thread |
---|
356 | n/a | except ImportError: |
---|
357 | n/a | have_threads = False |
---|
358 | n/a | |
---|
359 | n/a | if have_threads: |
---|
360 | n/a | self.current_frames_with_threads() |
---|
361 | n/a | else: |
---|
362 | n/a | self.current_frames_without_threads() |
---|
363 | n/a | |
---|
364 | n/a | # Test sys._current_frames() in a WITH_THREADS build. |
---|
365 | n/a | @test.support.reap_threads |
---|
366 | n/a | def current_frames_with_threads(self): |
---|
367 | n/a | import threading |
---|
368 | n/a | import traceback |
---|
369 | n/a | |
---|
370 | n/a | # Spawn a thread that blocks at a known place. Then the main |
---|
371 | n/a | # thread does sys._current_frames(), and verifies that the frames |
---|
372 | n/a | # returned make sense. |
---|
373 | n/a | entered_g = threading.Event() |
---|
374 | n/a | leave_g = threading.Event() |
---|
375 | n/a | thread_info = [] # the thread's id |
---|
376 | n/a | |
---|
377 | n/a | def f123(): |
---|
378 | n/a | g456() |
---|
379 | n/a | |
---|
380 | n/a | def g456(): |
---|
381 | n/a | thread_info.append(threading.get_ident()) |
---|
382 | n/a | entered_g.set() |
---|
383 | n/a | leave_g.wait() |
---|
384 | n/a | |
---|
385 | n/a | t = threading.Thread(target=f123) |
---|
386 | n/a | t.start() |
---|
387 | n/a | entered_g.wait() |
---|
388 | n/a | |
---|
389 | n/a | # At this point, t has finished its entered_g.set(), although it's |
---|
390 | n/a | # impossible to guess whether it's still on that line or has moved on |
---|
391 | n/a | # to its leave_g.wait(). |
---|
392 | n/a | self.assertEqual(len(thread_info), 1) |
---|
393 | n/a | thread_id = thread_info[0] |
---|
394 | n/a | |
---|
395 | n/a | d = sys._current_frames() |
---|
396 | n/a | |
---|
397 | n/a | main_id = threading.get_ident() |
---|
398 | n/a | self.assertIn(main_id, d) |
---|
399 | n/a | self.assertIn(thread_id, d) |
---|
400 | n/a | |
---|
401 | n/a | # Verify that the captured main-thread frame is _this_ frame. |
---|
402 | n/a | frame = d.pop(main_id) |
---|
403 | n/a | self.assertTrue(frame is sys._getframe()) |
---|
404 | n/a | |
---|
405 | n/a | # Verify that the captured thread frame is blocked in g456, called |
---|
406 | n/a | # from f123. This is a litte tricky, since various bits of |
---|
407 | n/a | # threading.py are also in the thread's call stack. |
---|
408 | n/a | frame = d.pop(thread_id) |
---|
409 | n/a | stack = traceback.extract_stack(frame) |
---|
410 | n/a | for i, (filename, lineno, funcname, sourceline) in enumerate(stack): |
---|
411 | n/a | if funcname == "f123": |
---|
412 | n/a | break |
---|
413 | n/a | else: |
---|
414 | n/a | self.fail("didn't find f123() on thread's call stack") |
---|
415 | n/a | |
---|
416 | n/a | self.assertEqual(sourceline, "g456()") |
---|
417 | n/a | |
---|
418 | n/a | # And the next record must be for g456(). |
---|
419 | n/a | filename, lineno, funcname, sourceline = stack[i+1] |
---|
420 | n/a | self.assertEqual(funcname, "g456") |
---|
421 | n/a | self.assertIn(sourceline, ["leave_g.wait()", "entered_g.set()"]) |
---|
422 | n/a | |
---|
423 | n/a | # Reap the spawned thread. |
---|
424 | n/a | leave_g.set() |
---|
425 | n/a | t.join() |
---|
426 | n/a | |
---|
427 | n/a | # Test sys._current_frames() when thread support doesn't exist. |
---|
428 | n/a | def current_frames_without_threads(self): |
---|
429 | n/a | # Not much happens here: there is only one thread, with artificial |
---|
430 | n/a | # "thread id" 0. |
---|
431 | n/a | d = sys._current_frames() |
---|
432 | n/a | self.assertEqual(len(d), 1) |
---|
433 | n/a | self.assertIn(0, d) |
---|
434 | n/a | self.assertTrue(d[0] is sys._getframe()) |
---|
435 | n/a | |
---|
436 | n/a | def test_attributes(self): |
---|
437 | n/a | self.assertIsInstance(sys.api_version, int) |
---|
438 | n/a | self.assertIsInstance(sys.argv, list) |
---|
439 | n/a | self.assertIn(sys.byteorder, ("little", "big")) |
---|
440 | n/a | self.assertIsInstance(sys.builtin_module_names, tuple) |
---|
441 | n/a | self.assertIsInstance(sys.copyright, str) |
---|
442 | n/a | self.assertIsInstance(sys.exec_prefix, str) |
---|
443 | n/a | self.assertIsInstance(sys.base_exec_prefix, str) |
---|
444 | n/a | self.assertIsInstance(sys.executable, str) |
---|
445 | n/a | self.assertEqual(len(sys.float_info), 11) |
---|
446 | n/a | self.assertEqual(sys.float_info.radix, 2) |
---|
447 | n/a | self.assertEqual(len(sys.int_info), 2) |
---|
448 | n/a | self.assertTrue(sys.int_info.bits_per_digit % 5 == 0) |
---|
449 | n/a | self.assertTrue(sys.int_info.sizeof_digit >= 1) |
---|
450 | n/a | self.assertEqual(type(sys.int_info.bits_per_digit), int) |
---|
451 | n/a | self.assertEqual(type(sys.int_info.sizeof_digit), int) |
---|
452 | n/a | self.assertIsInstance(sys.hexversion, int) |
---|
453 | n/a | |
---|
454 | n/a | self.assertEqual(len(sys.hash_info), 9) |
---|
455 | n/a | self.assertLess(sys.hash_info.modulus, 2**sys.hash_info.width) |
---|
456 | n/a | # sys.hash_info.modulus should be a prime; we do a quick |
---|
457 | n/a | # probable primality test (doesn't exclude the possibility of |
---|
458 | n/a | # a Carmichael number) |
---|
459 | n/a | for x in range(1, 100): |
---|
460 | n/a | self.assertEqual( |
---|
461 | n/a | pow(x, sys.hash_info.modulus-1, sys.hash_info.modulus), |
---|
462 | n/a | 1, |
---|
463 | n/a | "sys.hash_info.modulus {} is a non-prime".format( |
---|
464 | n/a | sys.hash_info.modulus) |
---|
465 | n/a | ) |
---|
466 | n/a | self.assertIsInstance(sys.hash_info.inf, int) |
---|
467 | n/a | self.assertIsInstance(sys.hash_info.nan, int) |
---|
468 | n/a | self.assertIsInstance(sys.hash_info.imag, int) |
---|
469 | n/a | algo = sysconfig.get_config_var("Py_HASH_ALGORITHM") |
---|
470 | n/a | if sys.hash_info.algorithm in {"fnv", "siphash24"}: |
---|
471 | n/a | self.assertIn(sys.hash_info.hash_bits, {32, 64}) |
---|
472 | n/a | self.assertIn(sys.hash_info.seed_bits, {32, 64, 128}) |
---|
473 | n/a | |
---|
474 | n/a | if algo == 1: |
---|
475 | n/a | self.assertEqual(sys.hash_info.algorithm, "siphash24") |
---|
476 | n/a | elif algo == 2: |
---|
477 | n/a | self.assertEqual(sys.hash_info.algorithm, "fnv") |
---|
478 | n/a | else: |
---|
479 | n/a | self.assertIn(sys.hash_info.algorithm, {"fnv", "siphash24"}) |
---|
480 | n/a | else: |
---|
481 | n/a | # PY_HASH_EXTERNAL |
---|
482 | n/a | self.assertEqual(algo, 0) |
---|
483 | n/a | self.assertGreaterEqual(sys.hash_info.cutoff, 0) |
---|
484 | n/a | self.assertLess(sys.hash_info.cutoff, 8) |
---|
485 | n/a | |
---|
486 | n/a | self.assertIsInstance(sys.maxsize, int) |
---|
487 | n/a | self.assertIsInstance(sys.maxunicode, int) |
---|
488 | n/a | self.assertEqual(sys.maxunicode, 0x10FFFF) |
---|
489 | n/a | self.assertIsInstance(sys.platform, str) |
---|
490 | n/a | self.assertIsInstance(sys.prefix, str) |
---|
491 | n/a | self.assertIsInstance(sys.base_prefix, str) |
---|
492 | n/a | self.assertIsInstance(sys.version, str) |
---|
493 | n/a | vi = sys.version_info |
---|
494 | n/a | self.assertIsInstance(vi[:], tuple) |
---|
495 | n/a | self.assertEqual(len(vi), 5) |
---|
496 | n/a | self.assertIsInstance(vi[0], int) |
---|
497 | n/a | self.assertIsInstance(vi[1], int) |
---|
498 | n/a | self.assertIsInstance(vi[2], int) |
---|
499 | n/a | self.assertIn(vi[3], ("alpha", "beta", "candidate", "final")) |
---|
500 | n/a | self.assertIsInstance(vi[4], int) |
---|
501 | n/a | self.assertIsInstance(vi.major, int) |
---|
502 | n/a | self.assertIsInstance(vi.minor, int) |
---|
503 | n/a | self.assertIsInstance(vi.micro, int) |
---|
504 | n/a | self.assertIn(vi.releaselevel, ("alpha", "beta", "candidate", "final")) |
---|
505 | n/a | self.assertIsInstance(vi.serial, int) |
---|
506 | n/a | self.assertEqual(vi[0], vi.major) |
---|
507 | n/a | self.assertEqual(vi[1], vi.minor) |
---|
508 | n/a | self.assertEqual(vi[2], vi.micro) |
---|
509 | n/a | self.assertEqual(vi[3], vi.releaselevel) |
---|
510 | n/a | self.assertEqual(vi[4], vi.serial) |
---|
511 | n/a | self.assertTrue(vi > (1,0,0)) |
---|
512 | n/a | self.assertIsInstance(sys.float_repr_style, str) |
---|
513 | n/a | self.assertIn(sys.float_repr_style, ('short', 'legacy')) |
---|
514 | n/a | if not sys.platform.startswith('win'): |
---|
515 | n/a | self.assertIsInstance(sys.abiflags, str) |
---|
516 | n/a | |
---|
517 | n/a | @unittest.skipUnless(hasattr(sys, 'thread_info'), |
---|
518 | n/a | 'Threading required for this test.') |
---|
519 | n/a | def test_thread_info(self): |
---|
520 | n/a | info = sys.thread_info |
---|
521 | n/a | self.assertEqual(len(info), 3) |
---|
522 | n/a | self.assertIn(info.name, ('nt', 'pthread', 'solaris', None)) |
---|
523 | n/a | self.assertIn(info.lock, ('semaphore', 'mutex+cond', None)) |
---|
524 | n/a | |
---|
525 | n/a | def test_43581(self): |
---|
526 | n/a | # Can't use sys.stdout, as this is a StringIO object when |
---|
527 | n/a | # the test runs under regrtest. |
---|
528 | n/a | self.assertEqual(sys.__stdout__.encoding, sys.__stderr__.encoding) |
---|
529 | n/a | |
---|
530 | n/a | def test_intern(self): |
---|
531 | n/a | global numruns |
---|
532 | n/a | numruns += 1 |
---|
533 | n/a | self.assertRaises(TypeError, sys.intern) |
---|
534 | n/a | s = "never interned before" + str(numruns) |
---|
535 | n/a | self.assertTrue(sys.intern(s) is s) |
---|
536 | n/a | s2 = s.swapcase().swapcase() |
---|
537 | n/a | self.assertTrue(sys.intern(s2) is s) |
---|
538 | n/a | |
---|
539 | n/a | # Subclasses of string can't be interned, because they |
---|
540 | n/a | # provide too much opportunity for insane things to happen. |
---|
541 | n/a | # We don't want them in the interned dict and if they aren't |
---|
542 | n/a | # actually interned, we don't want to create the appearance |
---|
543 | n/a | # that they are by allowing intern() to succeed. |
---|
544 | n/a | class S(str): |
---|
545 | n/a | def __hash__(self): |
---|
546 | n/a | return 123 |
---|
547 | n/a | |
---|
548 | n/a | self.assertRaises(TypeError, sys.intern, S("abc")) |
---|
549 | n/a | |
---|
550 | n/a | def test_sys_flags(self): |
---|
551 | n/a | self.assertTrue(sys.flags) |
---|
552 | n/a | attrs = ("debug", |
---|
553 | n/a | "inspect", "interactive", "optimize", "dont_write_bytecode", |
---|
554 | n/a | "no_user_site", "no_site", "ignore_environment", "verbose", |
---|
555 | n/a | "bytes_warning", "quiet", "hash_randomization", "isolated") |
---|
556 | n/a | for attr in attrs: |
---|
557 | n/a | self.assertTrue(hasattr(sys.flags, attr), attr) |
---|
558 | n/a | self.assertEqual(type(getattr(sys.flags, attr)), int, attr) |
---|
559 | n/a | self.assertTrue(repr(sys.flags)) |
---|
560 | n/a | self.assertEqual(len(sys.flags), len(attrs)) |
---|
561 | n/a | |
---|
562 | n/a | def assert_raise_on_new_sys_type(self, sys_attr): |
---|
563 | n/a | # Users are intentionally prevented from creating new instances of |
---|
564 | n/a | # sys.flags, sys.version_info, and sys.getwindowsversion. |
---|
565 | n/a | attr_type = type(sys_attr) |
---|
566 | n/a | with self.assertRaises(TypeError): |
---|
567 | n/a | attr_type() |
---|
568 | n/a | with self.assertRaises(TypeError): |
---|
569 | n/a | attr_type.__new__(attr_type) |
---|
570 | n/a | |
---|
571 | n/a | def test_sys_flags_no_instantiation(self): |
---|
572 | n/a | self.assert_raise_on_new_sys_type(sys.flags) |
---|
573 | n/a | |
---|
574 | n/a | def test_sys_version_info_no_instantiation(self): |
---|
575 | n/a | self.assert_raise_on_new_sys_type(sys.version_info) |
---|
576 | n/a | |
---|
577 | n/a | def test_sys_getwindowsversion_no_instantiation(self): |
---|
578 | n/a | # Skip if not being run on Windows. |
---|
579 | n/a | test.support.get_attribute(sys, "getwindowsversion") |
---|
580 | n/a | self.assert_raise_on_new_sys_type(sys.getwindowsversion()) |
---|
581 | n/a | |
---|
582 | n/a | @test.support.cpython_only |
---|
583 | n/a | def test_clear_type_cache(self): |
---|
584 | n/a | sys._clear_type_cache() |
---|
585 | n/a | |
---|
586 | n/a | def test_ioencoding(self): |
---|
587 | n/a | env = dict(os.environ) |
---|
588 | n/a | |
---|
589 | n/a | # Test character: cent sign, encoded as 0x4A (ASCII J) in CP424, |
---|
590 | n/a | # not representable in ASCII. |
---|
591 | n/a | |
---|
592 | n/a | env["PYTHONIOENCODING"] = "cp424" |
---|
593 | n/a | p = subprocess.Popen([sys.executable, "-c", 'print(chr(0xa2))'], |
---|
594 | n/a | stdout = subprocess.PIPE, env=env) |
---|
595 | n/a | out = p.communicate()[0].strip() |
---|
596 | n/a | expected = ("\xa2" + os.linesep).encode("cp424") |
---|
597 | n/a | self.assertEqual(out, expected) |
---|
598 | n/a | |
---|
599 | n/a | env["PYTHONIOENCODING"] = "ascii:replace" |
---|
600 | n/a | p = subprocess.Popen([sys.executable, "-c", 'print(chr(0xa2))'], |
---|
601 | n/a | stdout = subprocess.PIPE, env=env) |
---|
602 | n/a | out = p.communicate()[0].strip() |
---|
603 | n/a | self.assertEqual(out, b'?') |
---|
604 | n/a | |
---|
605 | n/a | env["PYTHONIOENCODING"] = "ascii" |
---|
606 | n/a | p = subprocess.Popen([sys.executable, "-c", 'print(chr(0xa2))'], |
---|
607 | n/a | stdout=subprocess.PIPE, stderr=subprocess.PIPE, |
---|
608 | n/a | env=env) |
---|
609 | n/a | out, err = p.communicate() |
---|
610 | n/a | self.assertEqual(out, b'') |
---|
611 | n/a | self.assertIn(b'UnicodeEncodeError:', err) |
---|
612 | n/a | self.assertIn(rb"'\xa2'", err) |
---|
613 | n/a | |
---|
614 | n/a | env["PYTHONIOENCODING"] = "ascii:" |
---|
615 | n/a | p = subprocess.Popen([sys.executable, "-c", 'print(chr(0xa2))'], |
---|
616 | n/a | stdout=subprocess.PIPE, stderr=subprocess.PIPE, |
---|
617 | n/a | env=env) |
---|
618 | n/a | out, err = p.communicate() |
---|
619 | n/a | self.assertEqual(out, b'') |
---|
620 | n/a | self.assertIn(b'UnicodeEncodeError:', err) |
---|
621 | n/a | self.assertIn(rb"'\xa2'", err) |
---|
622 | n/a | |
---|
623 | n/a | env["PYTHONIOENCODING"] = ":surrogateescape" |
---|
624 | n/a | p = subprocess.Popen([sys.executable, "-c", 'print(chr(0xdcbd))'], |
---|
625 | n/a | stdout=subprocess.PIPE, env=env) |
---|
626 | n/a | out = p.communicate()[0].strip() |
---|
627 | n/a | self.assertEqual(out, b'\xbd') |
---|
628 | n/a | |
---|
629 | n/a | @unittest.skipUnless(test.support.FS_NONASCII, |
---|
630 | n/a | 'requires OS support of non-ASCII encodings') |
---|
631 | n/a | @unittest.skipUnless(sys.getfilesystemencoding() == locale.getpreferredencoding(False), |
---|
632 | n/a | 'requires FS encoding to match locale') |
---|
633 | n/a | def test_ioencoding_nonascii(self): |
---|
634 | n/a | env = dict(os.environ) |
---|
635 | n/a | |
---|
636 | n/a | env["PYTHONIOENCODING"] = "" |
---|
637 | n/a | p = subprocess.Popen([sys.executable, "-c", |
---|
638 | n/a | 'print(%a)' % test.support.FS_NONASCII], |
---|
639 | n/a | stdout=subprocess.PIPE, env=env) |
---|
640 | n/a | out = p.communicate()[0].strip() |
---|
641 | n/a | self.assertEqual(out, os.fsencode(test.support.FS_NONASCII)) |
---|
642 | n/a | |
---|
643 | n/a | @unittest.skipIf(sys.base_prefix != sys.prefix, |
---|
644 | n/a | 'Test is not venv-compatible') |
---|
645 | n/a | def test_executable(self): |
---|
646 | n/a | # sys.executable should be absolute |
---|
647 | n/a | self.assertEqual(os.path.abspath(sys.executable), sys.executable) |
---|
648 | n/a | |
---|
649 | n/a | # Issue #7774: Ensure that sys.executable is an empty string if argv[0] |
---|
650 | n/a | # has been set to a non existent program name and Python is unable to |
---|
651 | n/a | # retrieve the real program name |
---|
652 | n/a | |
---|
653 | n/a | # For a normal installation, it should work without 'cwd' |
---|
654 | n/a | # argument. For test runs in the build directory, see #7774. |
---|
655 | n/a | python_dir = os.path.dirname(os.path.realpath(sys.executable)) |
---|
656 | n/a | p = subprocess.Popen( |
---|
657 | n/a | ["nonexistent", "-c", |
---|
658 | n/a | 'import sys; print(sys.executable.encode("ascii", "backslashreplace"))'], |
---|
659 | n/a | executable=sys.executable, stdout=subprocess.PIPE, cwd=python_dir) |
---|
660 | n/a | stdout = p.communicate()[0] |
---|
661 | n/a | executable = stdout.strip().decode("ASCII") |
---|
662 | n/a | p.wait() |
---|
663 | n/a | self.assertIn(executable, ["b''", repr(sys.executable.encode("ascii", "backslashreplace"))]) |
---|
664 | n/a | |
---|
665 | n/a | def check_fsencoding(self, fs_encoding, expected=None): |
---|
666 | n/a | self.assertIsNotNone(fs_encoding) |
---|
667 | n/a | codecs.lookup(fs_encoding) |
---|
668 | n/a | if expected: |
---|
669 | n/a | self.assertEqual(fs_encoding, expected) |
---|
670 | n/a | |
---|
671 | n/a | def test_getfilesystemencoding(self): |
---|
672 | n/a | fs_encoding = sys.getfilesystemencoding() |
---|
673 | n/a | if sys.platform == 'darwin': |
---|
674 | n/a | expected = 'utf-8' |
---|
675 | n/a | else: |
---|
676 | n/a | expected = None |
---|
677 | n/a | self.check_fsencoding(fs_encoding, expected) |
---|
678 | n/a | |
---|
679 | n/a | def c_locale_get_error_handler(self, isolated=False, encoding=None): |
---|
680 | n/a | # Force the POSIX locale |
---|
681 | n/a | env = os.environ.copy() |
---|
682 | n/a | env["LC_ALL"] = "C" |
---|
683 | n/a | code = '\n'.join(( |
---|
684 | n/a | 'import sys', |
---|
685 | n/a | 'def dump(name):', |
---|
686 | n/a | ' std = getattr(sys, name)', |
---|
687 | n/a | ' print("%s: %s" % (name, std.errors))', |
---|
688 | n/a | 'dump("stdin")', |
---|
689 | n/a | 'dump("stdout")', |
---|
690 | n/a | 'dump("stderr")', |
---|
691 | n/a | )) |
---|
692 | n/a | args = [sys.executable, "-c", code] |
---|
693 | n/a | if isolated: |
---|
694 | n/a | args.append("-I") |
---|
695 | n/a | if encoding is not None: |
---|
696 | n/a | env['PYTHONIOENCODING'] = encoding |
---|
697 | n/a | else: |
---|
698 | n/a | env.pop('PYTHONIOENCODING', None) |
---|
699 | n/a | p = subprocess.Popen(args, |
---|
700 | n/a | stdout=subprocess.PIPE, |
---|
701 | n/a | stderr=subprocess.STDOUT, |
---|
702 | n/a | env=env, |
---|
703 | n/a | universal_newlines=True) |
---|
704 | n/a | stdout, stderr = p.communicate() |
---|
705 | n/a | return stdout |
---|
706 | n/a | |
---|
707 | n/a | def test_c_locale_surrogateescape(self): |
---|
708 | n/a | out = self.c_locale_get_error_handler(isolated=True) |
---|
709 | n/a | self.assertEqual(out, |
---|
710 | n/a | 'stdin: surrogateescape\n' |
---|
711 | n/a | 'stdout: surrogateescape\n' |
---|
712 | n/a | 'stderr: backslashreplace\n') |
---|
713 | n/a | |
---|
714 | n/a | # replace the default error handler |
---|
715 | n/a | out = self.c_locale_get_error_handler(encoding=':ignore') |
---|
716 | n/a | self.assertEqual(out, |
---|
717 | n/a | 'stdin: ignore\n' |
---|
718 | n/a | 'stdout: ignore\n' |
---|
719 | n/a | 'stderr: backslashreplace\n') |
---|
720 | n/a | |
---|
721 | n/a | # force the encoding |
---|
722 | n/a | out = self.c_locale_get_error_handler(encoding='iso8859-1') |
---|
723 | n/a | self.assertEqual(out, |
---|
724 | n/a | 'stdin: strict\n' |
---|
725 | n/a | 'stdout: strict\n' |
---|
726 | n/a | 'stderr: backslashreplace\n') |
---|
727 | n/a | out = self.c_locale_get_error_handler(encoding='iso8859-1:') |
---|
728 | n/a | self.assertEqual(out, |
---|
729 | n/a | 'stdin: strict\n' |
---|
730 | n/a | 'stdout: strict\n' |
---|
731 | n/a | 'stderr: backslashreplace\n') |
---|
732 | n/a | |
---|
733 | n/a | # have no any effect |
---|
734 | n/a | out = self.c_locale_get_error_handler(encoding=':') |
---|
735 | n/a | self.assertEqual(out, |
---|
736 | n/a | 'stdin: surrogateescape\n' |
---|
737 | n/a | 'stdout: surrogateescape\n' |
---|
738 | n/a | 'stderr: backslashreplace\n') |
---|
739 | n/a | out = self.c_locale_get_error_handler(encoding='') |
---|
740 | n/a | self.assertEqual(out, |
---|
741 | n/a | 'stdin: surrogateescape\n' |
---|
742 | n/a | 'stdout: surrogateescape\n' |
---|
743 | n/a | 'stderr: backslashreplace\n') |
---|
744 | n/a | |
---|
745 | n/a | def test_implementation(self): |
---|
746 | n/a | # This test applies to all implementations equally. |
---|
747 | n/a | |
---|
748 | n/a | levels = {'alpha': 0xA, 'beta': 0xB, 'candidate': 0xC, 'final': 0xF} |
---|
749 | n/a | |
---|
750 | n/a | self.assertTrue(hasattr(sys.implementation, 'name')) |
---|
751 | n/a | self.assertTrue(hasattr(sys.implementation, 'version')) |
---|
752 | n/a | self.assertTrue(hasattr(sys.implementation, 'hexversion')) |
---|
753 | n/a | self.assertTrue(hasattr(sys.implementation, 'cache_tag')) |
---|
754 | n/a | |
---|
755 | n/a | version = sys.implementation.version |
---|
756 | n/a | self.assertEqual(version[:2], (version.major, version.minor)) |
---|
757 | n/a | |
---|
758 | n/a | hexversion = (version.major << 24 | version.minor << 16 | |
---|
759 | n/a | version.micro << 8 | levels[version.releaselevel] << 4 | |
---|
760 | n/a | version.serial << 0) |
---|
761 | n/a | self.assertEqual(sys.implementation.hexversion, hexversion) |
---|
762 | n/a | |
---|
763 | n/a | # PEP 421 requires that .name be lower case. |
---|
764 | n/a | self.assertEqual(sys.implementation.name, |
---|
765 | n/a | sys.implementation.name.lower()) |
---|
766 | n/a | |
---|
767 | n/a | @test.support.cpython_only |
---|
768 | n/a | def test_debugmallocstats(self): |
---|
769 | n/a | # Test sys._debugmallocstats() |
---|
770 | n/a | from test.support.script_helper import assert_python_ok |
---|
771 | n/a | args = ['-c', 'import sys; sys._debugmallocstats()'] |
---|
772 | n/a | ret, out, err = assert_python_ok(*args) |
---|
773 | n/a | self.assertIn(b"free PyDictObjects", err) |
---|
774 | n/a | |
---|
775 | n/a | # The function has no parameter |
---|
776 | n/a | self.assertRaises(TypeError, sys._debugmallocstats, True) |
---|
777 | n/a | |
---|
778 | n/a | @unittest.skipUnless(hasattr(sys, "getallocatedblocks"), |
---|
779 | n/a | "sys.getallocatedblocks unavailable on this build") |
---|
780 | n/a | def test_getallocatedblocks(self): |
---|
781 | n/a | # Some sanity checks |
---|
782 | n/a | with_pymalloc = sysconfig.get_config_var('WITH_PYMALLOC') |
---|
783 | n/a | a = sys.getallocatedblocks() |
---|
784 | n/a | self.assertIs(type(a), int) |
---|
785 | n/a | if with_pymalloc: |
---|
786 | n/a | self.assertGreater(a, 0) |
---|
787 | n/a | else: |
---|
788 | n/a | # When WITH_PYMALLOC isn't available, we don't know anything |
---|
789 | n/a | # about the underlying implementation: the function might |
---|
790 | n/a | # return 0 or something greater. |
---|
791 | n/a | self.assertGreaterEqual(a, 0) |
---|
792 | n/a | try: |
---|
793 | n/a | # While we could imagine a Python session where the number of |
---|
794 | n/a | # multiple buffer objects would exceed the sharing of references, |
---|
795 | n/a | # it is unlikely to happen in a normal test run. |
---|
796 | n/a | self.assertLess(a, sys.gettotalrefcount()) |
---|
797 | n/a | except AttributeError: |
---|
798 | n/a | # gettotalrefcount() not available |
---|
799 | n/a | pass |
---|
800 | n/a | gc.collect() |
---|
801 | n/a | b = sys.getallocatedblocks() |
---|
802 | n/a | self.assertLessEqual(b, a) |
---|
803 | n/a | gc.collect() |
---|
804 | n/a | c = sys.getallocatedblocks() |
---|
805 | n/a | self.assertIn(c, range(b - 50, b + 50)) |
---|
806 | n/a | |
---|
807 | n/a | @test.support.requires_type_collecting |
---|
808 | n/a | def test_is_finalizing(self): |
---|
809 | n/a | self.assertIs(sys.is_finalizing(), False) |
---|
810 | n/a | # Don't use the atexit module because _Py_Finalizing is only set |
---|
811 | n/a | # after calling atexit callbacks |
---|
812 | n/a | code = """if 1: |
---|
813 | n/a | import sys |
---|
814 | n/a | |
---|
815 | n/a | class AtExit: |
---|
816 | n/a | is_finalizing = sys.is_finalizing |
---|
817 | n/a | print = print |
---|
818 | n/a | |
---|
819 | n/a | def __del__(self): |
---|
820 | n/a | self.print(self.is_finalizing(), flush=True) |
---|
821 | n/a | |
---|
822 | n/a | # Keep a reference in the __main__ module namespace, so the |
---|
823 | n/a | # AtExit destructor will be called at Python exit |
---|
824 | n/a | ref = AtExit() |
---|
825 | n/a | """ |
---|
826 | n/a | rc, stdout, stderr = assert_python_ok('-c', code) |
---|
827 | n/a | self.assertEqual(stdout.rstrip(), b'True') |
---|
828 | n/a | |
---|
829 | n/a | @unittest.skipUnless(hasattr(sys, 'getandroidapilevel'), |
---|
830 | n/a | 'need sys.getandroidapilevel()') |
---|
831 | n/a | def test_getandroidapilevel(self): |
---|
832 | n/a | level = sys.getandroidapilevel() |
---|
833 | n/a | self.assertIsInstance(level, int) |
---|
834 | n/a | self.assertGreater(level, 0) |
---|
835 | n/a | |
---|
836 | n/a | |
---|
837 | n/a | @test.support.cpython_only |
---|
838 | n/a | class SizeofTest(unittest.TestCase): |
---|
839 | n/a | |
---|
840 | n/a | def setUp(self): |
---|
841 | n/a | self.P = struct.calcsize('P') |
---|
842 | n/a | self.longdigit = sys.int_info.sizeof_digit |
---|
843 | n/a | import _testcapi |
---|
844 | n/a | self.gc_headsize = _testcapi.SIZEOF_PYGC_HEAD |
---|
845 | n/a | |
---|
846 | n/a | check_sizeof = test.support.check_sizeof |
---|
847 | n/a | |
---|
848 | n/a | def test_gc_head_size(self): |
---|
849 | n/a | # Check that the gc header size is added to objects tracked by the gc. |
---|
850 | n/a | vsize = test.support.calcvobjsize |
---|
851 | n/a | gc_header_size = self.gc_headsize |
---|
852 | n/a | # bool objects are not gc tracked |
---|
853 | n/a | self.assertEqual(sys.getsizeof(True), vsize('') + self.longdigit) |
---|
854 | n/a | # but lists are |
---|
855 | n/a | self.assertEqual(sys.getsizeof([]), vsize('Pn') + gc_header_size) |
---|
856 | n/a | |
---|
857 | n/a | def test_errors(self): |
---|
858 | n/a | class BadSizeof: |
---|
859 | n/a | def __sizeof__(self): |
---|
860 | n/a | raise ValueError |
---|
861 | n/a | self.assertRaises(ValueError, sys.getsizeof, BadSizeof()) |
---|
862 | n/a | |
---|
863 | n/a | class InvalidSizeof: |
---|
864 | n/a | def __sizeof__(self): |
---|
865 | n/a | return None |
---|
866 | n/a | self.assertRaises(TypeError, sys.getsizeof, InvalidSizeof()) |
---|
867 | n/a | sentinel = ["sentinel"] |
---|
868 | n/a | self.assertIs(sys.getsizeof(InvalidSizeof(), sentinel), sentinel) |
---|
869 | n/a | |
---|
870 | n/a | class FloatSizeof: |
---|
871 | n/a | def __sizeof__(self): |
---|
872 | n/a | return 4.5 |
---|
873 | n/a | self.assertRaises(TypeError, sys.getsizeof, FloatSizeof()) |
---|
874 | n/a | self.assertIs(sys.getsizeof(FloatSizeof(), sentinel), sentinel) |
---|
875 | n/a | |
---|
876 | n/a | class OverflowSizeof(int): |
---|
877 | n/a | def __sizeof__(self): |
---|
878 | n/a | return int(self) |
---|
879 | n/a | self.assertEqual(sys.getsizeof(OverflowSizeof(sys.maxsize)), |
---|
880 | n/a | sys.maxsize + self.gc_headsize) |
---|
881 | n/a | with self.assertRaises(OverflowError): |
---|
882 | n/a | sys.getsizeof(OverflowSizeof(sys.maxsize + 1)) |
---|
883 | n/a | with self.assertRaises(ValueError): |
---|
884 | n/a | sys.getsizeof(OverflowSizeof(-1)) |
---|
885 | n/a | with self.assertRaises((ValueError, OverflowError)): |
---|
886 | n/a | sys.getsizeof(OverflowSizeof(-sys.maxsize - 1)) |
---|
887 | n/a | |
---|
888 | n/a | def test_default(self): |
---|
889 | n/a | size = test.support.calcvobjsize |
---|
890 | n/a | self.assertEqual(sys.getsizeof(True), size('') + self.longdigit) |
---|
891 | n/a | self.assertEqual(sys.getsizeof(True, -1), size('') + self.longdigit) |
---|
892 | n/a | |
---|
893 | n/a | def test_objecttypes(self): |
---|
894 | n/a | # check all types defined in Objects/ |
---|
895 | n/a | calcsize = struct.calcsize |
---|
896 | n/a | size = test.support.calcobjsize |
---|
897 | n/a | vsize = test.support.calcvobjsize |
---|
898 | n/a | check = self.check_sizeof |
---|
899 | n/a | # bool |
---|
900 | n/a | check(True, vsize('') + self.longdigit) |
---|
901 | n/a | # buffer |
---|
902 | n/a | # XXX |
---|
903 | n/a | # builtin_function_or_method |
---|
904 | n/a | check(len, size('4P')) # XXX check layout |
---|
905 | n/a | # bytearray |
---|
906 | n/a | samples = [b'', b'u'*100000] |
---|
907 | n/a | for sample in samples: |
---|
908 | n/a | x = bytearray(sample) |
---|
909 | n/a | check(x, vsize('n2Pi') + x.__alloc__()) |
---|
910 | n/a | # bytearray_iterator |
---|
911 | n/a | check(iter(bytearray()), size('nP')) |
---|
912 | n/a | # bytes |
---|
913 | n/a | check(b'', vsize('n') + 1) |
---|
914 | n/a | check(b'x' * 10, vsize('n') + 11) |
---|
915 | n/a | # cell |
---|
916 | n/a | def get_cell(): |
---|
917 | n/a | x = 42 |
---|
918 | n/a | def inner(): |
---|
919 | n/a | return x |
---|
920 | n/a | return inner |
---|
921 | n/a | check(get_cell().__closure__[0], size('P')) |
---|
922 | n/a | # code |
---|
923 | n/a | check(get_cell().__code__, size('6i13P')) |
---|
924 | n/a | check(get_cell.__code__, size('6i13P')) |
---|
925 | n/a | def get_cell2(x): |
---|
926 | n/a | def inner(): |
---|
927 | n/a | return x |
---|
928 | n/a | return inner |
---|
929 | n/a | check(get_cell2.__code__, size('6i13P') + calcsize('n')) |
---|
930 | n/a | # complex |
---|
931 | n/a | check(complex(0,1), size('2d')) |
---|
932 | n/a | # method_descriptor (descriptor object) |
---|
933 | n/a | check(str.lower, size('3PP')) |
---|
934 | n/a | # classmethod_descriptor (descriptor object) |
---|
935 | n/a | # XXX |
---|
936 | n/a | # member_descriptor (descriptor object) |
---|
937 | n/a | import datetime |
---|
938 | n/a | check(datetime.timedelta.days, size('3PP')) |
---|
939 | n/a | # getset_descriptor (descriptor object) |
---|
940 | n/a | import collections |
---|
941 | n/a | check(collections.defaultdict.default_factory, size('3PP')) |
---|
942 | n/a | # wrapper_descriptor (descriptor object) |
---|
943 | n/a | check(int.__add__, size('3P2P')) |
---|
944 | n/a | # method-wrapper (descriptor object) |
---|
945 | n/a | check({}.__iter__, size('2P')) |
---|
946 | n/a | # dict |
---|
947 | n/a | check({}, size('nQ2P') + calcsize('2nP2n') + 8 + (8*2//3)*calcsize('n2P')) |
---|
948 | n/a | longdict = {1:1, 2:2, 3:3, 4:4, 5:5, 6:6, 7:7, 8:8} |
---|
949 | n/a | check(longdict, size('nQ2P') + calcsize('2nP2n') + 16 + (16*2//3)*calcsize('n2P')) |
---|
950 | n/a | # dictionary-keyview |
---|
951 | n/a | check({}.keys(), size('P')) |
---|
952 | n/a | # dictionary-valueview |
---|
953 | n/a | check({}.values(), size('P')) |
---|
954 | n/a | # dictionary-itemview |
---|
955 | n/a | check({}.items(), size('P')) |
---|
956 | n/a | # dictionary iterator |
---|
957 | n/a | check(iter({}), size('P2nPn')) |
---|
958 | n/a | # dictionary-keyiterator |
---|
959 | n/a | check(iter({}.keys()), size('P2nPn')) |
---|
960 | n/a | # dictionary-valueiterator |
---|
961 | n/a | check(iter({}.values()), size('P2nPn')) |
---|
962 | n/a | # dictionary-itemiterator |
---|
963 | n/a | check(iter({}.items()), size('P2nPn')) |
---|
964 | n/a | # dictproxy |
---|
965 | n/a | class C(object): pass |
---|
966 | n/a | check(C.__dict__, size('P')) |
---|
967 | n/a | # BaseException |
---|
968 | n/a | check(BaseException(), size('5Pb')) |
---|
969 | n/a | # UnicodeEncodeError |
---|
970 | n/a | check(UnicodeEncodeError("", "", 0, 0, ""), size('5Pb 2P2nP')) |
---|
971 | n/a | # UnicodeDecodeError |
---|
972 | n/a | check(UnicodeDecodeError("", b"", 0, 0, ""), size('5Pb 2P2nP')) |
---|
973 | n/a | # UnicodeTranslateError |
---|
974 | n/a | check(UnicodeTranslateError("", 0, 1, ""), size('5Pb 2P2nP')) |
---|
975 | n/a | # ellipses |
---|
976 | n/a | check(Ellipsis, size('')) |
---|
977 | n/a | # EncodingMap |
---|
978 | n/a | import codecs, encodings.iso8859_3 |
---|
979 | n/a | x = codecs.charmap_build(encodings.iso8859_3.decoding_table) |
---|
980 | n/a | check(x, size('32B2iB')) |
---|
981 | n/a | # enumerate |
---|
982 | n/a | check(enumerate([]), size('n3P')) |
---|
983 | n/a | # reverse |
---|
984 | n/a | check(reversed(''), size('nP')) |
---|
985 | n/a | # float |
---|
986 | n/a | check(float(0), size('d')) |
---|
987 | n/a | # sys.floatinfo |
---|
988 | n/a | check(sys.float_info, vsize('') + self.P * len(sys.float_info)) |
---|
989 | n/a | # frame |
---|
990 | n/a | import inspect |
---|
991 | n/a | CO_MAXBLOCKS = 20 |
---|
992 | n/a | x = inspect.currentframe() |
---|
993 | n/a | ncells = len(x.f_code.co_cellvars) |
---|
994 | n/a | nfrees = len(x.f_code.co_freevars) |
---|
995 | n/a | extras = x.f_code.co_stacksize + x.f_code.co_nlocals +\ |
---|
996 | n/a | ncells + nfrees - 1 |
---|
997 | n/a | check(x, vsize('12P3ic' + CO_MAXBLOCKS*'3i' + 'P' + extras*'P')) |
---|
998 | n/a | # function |
---|
999 | n/a | def func(): pass |
---|
1000 | n/a | check(func, size('12P')) |
---|
1001 | n/a | class c(): |
---|
1002 | n/a | @staticmethod |
---|
1003 | n/a | def foo(): |
---|
1004 | n/a | pass |
---|
1005 | n/a | @classmethod |
---|
1006 | n/a | def bar(cls): |
---|
1007 | n/a | pass |
---|
1008 | n/a | # staticmethod |
---|
1009 | n/a | check(foo, size('PP')) |
---|
1010 | n/a | # classmethod |
---|
1011 | n/a | check(bar, size('PP')) |
---|
1012 | n/a | # generator |
---|
1013 | n/a | def get_gen(): yield 1 |
---|
1014 | n/a | check(get_gen(), size('Pb2PPP')) |
---|
1015 | n/a | # iterator |
---|
1016 | n/a | check(iter('abc'), size('lP')) |
---|
1017 | n/a | # callable-iterator |
---|
1018 | n/a | import re |
---|
1019 | n/a | check(re.finditer('',''), size('2P')) |
---|
1020 | n/a | # list |
---|
1021 | n/a | samples = [[], [1,2,3], ['1', '2', '3']] |
---|
1022 | n/a | for sample in samples: |
---|
1023 | n/a | check(sample, vsize('Pn') + len(sample)*self.P) |
---|
1024 | n/a | # sortwrapper (list) |
---|
1025 | n/a | # XXX |
---|
1026 | n/a | # cmpwrapper (list) |
---|
1027 | n/a | # XXX |
---|
1028 | n/a | # listiterator (list) |
---|
1029 | n/a | check(iter([]), size('lP')) |
---|
1030 | n/a | # listreverseiterator (list) |
---|
1031 | n/a | check(reversed([]), size('nP')) |
---|
1032 | n/a | # int |
---|
1033 | n/a | check(0, vsize('')) |
---|
1034 | n/a | check(1, vsize('') + self.longdigit) |
---|
1035 | n/a | check(-1, vsize('') + self.longdigit) |
---|
1036 | n/a | PyLong_BASE = 2**sys.int_info.bits_per_digit |
---|
1037 | n/a | check(int(PyLong_BASE), vsize('') + 2*self.longdigit) |
---|
1038 | n/a | check(int(PyLong_BASE**2-1), vsize('') + 2*self.longdigit) |
---|
1039 | n/a | check(int(PyLong_BASE**2), vsize('') + 3*self.longdigit) |
---|
1040 | n/a | # module |
---|
1041 | n/a | check(unittest, size('PnPPP')) |
---|
1042 | n/a | # None |
---|
1043 | n/a | check(None, size('')) |
---|
1044 | n/a | # NotImplementedType |
---|
1045 | n/a | check(NotImplemented, size('')) |
---|
1046 | n/a | # object |
---|
1047 | n/a | check(object(), size('')) |
---|
1048 | n/a | # property (descriptor object) |
---|
1049 | n/a | class C(object): |
---|
1050 | n/a | def getx(self): return self.__x |
---|
1051 | n/a | def setx(self, value): self.__x = value |
---|
1052 | n/a | def delx(self): del self.__x |
---|
1053 | n/a | x = property(getx, setx, delx, "") |
---|
1054 | n/a | check(x, size('4Pi')) |
---|
1055 | n/a | # PyCapsule |
---|
1056 | n/a | # XXX |
---|
1057 | n/a | # rangeiterator |
---|
1058 | n/a | check(iter(range(1)), size('4l')) |
---|
1059 | n/a | # reverse |
---|
1060 | n/a | check(reversed(''), size('nP')) |
---|
1061 | n/a | # range |
---|
1062 | n/a | check(range(1), size('4P')) |
---|
1063 | n/a | check(range(66000), size('4P')) |
---|
1064 | n/a | # set |
---|
1065 | n/a | # frozenset |
---|
1066 | n/a | PySet_MINSIZE = 8 |
---|
1067 | n/a | samples = [[], range(10), range(50)] |
---|
1068 | n/a | s = size('3nP' + PySet_MINSIZE*'nP' + '2nP') |
---|
1069 | n/a | for sample in samples: |
---|
1070 | n/a | minused = len(sample) |
---|
1071 | n/a | if minused == 0: tmp = 1 |
---|
1072 | n/a | # the computation of minused is actually a bit more complicated |
---|
1073 | n/a | # but this suffices for the sizeof test |
---|
1074 | n/a | minused = minused*2 |
---|
1075 | n/a | newsize = PySet_MINSIZE |
---|
1076 | n/a | while newsize <= minused: |
---|
1077 | n/a | newsize = newsize << 1 |
---|
1078 | n/a | if newsize <= 8: |
---|
1079 | n/a | check(set(sample), s) |
---|
1080 | n/a | check(frozenset(sample), s) |
---|
1081 | n/a | else: |
---|
1082 | n/a | check(set(sample), s + newsize*calcsize('nP')) |
---|
1083 | n/a | check(frozenset(sample), s + newsize*calcsize('nP')) |
---|
1084 | n/a | # setiterator |
---|
1085 | n/a | check(iter(set()), size('P3n')) |
---|
1086 | n/a | # slice |
---|
1087 | n/a | check(slice(0), size('3P')) |
---|
1088 | n/a | # super |
---|
1089 | n/a | check(super(int), size('3P')) |
---|
1090 | n/a | # tuple |
---|
1091 | n/a | check((), vsize('')) |
---|
1092 | n/a | check((1,2,3), vsize('') + 3*self.P) |
---|
1093 | n/a | # type |
---|
1094 | n/a | # static type: PyTypeObject |
---|
1095 | n/a | fmt = 'P2n15Pl4Pn9Pn11PIP' |
---|
1096 | n/a | if hasattr(sys, 'getcounts'): |
---|
1097 | n/a | fmt += '3n2P' |
---|
1098 | n/a | s = vsize(fmt) |
---|
1099 | n/a | check(int, s) |
---|
1100 | n/a | s = vsize(fmt + # PyTypeObject |
---|
1101 | n/a | '3P' # PyAsyncMethods |
---|
1102 | n/a | '36P' # PyNumberMethods |
---|
1103 | n/a | '3P' # PyMappingMethods |
---|
1104 | n/a | '10P' # PySequenceMethods |
---|
1105 | n/a | '2P' # PyBufferProcs |
---|
1106 | n/a | '4P') |
---|
1107 | n/a | # Separate block for PyDictKeysObject with 8 keys and 5 entries |
---|
1108 | n/a | s += calcsize("2nP2n") + 8 + 5*calcsize("n2P") |
---|
1109 | n/a | # class |
---|
1110 | n/a | class newstyleclass(object): pass |
---|
1111 | n/a | check(newstyleclass, s) |
---|
1112 | n/a | # dict with shared keys |
---|
1113 | n/a | check(newstyleclass().__dict__, size('nQ2P' + '2nP2n')) |
---|
1114 | n/a | # unicode |
---|
1115 | n/a | # each tuple contains a string and its expected character size |
---|
1116 | n/a | # don't put any static strings here, as they may contain |
---|
1117 | n/a | # wchar_t or UTF-8 representations |
---|
1118 | n/a | samples = ['1'*100, '\xff'*50, |
---|
1119 | n/a | '\u0100'*40, '\uffff'*100, |
---|
1120 | n/a | '\U00010000'*30, '\U0010ffff'*100] |
---|
1121 | n/a | asciifields = "nnbP" |
---|
1122 | n/a | compactfields = asciifields + "nPn" |
---|
1123 | n/a | unicodefields = compactfields + "P" |
---|
1124 | n/a | for s in samples: |
---|
1125 | n/a | maxchar = ord(max(s)) |
---|
1126 | n/a | if maxchar < 128: |
---|
1127 | n/a | L = size(asciifields) + len(s) + 1 |
---|
1128 | n/a | elif maxchar < 256: |
---|
1129 | n/a | L = size(compactfields) + len(s) + 1 |
---|
1130 | n/a | elif maxchar < 65536: |
---|
1131 | n/a | L = size(compactfields) + 2*(len(s) + 1) |
---|
1132 | n/a | else: |
---|
1133 | n/a | L = size(compactfields) + 4*(len(s) + 1) |
---|
1134 | n/a | check(s, L) |
---|
1135 | n/a | # verify that the UTF-8 size is accounted for |
---|
1136 | n/a | s = chr(0x4000) # 4 bytes canonical representation |
---|
1137 | n/a | check(s, size(compactfields) + 4) |
---|
1138 | n/a | # compile() will trigger the generation of the UTF-8 |
---|
1139 | n/a | # representation as a side effect |
---|
1140 | n/a | compile(s, "<stdin>", "eval") |
---|
1141 | n/a | check(s, size(compactfields) + 4 + 4) |
---|
1142 | n/a | # TODO: add check that forces the presence of wchar_t representation |
---|
1143 | n/a | # TODO: add check that forces layout of unicodefields |
---|
1144 | n/a | # weakref |
---|
1145 | n/a | import weakref |
---|
1146 | n/a | check(weakref.ref(int), size('2Pn2P')) |
---|
1147 | n/a | # weakproxy |
---|
1148 | n/a | # XXX |
---|
1149 | n/a | # weakcallableproxy |
---|
1150 | n/a | check(weakref.proxy(int), size('2Pn2P')) |
---|
1151 | n/a | |
---|
1152 | n/a | def check_slots(self, obj, base, extra): |
---|
1153 | n/a | expected = sys.getsizeof(base) + struct.calcsize(extra) |
---|
1154 | n/a | if gc.is_tracked(obj) and not gc.is_tracked(base): |
---|
1155 | n/a | expected += self.gc_headsize |
---|
1156 | n/a | self.assertEqual(sys.getsizeof(obj), expected) |
---|
1157 | n/a | |
---|
1158 | n/a | def test_slots(self): |
---|
1159 | n/a | # check all subclassable types defined in Objects/ that allow |
---|
1160 | n/a | # non-empty __slots__ |
---|
1161 | n/a | check = self.check_slots |
---|
1162 | n/a | class BA(bytearray): |
---|
1163 | n/a | __slots__ = 'a', 'b', 'c' |
---|
1164 | n/a | check(BA(), bytearray(), '3P') |
---|
1165 | n/a | class D(dict): |
---|
1166 | n/a | __slots__ = 'a', 'b', 'c' |
---|
1167 | n/a | check(D(x=[]), {'x': []}, '3P') |
---|
1168 | n/a | class L(list): |
---|
1169 | n/a | __slots__ = 'a', 'b', 'c' |
---|
1170 | n/a | check(L(), [], '3P') |
---|
1171 | n/a | class S(set): |
---|
1172 | n/a | __slots__ = 'a', 'b', 'c' |
---|
1173 | n/a | check(S(), set(), '3P') |
---|
1174 | n/a | class FS(frozenset): |
---|
1175 | n/a | __slots__ = 'a', 'b', 'c' |
---|
1176 | n/a | check(FS(), frozenset(), '3P') |
---|
1177 | n/a | from collections import OrderedDict |
---|
1178 | n/a | class OD(OrderedDict): |
---|
1179 | n/a | __slots__ = 'a', 'b', 'c' |
---|
1180 | n/a | check(OD(x=[]), OrderedDict(x=[]), '3P') |
---|
1181 | n/a | |
---|
1182 | n/a | def test_pythontypes(self): |
---|
1183 | n/a | # check all types defined in Python/ |
---|
1184 | n/a | size = test.support.calcobjsize |
---|
1185 | n/a | vsize = test.support.calcvobjsize |
---|
1186 | n/a | check = self.check_sizeof |
---|
1187 | n/a | # _ast.AST |
---|
1188 | n/a | import _ast |
---|
1189 | n/a | check(_ast.AST(), size('P')) |
---|
1190 | n/a | try: |
---|
1191 | n/a | raise TypeError |
---|
1192 | n/a | except TypeError: |
---|
1193 | n/a | tb = sys.exc_info()[2] |
---|
1194 | n/a | # traceback |
---|
1195 | n/a | if tb is not None: |
---|
1196 | n/a | check(tb, size('2P2i')) |
---|
1197 | n/a | # symtable entry |
---|
1198 | n/a | # XXX |
---|
1199 | n/a | # sys.flags |
---|
1200 | n/a | check(sys.flags, vsize('') + self.P * len(sys.flags)) |
---|
1201 | n/a | |
---|
1202 | n/a | def test_asyncgen_hooks(self): |
---|
1203 | n/a | old = sys.get_asyncgen_hooks() |
---|
1204 | n/a | self.assertIsNone(old.firstiter) |
---|
1205 | n/a | self.assertIsNone(old.finalizer) |
---|
1206 | n/a | |
---|
1207 | n/a | firstiter = lambda *a: None |
---|
1208 | n/a | sys.set_asyncgen_hooks(firstiter=firstiter) |
---|
1209 | n/a | hooks = sys.get_asyncgen_hooks() |
---|
1210 | n/a | self.assertIs(hooks.firstiter, firstiter) |
---|
1211 | n/a | self.assertIs(hooks[0], firstiter) |
---|
1212 | n/a | self.assertIs(hooks.finalizer, None) |
---|
1213 | n/a | self.assertIs(hooks[1], None) |
---|
1214 | n/a | |
---|
1215 | n/a | finalizer = lambda *a: None |
---|
1216 | n/a | sys.set_asyncgen_hooks(finalizer=finalizer) |
---|
1217 | n/a | hooks = sys.get_asyncgen_hooks() |
---|
1218 | n/a | self.assertIs(hooks.firstiter, firstiter) |
---|
1219 | n/a | self.assertIs(hooks[0], firstiter) |
---|
1220 | n/a | self.assertIs(hooks.finalizer, finalizer) |
---|
1221 | n/a | self.assertIs(hooks[1], finalizer) |
---|
1222 | n/a | |
---|
1223 | n/a | sys.set_asyncgen_hooks(*old) |
---|
1224 | n/a | cur = sys.get_asyncgen_hooks() |
---|
1225 | n/a | self.assertIsNone(cur.firstiter) |
---|
1226 | n/a | self.assertIsNone(cur.finalizer) |
---|
1227 | n/a | |
---|
1228 | n/a | |
---|
1229 | n/a | def test_main(): |
---|
1230 | n/a | test.support.run_unittest(SysModuleTest, SizeofTest) |
---|
1231 | n/a | |
---|
1232 | n/a | if __name__ == "__main__": |
---|
1233 | n/a | test_main() |
---|