python (3.12.0)
1 import builtins
2 import codecs
3 import gc
4 import locale
5 import operator
6 import os
7 import struct
8 import subprocess
9 import sys
10 import sysconfig
11 import test.support
12 from test import support
13 from test.support import os_helper
14 from test.support.script_helper import assert_python_ok, assert_python_failure
15 from test.support import threading_helper
16 from test.support import import_helper
17 import textwrap
18 import unittest
19 import warnings
20
21
22 # count the number of test runs, used to create unique
23 # strings to intern in test_intern()
24 INTERN_NUMRUNS = 0
25
26 DICT_KEY_STRUCT_FORMAT = 'n2BI2n'
27
28 class ESC[4;38;5;81mDisplayHookTest(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
29
30 def test_original_displayhook(self):
31 dh = sys.__displayhook__
32
33 with support.captured_stdout() as out:
34 dh(42)
35
36 self.assertEqual(out.getvalue(), "42\n")
37 self.assertEqual(builtins._, 42)
38
39 del builtins._
40
41 with support.captured_stdout() as out:
42 dh(None)
43
44 self.assertEqual(out.getvalue(), "")
45 self.assertTrue(not hasattr(builtins, "_"))
46
47 # sys.displayhook() requires arguments
48 self.assertRaises(TypeError, dh)
49
50 stdout = sys.stdout
51 try:
52 del sys.stdout
53 self.assertRaises(RuntimeError, dh, 42)
54 finally:
55 sys.stdout = stdout
56
57 def test_lost_displayhook(self):
58 displayhook = sys.displayhook
59 try:
60 del sys.displayhook
61 code = compile("42", "<string>", "single")
62 self.assertRaises(RuntimeError, eval, code)
63 finally:
64 sys.displayhook = displayhook
65
66 def test_custom_displayhook(self):
67 def baddisplayhook(obj):
68 raise ValueError
69
70 with support.swap_attr(sys, 'displayhook', baddisplayhook):
71 code = compile("42", "<string>", "single")
72 self.assertRaises(ValueError, eval, code)
73
74 class ESC[4;38;5;81mActiveExceptionTests(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
75 def test_exc_info_no_exception(self):
76 self.assertEqual(sys.exc_info(), (None, None, None))
77
78 def test_sys_exception_no_exception(self):
79 self.assertEqual(sys.exception(), None)
80
81 def test_exc_info_with_exception_instance(self):
82 def f():
83 raise ValueError(42)
84
85 try:
86 f()
87 except Exception as e_:
88 e = e_
89 exc_info = sys.exc_info()
90
91 self.assertIsInstance(e, ValueError)
92 self.assertIs(exc_info[0], ValueError)
93 self.assertIs(exc_info[1], e)
94 self.assertIs(exc_info[2], e.__traceback__)
95
96 def test_exc_info_with_exception_type(self):
97 def f():
98 raise ValueError
99
100 try:
101 f()
102 except Exception as e_:
103 e = e_
104 exc_info = sys.exc_info()
105
106 self.assertIsInstance(e, ValueError)
107 self.assertIs(exc_info[0], ValueError)
108 self.assertIs(exc_info[1], e)
109 self.assertIs(exc_info[2], e.__traceback__)
110
111 def test_sys_exception_with_exception_instance(self):
112 def f():
113 raise ValueError(42)
114
115 try:
116 f()
117 except Exception as e_:
118 e = e_
119 exc = sys.exception()
120
121 self.assertIsInstance(e, ValueError)
122 self.assertIs(exc, e)
123
124 def test_sys_exception_with_exception_type(self):
125 def f():
126 raise ValueError
127
128 try:
129 f()
130 except Exception as e_:
131 e = e_
132 exc = sys.exception()
133
134 self.assertIsInstance(e, ValueError)
135 self.assertIs(exc, e)
136
137
138 class ESC[4;38;5;81mExceptHookTest(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
139
140 def test_original_excepthook(self):
141 try:
142 raise ValueError(42)
143 except ValueError as exc:
144 with support.captured_stderr() as err:
145 sys.__excepthook__(*sys.exc_info())
146
147 self.assertTrue(err.getvalue().endswith("ValueError: 42\n"))
148
149 self.assertRaises(TypeError, sys.__excepthook__)
150
151 def test_excepthook_bytes_filename(self):
152 # bpo-37467: sys.excepthook() must not crash if a filename
153 # is a bytes string
154 with warnings.catch_warnings():
155 warnings.simplefilter('ignore', BytesWarning)
156
157 try:
158 raise SyntaxError("msg", (b"bytes_filename", 123, 0, "text"))
159 except SyntaxError as exc:
160 with support.captured_stderr() as err:
161 sys.__excepthook__(*sys.exc_info())
162
163 err = err.getvalue()
164 self.assertIn(""" File "b'bytes_filename'", line 123\n""", err)
165 self.assertIn(""" text\n""", err)
166 self.assertTrue(err.endswith("SyntaxError: msg\n"))
167
168 def test_excepthook(self):
169 with test.support.captured_output("stderr") as stderr:
170 sys.excepthook(1, '1', 1)
171 self.assertTrue("TypeError: print_exception(): Exception expected for " \
172 "value, str found" in stderr.getvalue())
173
174 # FIXME: testing the code for a lost or replaced excepthook in
175 # Python/pythonrun.c::PyErr_PrintEx() is tricky.
176
177
178 class ESC[4;38;5;81mSysModuleTest(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
179
180 def tearDown(self):
181 test.support.reap_children()
182
183 def test_exit(self):
184 # call with two arguments
185 self.assertRaises(TypeError, sys.exit, 42, 42)
186
187 # call without argument
188 with self.assertRaises(SystemExit) as cm:
189 sys.exit()
190 self.assertIsNone(cm.exception.code)
191
192 rc, out, err = assert_python_ok('-c', 'import sys; sys.exit()')
193 self.assertEqual(rc, 0)
194 self.assertEqual(out, b'')
195 self.assertEqual(err, b'')
196
197 # call with integer argument
198 with self.assertRaises(SystemExit) as cm:
199 sys.exit(42)
200 self.assertEqual(cm.exception.code, 42)
201
202 # call with tuple argument with one entry
203 # entry will be unpacked
204 with self.assertRaises(SystemExit) as cm:
205 sys.exit((42,))
206 self.assertEqual(cm.exception.code, 42)
207
208 # call with string argument
209 with self.assertRaises(SystemExit) as cm:
210 sys.exit("exit")
211 self.assertEqual(cm.exception.code, "exit")
212
213 # call with tuple argument with two entries
214 with self.assertRaises(SystemExit) as cm:
215 sys.exit((17, 23))
216 self.assertEqual(cm.exception.code, (17, 23))
217
218 # test that the exit machinery handles SystemExits properly
219 rc, out, err = assert_python_failure('-c', 'raise SystemExit(47)')
220 self.assertEqual(rc, 47)
221 self.assertEqual(out, b'')
222 self.assertEqual(err, b'')
223
224 def check_exit_message(code, expected, **env_vars):
225 rc, out, err = assert_python_failure('-c', code, **env_vars)
226 self.assertEqual(rc, 1)
227 self.assertEqual(out, b'')
228 self.assertTrue(err.startswith(expected),
229 "%s doesn't start with %s" % (ascii(err), ascii(expected)))
230
231 # test that stderr buffer is flushed before the exit message is written
232 # into stderr
233 check_exit_message(
234 r'import sys; sys.stderr.write("unflushed,"); sys.exit("message")',
235 b"unflushed,message")
236
237 # test that the exit message is written with backslashreplace error
238 # handler to stderr
239 check_exit_message(
240 r'import sys; sys.exit("surrogates:\uDCFF")',
241 b"surrogates:\\udcff")
242
243 # test that the unicode message is encoded to the stderr encoding
244 # instead of the default encoding (utf8)
245 check_exit_message(
246 r'import sys; sys.exit("h\xe9")',
247 b"h\xe9", PYTHONIOENCODING='latin-1')
248
249 def test_getdefaultencoding(self):
250 self.assertRaises(TypeError, sys.getdefaultencoding, 42)
251 # can't check more than the type, as the user might have changed it
252 self.assertIsInstance(sys.getdefaultencoding(), str)
253
254 # testing sys.settrace() is done in test_sys_settrace.py
255 # testing sys.setprofile() is done in test_sys_setprofile.py
256
257 def test_switchinterval(self):
258 self.assertRaises(TypeError, sys.setswitchinterval)
259 self.assertRaises(TypeError, sys.setswitchinterval, "a")
260 self.assertRaises(ValueError, sys.setswitchinterval, -1.0)
261 self.assertRaises(ValueError, sys.setswitchinterval, 0.0)
262 orig = sys.getswitchinterval()
263 # sanity check
264 self.assertTrue(orig < 0.5, orig)
265 try:
266 for n in 0.00001, 0.05, 3.0, orig:
267 sys.setswitchinterval(n)
268 self.assertAlmostEqual(sys.getswitchinterval(), n)
269 finally:
270 sys.setswitchinterval(orig)
271
272 def test_getrecursionlimit(self):
273 limit = sys.getrecursionlimit()
274 self.assertIsInstance(limit, int)
275 self.assertGreater(limit, 1)
276
277 self.assertRaises(TypeError, sys.getrecursionlimit, 42)
278
279 def test_setrecursionlimit(self):
280 old_limit = sys.getrecursionlimit()
281 try:
282 sys.setrecursionlimit(10_005)
283 self.assertEqual(sys.getrecursionlimit(), 10_005)
284
285 self.assertRaises(TypeError, sys.setrecursionlimit)
286 self.assertRaises(ValueError, sys.setrecursionlimit, -42)
287 finally:
288 sys.setrecursionlimit(old_limit)
289
290 def test_recursionlimit_recovery(self):
291 if hasattr(sys, 'gettrace') and sys.gettrace():
292 self.skipTest('fatal error if run with a trace function')
293
294 old_limit = sys.getrecursionlimit()
295 def f():
296 f()
297 try:
298 for depth in (50, 75, 100, 250, 1000):
299 try:
300 sys.setrecursionlimit(depth)
301 except RecursionError:
302 # Issue #25274: The recursion limit is too low at the
303 # current recursion depth
304 continue
305
306 # Issue #5392: test stack overflow after hitting recursion
307 # limit twice
308 with self.assertRaises(RecursionError):
309 f()
310 with self.assertRaises(RecursionError):
311 f()
312 finally:
313 sys.setrecursionlimit(old_limit)
314
315 @test.support.cpython_only
316 def test_setrecursionlimit_to_depth(self):
317 # Issue #25274: Setting a low recursion limit must be blocked if the
318 # current recursion depth is already higher than limit.
319
320 old_limit = sys.getrecursionlimit()
321 try:
322 depth = support.get_recursion_depth()
323 with self.subTest(limit=sys.getrecursionlimit(), depth=depth):
324 # depth + 1 is OK
325 sys.setrecursionlimit(depth + 1)
326
327 # reset the limit to be able to call self.assertRaises()
328 # context manager
329 sys.setrecursionlimit(old_limit)
330 with self.assertRaises(RecursionError) as cm:
331 sys.setrecursionlimit(depth)
332 self.assertRegex(str(cm.exception),
333 "cannot set the recursion limit to [0-9]+ "
334 "at the recursion depth [0-9]+: "
335 "the limit is too low")
336 finally:
337 sys.setrecursionlimit(old_limit)
338
339 def test_getwindowsversion(self):
340 # Raise SkipTest if sys doesn't have getwindowsversion attribute
341 test.support.get_attribute(sys, "getwindowsversion")
342 v = sys.getwindowsversion()
343 self.assertEqual(len(v), 5)
344 self.assertIsInstance(v[0], int)
345 self.assertIsInstance(v[1], int)
346 self.assertIsInstance(v[2], int)
347 self.assertIsInstance(v[3], int)
348 self.assertIsInstance(v[4], str)
349 self.assertRaises(IndexError, operator.getitem, v, 5)
350 self.assertIsInstance(v.major, int)
351 self.assertIsInstance(v.minor, int)
352 self.assertIsInstance(v.build, int)
353 self.assertIsInstance(v.platform, int)
354 self.assertIsInstance(v.service_pack, str)
355 self.assertIsInstance(v.service_pack_minor, int)
356 self.assertIsInstance(v.service_pack_major, int)
357 self.assertIsInstance(v.suite_mask, int)
358 self.assertIsInstance(v.product_type, int)
359 self.assertEqual(v[0], v.major)
360 self.assertEqual(v[1], v.minor)
361 self.assertEqual(v[2], v.build)
362 self.assertEqual(v[3], v.platform)
363 self.assertEqual(v[4], v.service_pack)
364
365 # This is how platform.py calls it. Make sure tuple
366 # still has 5 elements
367 maj, min, buildno, plat, csd = sys.getwindowsversion()
368
369 def test_call_tracing(self):
370 self.assertRaises(TypeError, sys.call_tracing, type, 2)
371
372 @unittest.skipUnless(hasattr(sys, "setdlopenflags"),
373 'test needs sys.setdlopenflags()')
374 def test_dlopenflags(self):
375 self.assertTrue(hasattr(sys, "getdlopenflags"))
376 self.assertRaises(TypeError, sys.getdlopenflags, 42)
377 oldflags = sys.getdlopenflags()
378 self.assertRaises(TypeError, sys.setdlopenflags)
379 sys.setdlopenflags(oldflags+1)
380 self.assertEqual(sys.getdlopenflags(), oldflags+1)
381 sys.setdlopenflags(oldflags)
382
383 @test.support.refcount_test
384 def test_refcount(self):
385 # n here must be a global in order for this test to pass while
386 # tracing with a python function. Tracing calls PyFrame_FastToLocals
387 # which will add a copy of any locals to the frame object, causing
388 # the reference count to increase by 2 instead of 1.
389 global n
390 self.assertRaises(TypeError, sys.getrefcount)
391 c = sys.getrefcount(None)
392 n = None
393 # Singleton refcnts don't change
394 self.assertEqual(sys.getrefcount(None), c)
395 del n
396 self.assertEqual(sys.getrefcount(None), c)
397 if hasattr(sys, "gettotalrefcount"):
398 self.assertIsInstance(sys.gettotalrefcount(), int)
399
400 def test_getframe(self):
401 self.assertRaises(TypeError, sys._getframe, 42, 42)
402 self.assertRaises(ValueError, sys._getframe, 2000000000)
403 self.assertTrue(
404 SysModuleTest.test_getframe.__code__ \
405 is sys._getframe().f_code
406 )
407
408 def test_getframemodulename(self):
409 # Default depth gets ourselves
410 self.assertEqual(__name__, sys._getframemodulename())
411 self.assertEqual("unittest.case", sys._getframemodulename(1))
412 i = 0
413 f = sys._getframe(i)
414 while f:
415 self.assertEqual(
416 f.f_globals['__name__'],
417 sys._getframemodulename(i) or '__main__'
418 )
419 i += 1
420 f2 = f.f_back
421 try:
422 f = sys._getframe(i)
423 except ValueError:
424 break
425 self.assertIs(f, f2)
426 self.assertIsNone(sys._getframemodulename(i))
427
428 # sys._current_frames() is a CPython-only gimmick.
429 @threading_helper.reap_threads
430 @threading_helper.requires_working_threading()
431 def test_current_frames(self):
432 import threading
433 import traceback
434
435 # Spawn a thread that blocks at a known place. Then the main
436 # thread does sys._current_frames(), and verifies that the frames
437 # returned make sense.
438 entered_g = threading.Event()
439 leave_g = threading.Event()
440 thread_info = [] # the thread's id
441
442 def f123():
443 g456()
444
445 def g456():
446 thread_info.append(threading.get_ident())
447 entered_g.set()
448 leave_g.wait()
449
450 t = threading.Thread(target=f123)
451 t.start()
452 entered_g.wait()
453
454 try:
455 # At this point, t has finished its entered_g.set(), although it's
456 # impossible to guess whether it's still on that line or has moved on
457 # to its leave_g.wait().
458 self.assertEqual(len(thread_info), 1)
459 thread_id = thread_info[0]
460
461 d = sys._current_frames()
462 for tid in d:
463 self.assertIsInstance(tid, int)
464 self.assertGreater(tid, 0)
465
466 main_id = threading.get_ident()
467 self.assertIn(main_id, d)
468 self.assertIn(thread_id, d)
469
470 # Verify that the captured main-thread frame is _this_ frame.
471 frame = d.pop(main_id)
472 self.assertTrue(frame is sys._getframe())
473
474 # Verify that the captured thread frame is blocked in g456, called
475 # from f123. This is a little tricky, since various bits of
476 # threading.py are also in the thread's call stack.
477 frame = d.pop(thread_id)
478 stack = traceback.extract_stack(frame)
479 for i, (filename, lineno, funcname, sourceline) in enumerate(stack):
480 if funcname == "f123":
481 break
482 else:
483 self.fail("didn't find f123() on thread's call stack")
484
485 self.assertEqual(sourceline, "g456()")
486
487 # And the next record must be for g456().
488 filename, lineno, funcname, sourceline = stack[i+1]
489 self.assertEqual(funcname, "g456")
490 self.assertIn(sourceline, ["leave_g.wait()", "entered_g.set()"])
491 finally:
492 # Reap the spawned thread.
493 leave_g.set()
494 t.join()
495
496 @threading_helper.reap_threads
497 @threading_helper.requires_working_threading()
498 def test_current_exceptions(self):
499 import threading
500 import traceback
501
502 # Spawn a thread that blocks at a known place. Then the main
503 # thread does sys._current_frames(), and verifies that the frames
504 # returned make sense.
505 entered_g = threading.Event()
506 leave_g = threading.Event()
507 thread_info = [] # the thread's id
508
509 def f123():
510 g456()
511
512 def g456():
513 thread_info.append(threading.get_ident())
514 entered_g.set()
515 while True:
516 try:
517 raise ValueError("oops")
518 except ValueError:
519 if leave_g.wait(timeout=support.LONG_TIMEOUT):
520 break
521
522 t = threading.Thread(target=f123)
523 t.start()
524 entered_g.wait()
525
526 try:
527 # At this point, t has finished its entered_g.set(), although it's
528 # impossible to guess whether it's still on that line or has moved on
529 # to its leave_g.wait().
530 self.assertEqual(len(thread_info), 1)
531 thread_id = thread_info[0]
532
533 d = sys._current_exceptions()
534 for tid in d:
535 self.assertIsInstance(tid, int)
536 self.assertGreater(tid, 0)
537
538 main_id = threading.get_ident()
539 self.assertIn(main_id, d)
540 self.assertIn(thread_id, d)
541 self.assertEqual(None, d.pop(main_id))
542
543 # Verify that the captured thread frame is blocked in g456, called
544 # from f123. This is a little tricky, since various bits of
545 # threading.py are also in the thread's call stack.
546 exc_value = d.pop(thread_id)
547 stack = traceback.extract_stack(exc_value.__traceback__.tb_frame)
548 for i, (filename, lineno, funcname, sourceline) in enumerate(stack):
549 if funcname == "f123":
550 break
551 else:
552 self.fail("didn't find f123() on thread's call stack")
553
554 self.assertEqual(sourceline, "g456()")
555
556 # And the next record must be for g456().
557 filename, lineno, funcname, sourceline = stack[i+1]
558 self.assertEqual(funcname, "g456")
559 self.assertTrue(sourceline.startswith("if leave_g.wait("))
560 finally:
561 # Reap the spawned thread.
562 leave_g.set()
563 t.join()
564
565 def test_attributes(self):
566 self.assertIsInstance(sys.api_version, int)
567 self.assertIsInstance(sys.argv, list)
568 for arg in sys.argv:
569 self.assertIsInstance(arg, str)
570 self.assertIsInstance(sys.orig_argv, list)
571 for arg in sys.orig_argv:
572 self.assertIsInstance(arg, str)
573 self.assertIn(sys.byteorder, ("little", "big"))
574 self.assertIsInstance(sys.builtin_module_names, tuple)
575 self.assertIsInstance(sys.copyright, str)
576 self.assertIsInstance(sys.exec_prefix, str)
577 self.assertIsInstance(sys.base_exec_prefix, str)
578 self.assertIsInstance(sys.executable, str)
579 self.assertEqual(len(sys.float_info), 11)
580 self.assertEqual(sys.float_info.radix, 2)
581 self.assertEqual(len(sys.int_info), 4)
582 self.assertTrue(sys.int_info.bits_per_digit % 5 == 0)
583 self.assertTrue(sys.int_info.sizeof_digit >= 1)
584 self.assertGreaterEqual(sys.int_info.default_max_str_digits, 500)
585 self.assertGreaterEqual(sys.int_info.str_digits_check_threshold, 100)
586 self.assertGreater(sys.int_info.default_max_str_digits,
587 sys.int_info.str_digits_check_threshold)
588 self.assertEqual(type(sys.int_info.bits_per_digit), int)
589 self.assertEqual(type(sys.int_info.sizeof_digit), int)
590 self.assertIsInstance(sys.int_info.default_max_str_digits, int)
591 self.assertIsInstance(sys.int_info.str_digits_check_threshold, int)
592 self.assertIsInstance(sys.hexversion, int)
593
594 self.assertEqual(len(sys.hash_info), 9)
595 self.assertLess(sys.hash_info.modulus, 2**sys.hash_info.width)
596 # sys.hash_info.modulus should be a prime; we do a quick
597 # probable primality test (doesn't exclude the possibility of
598 # a Carmichael number)
599 for x in range(1, 100):
600 self.assertEqual(
601 pow(x, sys.hash_info.modulus-1, sys.hash_info.modulus),
602 1,
603 "sys.hash_info.modulus {} is a non-prime".format(
604 sys.hash_info.modulus)
605 )
606 self.assertIsInstance(sys.hash_info.inf, int)
607 self.assertIsInstance(sys.hash_info.nan, int)
608 self.assertIsInstance(sys.hash_info.imag, int)
609 algo = sysconfig.get_config_var("Py_HASH_ALGORITHM")
610 if sys.hash_info.algorithm in {"fnv", "siphash13", "siphash24"}:
611 self.assertIn(sys.hash_info.hash_bits, {32, 64})
612 self.assertIn(sys.hash_info.seed_bits, {32, 64, 128})
613
614 if algo == 1:
615 self.assertEqual(sys.hash_info.algorithm, "siphash24")
616 elif algo == 2:
617 self.assertEqual(sys.hash_info.algorithm, "fnv")
618 elif algo == 3:
619 self.assertEqual(sys.hash_info.algorithm, "siphash13")
620 else:
621 self.assertIn(sys.hash_info.algorithm, {"fnv", "siphash13", "siphash24"})
622 else:
623 # PY_HASH_EXTERNAL
624 self.assertEqual(algo, 0)
625 self.assertGreaterEqual(sys.hash_info.cutoff, 0)
626 self.assertLess(sys.hash_info.cutoff, 8)
627
628 self.assertIsInstance(sys.maxsize, int)
629 self.assertIsInstance(sys.maxunicode, int)
630 self.assertEqual(sys.maxunicode, 0x10FFFF)
631 self.assertIsInstance(sys.platform, str)
632 self.assertIsInstance(sys.prefix, str)
633 self.assertIsInstance(sys.base_prefix, str)
634 self.assertIsInstance(sys.platlibdir, str)
635 self.assertIsInstance(sys.version, str)
636 vi = sys.version_info
637 self.assertIsInstance(vi[:], tuple)
638 self.assertEqual(len(vi), 5)
639 self.assertIsInstance(vi[0], int)
640 self.assertIsInstance(vi[1], int)
641 self.assertIsInstance(vi[2], int)
642 self.assertIn(vi[3], ("alpha", "beta", "candidate", "final"))
643 self.assertIsInstance(vi[4], int)
644 self.assertIsInstance(vi.major, int)
645 self.assertIsInstance(vi.minor, int)
646 self.assertIsInstance(vi.micro, int)
647 self.assertIn(vi.releaselevel, ("alpha", "beta", "candidate", "final"))
648 self.assertIsInstance(vi.serial, int)
649 self.assertEqual(vi[0], vi.major)
650 self.assertEqual(vi[1], vi.minor)
651 self.assertEqual(vi[2], vi.micro)
652 self.assertEqual(vi[3], vi.releaselevel)
653 self.assertEqual(vi[4], vi.serial)
654 self.assertTrue(vi > (1,0,0))
655 self.assertIsInstance(sys.float_repr_style, str)
656 self.assertIn(sys.float_repr_style, ('short', 'legacy'))
657 if not sys.platform.startswith('win'):
658 self.assertIsInstance(sys.abiflags, str)
659
660 def test_thread_info(self):
661 info = sys.thread_info
662 self.assertEqual(len(info), 3)
663 self.assertIn(info.name, ('nt', 'pthread', 'pthread-stubs', 'solaris', None))
664 self.assertIn(info.lock, ('semaphore', 'mutex+cond', None))
665 if sys.platform.startswith(("linux", "freebsd")):
666 self.assertEqual(info.name, "pthread")
667 elif sys.platform == "win32":
668 self.assertEqual(info.name, "nt")
669 elif sys.platform == "emscripten":
670 self.assertIn(info.name, {"pthread", "pthread-stubs"})
671 elif sys.platform == "wasi":
672 self.assertEqual(info.name, "pthread-stubs")
673
674 @unittest.skipUnless(support.is_emscripten, "only available on Emscripten")
675 def test_emscripten_info(self):
676 self.assertEqual(len(sys._emscripten_info), 4)
677 self.assertIsInstance(sys._emscripten_info.emscripten_version, tuple)
678 self.assertIsInstance(sys._emscripten_info.runtime, (str, type(None)))
679 self.assertIsInstance(sys._emscripten_info.pthreads, bool)
680 self.assertIsInstance(sys._emscripten_info.shared_memory, bool)
681
682 def test_43581(self):
683 # Can't use sys.stdout, as this is a StringIO object when
684 # the test runs under regrtest.
685 self.assertEqual(sys.__stdout__.encoding, sys.__stderr__.encoding)
686
687 def test_intern(self):
688 global INTERN_NUMRUNS
689 INTERN_NUMRUNS += 1
690 self.assertRaises(TypeError, sys.intern)
691 s = "never interned before" + str(INTERN_NUMRUNS)
692 self.assertTrue(sys.intern(s) is s)
693 s2 = s.swapcase().swapcase()
694 self.assertTrue(sys.intern(s2) is s)
695
696 # Subclasses of string can't be interned, because they
697 # provide too much opportunity for insane things to happen.
698 # We don't want them in the interned dict and if they aren't
699 # actually interned, we don't want to create the appearance
700 # that they are by allowing intern() to succeed.
701 class ESC[4;38;5;81mS(ESC[4;38;5;149mstr):
702 def __hash__(self):
703 return 123
704
705 self.assertRaises(TypeError, sys.intern, S("abc"))
706
707 def test_sys_flags(self):
708 self.assertTrue(sys.flags)
709 attrs = ("debug",
710 "inspect", "interactive", "optimize",
711 "dont_write_bytecode", "no_user_site", "no_site",
712 "ignore_environment", "verbose", "bytes_warning", "quiet",
713 "hash_randomization", "isolated", "dev_mode", "utf8_mode",
714 "warn_default_encoding", "safe_path", "int_max_str_digits")
715 for attr in attrs:
716 self.assertTrue(hasattr(sys.flags, attr), attr)
717 attr_type = bool if attr in ("dev_mode", "safe_path") else int
718 self.assertEqual(type(getattr(sys.flags, attr)), attr_type, attr)
719 self.assertTrue(repr(sys.flags))
720 self.assertEqual(len(sys.flags), len(attrs))
721
722 self.assertIn(sys.flags.utf8_mode, {0, 1, 2})
723
724 def assert_raise_on_new_sys_type(self, sys_attr):
725 # Users are intentionally prevented from creating new instances of
726 # sys.flags, sys.version_info, and sys.getwindowsversion.
727 arg = sys_attr
728 attr_type = type(sys_attr)
729 with self.assertRaises(TypeError):
730 attr_type(arg)
731 with self.assertRaises(TypeError):
732 attr_type.__new__(attr_type, arg)
733
734 def test_sys_flags_no_instantiation(self):
735 self.assert_raise_on_new_sys_type(sys.flags)
736
737 def test_sys_version_info_no_instantiation(self):
738 self.assert_raise_on_new_sys_type(sys.version_info)
739
740 def test_sys_getwindowsversion_no_instantiation(self):
741 # Skip if not being run on Windows.
742 test.support.get_attribute(sys, "getwindowsversion")
743 self.assert_raise_on_new_sys_type(sys.getwindowsversion())
744
745 @test.support.cpython_only
746 def test_clear_type_cache(self):
747 sys._clear_type_cache()
748
749 @support.requires_subprocess()
750 def test_ioencoding(self):
751 env = dict(os.environ)
752
753 # Test character: cent sign, encoded as 0x4A (ASCII J) in CP424,
754 # not representable in ASCII.
755
756 env["PYTHONIOENCODING"] = "cp424"
757 p = subprocess.Popen([sys.executable, "-c", 'print(chr(0xa2))'],
758 stdout = subprocess.PIPE, env=env)
759 out = p.communicate()[0].strip()
760 expected = ("\xa2" + os.linesep).encode("cp424")
761 self.assertEqual(out, expected)
762
763 env["PYTHONIOENCODING"] = "ascii:replace"
764 p = subprocess.Popen([sys.executable, "-c", 'print(chr(0xa2))'],
765 stdout = subprocess.PIPE, env=env)
766 out = p.communicate()[0].strip()
767 self.assertEqual(out, b'?')
768
769 env["PYTHONIOENCODING"] = "ascii"
770 p = subprocess.Popen([sys.executable, "-c", 'print(chr(0xa2))'],
771 stdout=subprocess.PIPE, stderr=subprocess.PIPE,
772 env=env)
773 out, err = p.communicate()
774 self.assertEqual(out, b'')
775 self.assertIn(b'UnicodeEncodeError:', err)
776 self.assertIn(rb"'\xa2'", err)
777
778 env["PYTHONIOENCODING"] = "ascii:"
779 p = subprocess.Popen([sys.executable, "-c", 'print(chr(0xa2))'],
780 stdout=subprocess.PIPE, stderr=subprocess.PIPE,
781 env=env)
782 out, err = p.communicate()
783 self.assertEqual(out, b'')
784 self.assertIn(b'UnicodeEncodeError:', err)
785 self.assertIn(rb"'\xa2'", err)
786
787 env["PYTHONIOENCODING"] = ":surrogateescape"
788 p = subprocess.Popen([sys.executable, "-c", 'print(chr(0xdcbd))'],
789 stdout=subprocess.PIPE, env=env)
790 out = p.communicate()[0].strip()
791 self.assertEqual(out, b'\xbd')
792
793 @unittest.skipUnless(os_helper.FS_NONASCII,
794 'requires OS support of non-ASCII encodings')
795 @unittest.skipUnless(sys.getfilesystemencoding() == locale.getpreferredencoding(False),
796 'requires FS encoding to match locale')
797 @support.requires_subprocess()
798 def test_ioencoding_nonascii(self):
799 env = dict(os.environ)
800
801 env["PYTHONIOENCODING"] = ""
802 p = subprocess.Popen([sys.executable, "-c",
803 'print(%a)' % os_helper.FS_NONASCII],
804 stdout=subprocess.PIPE, env=env)
805 out = p.communicate()[0].strip()
806 self.assertEqual(out, os.fsencode(os_helper.FS_NONASCII))
807
808 @unittest.skipIf(sys.base_prefix != sys.prefix,
809 'Test is not venv-compatible')
810 @support.requires_subprocess()
811 def test_executable(self):
812 # sys.executable should be absolute
813 self.assertEqual(os.path.abspath(sys.executable), sys.executable)
814
815 # Issue #7774: Ensure that sys.executable is an empty string if argv[0]
816 # has been set to a non existent program name and Python is unable to
817 # retrieve the real program name
818
819 # For a normal installation, it should work without 'cwd'
820 # argument. For test runs in the build directory, see #7774.
821 python_dir = os.path.dirname(os.path.realpath(sys.executable))
822 p = subprocess.Popen(
823 ["nonexistent", "-c",
824 'import sys; print(sys.executable.encode("ascii", "backslashreplace"))'],
825 executable=sys.executable, stdout=subprocess.PIPE, cwd=python_dir)
826 stdout = p.communicate()[0]
827 executable = stdout.strip().decode("ASCII")
828 p.wait()
829 self.assertIn(executable, ["b''", repr(sys.executable.encode("ascii", "backslashreplace"))])
830
831 def check_fsencoding(self, fs_encoding, expected=None):
832 self.assertIsNotNone(fs_encoding)
833 codecs.lookup(fs_encoding)
834 if expected:
835 self.assertEqual(fs_encoding, expected)
836
837 def test_getfilesystemencoding(self):
838 fs_encoding = sys.getfilesystemencoding()
839 if sys.platform == 'darwin':
840 expected = 'utf-8'
841 else:
842 expected = None
843 self.check_fsencoding(fs_encoding, expected)
844
845 def c_locale_get_error_handler(self, locale, isolated=False, encoding=None):
846 # Force the POSIX locale
847 env = os.environ.copy()
848 env["LC_ALL"] = locale
849 env["PYTHONCOERCECLOCALE"] = "0"
850 code = '\n'.join((
851 'import sys',
852 'def dump(name):',
853 ' std = getattr(sys, name)',
854 ' print("%s: %s" % (name, std.errors))',
855 'dump("stdin")',
856 'dump("stdout")',
857 'dump("stderr")',
858 ))
859 args = [sys.executable, "-X", "utf8=0", "-c", code]
860 if isolated:
861 args.append("-I")
862 if encoding is not None:
863 env['PYTHONIOENCODING'] = encoding
864 else:
865 env.pop('PYTHONIOENCODING', None)
866 p = subprocess.Popen(args,
867 stdout=subprocess.PIPE,
868 stderr=subprocess.STDOUT,
869 env=env,
870 universal_newlines=True)
871 stdout, stderr = p.communicate()
872 return stdout
873
874 def check_locale_surrogateescape(self, locale):
875 out = self.c_locale_get_error_handler(locale, isolated=True)
876 self.assertEqual(out,
877 'stdin: surrogateescape\n'
878 'stdout: surrogateescape\n'
879 'stderr: backslashreplace\n')
880
881 # replace the default error handler
882 out = self.c_locale_get_error_handler(locale, encoding=':ignore')
883 self.assertEqual(out,
884 'stdin: ignore\n'
885 'stdout: ignore\n'
886 'stderr: backslashreplace\n')
887
888 # force the encoding
889 out = self.c_locale_get_error_handler(locale, encoding='iso8859-1')
890 self.assertEqual(out,
891 'stdin: strict\n'
892 'stdout: strict\n'
893 'stderr: backslashreplace\n')
894 out = self.c_locale_get_error_handler(locale, encoding='iso8859-1:')
895 self.assertEqual(out,
896 'stdin: strict\n'
897 'stdout: strict\n'
898 'stderr: backslashreplace\n')
899
900 # have no any effect
901 out = self.c_locale_get_error_handler(locale, encoding=':')
902 self.assertEqual(out,
903 'stdin: surrogateescape\n'
904 'stdout: surrogateescape\n'
905 'stderr: backslashreplace\n')
906 out = self.c_locale_get_error_handler(locale, encoding='')
907 self.assertEqual(out,
908 'stdin: surrogateescape\n'
909 'stdout: surrogateescape\n'
910 'stderr: backslashreplace\n')
911
912 @support.requires_subprocess()
913 def test_c_locale_surrogateescape(self):
914 self.check_locale_surrogateescape('C')
915
916 @support.requires_subprocess()
917 def test_posix_locale_surrogateescape(self):
918 self.check_locale_surrogateescape('POSIX')
919
920 def test_implementation(self):
921 # This test applies to all implementations equally.
922
923 levels = {'alpha': 0xA, 'beta': 0xB, 'candidate': 0xC, 'final': 0xF}
924
925 self.assertTrue(hasattr(sys.implementation, 'name'))
926 self.assertTrue(hasattr(sys.implementation, 'version'))
927 self.assertTrue(hasattr(sys.implementation, 'hexversion'))
928 self.assertTrue(hasattr(sys.implementation, 'cache_tag'))
929
930 version = sys.implementation.version
931 self.assertEqual(version[:2], (version.major, version.minor))
932
933 hexversion = (version.major << 24 | version.minor << 16 |
934 version.micro << 8 | levels[version.releaselevel] << 4 |
935 version.serial << 0)
936 self.assertEqual(sys.implementation.hexversion, hexversion)
937
938 # PEP 421 requires that .name be lower case.
939 self.assertEqual(sys.implementation.name,
940 sys.implementation.name.lower())
941
942 @test.support.cpython_only
943 def test_debugmallocstats(self):
944 # Test sys._debugmallocstats()
945 from test.support.script_helper import assert_python_ok
946 args = ['-c', 'import sys; sys._debugmallocstats()']
947 ret, out, err = assert_python_ok(*args)
948
949 # Output of sys._debugmallocstats() depends on configure flags.
950 # The sysconfig vars are not available on Windows.
951 if sys.platform != "win32":
952 with_freelists = sysconfig.get_config_var("WITH_FREELISTS")
953 with_pymalloc = sysconfig.get_config_var("WITH_PYMALLOC")
954 if with_freelists:
955 self.assertIn(b"free PyDictObjects", err)
956 if with_pymalloc:
957 self.assertIn(b'Small block threshold', err)
958 if not with_freelists and not with_pymalloc:
959 self.assertFalse(err)
960
961 # The function has no parameter
962 self.assertRaises(TypeError, sys._debugmallocstats, True)
963
964 @unittest.skipUnless(hasattr(sys, "getallocatedblocks"),
965 "sys.getallocatedblocks unavailable on this build")
966 def test_getallocatedblocks(self):
967 try:
968 import _testcapi
969 except ImportError:
970 with_pymalloc = support.with_pymalloc()
971 else:
972 try:
973 alloc_name = _testcapi.pymem_getallocatorsname()
974 except RuntimeError as exc:
975 # "cannot get allocators name" (ex: tracemalloc is used)
976 with_pymalloc = True
977 else:
978 with_pymalloc = (alloc_name in ('pymalloc', 'pymalloc_debug'))
979
980 # Some sanity checks
981 a = sys.getallocatedblocks()
982 self.assertIs(type(a), int)
983 if with_pymalloc:
984 self.assertGreater(a, 0)
985 else:
986 # When WITH_PYMALLOC isn't available, we don't know anything
987 # about the underlying implementation: the function might
988 # return 0 or something greater.
989 self.assertGreaterEqual(a, 0)
990 try:
991 # While we could imagine a Python session where the number of
992 # multiple buffer objects would exceed the sharing of references,
993 # it is unlikely to happen in a normal test run.
994 self.assertLess(a, sys.gettotalrefcount())
995 except AttributeError:
996 # gettotalrefcount() not available
997 pass
998 gc.collect()
999 b = sys.getallocatedblocks()
1000 self.assertLessEqual(b, a)
1001 gc.collect()
1002 c = sys.getallocatedblocks()
1003 self.assertIn(c, range(b - 50, b + 50))
1004
1005 def test_is_finalizing(self):
1006 self.assertIs(sys.is_finalizing(), False)
1007 # Don't use the atexit module because _Py_Finalizing is only set
1008 # after calling atexit callbacks
1009 code = """if 1:
1010 import sys
1011
1012 class AtExit:
1013 is_finalizing = sys.is_finalizing
1014 print = print
1015
1016 def __del__(self):
1017 self.print(self.is_finalizing(), flush=True)
1018
1019 # Keep a reference in the __main__ module namespace, so the
1020 # AtExit destructor will be called at Python exit
1021 ref = AtExit()
1022 """
1023 rc, stdout, stderr = assert_python_ok('-c', code)
1024 self.assertEqual(stdout.rstrip(), b'True')
1025
1026 def test_issue20602(self):
1027 # sys.flags and sys.float_info were wiped during shutdown.
1028 code = """if 1:
1029 import sys
1030 class A:
1031 def __del__(self, sys=sys):
1032 print(sys.flags)
1033 print(sys.float_info)
1034 a = A()
1035 """
1036 rc, out, err = assert_python_ok('-c', code)
1037 out = out.splitlines()
1038 self.assertIn(b'sys.flags', out[0])
1039 self.assertIn(b'sys.float_info', out[1])
1040
1041 def test_sys_ignores_cleaning_up_user_data(self):
1042 code = """if 1:
1043 import struct, sys
1044
1045 class C:
1046 def __init__(self):
1047 self.pack = struct.pack
1048 def __del__(self):
1049 self.pack('I', -42)
1050
1051 sys.x = C()
1052 """
1053 rc, stdout, stderr = assert_python_ok('-c', code)
1054 self.assertEqual(rc, 0)
1055 self.assertEqual(stdout.rstrip(), b"")
1056 self.assertEqual(stderr.rstrip(), b"")
1057
1058 @unittest.skipUnless(hasattr(sys, 'getandroidapilevel'),
1059 'need sys.getandroidapilevel()')
1060 def test_getandroidapilevel(self):
1061 level = sys.getandroidapilevel()
1062 self.assertIsInstance(level, int)
1063 self.assertGreater(level, 0)
1064
1065 @support.requires_subprocess()
1066 def test_sys_tracebacklimit(self):
1067 code = """if 1:
1068 import sys
1069 def f1():
1070 1 / 0
1071 def f2():
1072 f1()
1073 sys.tracebacklimit = %r
1074 f2()
1075 """
1076 def check(tracebacklimit, expected):
1077 p = subprocess.Popen([sys.executable, '-c', code % tracebacklimit],
1078 stderr=subprocess.PIPE)
1079 out = p.communicate()[1]
1080 self.assertEqual(out.splitlines(), expected)
1081
1082 traceback = [
1083 b'Traceback (most recent call last):',
1084 b' File "<string>", line 8, in <module>',
1085 b' File "<string>", line 6, in f2',
1086 b' File "<string>", line 4, in f1',
1087 b'ZeroDivisionError: division by zero'
1088 ]
1089 check(10, traceback)
1090 check(3, traceback)
1091 check(2, traceback[:1] + traceback[2:])
1092 check(1, traceback[:1] + traceback[3:])
1093 check(0, [traceback[-1]])
1094 check(-1, [traceback[-1]])
1095 check(1<<1000, traceback)
1096 check(-1<<1000, [traceback[-1]])
1097 check(None, traceback)
1098
1099 def test_no_duplicates_in_meta_path(self):
1100 self.assertEqual(len(sys.meta_path), len(set(sys.meta_path)))
1101
1102 @unittest.skipUnless(hasattr(sys, "_enablelegacywindowsfsencoding"),
1103 'needs sys._enablelegacywindowsfsencoding()')
1104 def test__enablelegacywindowsfsencoding(self):
1105 code = ('import sys',
1106 'sys._enablelegacywindowsfsencoding()',
1107 'print(sys.getfilesystemencoding(), sys.getfilesystemencodeerrors())')
1108 rc, out, err = assert_python_ok('-c', '; '.join(code))
1109 out = out.decode('ascii', 'replace').rstrip()
1110 self.assertEqual(out, 'mbcs replace')
1111
1112 @support.requires_subprocess()
1113 def test_orig_argv(self):
1114 code = textwrap.dedent('''
1115 import sys
1116 print(sys.argv)
1117 print(sys.orig_argv)
1118 ''')
1119 args = [sys.executable, '-I', '-X', 'utf8', '-c', code, 'arg']
1120 proc = subprocess.run(args, check=True, capture_output=True, text=True)
1121 expected = [
1122 repr(['-c', 'arg']), # sys.argv
1123 repr(args), # sys.orig_argv
1124 ]
1125 self.assertEqual(proc.stdout.rstrip().splitlines(), expected,
1126 proc)
1127
1128 def test_module_names(self):
1129 self.assertIsInstance(sys.stdlib_module_names, frozenset)
1130 for name in sys.stdlib_module_names:
1131 self.assertIsInstance(name, str)
1132
1133 def test_stdlib_dir(self):
1134 os = import_helper.import_fresh_module('os')
1135 marker = getattr(os, '__file__', None)
1136 if marker and not os.path.exists(marker):
1137 marker = None
1138 expected = os.path.dirname(marker) if marker else None
1139 self.assertEqual(os.path.normpath(sys._stdlib_dir),
1140 os.path.normpath(expected))
1141
1142
1143 @test.support.cpython_only
1144 class ESC[4;38;5;81mUnraisableHookTest(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
1145 def write_unraisable_exc(self, exc, err_msg, obj):
1146 import _testcapi
1147 import types
1148 err_msg2 = f"Exception ignored {err_msg}"
1149 try:
1150 _testcapi.write_unraisable_exc(exc, err_msg, obj)
1151 return types.SimpleNamespace(exc_type=type(exc),
1152 exc_value=exc,
1153 exc_traceback=exc.__traceback__,
1154 err_msg=err_msg2,
1155 object=obj)
1156 finally:
1157 # Explicitly break any reference cycle
1158 exc = None
1159
1160 def test_original_unraisablehook(self):
1161 for err_msg in (None, "original hook"):
1162 with self.subTest(err_msg=err_msg):
1163 obj = "an object"
1164
1165 with test.support.captured_output("stderr") as stderr:
1166 with test.support.swap_attr(sys, 'unraisablehook',
1167 sys.__unraisablehook__):
1168 self.write_unraisable_exc(ValueError(42), err_msg, obj)
1169
1170 err = stderr.getvalue()
1171 if err_msg is not None:
1172 self.assertIn(f'Exception ignored {err_msg}: {obj!r}\n', err)
1173 else:
1174 self.assertIn(f'Exception ignored in: {obj!r}\n', err)
1175 self.assertIn('Traceback (most recent call last):\n', err)
1176 self.assertIn('ValueError: 42\n', err)
1177
1178 def test_original_unraisablehook_err(self):
1179 # bpo-22836: PyErr_WriteUnraisable() should give sensible reports
1180 class ESC[4;38;5;81mBrokenDel:
1181 def __del__(self):
1182 exc = ValueError("del is broken")
1183 # The following line is included in the traceback report:
1184 raise exc
1185
1186 class ESC[4;38;5;81mBrokenStrException(ESC[4;38;5;149mException):
1187 def __str__(self):
1188 raise Exception("str() is broken")
1189
1190 class ESC[4;38;5;81mBrokenExceptionDel:
1191 def __del__(self):
1192 exc = BrokenStrException()
1193 # The following line is included in the traceback report:
1194 raise exc
1195
1196 for test_class in (BrokenDel, BrokenExceptionDel):
1197 with self.subTest(test_class):
1198 obj = test_class()
1199 with test.support.captured_stderr() as stderr, \
1200 test.support.swap_attr(sys, 'unraisablehook',
1201 sys.__unraisablehook__):
1202 # Trigger obj.__del__()
1203 del obj
1204
1205 report = stderr.getvalue()
1206 self.assertIn("Exception ignored", report)
1207 self.assertIn(test_class.__del__.__qualname__, report)
1208 self.assertIn("test_sys.py", report)
1209 self.assertIn("raise exc", report)
1210 if test_class is BrokenExceptionDel:
1211 self.assertIn("BrokenStrException", report)
1212 self.assertIn("<exception str() failed>", report)
1213 else:
1214 self.assertIn("ValueError", report)
1215 self.assertIn("del is broken", report)
1216 self.assertTrue(report.endswith("\n"))
1217
1218 def test_original_unraisablehook_exception_qualname(self):
1219 # See bpo-41031, bpo-45083.
1220 # Check that the exception is printed with its qualified name
1221 # rather than just classname, and the module names appears
1222 # unless it is one of the hard-coded exclusions.
1223 class ESC[4;38;5;81mA:
1224 class ESC[4;38;5;81mB:
1225 class ESC[4;38;5;81mX(ESC[4;38;5;149mException):
1226 pass
1227
1228 for moduleName in 'builtins', '__main__', 'some_module':
1229 with self.subTest(moduleName=moduleName):
1230 A.B.X.__module__ = moduleName
1231 with test.support.captured_stderr() as stderr, test.support.swap_attr(
1232 sys, 'unraisablehook', sys.__unraisablehook__
1233 ):
1234 expected = self.write_unraisable_exc(
1235 A.B.X(), "msg", "obj"
1236 )
1237 report = stderr.getvalue()
1238 self.assertIn(A.B.X.__qualname__, report)
1239 if moduleName in ['builtins', '__main__']:
1240 self.assertNotIn(moduleName + '.', report)
1241 else:
1242 self.assertIn(moduleName + '.', report)
1243
1244 def test_original_unraisablehook_wrong_type(self):
1245 exc = ValueError(42)
1246 with test.support.swap_attr(sys, 'unraisablehook',
1247 sys.__unraisablehook__):
1248 with self.assertRaises(TypeError):
1249 sys.unraisablehook(exc)
1250
1251 def test_custom_unraisablehook(self):
1252 hook_args = None
1253
1254 def hook_func(args):
1255 nonlocal hook_args
1256 hook_args = args
1257
1258 obj = object()
1259 try:
1260 with test.support.swap_attr(sys, 'unraisablehook', hook_func):
1261 expected = self.write_unraisable_exc(ValueError(42),
1262 "custom hook", obj)
1263 for attr in "exc_type exc_value exc_traceback err_msg object".split():
1264 self.assertEqual(getattr(hook_args, attr),
1265 getattr(expected, attr),
1266 (hook_args, expected))
1267 finally:
1268 # expected and hook_args contain an exception: break reference cycle
1269 expected = None
1270 hook_args = None
1271
1272 def test_custom_unraisablehook_fail(self):
1273 def hook_func(*args):
1274 raise Exception("hook_func failed")
1275
1276 with test.support.captured_output("stderr") as stderr:
1277 with test.support.swap_attr(sys, 'unraisablehook', hook_func):
1278 self.write_unraisable_exc(ValueError(42),
1279 "custom hook fail", None)
1280
1281 err = stderr.getvalue()
1282 self.assertIn(f'Exception ignored in sys.unraisablehook: '
1283 f'{hook_func!r}\n',
1284 err)
1285 self.assertIn('Traceback (most recent call last):\n', err)
1286 self.assertIn('Exception: hook_func failed\n', err)
1287
1288
1289 @test.support.cpython_only
1290 class ESC[4;38;5;81mSizeofTest(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
1291
1292 def setUp(self):
1293 self.P = struct.calcsize('P')
1294 self.longdigit = sys.int_info.sizeof_digit
1295 import _testinternalcapi
1296 self.gc_headsize = _testinternalcapi.SIZEOF_PYGC_HEAD
1297
1298 check_sizeof = test.support.check_sizeof
1299
1300 def test_gc_head_size(self):
1301 # Check that the gc header size is added to objects tracked by the gc.
1302 vsize = test.support.calcvobjsize
1303 gc_header_size = self.gc_headsize
1304 # bool objects are not gc tracked
1305 self.assertEqual(sys.getsizeof(True), vsize('') + self.longdigit)
1306 # but lists are
1307 self.assertEqual(sys.getsizeof([]), vsize('Pn') + gc_header_size)
1308
1309 def test_errors(self):
1310 class ESC[4;38;5;81mBadSizeof:
1311 def __sizeof__(self):
1312 raise ValueError
1313 self.assertRaises(ValueError, sys.getsizeof, BadSizeof())
1314
1315 class ESC[4;38;5;81mInvalidSizeof:
1316 def __sizeof__(self):
1317 return None
1318 self.assertRaises(TypeError, sys.getsizeof, InvalidSizeof())
1319 sentinel = ["sentinel"]
1320 self.assertIs(sys.getsizeof(InvalidSizeof(), sentinel), sentinel)
1321
1322 class ESC[4;38;5;81mFloatSizeof:
1323 def __sizeof__(self):
1324 return 4.5
1325 self.assertRaises(TypeError, sys.getsizeof, FloatSizeof())
1326 self.assertIs(sys.getsizeof(FloatSizeof(), sentinel), sentinel)
1327
1328 class ESC[4;38;5;81mOverflowSizeof(ESC[4;38;5;149mint):
1329 def __sizeof__(self):
1330 return int(self)
1331 self.assertEqual(sys.getsizeof(OverflowSizeof(sys.maxsize)),
1332 sys.maxsize + self.gc_headsize*2)
1333 with self.assertRaises(OverflowError):
1334 sys.getsizeof(OverflowSizeof(sys.maxsize + 1))
1335 with self.assertRaises(ValueError):
1336 sys.getsizeof(OverflowSizeof(-1))
1337 with self.assertRaises((ValueError, OverflowError)):
1338 sys.getsizeof(OverflowSizeof(-sys.maxsize - 1))
1339
1340 def test_default(self):
1341 size = test.support.calcvobjsize
1342 self.assertEqual(sys.getsizeof(True), size('') + self.longdigit)
1343 self.assertEqual(sys.getsizeof(True, -1), size('') + self.longdigit)
1344
1345 def test_objecttypes(self):
1346 # check all types defined in Objects/
1347 calcsize = struct.calcsize
1348 size = test.support.calcobjsize
1349 vsize = test.support.calcvobjsize
1350 check = self.check_sizeof
1351 # bool
1352 check(True, vsize('') + self.longdigit)
1353 check(False, vsize('') + self.longdigit)
1354 # buffer
1355 # XXX
1356 # builtin_function_or_method
1357 check(len, size('5P'))
1358 # bytearray
1359 samples = [b'', b'u'*100000]
1360 for sample in samples:
1361 x = bytearray(sample)
1362 check(x, vsize('n2Pi') + x.__alloc__())
1363 # bytearray_iterator
1364 check(iter(bytearray()), size('nP'))
1365 # bytes
1366 check(b'', vsize('n') + 1)
1367 check(b'x' * 10, vsize('n') + 11)
1368 # cell
1369 def get_cell():
1370 x = 42
1371 def inner():
1372 return x
1373 return inner
1374 check(get_cell().__closure__[0], size('P'))
1375 # code
1376 def check_code_size(a, expected_size):
1377 self.assertGreaterEqual(sys.getsizeof(a), expected_size)
1378 check_code_size(get_cell().__code__, size('6i13P'))
1379 check_code_size(get_cell.__code__, size('6i13P'))
1380 def get_cell2(x):
1381 def inner():
1382 return x
1383 return inner
1384 check_code_size(get_cell2.__code__, size('6i13P') + calcsize('n'))
1385 # complex
1386 check(complex(0,1), size('2d'))
1387 # method_descriptor (descriptor object)
1388 check(str.lower, size('3PPP'))
1389 # classmethod_descriptor (descriptor object)
1390 # XXX
1391 # member_descriptor (descriptor object)
1392 import datetime
1393 check(datetime.timedelta.days, size('3PP'))
1394 # getset_descriptor (descriptor object)
1395 import collections
1396 check(collections.defaultdict.default_factory, size('3PP'))
1397 # wrapper_descriptor (descriptor object)
1398 check(int.__add__, size('3P2P'))
1399 # method-wrapper (descriptor object)
1400 check({}.__iter__, size('2P'))
1401 # empty dict
1402 check({}, size('nQ2P'))
1403 # dict (string key)
1404 check({"a": 1}, size('nQ2P') + calcsize(DICT_KEY_STRUCT_FORMAT) + 8 + (8*2//3)*calcsize('2P'))
1405 longdict = {str(i): i for i in range(8)}
1406 check(longdict, size('nQ2P') + calcsize(DICT_KEY_STRUCT_FORMAT) + 16 + (16*2//3)*calcsize('2P'))
1407 # dict (non-string key)
1408 check({1: 1}, size('nQ2P') + calcsize(DICT_KEY_STRUCT_FORMAT) + 8 + (8*2//3)*calcsize('n2P'))
1409 longdict = {1:1, 2:2, 3:3, 4:4, 5:5, 6:6, 7:7, 8:8}
1410 check(longdict, size('nQ2P') + calcsize(DICT_KEY_STRUCT_FORMAT) + 16 + (16*2//3)*calcsize('n2P'))
1411 # dictionary-keyview
1412 check({}.keys(), size('P'))
1413 # dictionary-valueview
1414 check({}.values(), size('P'))
1415 # dictionary-itemview
1416 check({}.items(), size('P'))
1417 # dictionary iterator
1418 check(iter({}), size('P2nPn'))
1419 # dictionary-keyiterator
1420 check(iter({}.keys()), size('P2nPn'))
1421 # dictionary-valueiterator
1422 check(iter({}.values()), size('P2nPn'))
1423 # dictionary-itemiterator
1424 check(iter({}.items()), size('P2nPn'))
1425 # dictproxy
1426 class ESC[4;38;5;81mC(ESC[4;38;5;149mobject): pass
1427 check(C.__dict__, size('P'))
1428 # BaseException
1429 check(BaseException(), size('6Pb'))
1430 # UnicodeEncodeError
1431 check(UnicodeEncodeError("", "", 0, 0, ""), size('6Pb 2P2nP'))
1432 # UnicodeDecodeError
1433 check(UnicodeDecodeError("", b"", 0, 0, ""), size('6Pb 2P2nP'))
1434 # UnicodeTranslateError
1435 check(UnicodeTranslateError("", 0, 1, ""), size('6Pb 2P2nP'))
1436 # ellipses
1437 check(Ellipsis, size(''))
1438 # EncodingMap
1439 import codecs, encodings.iso8859_3
1440 x = codecs.charmap_build(encodings.iso8859_3.decoding_table)
1441 check(x, size('32B2iB'))
1442 # enumerate
1443 check(enumerate([]), size('n4P'))
1444 # reverse
1445 check(reversed(''), size('nP'))
1446 # float
1447 check(float(0), size('d'))
1448 # sys.floatinfo
1449 check(sys.float_info, vsize('') + self.P * len(sys.float_info))
1450 # frame
1451 def func():
1452 return sys._getframe()
1453 x = func()
1454 check(x, size('3Pi3c7P2ic??2P'))
1455 # function
1456 def func(): pass
1457 check(func, size('15Pi'))
1458 class ESC[4;38;5;81mc():
1459 @staticmethod
1460 def foo():
1461 pass
1462 @classmethod
1463 def bar(cls):
1464 pass
1465 # staticmethod
1466 check(foo, size('PP'))
1467 # classmethod
1468 check(bar, size('PP'))
1469 # generator
1470 def get_gen(): yield 1
1471 check(get_gen(), size('PP4P4c7P2ic??2P'))
1472 # iterator
1473 check(iter('abc'), size('lP'))
1474 # callable-iterator
1475 import re
1476 check(re.finditer('',''), size('2P'))
1477 # list
1478 check(list([]), vsize('Pn'))
1479 check(list([1]), vsize('Pn') + 2*self.P)
1480 check(list([1, 2]), vsize('Pn') + 2*self.P)
1481 check(list([1, 2, 3]), vsize('Pn') + 4*self.P)
1482 # sortwrapper (list)
1483 # XXX
1484 # cmpwrapper (list)
1485 # XXX
1486 # listiterator (list)
1487 check(iter([]), size('lP'))
1488 # listreverseiterator (list)
1489 check(reversed([]), size('nP'))
1490 # int
1491 check(0, vsize('') + self.longdigit)
1492 check(1, vsize('') + self.longdigit)
1493 check(-1, vsize('') + self.longdigit)
1494 PyLong_BASE = 2**sys.int_info.bits_per_digit
1495 check(int(PyLong_BASE), vsize('') + 2*self.longdigit)
1496 check(int(PyLong_BASE**2-1), vsize('') + 2*self.longdigit)
1497 check(int(PyLong_BASE**2), vsize('') + 3*self.longdigit)
1498 # module
1499 check(unittest, size('PnPPP'))
1500 # None
1501 check(None, size(''))
1502 # NotImplementedType
1503 check(NotImplemented, size(''))
1504 # object
1505 check(object(), size(''))
1506 # property (descriptor object)
1507 class ESC[4;38;5;81mC(ESC[4;38;5;149mobject):
1508 def getx(self): return self.__x
1509 def setx(self, value): self.__x = value
1510 def delx(self): del self.__x
1511 x = property(getx, setx, delx, "")
1512 check(x, size('5Pi'))
1513 # PyCapsule
1514 # XXX
1515 # rangeiterator
1516 check(iter(range(1)), size('3l'))
1517 check(iter(range(2**65)), size('3P'))
1518 # reverse
1519 check(reversed(''), size('nP'))
1520 # range
1521 check(range(1), size('4P'))
1522 check(range(66000), size('4P'))
1523 # set
1524 # frozenset
1525 PySet_MINSIZE = 8
1526 samples = [[], range(10), range(50)]
1527 s = size('3nP' + PySet_MINSIZE*'nP' + '2nP')
1528 for sample in samples:
1529 minused = len(sample)
1530 if minused == 0: tmp = 1
1531 # the computation of minused is actually a bit more complicated
1532 # but this suffices for the sizeof test
1533 minused = minused*2
1534 newsize = PySet_MINSIZE
1535 while newsize <= minused:
1536 newsize = newsize << 1
1537 if newsize <= 8:
1538 check(set(sample), s)
1539 check(frozenset(sample), s)
1540 else:
1541 check(set(sample), s + newsize*calcsize('nP'))
1542 check(frozenset(sample), s + newsize*calcsize('nP'))
1543 # setiterator
1544 check(iter(set()), size('P3n'))
1545 # slice
1546 check(slice(0), size('3P'))
1547 # super
1548 check(super(int), size('3P'))
1549 # tuple
1550 check((), vsize(''))
1551 check((1,2,3), vsize('') + 3*self.P)
1552 # type
1553 # static type: PyTypeObject
1554 fmt = 'P2nPI13Pl4Pn9Pn12PIPc'
1555 s = vsize('2P' + fmt)
1556 check(int, s)
1557 # class
1558 s = vsize(fmt + # PyTypeObject
1559 '4P' # PyAsyncMethods
1560 '36P' # PyNumberMethods
1561 '3P' # PyMappingMethods
1562 '10P' # PySequenceMethods
1563 '2P' # PyBufferProcs
1564 '6P'
1565 '1PI' # Specializer cache
1566 )
1567 class ESC[4;38;5;81mnewstyleclass(ESC[4;38;5;149mobject): pass
1568 # Separate block for PyDictKeysObject with 8 keys and 5 entries
1569 check(newstyleclass, s + calcsize(DICT_KEY_STRUCT_FORMAT) + 64 + 42*calcsize("2P"))
1570 # dict with shared keys
1571 [newstyleclass() for _ in range(100)]
1572 check(newstyleclass().__dict__, size('nQ2P') + self.P)
1573 o = newstyleclass()
1574 o.a = o.b = o.c = o.d = o.e = o.f = o.g = o.h = 1
1575 # Separate block for PyDictKeysObject with 16 keys and 10 entries
1576 check(newstyleclass, s + calcsize(DICT_KEY_STRUCT_FORMAT) + 64 + 42*calcsize("2P"))
1577 # dict with shared keys
1578 check(newstyleclass().__dict__, size('nQ2P') + self.P)
1579 # unicode
1580 # each tuple contains a string and its expected character size
1581 # don't put any static strings here, as they may contain
1582 # wchar_t or UTF-8 representations
1583 samples = ['1'*100, '\xff'*50,
1584 '\u0100'*40, '\uffff'*100,
1585 '\U00010000'*30, '\U0010ffff'*100]
1586 # also update field definitions in test_unicode.test_raiseMemError
1587 asciifields = "nnb"
1588 compactfields = asciifields + "nP"
1589 unicodefields = compactfields + "P"
1590 for s in samples:
1591 maxchar = ord(max(s))
1592 if maxchar < 128:
1593 L = size(asciifields) + len(s) + 1
1594 elif maxchar < 256:
1595 L = size(compactfields) + len(s) + 1
1596 elif maxchar < 65536:
1597 L = size(compactfields) + 2*(len(s) + 1)
1598 else:
1599 L = size(compactfields) + 4*(len(s) + 1)
1600 check(s, L)
1601 # verify that the UTF-8 size is accounted for
1602 s = chr(0x4000) # 4 bytes canonical representation
1603 check(s, size(compactfields) + 4)
1604 # compile() will trigger the generation of the UTF-8
1605 # representation as a side effect
1606 compile(s, "<stdin>", "eval")
1607 check(s, size(compactfields) + 4 + 4)
1608 # TODO: add check that forces the presence of wchar_t representation
1609 # TODO: add check that forces layout of unicodefields
1610 # weakref
1611 import weakref
1612 check(weakref.ref(int), size('2Pn3P'))
1613 # weakproxy
1614 # XXX
1615 # weakcallableproxy
1616 check(weakref.proxy(int), size('2Pn3P'))
1617
1618 def check_slots(self, obj, base, extra):
1619 expected = sys.getsizeof(base) + struct.calcsize(extra)
1620 if gc.is_tracked(obj) and not gc.is_tracked(base):
1621 expected += self.gc_headsize
1622 self.assertEqual(sys.getsizeof(obj), expected)
1623
1624 def test_slots(self):
1625 # check all subclassable types defined in Objects/ that allow
1626 # non-empty __slots__
1627 check = self.check_slots
1628 class ESC[4;38;5;81mBA(ESC[4;38;5;149mbytearray):
1629 __slots__ = 'a', 'b', 'c'
1630 check(BA(), bytearray(), '3P')
1631 class ESC[4;38;5;81mD(ESC[4;38;5;149mdict):
1632 __slots__ = 'a', 'b', 'c'
1633 check(D(x=[]), {'x': []}, '3P')
1634 class ESC[4;38;5;81mL(ESC[4;38;5;149mlist):
1635 __slots__ = 'a', 'b', 'c'
1636 check(L(), [], '3P')
1637 class ESC[4;38;5;81mS(ESC[4;38;5;149mset):
1638 __slots__ = 'a', 'b', 'c'
1639 check(S(), set(), '3P')
1640 class ESC[4;38;5;81mFS(ESC[4;38;5;149mfrozenset):
1641 __slots__ = 'a', 'b', 'c'
1642 check(FS(), frozenset(), '3P')
1643 from collections import OrderedDict
1644 class ESC[4;38;5;81mOD(ESC[4;38;5;149mOrderedDict):
1645 __slots__ = 'a', 'b', 'c'
1646 check(OD(x=[]), OrderedDict(x=[]), '3P')
1647
1648 def test_pythontypes(self):
1649 # check all types defined in Python/
1650 size = test.support.calcobjsize
1651 vsize = test.support.calcvobjsize
1652 check = self.check_sizeof
1653 # _ast.AST
1654 import _ast
1655 check(_ast.AST(), size('P'))
1656 try:
1657 raise TypeError
1658 except TypeError as e:
1659 tb = e.__traceback__
1660 # traceback
1661 if tb is not None:
1662 check(tb, size('2P2i'))
1663 # symtable entry
1664 # XXX
1665 # sys.flags
1666 check(sys.flags, vsize('') + self.P * len(sys.flags))
1667
1668 def test_asyncgen_hooks(self):
1669 old = sys.get_asyncgen_hooks()
1670 self.assertIsNone(old.firstiter)
1671 self.assertIsNone(old.finalizer)
1672
1673 firstiter = lambda *a: None
1674 sys.set_asyncgen_hooks(firstiter=firstiter)
1675 hooks = sys.get_asyncgen_hooks()
1676 self.assertIs(hooks.firstiter, firstiter)
1677 self.assertIs(hooks[0], firstiter)
1678 self.assertIs(hooks.finalizer, None)
1679 self.assertIs(hooks[1], None)
1680
1681 finalizer = lambda *a: None
1682 sys.set_asyncgen_hooks(finalizer=finalizer)
1683 hooks = sys.get_asyncgen_hooks()
1684 self.assertIs(hooks.firstiter, firstiter)
1685 self.assertIs(hooks[0], firstiter)
1686 self.assertIs(hooks.finalizer, finalizer)
1687 self.assertIs(hooks[1], finalizer)
1688
1689 sys.set_asyncgen_hooks(*old)
1690 cur = sys.get_asyncgen_hooks()
1691 self.assertIsNone(cur.firstiter)
1692 self.assertIsNone(cur.finalizer)
1693
1694 def test_changing_sys_stderr_and_removing_reference(self):
1695 # If the default displayhook doesn't take a strong reference
1696 # to sys.stderr the following code can crash. See bpo-43660
1697 # for more details.
1698 code = textwrap.dedent('''
1699 import sys
1700 class MyStderr:
1701 def write(self, s):
1702 sys.stderr = None
1703 sys.stderr = MyStderr()
1704 1/0
1705 ''')
1706 rc, out, err = assert_python_failure('-c', code)
1707 self.assertEqual(out, b"")
1708 self.assertEqual(err, b"")
1709
1710 if __name__ == "__main__":
1711 unittest.main()