| 1 |
import builtins |
| 2 |
import codecs |
| 3 |
import gc |
| 4 |
import io |
| 5 |
import locale |
| 6 |
import operator |
| 7 |
import os |
| 8 |
import random |
| 9 |
import socket |
| 10 |
import struct |
| 11 |
import subprocess |
| 12 |
import sys |
| 13 |
import sysconfig |
| 14 |
import test.support |
| 15 |
from io import StringIO |
| 16 |
from unittest import mock |
| 17 |
from test import support |
| 18 |
from test.support import os_helper |
| 19 |
from test.support.script_helper import assert_python_ok, assert_python_failure |
| 20 |
from test.support.socket_helper import find_unused_port |
| 21 |
from test.support import threading_helper |
| 22 |
from test.support import import_helper |
| 23 |
from test.support import force_not_colorized |
| 24 |
from test.support import SHORT_TIMEOUT |
| 25 |
try: |
| 26 |
from concurrent import interpreters |
| 27 |
except ImportError: |
| 28 |
interpreters = None |
| 29 |
import textwrap |
| 30 |
import unittest |
| 31 |
import warnings |
| 32 |
|
| 33 |
|
| 34 |
def requires_subinterpreters(meth): |
| 35 |
"""Decorator to skip a test if subinterpreters are not supported.""" |
| 36 |
return unittest.skipIf(interpreters is None, |
| 37 |
'subinterpreters required')(meth) |
| 38 |
|
| 39 |
|
| 40 |
DICT_KEY_STRUCT_FORMAT = 'n2BI2n' |
| 41 |
|
| 42 |
class DisplayHookTest(unittest.TestCase): |
| 43 |
|
| 44 |
def test_original_displayhook(self): |
| 45 |
dh = sys.__displayhook__ |
| 46 |
|
| 47 |
with support.captured_stdout() as out: |
| 48 |
dh(42) |
| 49 |
|
| 50 |
self.assertEqual(out.getvalue(), "42\n") |
| 51 |
self.assertEqual(builtins._, 42) |
| 52 |
|
| 53 |
del builtins._ |
| 54 |
|
| 55 |
with support.captured_stdout() as out: |
| 56 |
dh(None) |
| 57 |
|
| 58 |
self.assertEqual(out.getvalue(), "") |
| 59 |
self.assertNotHasAttr(builtins, "_") |
| 60 |
|
| 61 |
# sys.displayhook() requires arguments |
| 62 |
self.assertRaises(TypeError, dh) |
| 63 |
|
| 64 |
stdout = sys.stdout |
| 65 |
try: |
| 66 |
del sys.stdout |
| 67 |
self.assertRaises(RuntimeError, dh, 42) |
| 68 |
finally: |
| 69 |
sys.stdout = stdout |
| 70 |
|
| 71 |
def test_lost_displayhook(self): |
| 72 |
displayhook = sys.displayhook |
| 73 |
try: |
| 74 |
del sys.displayhook |
| 75 |
code = compile("42", "<string>", "single") |
| 76 |
self.assertRaises(RuntimeError, eval, code) |
| 77 |
finally: |
| 78 |
sys.displayhook = displayhook |
| 79 |
|
| 80 |
def test_custom_displayhook(self): |
| 81 |
def baddisplayhook(obj): |
| 82 |
raise ValueError |
| 83 |
|
| 84 |
with support.swap_attr(sys, 'displayhook', baddisplayhook): |
| 85 |
code = compile("42", "<string>", "single") |
| 86 |
self.assertRaises(ValueError, eval, code) |
| 87 |
|
| 88 |
def test_gh130163(self): |
| 89 |
class X: |
| 90 |
def __repr__(self): |
| 91 |
sys.stdout = io.StringIO() |
| 92 |
support.gc_collect() |
| 93 |
return 'foo' |
| 94 |
|
| 95 |
with support.swap_attr(sys, 'stdout', None): |
| 96 |
sys.stdout = io.StringIO() # the only reference |
| 97 |
sys.displayhook(X()) # should not crash |
| 98 |
|
| 99 |
|
| 100 |
class ActiveExceptionTests(unittest.TestCase): |
| 101 |
def test_exc_info_no_exception(self): |
| 102 |
self.assertEqual(sys.exc_info(), (None, None, None)) |
| 103 |
|
| 104 |
def test_sys_exception_no_exception(self): |
| 105 |
self.assertEqual(sys.exception(), None) |
| 106 |
|
| 107 |
def test_exc_info_with_exception_instance(self): |
| 108 |
def f(): |
| 109 |
raise ValueError(42) |
| 110 |
|
| 111 |
try: |
| 112 |
f() |
| 113 |
except Exception as e_: |
| 114 |
e = e_ |
| 115 |
exc_info = sys.exc_info() |
| 116 |
|
| 117 |
self.assertIsInstance(e, ValueError) |
| 118 |
self.assertIs(exc_info[0], ValueError) |
| 119 |
self.assertIs(exc_info[1], e) |
| 120 |
self.assertIs(exc_info[2], e.__traceback__) |
| 121 |
|
| 122 |
def test_exc_info_with_exception_type(self): |
| 123 |
def f(): |
| 124 |
raise ValueError |
| 125 |
|
| 126 |
try: |
| 127 |
f() |
| 128 |
except Exception as e_: |
| 129 |
e = e_ |
| 130 |
exc_info = sys.exc_info() |
| 131 |
|
| 132 |
self.assertIsInstance(e, ValueError) |
| 133 |
self.assertIs(exc_info[0], ValueError) |
| 134 |
self.assertIs(exc_info[1], e) |
| 135 |
self.assertIs(exc_info[2], e.__traceback__) |
| 136 |
|
| 137 |
def test_sys_exception_with_exception_instance(self): |
| 138 |
def f(): |
| 139 |
raise ValueError(42) |
| 140 |
|
| 141 |
try: |
| 142 |
f() |
| 143 |
except Exception as e_: |
| 144 |
e = e_ |
| 145 |
exc = sys.exception() |
| 146 |
|
| 147 |
self.assertIsInstance(e, ValueError) |
| 148 |
self.assertIs(exc, e) |
| 149 |
|
| 150 |
def test_sys_exception_with_exception_type(self): |
| 151 |
def f(): |
| 152 |
raise ValueError |
| 153 |
|
| 154 |
try: |
| 155 |
f() |
| 156 |
except Exception as e_: |
| 157 |
e = e_ |
| 158 |
exc = sys.exception() |
| 159 |
|
| 160 |
self.assertIsInstance(e, ValueError) |
| 161 |
self.assertIs(exc, e) |
| 162 |
|
| 163 |
|
| 164 |
class ExceptHookTest(unittest.TestCase): |
| 165 |
|
| 166 |
@force_not_colorized |
| 167 |
def test_original_excepthook(self): |
| 168 |
try: |
| 169 |
raise ValueError(42) |
| 170 |
except ValueError as exc: |
| 171 |
with support.captured_stderr() as err: |
| 172 |
sys.__excepthook__(*sys.exc_info()) |
| 173 |
|
| 174 |
self.assertEndsWith(err.getvalue(), "ValueError: 42\n") |
| 175 |
|
| 176 |
self.assertRaises(TypeError, sys.__excepthook__) |
| 177 |
|
| 178 |
@force_not_colorized |
| 179 |
def test_excepthook_bytes_filename(self): |
| 180 |
# bpo-37467: sys.excepthook() must not crash if a filename |
| 181 |
# is a bytes string |
| 182 |
with warnings.catch_warnings(): |
| 183 |
warnings.simplefilter('ignore', BytesWarning) |
| 184 |
|
| 185 |
try: |
| 186 |
raise SyntaxError("msg", (b"bytes_filename", 123, 0, "text")) |
| 187 |
except SyntaxError as exc: |
| 188 |
with support.captured_stderr() as err: |
| 189 |
sys.__excepthook__(*sys.exc_info()) |
| 190 |
|
| 191 |
err = err.getvalue() |
| 192 |
self.assertIn(""" File "b'bytes_filename'", line 123\n""", err) |
| 193 |
self.assertIn(""" text\n""", err) |
| 194 |
self.assertEndsWith(err, "SyntaxError: msg\n") |
| 195 |
|
| 196 |
def test_excepthook(self): |
| 197 |
with test.support.captured_output("stderr") as stderr: |
| 198 |
with test.support.catch_unraisable_exception(): |
| 199 |
sys.excepthook(1, '1', 1) |
| 200 |
self.assertTrue("TypeError: print_exception(): Exception expected for " \ |
| 201 |
"value, str found" in stderr.getvalue()) |
| 202 |
|
| 203 |
# FIXME: testing the code for a lost or replaced excepthook in |
| 204 |
# Python/pythonrun.c::PyErr_PrintEx() is tricky. |
| 205 |
|
| 206 |
|
| 207 |
class SysModuleTest(unittest.TestCase): |
| 208 |
|
| 209 |
def tearDown(self): |
| 210 |
test.support.reap_children() |
| 211 |
|
| 212 |
def test_exit(self): |
| 213 |
# call with two arguments |
| 214 |
self.assertRaises(TypeError, sys.exit, 42, 42) |
| 215 |
|
| 216 |
# call without argument |
| 217 |
with self.assertRaises(SystemExit) as cm: |
| 218 |
sys.exit() |
| 219 |
self.assertIsNone(cm.exception.code) |
| 220 |
|
| 221 |
rc, out, err = assert_python_ok('-c', 'import sys; sys.exit()') |
| 222 |
self.assertEqual(rc, 0) |
| 223 |
self.assertEqual(out, b'') |
| 224 |
self.assertEqual(err, b'') |
| 225 |
|
| 226 |
# gh-125842: Windows uses 32-bit unsigned integers for exit codes |
| 227 |
# so a -1 exit code is sometimes interpreted as 0xffff_ffff. |
| 228 |
rc, out, err = assert_python_failure('-c', 'import sys; sys.exit(0xffff_ffff)') |
| 229 |
self.assertIn(rc, (-1, 0xff, 0xffff_ffff)) |
| 230 |
self.assertEqual(out, b'') |
| 231 |
self.assertEqual(err, b'') |
| 232 |
|
| 233 |
# Overflow results in a -1 exit code, which may be converted to 0xff |
| 234 |
# or 0xffff_ffff. |
| 235 |
rc, out, err = assert_python_failure('-c', 'import sys; sys.exit(2**128)') |
| 236 |
self.assertIn(rc, (-1, 0xff, 0xffff_ffff)) |
| 237 |
self.assertEqual(out, b'') |
| 238 |
self.assertEqual(err, b'') |
| 239 |
|
| 240 |
# call with integer argument |
| 241 |
with self.assertRaises(SystemExit) as cm: |
| 242 |
sys.exit(42) |
| 243 |
self.assertEqual(cm.exception.code, 42) |
| 244 |
|
| 245 |
# call with tuple argument with one entry |
| 246 |
# entry will be unpacked |
| 247 |
with self.assertRaises(SystemExit) as cm: |
| 248 |
sys.exit((42,)) |
| 249 |
self.assertEqual(cm.exception.code, 42) |
| 250 |
|
| 251 |
# call with string argument |
| 252 |
with self.assertRaises(SystemExit) as cm: |
| 253 |
sys.exit("exit") |
| 254 |
self.assertEqual(cm.exception.code, "exit") |
| 255 |
|
| 256 |
# call with tuple argument with two entries |
| 257 |
with self.assertRaises(SystemExit) as cm: |
| 258 |
sys.exit((17, 23)) |
| 259 |
self.assertEqual(cm.exception.code, (17, 23)) |
| 260 |
|
| 261 |
# test that the exit machinery handles SystemExits properly |
| 262 |
rc, out, err = assert_python_failure('-c', 'raise SystemExit(47)') |
| 263 |
self.assertEqual(rc, 47) |
| 264 |
self.assertEqual(out, b'') |
| 265 |
self.assertEqual(err, b'') |
| 266 |
|
| 267 |
def check_exit_message(code, expected, **env_vars): |
| 268 |
rc, out, err = assert_python_failure('-c', code, **env_vars) |
| 269 |
self.assertEqual(rc, 1) |
| 270 |
self.assertEqual(out, b'') |
| 271 |
self.assertStartsWith(err, expected) |
| 272 |
|
| 273 |
# test that stderr buffer is flushed before the exit message is written |
| 274 |
# into stderr |
| 275 |
check_exit_message( |
| 276 |
r'import sys; sys.stderr.write("unflushed,"); sys.exit("message")', |
| 277 |
b"unflushed,message") |
| 278 |
|
| 279 |
# test that the exit message is written with backslashreplace error |
| 280 |
# handler to stderr |
| 281 |
check_exit_message( |
| 282 |
r'import sys; sys.exit("surrogates:\uDCFF")', |
| 283 |
b"surrogates:\\udcff") |
| 284 |
|
| 285 |
# test that the unicode message is encoded to the stderr encoding |
| 286 |
# instead of the default encoding (utf8) |
| 287 |
check_exit_message( |
| 288 |
r'import sys; sys.exit("h\xe9")', |
| 289 |
b"h\xe9", PYTHONIOENCODING='latin-1') |
| 290 |
|
| 291 |
@support.requires_subprocess() |
| 292 |
def test_exit_codes_under_repl(self): |
| 293 |
# GH-129900: SystemExit, or things that raised it, didn't |
| 294 |
# get their return code propagated by the REPL |
| 295 |
import tempfile |
| 296 |
|
| 297 |
exit_ways = [ |
| 298 |
"exit", |
| 299 |
"__import__('sys').exit", |
| 300 |
"raise SystemExit" |
| 301 |
] |
| 302 |
|
| 303 |
for exitfunc in exit_ways: |
| 304 |
for return_code in (0, 123): |
| 305 |
with self.subTest(exitfunc=exitfunc, return_code=return_code): |
| 306 |
with tempfile.TemporaryFile("w+") as stdin: |
| 307 |
stdin.write(f"{exitfunc}({return_code})\n") |
| 308 |
stdin.seek(0) |
| 309 |
proc = subprocess.run([sys.executable], stdin=stdin) |
| 310 |
self.assertEqual(proc.returncode, return_code) |
| 311 |
|
| 312 |
def test_getdefaultencoding(self): |
| 313 |
self.assertRaises(TypeError, sys.getdefaultencoding, 42) |
| 314 |
# can't check more than the type, as the user might have changed it |
| 315 |
self.assertIsInstance(sys.getdefaultencoding(), str) |
| 316 |
|
| 317 |
# testing sys.settrace() is done in test_sys_settrace.py |
| 318 |
# testing sys.setprofile() is done in test_sys_setprofile.py |
| 319 |
|
| 320 |
def test_switchinterval(self): |
| 321 |
self.assertRaises(TypeError, sys.setswitchinterval) |
| 322 |
self.assertRaises(TypeError, sys.setswitchinterval, "a") |
| 323 |
self.assertRaises(ValueError, sys.setswitchinterval, -1.0) |
| 324 |
self.assertRaises(ValueError, sys.setswitchinterval, 0.0) |
| 325 |
orig = sys.getswitchinterval() |
| 326 |
# sanity check |
| 327 |
self.assertTrue(orig < 0.5, orig) |
| 328 |
try: |
| 329 |
for n in 0.00001, 0.05, 3.0, orig: |
| 330 |
sys.setswitchinterval(n) |
| 331 |
self.assertAlmostEqual(sys.getswitchinterval(), n) |
| 332 |
finally: |
| 333 |
sys.setswitchinterval(orig) |
| 334 |
|
| 335 |
def test_getrecursionlimit(self): |
| 336 |
limit = sys.getrecursionlimit() |
| 337 |
self.assertIsInstance(limit, int) |
| 338 |
self.assertGreater(limit, 1) |
| 339 |
|
| 340 |
self.assertRaises(TypeError, sys.getrecursionlimit, 42) |
| 341 |
|
| 342 |
def test_setrecursionlimit(self): |
| 343 |
old_limit = sys.getrecursionlimit() |
| 344 |
try: |
| 345 |
sys.setrecursionlimit(10_005) |
| 346 |
self.assertEqual(sys.getrecursionlimit(), 10_005) |
| 347 |
|
| 348 |
self.assertRaises(TypeError, sys.setrecursionlimit) |
| 349 |
self.assertRaises(ValueError, sys.setrecursionlimit, -42) |
| 350 |
finally: |
| 351 |
sys.setrecursionlimit(old_limit) |
| 352 |
|
| 353 |
def test_recursionlimit_recovery(self): |
| 354 |
if hasattr(sys, 'gettrace') and sys.gettrace(): |
| 355 |
self.skipTest('fatal error if run with a trace function') |
| 356 |
|
| 357 |
old_limit = sys.getrecursionlimit() |
| 358 |
def f(): |
| 359 |
f() |
| 360 |
try: |
| 361 |
for depth in (50, 75, 100, 250, 1000): |
| 362 |
try: |
| 363 |
sys.setrecursionlimit(depth) |
| 364 |
except RecursionError: |
| 365 |
# Issue #25274: The recursion limit is too low at the |
| 366 |
# current recursion depth |
| 367 |
continue |
| 368 |
|
| 369 |
# Issue #5392: test stack overflow after hitting recursion |
| 370 |
# limit twice |
| 371 |
with self.assertRaises(RecursionError): |
| 372 |
f() |
| 373 |
with self.assertRaises(RecursionError): |
| 374 |
f() |
| 375 |
finally: |
| 376 |
sys.setrecursionlimit(old_limit) |
| 377 |
|
| 378 |
@test.support.cpython_only |
| 379 |
def test_setrecursionlimit_to_depth(self): |
| 380 |
# Issue #25274: Setting a low recursion limit must be blocked if the |
| 381 |
# current recursion depth is already higher than limit. |
| 382 |
|
| 383 |
old_limit = sys.getrecursionlimit() |
| 384 |
try: |
| 385 |
depth = support.get_recursion_depth() |
| 386 |
with self.subTest(limit=sys.getrecursionlimit(), depth=depth): |
| 387 |
# depth + 1 is OK |
| 388 |
sys.setrecursionlimit(depth + 1) |
| 389 |
|
| 390 |
# reset the limit to be able to call self.assertRaises() |
| 391 |
# context manager |
| 392 |
sys.setrecursionlimit(old_limit) |
| 393 |
with self.assertRaises(RecursionError) as cm: |
| 394 |
sys.setrecursionlimit(depth) |
| 395 |
self.assertRegex(str(cm.exception), |
| 396 |
"cannot set the recursion limit to [0-9]+ " |
| 397 |
"at the recursion depth [0-9]+: " |
| 398 |
"the limit is too low") |
| 399 |
finally: |
| 400 |
sys.setrecursionlimit(old_limit) |
| 401 |
|
| 402 |
def test_getwindowsversion(self): |
| 403 |
# Raise SkipTest if sys doesn't have getwindowsversion attribute |
| 404 |
test.support.get_attribute(sys, "getwindowsversion") |
| 405 |
v = sys.getwindowsversion() |
| 406 |
self.assertEqual(len(v), 5) |
| 407 |
self.assertIsInstance(v[0], int) |
| 408 |
self.assertIsInstance(v[1], int) |
| 409 |
self.assertIsInstance(v[2], int) |
| 410 |
self.assertIsInstance(v[3], int) |
| 411 |
self.assertIsInstance(v[4], str) |
| 412 |
self.assertRaises(IndexError, operator.getitem, v, 5) |
| 413 |
self.assertIsInstance(v.major, int) |
| 414 |
self.assertIsInstance(v.minor, int) |
| 415 |
self.assertIsInstance(v.build, int) |
| 416 |
self.assertIsInstance(v.platform, int) |
| 417 |
self.assertIsInstance(v.service_pack, str) |
| 418 |
self.assertIsInstance(v.service_pack_minor, int) |
| 419 |
self.assertIsInstance(v.service_pack_major, int) |
| 420 |
self.assertIsInstance(v.suite_mask, int) |
| 421 |
self.assertIsInstance(v.product_type, int) |
| 422 |
self.assertEqual(v[0], v.major) |
| 423 |
self.assertEqual(v[1], v.minor) |
| 424 |
self.assertEqual(v[2], v.build) |
| 425 |
self.assertEqual(v[3], v.platform) |
| 426 |
self.assertEqual(v[4], v.service_pack) |
| 427 |
|
| 428 |
# This is how platform.py calls it. Make sure tuple |
| 429 |
# still has 5 elements |
| 430 |
maj, min, buildno, plat, csd = sys.getwindowsversion() |
| 431 |
|
| 432 |
def test_call_tracing(self): |
| 433 |
self.assertRaises(TypeError, sys.call_tracing, type, 2) |
| 434 |
|
| 435 |
@unittest.skipUnless(hasattr(sys, "setdlopenflags"), |
| 436 |
'test needs sys.setdlopenflags()') |
| 437 |
def test_dlopenflags(self): |
| 438 |
self.assertHasAttr(sys, "getdlopenflags") |
| 439 |
self.assertRaises(TypeError, sys.getdlopenflags, 42) |
| 440 |
oldflags = sys.getdlopenflags() |
| 441 |
self.assertRaises(TypeError, sys.setdlopenflags) |
| 442 |
sys.setdlopenflags(oldflags+1) |
| 443 |
self.assertEqual(sys.getdlopenflags(), oldflags+1) |
| 444 |
sys.setdlopenflags(oldflags) |
| 445 |
|
| 446 |
@test.support.refcount_test |
| 447 |
def test_refcount(self): |
| 448 |
# n here originally had to be a global in order for this test to pass |
| 449 |
# while tracing with a python function. Tracing used to call |
| 450 |
# PyFrame_FastToLocals, which would add a copy of any locals to the |
| 451 |
# frame object, causing the ref count to increase by 2 instead of 1. |
| 452 |
# While that no longer happens (due to PEP 667), this test case retains |
| 453 |
# its original global-based implementation |
| 454 |
# PEP 683's immortal objects also made this point moot, since the |
| 455 |
# refcount for None doesn't change anyway. Maybe this test should be |
| 456 |
# using a different constant value? (e.g. an integer) |
| 457 |
global n |
| 458 |
self.assertRaises(TypeError, sys.getrefcount) |
| 459 |
c = sys.getrefcount(None) |
| 460 |
n = None |
| 461 |
# Singleton refcnts don't change |
| 462 |
self.assertEqual(sys.getrefcount(None), c) |
| 463 |
del n |
| 464 |
self.assertEqual(sys.getrefcount(None), c) |
| 465 |
if hasattr(sys, "gettotalrefcount"): |
| 466 |
self.assertIsInstance(sys.gettotalrefcount(), int) |
| 467 |
|
| 468 |
def test_getframe(self): |
| 469 |
self.assertRaises(TypeError, sys._getframe, 42, 42) |
| 470 |
self.assertRaises(ValueError, sys._getframe, 2000000000) |
| 471 |
self.assertTrue( |
| 472 |
SysModuleTest.test_getframe.__code__ \ |
| 473 |
is sys._getframe().f_code |
| 474 |
) |
| 475 |
|
| 476 |
def test_getframemodulename(self): |
| 477 |
# Default depth gets ourselves |
| 478 |
self.assertEqual(__name__, sys._getframemodulename()) |
| 479 |
self.assertEqual("unittest.case", sys._getframemodulename(1)) |
| 480 |
i = 0 |
| 481 |
f = sys._getframe(i) |
| 482 |
while f: |
| 483 |
self.assertEqual( |
| 484 |
f.f_globals['__name__'], |
| 485 |
sys._getframemodulename(i) or '__main__' |
| 486 |
) |
| 487 |
i += 1 |
| 488 |
f2 = f.f_back |
| 489 |
try: |
| 490 |
f = sys._getframe(i) |
| 491 |
except ValueError: |
| 492 |
break |
| 493 |
self.assertIs(f, f2) |
| 494 |
self.assertIsNone(sys._getframemodulename(i)) |
| 495 |
|
| 496 |
# sys._current_frames() is a CPython-only gimmick. |
| 497 |
@threading_helper.reap_threads |
| 498 |
@threading_helper.requires_working_threading() |
| 499 |
def test_current_frames(self): |
| 500 |
import threading |
| 501 |
import traceback |
| 502 |
|
| 503 |
# Spawn a thread that blocks at a known place. Then the main |
| 504 |
# thread does sys._current_frames(), and verifies that the frames |
| 505 |
# returned make sense. |
| 506 |
entered_g = threading.Event() |
| 507 |
leave_g = threading.Event() |
| 508 |
thread_info = [] # the thread's id |
| 509 |
|
| 510 |
def f123(): |
| 511 |
g456() |
| 512 |
|
| 513 |
def g456(): |
| 514 |
thread_info.append(threading.get_ident()) |
| 515 |
entered_g.set() |
| 516 |
leave_g.wait() |
| 517 |
|
| 518 |
t = threading.Thread(target=f123) |
| 519 |
t.start() |
| 520 |
entered_g.wait() |
| 521 |
|
| 522 |
try: |
| 523 |
# At this point, t has finished its entered_g.set(), although it's |
| 524 |
# impossible to guess whether it's still on that line or has moved on |
| 525 |
# to its leave_g.wait(). |
| 526 |
self.assertEqual(len(thread_info), 1) |
| 527 |
thread_id = thread_info[0] |
| 528 |
|
| 529 |
d = sys._current_frames() |
| 530 |
for tid in d: |
| 531 |
self.assertIsInstance(tid, int) |
| 532 |
self.assertGreater(tid, 0) |
| 533 |
|
| 534 |
main_id = threading.get_ident() |
| 535 |
self.assertIn(main_id, d) |
| 536 |
self.assertIn(thread_id, d) |
| 537 |
|
| 538 |
# Verify that the captured main-thread frame is _this_ frame. |
| 539 |
frame = d.pop(main_id) |
| 540 |
self.assertTrue(frame is sys._getframe()) |
| 541 |
|
| 542 |
# Verify that the captured thread frame is blocked in g456, called |
| 543 |
# from f123. This is a little tricky, since various bits of |
| 544 |
# threading.py are also in the thread's call stack. |
| 545 |
frame = d.pop(thread_id) |
| 546 |
stack = traceback.extract_stack(frame) |
| 547 |
for i, (filename, lineno, funcname, sourceline) in enumerate(stack): |
| 548 |
if funcname == "f123": |
| 549 |
break |
| 550 |
else: |
| 551 |
self.fail("didn't find f123() on thread's call stack") |
| 552 |
|
| 553 |
self.assertEqual(sourceline, "g456()") |
| 554 |
|
| 555 |
# And the next record must be for g456(). |
| 556 |
filename, lineno, funcname, sourceline = stack[i+1] |
| 557 |
self.assertEqual(funcname, "g456") |
| 558 |
self.assertIn(sourceline, ["leave_g.wait()", "entered_g.set()"]) |
| 559 |
finally: |
| 560 |
# Reap the spawned thread. |
| 561 |
leave_g.set() |
| 562 |
t.join() |
| 563 |
|
| 564 |
@threading_helper.reap_threads |
| 565 |
@threading_helper.requires_working_threading() |
| 566 |
def test_current_exceptions(self): |
| 567 |
import threading |
| 568 |
import traceback |
| 569 |
|
| 570 |
# Spawn a thread that blocks at a known place. Then the main |
| 571 |
# thread does sys._current_frames(), and verifies that the frames |
| 572 |
# returned make sense. |
| 573 |
g_raised = threading.Event() |
| 574 |
leave_g = threading.Event() |
| 575 |
thread_info = [] # the thread's id |
| 576 |
|
| 577 |
def f123(): |
| 578 |
g456() |
| 579 |
|
| 580 |
def g456(): |
| 581 |
thread_info.append(threading.get_ident()) |
| 582 |
while True: |
| 583 |
try: |
| 584 |
raise ValueError("oops") |
| 585 |
except ValueError: |
| 586 |
g_raised.set() |
| 587 |
if leave_g.wait(timeout=support.LONG_TIMEOUT): |
| 588 |
break |
| 589 |
|
| 590 |
t = threading.Thread(target=f123) |
| 591 |
t.start() |
| 592 |
g_raised.wait(timeout=support.LONG_TIMEOUT) |
| 593 |
|
| 594 |
try: |
| 595 |
self.assertEqual(len(thread_info), 1) |
| 596 |
thread_id = thread_info[0] |
| 597 |
|
| 598 |
d = sys._current_exceptions() |
| 599 |
for tid in d: |
| 600 |
self.assertIsInstance(tid, int) |
| 601 |
self.assertGreater(tid, 0) |
| 602 |
|
| 603 |
main_id = threading.get_ident() |
| 604 |
self.assertIn(main_id, d) |
| 605 |
self.assertIn(thread_id, d) |
| 606 |
self.assertEqual(None, d.pop(main_id)) |
| 607 |
|
| 608 |
# Verify that the captured thread frame is blocked in g456, called |
| 609 |
# from f123. This is a little tricky, since various bits of |
| 610 |
# threading.py are also in the thread's call stack. |
| 611 |
exc_value = d.pop(thread_id) |
| 612 |
stack = traceback.extract_stack(exc_value.__traceback__.tb_frame) |
| 613 |
for i, (filename, lineno, funcname, sourceline) in enumerate(stack): |
| 614 |
if funcname == "f123": |
| 615 |
break |
| 616 |
else: |
| 617 |
self.fail("didn't find f123() on thread's call stack") |
| 618 |
|
| 619 |
self.assertEqual(sourceline, "g456()") |
| 620 |
|
| 621 |
# And the next record must be for g456(). |
| 622 |
filename, lineno, funcname, sourceline = stack[i+1] |
| 623 |
self.assertEqual(funcname, "g456") |
| 624 |
self.assertStartsWith(sourceline, ("if leave_g.wait(", "g_raised.set()")) |
| 625 |
finally: |
| 626 |
# Reap the spawned thread. |
| 627 |
leave_g.set() |
| 628 |
t.join() |
| 629 |
|
| 630 |
def test_attributes(self): |
| 631 |
self.assertIsInstance(sys.api_version, int) |
| 632 |
self.assertIsInstance(sys.argv, list) |
| 633 |
for arg in sys.argv: |
| 634 |
self.assertIsInstance(arg, str) |
| 635 |
self.assertIsInstance(sys.orig_argv, list) |
| 636 |
for arg in sys.orig_argv: |
| 637 |
self.assertIsInstance(arg, str) |
| 638 |
self.assertIn(sys.byteorder, ("little", "big")) |
| 639 |
self.assertIsInstance(sys.builtin_module_names, tuple) |
| 640 |
self.assertIsInstance(sys.copyright, str) |
| 641 |
self.assertIsInstance(sys.exec_prefix, str) |
| 642 |
self.assertIsInstance(sys.base_exec_prefix, str) |
| 643 |
self.assertIsInstance(sys.executable, str) |
| 644 |
self.assertEqual(len(sys.float_info), 11) |
| 645 |
self.assertEqual(sys.float_info.radix, 2) |
| 646 |
self.assertEqual(len(sys.int_info), 4) |
| 647 |
self.assertTrue(sys.int_info.bits_per_digit % 5 == 0) |
| 648 |
self.assertTrue(sys.int_info.sizeof_digit >= 1) |
| 649 |
self.assertGreaterEqual(sys.int_info.default_max_str_digits, 500) |
| 650 |
self.assertGreaterEqual(sys.int_info.str_digits_check_threshold, 100) |
| 651 |
self.assertGreater(sys.int_info.default_max_str_digits, |
| 652 |
sys.int_info.str_digits_check_threshold) |
| 653 |
self.assertEqual(type(sys.int_info.bits_per_digit), int) |
| 654 |
self.assertEqual(type(sys.int_info.sizeof_digit), int) |
| 655 |
self.assertIsInstance(sys.int_info.default_max_str_digits, int) |
| 656 |
self.assertIsInstance(sys.int_info.str_digits_check_threshold, int) |
| 657 |
self.assertIsInstance(sys.hexversion, int) |
| 658 |
|
| 659 |
self.assertEqual(len(sys.hash_info), 9) |
| 660 |
self.assertLess(sys.hash_info.modulus, 2**sys.hash_info.width) |
| 661 |
# sys.hash_info.modulus should be a prime; we do a quick |
| 662 |
# probable primality test (doesn't exclude the possibility of |
| 663 |
# a Carmichael number) |
| 664 |
for x in range(1, 100): |
| 665 |
self.assertEqual( |
| 666 |
pow(x, sys.hash_info.modulus-1, sys.hash_info.modulus), |
| 667 |
1, |
| 668 |
"sys.hash_info.modulus {} is a non-prime".format( |
| 669 |
sys.hash_info.modulus) |
| 670 |
) |
| 671 |
self.assertIsInstance(sys.hash_info.inf, int) |
| 672 |
self.assertIsInstance(sys.hash_info.nan, int) |
| 673 |
self.assertIsInstance(sys.hash_info.imag, int) |
| 674 |
algo = sysconfig.get_config_var("Py_HASH_ALGORITHM") |
| 675 |
if sys.hash_info.algorithm in {"fnv", "siphash13", "siphash24"}: |
| 676 |
self.assertIn(sys.hash_info.hash_bits, {32, 64}) |
| 677 |
self.assertIn(sys.hash_info.seed_bits, {32, 64, 128}) |
| 678 |
|
| 679 |
if algo == 1: |
| 680 |
self.assertEqual(sys.hash_info.algorithm, "siphash24") |
| 681 |
elif algo == 2: |
| 682 |
self.assertEqual(sys.hash_info.algorithm, "fnv") |
| 683 |
elif algo == 3: |
| 684 |
self.assertEqual(sys.hash_info.algorithm, "siphash13") |
| 685 |
else: |
| 686 |
self.assertIn(sys.hash_info.algorithm, {"fnv", "siphash13", "siphash24"}) |
| 687 |
else: |
| 688 |
# PY_HASH_EXTERNAL |
| 689 |
self.assertEqual(algo, 0) |
| 690 |
self.assertGreaterEqual(sys.hash_info.cutoff, 0) |
| 691 |
self.assertLess(sys.hash_info.cutoff, 8) |
| 692 |
|
| 693 |
self.assertIsInstance(sys.maxsize, int) |
| 694 |
self.assertIsInstance(sys.maxunicode, int) |
| 695 |
self.assertEqual(sys.maxunicode, 0x10FFFF) |
| 696 |
self.assertIsInstance(sys.platform, str) |
| 697 |
self.assertIsInstance(sys.prefix, str) |
| 698 |
self.assertIsInstance(sys.base_prefix, str) |
| 699 |
self.assertIsInstance(sys.platlibdir, str) |
| 700 |
self.assertIsInstance(sys.version, str) |
| 701 |
vi = sys.version_info |
| 702 |
self.assertIsInstance(vi[:], tuple) |
| 703 |
self.assertEqual(len(vi), 5) |
| 704 |
self.assertIsInstance(vi[0], int) |
| 705 |
self.assertIsInstance(vi[1], int) |
| 706 |
self.assertIsInstance(vi[2], int) |
| 707 |
self.assertIn(vi[3], ("alpha", "beta", "candidate", "final")) |
| 708 |
self.assertIsInstance(vi[4], int) |
| 709 |
self.assertIsInstance(vi.major, int) |
| 710 |
self.assertIsInstance(vi.minor, int) |
| 711 |
self.assertIsInstance(vi.micro, int) |
| 712 |
self.assertIn(vi.releaselevel, ("alpha", "beta", "candidate", "final")) |
| 713 |
self.assertIsInstance(vi.serial, int) |
| 714 |
self.assertEqual(vi[0], vi.major) |
| 715 |
self.assertEqual(vi[1], vi.minor) |
| 716 |
self.assertEqual(vi[2], vi.micro) |
| 717 |
self.assertEqual(vi[3], vi.releaselevel) |
| 718 |
self.assertEqual(vi[4], vi.serial) |
| 719 |
self.assertTrue(vi > (1,0,0)) |
| 720 |
self.assertIsInstance(sys.float_repr_style, str) |
| 721 |
self.assertIn(sys.float_repr_style, ('short', 'legacy')) |
| 722 |
if not sys.platform.startswith('win'): |
| 723 |
self.assertIsInstance(sys.abiflags, str) |
| 724 |
else: |
| 725 |
self.assertFalse(hasattr(sys, 'abiflags')) |
| 726 |
|
| 727 |
def test_thread_info(self): |
| 728 |
info = sys.thread_info |
| 729 |
self.assertEqual(len(info), 3) |
| 730 |
self.assertIn(info.name, ('nt', 'pthread', 'pthread-stubs', 'solaris', None)) |
| 731 |
self.assertIn(info.lock, ('semaphore', 'mutex+cond', None)) |
| 732 |
if sys.platform.startswith(("linux", "android", "freebsd")): |
| 733 |
self.assertEqual(info.name, "pthread") |
| 734 |
elif sys.platform == "win32": |
| 735 |
self.assertEqual(info.name, "nt") |
| 736 |
elif sys.platform == "emscripten": |
| 737 |
self.assertIn(info.name, {"pthread", "pthread-stubs"}) |
| 738 |
elif sys.platform == "wasi": |
| 739 |
self.assertEqual(info.name, "pthread-stubs") |
| 740 |
|
| 741 |
@unittest.skipUnless(support.is_emscripten, "only available on Emscripten") |
| 742 |
def test_emscripten_info(self): |
| 743 |
self.assertEqual(len(sys._emscripten_info), 4) |
| 744 |
self.assertIsInstance(sys._emscripten_info.emscripten_version, tuple) |
| 745 |
self.assertIsInstance(sys._emscripten_info.runtime, (str, type(None))) |
| 746 |
self.assertIsInstance(sys._emscripten_info.pthreads, bool) |
| 747 |
self.assertIsInstance(sys._emscripten_info.shared_memory, bool) |
| 748 |
|
| 749 |
def test_43581(self): |
| 750 |
# Can't use sys.stdout, as this is a StringIO object when |
| 751 |
# the test runs under regrtest. |
| 752 |
self.assertEqual(sys.__stdout__.encoding, sys.__stderr__.encoding) |
| 753 |
|
| 754 |
def test_intern(self): |
| 755 |
has_is_interned = (test.support.check_impl_detail(cpython=True) |
| 756 |
or hasattr(sys, '_is_interned')) |
| 757 |
self.assertRaises(TypeError, sys.intern) |
| 758 |
self.assertRaises(TypeError, sys.intern, b'abc') |
| 759 |
if has_is_interned: |
| 760 |
self.assertRaises(TypeError, sys._is_interned) |
| 761 |
self.assertRaises(TypeError, sys._is_interned, b'abc') |
| 762 |
s = "never interned before" + str(random.randrange(0, 10**9)) |
| 763 |
self.assertTrue(sys.intern(s) is s) |
| 764 |
if has_is_interned: |
| 765 |
self.assertIs(sys._is_interned(s), True) |
| 766 |
s2 = s.swapcase().swapcase() |
| 767 |
if has_is_interned: |
| 768 |
self.assertIs(sys._is_interned(s2), False) |
| 769 |
self.assertTrue(sys.intern(s2) is s) |
| 770 |
if has_is_interned: |
| 771 |
self.assertIs(sys._is_interned(s2), False) |
| 772 |
|
| 773 |
# Subclasses of string can't be interned, because they |
| 774 |
# provide too much opportunity for insane things to happen. |
| 775 |
# We don't want them in the interned dict and if they aren't |
| 776 |
# actually interned, we don't want to create the appearance |
| 777 |
# that they are by allowing intern() to succeed. |
| 778 |
class S(str): |
| 779 |
def __hash__(self): |
| 780 |
return 123 |
| 781 |
|
| 782 |
self.assertRaises(TypeError, sys.intern, S("abc")) |
| 783 |
if has_is_interned: |
| 784 |
self.assertIs(sys._is_interned(S("abc")), False) |
| 785 |
|
| 786 |
@support.cpython_only |
| 787 |
@requires_subinterpreters |
| 788 |
def test_subinterp_intern_dynamically_allocated(self): |
| 789 |
# Implementation detail: Dynamically allocated strings |
| 790 |
# are distinct between interpreters |
| 791 |
s = "never interned before" + str(random.randrange(0, 10**9)) |
| 792 |
t = sys.intern(s) |
| 793 |
self.assertIs(t, s) |
| 794 |
|
| 795 |
interp = interpreters.create() |
| 796 |
interp.exec(textwrap.dedent(f''' |
| 797 |
import sys |
| 798 |
|
| 799 |
# set `s`, avoid parser interning & constant folding |
| 800 |
s = str({s.encode()!r}, 'utf-8') |
| 801 |
|
| 802 |
t = sys.intern(s) |
| 803 |
|
| 804 |
assert id(t) != {id(s)}, (id(t), {id(s)}) |
| 805 |
assert id(t) != {id(t)}, (id(t), {id(t)}) |
| 806 |
''')) |
| 807 |
|
| 808 |
@support.cpython_only |
| 809 |
@requires_subinterpreters |
| 810 |
def test_subinterp_intern_statically_allocated(self): |
| 811 |
# Implementation detail: Statically allocated strings are shared |
| 812 |
# between interpreters. |
| 813 |
# See Tools/build/generate_global_objects.py for the list |
| 814 |
# of strings that are always statically allocated. |
| 815 |
for s in ('__init__', 'CANCELLED', '<module>', 'utf-8', |
| 816 |
'{{', '', '\n', '_', 'x', '\0', '\N{CEDILLA}', '\xff', |
| 817 |
): |
| 818 |
with self.subTest(s=s): |
| 819 |
t = sys.intern(s) |
| 820 |
|
| 821 |
interp = interpreters.create() |
| 822 |
interp.exec(textwrap.dedent(f''' |
| 823 |
import sys |
| 824 |
|
| 825 |
# set `s`, avoid parser interning & constant folding |
| 826 |
s = str({s.encode()!r}, 'utf-8') |
| 827 |
|
| 828 |
t = sys.intern(s) |
| 829 |
assert id(t) == {id(t)}, (id(t), {id(t)}) |
| 830 |
''')) |
| 831 |
|
| 832 |
@support.cpython_only |
| 833 |
@requires_subinterpreters |
| 834 |
def test_subinterp_intern_singleton(self): |
| 835 |
# Implementation detail: singletons are used for 0- and 1-character |
| 836 |
# latin1 strings. |
| 837 |
for s in '', '\n', '_', 'x', '\0', '\N{CEDILLA}', '\xff': |
| 838 |
with self.subTest(s=s): |
| 839 |
interp = interpreters.create() |
| 840 |
interp.exec(textwrap.dedent(f''' |
| 841 |
import sys |
| 842 |
|
| 843 |
# set `s`, avoid parser interning & constant folding |
| 844 |
s = str({s.encode()!r}, 'utf-8') |
| 845 |
|
| 846 |
assert id(s) == {id(s)} |
| 847 |
t = sys.intern(s) |
| 848 |
''')) |
| 849 |
self.assertTrue(sys._is_interned(s)) |
| 850 |
|
| 851 |
def test_sys_flags(self): |
| 852 |
self.assertTrue(sys.flags) |
| 853 |
attrs = ("debug", |
| 854 |
"inspect", "interactive", "optimize", |
| 855 |
"dont_write_bytecode", "no_user_site", "no_site", |
| 856 |
"ignore_environment", "verbose", "bytes_warning", "quiet", |
| 857 |
"hash_randomization", "isolated", "dev_mode", "utf8_mode", |
| 858 |
"warn_default_encoding", "safe_path", "int_max_str_digits") |
| 859 |
for attr in attrs: |
| 860 |
self.assertHasAttr(sys.flags, attr) |
| 861 |
attr_type = bool if attr in ("dev_mode", "safe_path") else int |
| 862 |
self.assertEqual(type(getattr(sys.flags, attr)), attr_type, attr) |
| 863 |
self.assertTrue(repr(sys.flags)) |
| 864 |
self.assertEqual(len(sys.flags), len(attrs)) |
| 865 |
|
| 866 |
self.assertIn(sys.flags.utf8_mode, {0, 1, 2}) |
| 867 |
|
| 868 |
def assert_raise_on_new_sys_type(self, sys_attr): |
| 869 |
# Users are intentionally prevented from creating new instances of |
| 870 |
# sys.flags, sys.version_info, and sys.getwindowsversion. |
| 871 |
support.check_disallow_instantiation(self, type(sys_attr), sys_attr) |
| 872 |
|
| 873 |
def test_sys_flags_no_instantiation(self): |
| 874 |
self.assert_raise_on_new_sys_type(sys.flags) |
| 875 |
|
| 876 |
def test_sys_version_info_no_instantiation(self): |
| 877 |
self.assert_raise_on_new_sys_type(sys.version_info) |
| 878 |
|
| 879 |
def test_sys_getwindowsversion_no_instantiation(self): |
| 880 |
# Skip if not being run on Windows. |
| 881 |
test.support.get_attribute(sys, "getwindowsversion") |
| 882 |
self.assert_raise_on_new_sys_type(sys.getwindowsversion()) |
| 883 |
|
| 884 |
@test.support.cpython_only |
| 885 |
def test_clear_type_cache(self): |
| 886 |
with self.assertWarnsRegex(DeprecationWarning, |
| 887 |
r"sys\._clear_type_cache\(\) is deprecated.*"): |
| 888 |
sys._clear_type_cache() |
| 889 |
|
| 890 |
@force_not_colorized |
| 891 |
@support.requires_subprocess() |
| 892 |
def test_ioencoding(self): |
| 893 |
env = dict(os.environ) |
| 894 |
|
| 895 |
# Test character: cent sign, encoded as 0x4A (ASCII J) in CP424, |
| 896 |
# not representable in ASCII. |
| 897 |
|
| 898 |
env["PYTHONIOENCODING"] = "cp424" |
| 899 |
p = subprocess.Popen([sys.executable, "-c", 'print(chr(0xa2))'], |
| 900 |
stdout = subprocess.PIPE, env=env) |
| 901 |
out = p.communicate()[0].strip() |
| 902 |
expected = ("\xa2" + os.linesep).encode("cp424") |
| 903 |
self.assertEqual(out, expected) |
| 904 |
|
| 905 |
env["PYTHONIOENCODING"] = "ascii:replace" |
| 906 |
p = subprocess.Popen([sys.executable, "-c", 'print(chr(0xa2))'], |
| 907 |
stdout = subprocess.PIPE, env=env) |
| 908 |
out = p.communicate()[0].strip() |
| 909 |
self.assertEqual(out, b'?') |
| 910 |
|
| 911 |
env["PYTHONIOENCODING"] = "ascii" |
| 912 |
p = subprocess.Popen([sys.executable, "-c", 'print(chr(0xa2))'], |
| 913 |
stdout=subprocess.PIPE, stderr=subprocess.PIPE, |
| 914 |
env=env) |
| 915 |
out, err = p.communicate() |
| 916 |
self.assertEqual(out, b'') |
| 917 |
self.assertIn(b'UnicodeEncodeError:', err) |
| 918 |
self.assertIn(rb"'\xa2'", err) |
| 919 |
|
| 920 |
env["PYTHONIOENCODING"] = "ascii:" |
| 921 |
p = subprocess.Popen([sys.executable, "-c", 'print(chr(0xa2))'], |
| 922 |
stdout=subprocess.PIPE, stderr=subprocess.PIPE, |
| 923 |
env=env) |
| 924 |
out, err = p.communicate() |
| 925 |
self.assertEqual(out, b'') |
| 926 |
self.assertIn(b'UnicodeEncodeError:', err) |
| 927 |
self.assertIn(rb"'\xa2'", err) |
| 928 |
|
| 929 |
env["PYTHONIOENCODING"] = ":surrogateescape" |
| 930 |
p = subprocess.Popen([sys.executable, "-c", 'print(chr(0xdcbd))'], |
| 931 |
stdout=subprocess.PIPE, env=env) |
| 932 |
out = p.communicate()[0].strip() |
| 933 |
self.assertEqual(out, b'\xbd') |
| 934 |
|
| 935 |
@unittest.skipUnless(os_helper.FS_NONASCII, |
| 936 |
'requires OS support of non-ASCII encodings') |
| 937 |
@unittest.skipUnless(sys.getfilesystemencoding() == locale.getpreferredencoding(False), |
| 938 |
'requires FS encoding to match locale') |
| 939 |
@support.requires_subprocess() |
| 940 |
def test_ioencoding_nonascii(self): |
| 941 |
env = dict(os.environ) |
| 942 |
|
| 943 |
env["PYTHONIOENCODING"] = "" |
| 944 |
p = subprocess.Popen([sys.executable, "-c", |
| 945 |
'print(%a)' % os_helper.FS_NONASCII], |
| 946 |
stdout=subprocess.PIPE, env=env) |
| 947 |
out = p.communicate()[0].strip() |
| 948 |
self.assertEqual(out, os.fsencode(os_helper.FS_NONASCII)) |
| 949 |
|
| 950 |
@unittest.skipIf(sys.base_prefix != sys.prefix, |
| 951 |
'Test is not venv-compatible') |
| 952 |
@support.requires_subprocess() |
| 953 |
def test_executable(self): |
| 954 |
# sys.executable should be absolute |
| 955 |
self.assertEqual(os.path.abspath(sys.executable), sys.executable) |
| 956 |
|
| 957 |
# Issue #7774: Ensure that sys.executable is an empty string if argv[0] |
| 958 |
# has been set to a non existent program name and Python is unable to |
| 959 |
# retrieve the real program name |
| 960 |
|
| 961 |
# For a normal installation, it should work without 'cwd' |
| 962 |
# argument. For test runs in the build directory, see #7774. |
| 963 |
python_dir = os.path.dirname(os.path.realpath(sys.executable)) |
| 964 |
p = subprocess.Popen( |
| 965 |
["nonexistent", "-c", |
| 966 |
'import sys; print(sys.executable.encode("ascii", "backslashreplace"))'], |
| 967 |
executable=sys.executable, stdout=subprocess.PIPE, cwd=python_dir) |
| 968 |
stdout = p.communicate()[0] |
| 969 |
executable = stdout.strip().decode("ASCII") |
| 970 |
p.wait() |
| 971 |
self.assertIn(executable, ["b''", repr(sys.executable.encode("ascii", "backslashreplace"))]) |
| 972 |
|
| 973 |
def check_fsencoding(self, fs_encoding, expected=None): |
| 974 |
self.assertIsNotNone(fs_encoding) |
| 975 |
codecs.lookup(fs_encoding) |
| 976 |
if expected: |
| 977 |
self.assertEqual(fs_encoding, expected) |
| 978 |
|
| 979 |
def test_getfilesystemencoding(self): |
| 980 |
fs_encoding = sys.getfilesystemencoding() |
| 981 |
if sys.platform == 'darwin': |
| 982 |
expected = 'utf-8' |
| 983 |
else: |
| 984 |
expected = None |
| 985 |
self.check_fsencoding(fs_encoding, expected) |
| 986 |
|
| 987 |
def c_locale_get_error_handler(self, locale, isolated=False, encoding=None): |
| 988 |
# Force the POSIX locale |
| 989 |
env = os.environ.copy() |
| 990 |
env["LC_ALL"] = locale |
| 991 |
env["PYTHONCOERCECLOCALE"] = "0" |
| 992 |
code = '\n'.join(( |
| 993 |
'import sys', |
| 994 |
'def dump(name):', |
| 995 |
' std = getattr(sys, name)', |
| 996 |
' print("%s: %s" % (name, std.errors))', |
| 997 |
'dump("stdin")', |
| 998 |
'dump("stdout")', |
| 999 |
'dump("stderr")', |
| 1000 |
)) |
| 1001 |
args = [sys.executable, "-X", "utf8=0", "-c", code] |
| 1002 |
if isolated: |
| 1003 |
args.append("-I") |
| 1004 |
if encoding is not None: |
| 1005 |
env['PYTHONIOENCODING'] = encoding |
| 1006 |
else: |
| 1007 |
env.pop('PYTHONIOENCODING', None) |
| 1008 |
p = subprocess.Popen(args, |
| 1009 |
stdout=subprocess.PIPE, |
| 1010 |
stderr=subprocess.STDOUT, |
| 1011 |
env=env, |
| 1012 |
universal_newlines=True) |
| 1013 |
stdout, stderr = p.communicate() |
| 1014 |
return stdout |
| 1015 |
|
| 1016 |
def check_locale_surrogateescape(self, locale): |
| 1017 |
out = self.c_locale_get_error_handler(locale, isolated=True) |
| 1018 |
self.assertEqual(out, |
| 1019 |
'stdin: surrogateescape\n' |
| 1020 |
'stdout: surrogateescape\n' |
| 1021 |
'stderr: backslashreplace\n') |
| 1022 |
|
| 1023 |
# replace the default error handler |
| 1024 |
out = self.c_locale_get_error_handler(locale, encoding=':ignore') |
| 1025 |
self.assertEqual(out, |
| 1026 |
'stdin: ignore\n' |
| 1027 |
'stdout: ignore\n' |
| 1028 |
'stderr: backslashreplace\n') |
| 1029 |
|
| 1030 |
# force the encoding |
| 1031 |
out = self.c_locale_get_error_handler(locale, encoding='iso8859-1') |
| 1032 |
self.assertEqual(out, |
| 1033 |
'stdin: strict\n' |
| 1034 |
'stdout: strict\n' |
| 1035 |
'stderr: backslashreplace\n') |
| 1036 |
out = self.c_locale_get_error_handler(locale, encoding='iso8859-1:') |
| 1037 |
self.assertEqual(out, |
| 1038 |
'stdin: strict\n' |
| 1039 |
'stdout: strict\n' |
| 1040 |
'stderr: backslashreplace\n') |
| 1041 |
|
| 1042 |
# have no any effect |
| 1043 |
out = self.c_locale_get_error_handler(locale, encoding=':') |
| 1044 |
self.assertEqual(out, |
| 1045 |
'stdin: surrogateescape\n' |
| 1046 |
'stdout: surrogateescape\n' |
| 1047 |
'stderr: backslashreplace\n') |
| 1048 |
out = self.c_locale_get_error_handler(locale, encoding='') |
| 1049 |
self.assertEqual(out, |
| 1050 |
'stdin: surrogateescape\n' |
| 1051 |
'stdout: surrogateescape\n' |
| 1052 |
'stderr: backslashreplace\n') |
| 1053 |
|
| 1054 |
@support.requires_subprocess() |
| 1055 |
def test_c_locale_surrogateescape(self): |
| 1056 |
self.check_locale_surrogateescape('C') |
| 1057 |
|
| 1058 |
@support.requires_subprocess() |
| 1059 |
def test_posix_locale_surrogateescape(self): |
| 1060 |
self.check_locale_surrogateescape('POSIX') |
| 1061 |
|
| 1062 |
def test_implementation(self): |
| 1063 |
# This test applies to all implementations equally. |
| 1064 |
|
| 1065 |
levels = {'alpha': 0xA, 'beta': 0xB, 'candidate': 0xC, 'final': 0xF} |
| 1066 |
|
| 1067 |
self.assertHasAttr(sys.implementation, 'name') |
| 1068 |
self.assertHasAttr(sys.implementation, 'version') |
| 1069 |
self.assertHasAttr(sys.implementation, 'hexversion') |
| 1070 |
self.assertHasAttr(sys.implementation, 'cache_tag') |
| 1071 |
self.assertHasAttr(sys.implementation, 'supports_isolated_interpreters') |
| 1072 |
|
| 1073 |
version = sys.implementation.version |
| 1074 |
self.assertEqual(version[:2], (version.major, version.minor)) |
| 1075 |
|
| 1076 |
hexversion = (version.major << 24 | version.minor << 16 | |
| 1077 |
version.micro << 8 | levels[version.releaselevel] << 4 | |
| 1078 |
version.serial << 0) |
| 1079 |
self.assertEqual(sys.implementation.hexversion, hexversion) |
| 1080 |
|
| 1081 |
# PEP 421 requires that .name be lower case. |
| 1082 |
self.assertEqual(sys.implementation.name, |
| 1083 |
sys.implementation.name.lower()) |
| 1084 |
|
| 1085 |
# https://peps.python.org/pep-0734 |
| 1086 |
sii = sys.implementation.supports_isolated_interpreters |
| 1087 |
self.assertIsInstance(sii, bool) |
| 1088 |
if test.support.check_impl_detail(cpython=True): |
| 1089 |
if test.support.is_emscripten or test.support.is_wasi: |
| 1090 |
self.assertFalse(sii) |
| 1091 |
else: |
| 1092 |
self.assertTrue(sii) |
| 1093 |
|
| 1094 |
@test.support.cpython_only |
| 1095 |
def test_debugmallocstats(self): |
| 1096 |
# Test sys._debugmallocstats() |
| 1097 |
from test.support.script_helper import assert_python_ok |
| 1098 |
args = ['-c', 'import sys; sys._debugmallocstats()'] |
| 1099 |
ret, out, err = assert_python_ok(*args) |
| 1100 |
|
| 1101 |
# Output of sys._debugmallocstats() depends on configure flags. |
| 1102 |
# The sysconfig vars are not available on Windows. |
| 1103 |
if sys.platform != "win32": |
| 1104 |
with_pymalloc = sysconfig.get_config_var("WITH_PYMALLOC") |
| 1105 |
self.assertIn(b"free PyDictObjects", err) |
| 1106 |
if with_pymalloc: |
| 1107 |
self.assertIn(b'Small block threshold', err) |
| 1108 |
|
| 1109 |
# The function has no parameter |
| 1110 |
self.assertRaises(TypeError, sys._debugmallocstats, True) |
| 1111 |
|
| 1112 |
@unittest.skipUnless(hasattr(sys, "getallocatedblocks"), |
| 1113 |
"sys.getallocatedblocks unavailable on this build") |
| 1114 |
def test_getallocatedblocks(self): |
| 1115 |
try: |
| 1116 |
import _testinternalcapi |
| 1117 |
except ImportError: |
| 1118 |
with_pymalloc = support.with_pymalloc() |
| 1119 |
else: |
| 1120 |
try: |
| 1121 |
alloc_name = _testinternalcapi.pymem_getallocatorsname() |
| 1122 |
except RuntimeError as exc: |
| 1123 |
# "cannot get allocators name" (ex: tracemalloc is used) |
| 1124 |
with_pymalloc = True |
| 1125 |
else: |
| 1126 |
with_pymalloc = (alloc_name in ('pymalloc', 'pymalloc_debug')) |
| 1127 |
|
| 1128 |
# Some sanity checks |
| 1129 |
a = sys.getallocatedblocks() |
| 1130 |
self.assertIs(type(a), int) |
| 1131 |
if with_pymalloc: |
| 1132 |
self.assertGreater(a, 0) |
| 1133 |
else: |
| 1134 |
# When WITH_PYMALLOC isn't available, we don't know anything |
| 1135 |
# about the underlying implementation: the function might |
| 1136 |
# return 0 or something greater. |
| 1137 |
self.assertGreaterEqual(a, 0) |
| 1138 |
gc.collect() |
| 1139 |
b = sys.getallocatedblocks() |
| 1140 |
self.assertLessEqual(b, a) |
| 1141 |
try: |
| 1142 |
# The reported blocks will include immortalized strings, but the |
| 1143 |
# total ref count will not. This will sanity check that among all |
| 1144 |
# other objects (those eligible for garbage collection) there |
| 1145 |
# are more references being tracked than allocated blocks. |
| 1146 |
interned_immortal = sys.getunicodeinternedsize(_only_immortal=True) |
| 1147 |
self.assertLess(a - interned_immortal, sys.gettotalrefcount()) |
| 1148 |
except AttributeError: |
| 1149 |
# gettotalrefcount() not available |
| 1150 |
pass |
| 1151 |
gc.collect() |
| 1152 |
c = sys.getallocatedblocks() |
| 1153 |
self.assertIn(c, range(b - 50, b + 50)) |
| 1154 |
|
| 1155 |
def test_is_gil_enabled(self): |
| 1156 |
if support.Py_GIL_DISABLED: |
| 1157 |
self.assertIs(type(sys._is_gil_enabled()), bool) |
| 1158 |
else: |
| 1159 |
self.assertTrue(sys._is_gil_enabled()) |
| 1160 |
|
| 1161 |
def test_is_finalizing(self): |
| 1162 |
self.assertIs(sys.is_finalizing(), False) |
| 1163 |
# Don't use the atexit module because _Py_Finalizing is only set |
| 1164 |
# after calling atexit callbacks |
| 1165 |
code = """if 1: |
| 1166 |
import sys |
| 1167 |
|
| 1168 |
class AtExit: |
| 1169 |
is_finalizing = sys.is_finalizing |
| 1170 |
print = print |
| 1171 |
|
| 1172 |
def __del__(self): |
| 1173 |
self.print(self.is_finalizing(), flush=True) |
| 1174 |
|
| 1175 |
# Keep a reference in the __main__ module namespace, so the |
| 1176 |
# AtExit destructor will be called at Python exit |
| 1177 |
ref = AtExit() |
| 1178 |
""" |
| 1179 |
rc, stdout, stderr = assert_python_ok('-c', code) |
| 1180 |
self.assertEqual(stdout.rstrip(), b'True') |
| 1181 |
|
| 1182 |
def test_issue20602(self): |
| 1183 |
# sys.flags and sys.float_info were wiped during shutdown. |
| 1184 |
code = """if 1: |
| 1185 |
import sys |
| 1186 |
class A: |
| 1187 |
def __del__(self, sys=sys): |
| 1188 |
print(sys.flags) |
| 1189 |
print(sys.float_info) |
| 1190 |
a = A() |
| 1191 |
""" |
| 1192 |
rc, out, err = assert_python_ok('-c', code) |
| 1193 |
out = out.splitlines() |
| 1194 |
self.assertIn(b'sys.flags', out[0]) |
| 1195 |
self.assertIn(b'sys.float_info', out[1]) |
| 1196 |
|
| 1197 |
def test_sys_ignores_cleaning_up_user_data(self): |
| 1198 |
code = """if 1: |
| 1199 |
import struct, sys |
| 1200 |
|
| 1201 |
class C: |
| 1202 |
def __init__(self): |
| 1203 |
self.pack = struct.pack |
| 1204 |
def __del__(self): |
| 1205 |
self.pack('I', -42) |
| 1206 |
|
| 1207 |
sys.x = C() |
| 1208 |
""" |
| 1209 |
rc, stdout, stderr = assert_python_ok('-c', code) |
| 1210 |
self.assertEqual(rc, 0) |
| 1211 |
self.assertEqual(stdout.rstrip(), b"") |
| 1212 |
self.assertEqual(stderr.rstrip(), b"") |
| 1213 |
|
| 1214 |
@unittest.skipUnless(sys.platform == "android", "Android only") |
| 1215 |
def test_getandroidapilevel(self): |
| 1216 |
level = sys.getandroidapilevel() |
| 1217 |
self.assertIsInstance(level, int) |
| 1218 |
self.assertGreater(level, 0) |
| 1219 |
|
| 1220 |
@force_not_colorized |
| 1221 |
@support.requires_subprocess() |
| 1222 |
def test_sys_tracebacklimit(self): |
| 1223 |
code = """if 1: |
| 1224 |
import sys |
| 1225 |
def f1(): |
| 1226 |
1 / 0 |
| 1227 |
def f2(): |
| 1228 |
f1() |
| 1229 |
sys.tracebacklimit = %r |
| 1230 |
f2() |
| 1231 |
""" |
| 1232 |
def check(tracebacklimit, expected): |
| 1233 |
p = subprocess.Popen([sys.executable, '-c', code % tracebacklimit], |
| 1234 |
stderr=subprocess.PIPE) |
| 1235 |
out = p.communicate()[1] |
| 1236 |
self.assertEqual(out.splitlines(), expected) |
| 1237 |
|
| 1238 |
traceback = [ |
| 1239 |
b'Traceback (most recent call last):', |
| 1240 |
b' File "<string>", line 8, in <module>', |
| 1241 |
b' f2()', |
| 1242 |
b' ~~^^', |
| 1243 |
b' File "<string>", line 6, in f2', |
| 1244 |
b' f1()', |
| 1245 |
b' ~~^^', |
| 1246 |
b' File "<string>", line 4, in f1', |
| 1247 |
b' 1 / 0', |
| 1248 |
b' ~~^~~', |
| 1249 |
b'ZeroDivisionError: division by zero' |
| 1250 |
] |
| 1251 |
check(10, traceback) |
| 1252 |
check(3, traceback) |
| 1253 |
check(2, traceback[:1] + traceback[4:]) |
| 1254 |
check(1, traceback[:1] + traceback[7:]) |
| 1255 |
check(0, [traceback[-1]]) |
| 1256 |
check(-1, [traceback[-1]]) |
| 1257 |
check(1<<1000, traceback) |
| 1258 |
check(-1<<1000, [traceback[-1]]) |
| 1259 |
check(None, traceback) |
| 1260 |
|
| 1261 |
def test_no_duplicates_in_meta_path(self): |
| 1262 |
self.assertEqual(len(sys.meta_path), len(set(sys.meta_path))) |
| 1263 |
|
| 1264 |
@unittest.skipUnless(hasattr(sys, "_enablelegacywindowsfsencoding"), |
| 1265 |
'needs sys._enablelegacywindowsfsencoding()') |
| 1266 |
def test__enablelegacywindowsfsencoding(self): |
| 1267 |
code = ('import sys', |
| 1268 |
'sys._enablelegacywindowsfsencoding()', |
| 1269 |
'print(sys.getfilesystemencoding(), sys.getfilesystemencodeerrors())') |
| 1270 |
rc, out, err = assert_python_ok('-c', '; '.join(code)) |
| 1271 |
out = out.decode('ascii', 'replace').rstrip() |
| 1272 |
self.assertEqual(out, 'mbcs replace') |
| 1273 |
|
| 1274 |
@support.requires_subprocess() |
| 1275 |
def test_orig_argv(self): |
| 1276 |
code = textwrap.dedent(''' |
| 1277 |
import sys |
| 1278 |
print(sys.argv) |
| 1279 |
print(sys.orig_argv) |
| 1280 |
''') |
| 1281 |
args = [sys.executable, '-I', '-X', 'utf8', '-c', code, 'arg'] |
| 1282 |
proc = subprocess.run(args, check=True, capture_output=True, text=True) |
| 1283 |
expected = [ |
| 1284 |
repr(['-c', 'arg']), # sys.argv |
| 1285 |
repr(args), # sys.orig_argv |
| 1286 |
] |
| 1287 |
self.assertEqual(proc.stdout.rstrip().splitlines(), expected, |
| 1288 |
proc) |
| 1289 |
|
| 1290 |
def test_module_names(self): |
| 1291 |
self.assertIsInstance(sys.stdlib_module_names, frozenset) |
| 1292 |
for name in sys.stdlib_module_names: |
| 1293 |
self.assertIsInstance(name, str) |
| 1294 |
|
| 1295 |
def test_stdlib_dir(self): |
| 1296 |
os = import_helper.import_fresh_module('os') |
| 1297 |
marker = getattr(os, '__file__', None) |
| 1298 |
if marker and not os.path.exists(marker): |
| 1299 |
marker = None |
| 1300 |
expected = os.path.dirname(marker) if marker else None |
| 1301 |
self.assertEqual(os.path.normpath(sys._stdlib_dir), |
| 1302 |
os.path.normpath(expected)) |
| 1303 |
|
| 1304 |
@unittest.skipUnless(hasattr(sys, 'getobjects'), 'need sys.getobjects()') |
| 1305 |
def test_getobjects(self): |
| 1306 |
# sys.getobjects(0) |
| 1307 |
all_objects = sys.getobjects(0) |
| 1308 |
self.assertIsInstance(all_objects, list) |
| 1309 |
self.assertGreater(len(all_objects), 0) |
| 1310 |
|
| 1311 |
# sys.getobjects(0, MyType) |
| 1312 |
class MyType: |
| 1313 |
pass |
| 1314 |
size = 100 |
| 1315 |
my_objects = [MyType() for _ in range(size)] |
| 1316 |
get_objects = sys.getobjects(0, MyType) |
| 1317 |
self.assertEqual(len(get_objects), size) |
| 1318 |
for obj in get_objects: |
| 1319 |
self.assertIsInstance(obj, MyType) |
| 1320 |
|
| 1321 |
# sys.getobjects(3, MyType) |
| 1322 |
get_objects = sys.getobjects(3, MyType) |
| 1323 |
self.assertEqual(len(get_objects), 3) |
| 1324 |
|
| 1325 |
@unittest.skipUnless(hasattr(sys, '_stats_on'), 'need Py_STATS build') |
| 1326 |
def test_pystats(self): |
| 1327 |
# Call the functions, just check that they don't crash |
| 1328 |
# Cannot save/restore state. |
| 1329 |
sys._stats_on() |
| 1330 |
sys._stats_off() |
| 1331 |
sys._stats_clear() |
| 1332 |
sys._stats_dump() |
| 1333 |
|
| 1334 |
@test.support.cpython_only |
| 1335 |
@unittest.skipUnless(hasattr(sys, 'abiflags'), 'need sys.abiflags') |
| 1336 |
def test_disable_gil_abi(self): |
| 1337 |
self.assertEqual('t' in sys.abiflags, support.Py_GIL_DISABLED) |
| 1338 |
|
| 1339 |
|
| 1340 |
@test.support.cpython_only |
| 1341 |
class UnraisableHookTest(unittest.TestCase): |
| 1342 |
def test_original_unraisablehook(self): |
| 1343 |
_testcapi = import_helper.import_module('_testcapi') |
| 1344 |
from _testcapi import err_writeunraisable, err_formatunraisable |
| 1345 |
obj = hex |
| 1346 |
|
| 1347 |
with support.swap_attr(sys, 'unraisablehook', |
| 1348 |
sys.__unraisablehook__): |
| 1349 |
with support.captured_stderr() as stderr: |
| 1350 |
err_writeunraisable(ValueError(42), obj) |
| 1351 |
lines = stderr.getvalue().splitlines() |
| 1352 |
self.assertEqual(lines[0], f'Exception ignored in: {obj!r}') |
| 1353 |
self.assertEqual(lines[1], 'Traceback (most recent call last):') |
| 1354 |
self.assertEqual(lines[-1], 'ValueError: 42') |
| 1355 |
|
| 1356 |
with support.captured_stderr() as stderr: |
| 1357 |
err_writeunraisable(ValueError(42), None) |
| 1358 |
lines = stderr.getvalue().splitlines() |
| 1359 |
self.assertEqual(lines[0], 'Traceback (most recent call last):') |
| 1360 |
self.assertEqual(lines[-1], 'ValueError: 42') |
| 1361 |
|
| 1362 |
with support.captured_stderr() as stderr: |
| 1363 |
err_formatunraisable(ValueError(42), 'Error in %R', obj) |
| 1364 |
lines = stderr.getvalue().splitlines() |
| 1365 |
self.assertEqual(lines[0], f'Error in {obj!r}:') |
| 1366 |
self.assertEqual(lines[1], 'Traceback (most recent call last):') |
| 1367 |
self.assertEqual(lines[-1], 'ValueError: 42') |
| 1368 |
|
| 1369 |
with support.captured_stderr() as stderr: |
| 1370 |
err_formatunraisable(ValueError(42), None) |
| 1371 |
lines = stderr.getvalue().splitlines() |
| 1372 |
self.assertEqual(lines[0], 'Traceback (most recent call last):') |
| 1373 |
self.assertEqual(lines[-1], 'ValueError: 42') |
| 1374 |
|
| 1375 |
def test_original_unraisablehook_err(self): |
| 1376 |
# bpo-22836: PyErr_WriteUnraisable() should give sensible reports |
| 1377 |
class BrokenDel: |
| 1378 |
def __del__(self): |
| 1379 |
exc = ValueError("del is broken") |
| 1380 |
# The following line is included in the traceback report: |
| 1381 |
raise exc |
| 1382 |
|
| 1383 |
class BrokenStrException(Exception): |
| 1384 |
def __str__(self): |
| 1385 |
raise Exception("str() is broken") |
| 1386 |
|
| 1387 |
class BrokenExceptionDel: |
| 1388 |
def __del__(self): |
| 1389 |
exc = BrokenStrException() |
| 1390 |
# The following line is included in the traceback report: |
| 1391 |
raise exc |
| 1392 |
|
| 1393 |
for test_class in (BrokenDel, BrokenExceptionDel): |
| 1394 |
with self.subTest(test_class): |
| 1395 |
obj = test_class() |
| 1396 |
with test.support.captured_stderr() as stderr, \ |
| 1397 |
test.support.swap_attr(sys, 'unraisablehook', |
| 1398 |
sys.__unraisablehook__): |
| 1399 |
# Trigger obj.__del__() |
| 1400 |
del obj |
| 1401 |
|
| 1402 |
report = stderr.getvalue() |
| 1403 |
self.assertIn("Exception ignored", report) |
| 1404 |
self.assertIn(test_class.__del__.__qualname__, report) |
| 1405 |
self.assertIn("test_sys.py", report) |
| 1406 |
self.assertIn("raise exc", report) |
| 1407 |
if test_class is BrokenExceptionDel: |
| 1408 |
self.assertIn("BrokenStrException", report) |
| 1409 |
self.assertIn("<exception str() failed>", report) |
| 1410 |
else: |
| 1411 |
self.assertIn("ValueError", report) |
| 1412 |
self.assertIn("del is broken", report) |
| 1413 |
self.assertEndsWith(report, "\n") |
| 1414 |
|
| 1415 |
def test_original_unraisablehook_exception_qualname(self): |
| 1416 |
# See bpo-41031, bpo-45083. |
| 1417 |
# Check that the exception is printed with its qualified name |
| 1418 |
# rather than just classname, and the module names appears |
| 1419 |
# unless it is one of the hard-coded exclusions. |
| 1420 |
_testcapi = import_helper.import_module('_testcapi') |
| 1421 |
from _testcapi import err_writeunraisable |
| 1422 |
class A: |
| 1423 |
class B: |
| 1424 |
class X(Exception): |
| 1425 |
pass |
| 1426 |
|
| 1427 |
for moduleName in 'builtins', '__main__', 'some_module': |
| 1428 |
with self.subTest(moduleName=moduleName): |
| 1429 |
A.B.X.__module__ = moduleName |
| 1430 |
with test.support.captured_stderr() as stderr, test.support.swap_attr( |
| 1431 |
sys, 'unraisablehook', sys.__unraisablehook__ |
| 1432 |
): |
| 1433 |
err_writeunraisable(A.B.X(), "obj") |
| 1434 |
report = stderr.getvalue() |
| 1435 |
self.assertIn(A.B.X.__qualname__, report) |
| 1436 |
if moduleName in ['builtins', '__main__']: |
| 1437 |
self.assertNotIn(moduleName + '.', report) |
| 1438 |
else: |
| 1439 |
self.assertIn(moduleName + '.', report) |
| 1440 |
|
| 1441 |
def test_original_unraisablehook_wrong_type(self): |
| 1442 |
exc = ValueError(42) |
| 1443 |
with test.support.swap_attr(sys, 'unraisablehook', |
| 1444 |
sys.__unraisablehook__): |
| 1445 |
with self.assertRaises(TypeError): |
| 1446 |
sys.unraisablehook(exc) |
| 1447 |
|
| 1448 |
def test_custom_unraisablehook(self): |
| 1449 |
_testcapi = import_helper.import_module('_testcapi') |
| 1450 |
from _testcapi import err_writeunraisable, err_formatunraisable |
| 1451 |
hook_args = None |
| 1452 |
|
| 1453 |
def hook_func(args): |
| 1454 |
nonlocal hook_args |
| 1455 |
hook_args = args |
| 1456 |
|
| 1457 |
obj = hex |
| 1458 |
try: |
| 1459 |
with test.support.swap_attr(sys, 'unraisablehook', hook_func): |
| 1460 |
exc = ValueError(42) |
| 1461 |
err_writeunraisable(exc, obj) |
| 1462 |
self.assertIs(hook_args.exc_type, type(exc)) |
| 1463 |
self.assertIs(hook_args.exc_value, exc) |
| 1464 |
self.assertIs(hook_args.exc_traceback, exc.__traceback__) |
| 1465 |
self.assertIsNone(hook_args.err_msg) |
| 1466 |
self.assertEqual(hook_args.object, obj) |
| 1467 |
|
| 1468 |
err_formatunraisable(exc, "custom hook %R", obj) |
| 1469 |
self.assertIs(hook_args.exc_type, type(exc)) |
| 1470 |
self.assertIs(hook_args.exc_value, exc) |
| 1471 |
self.assertIs(hook_args.exc_traceback, exc.__traceback__) |
| 1472 |
self.assertEqual(hook_args.err_msg, f'custom hook {obj!r}') |
| 1473 |
self.assertIsNone(hook_args.object) |
| 1474 |
finally: |
| 1475 |
# expected and hook_args contain an exception: break reference cycle |
| 1476 |
expected = None |
| 1477 |
hook_args = None |
| 1478 |
|
| 1479 |
def test_custom_unraisablehook_fail(self): |
| 1480 |
_testcapi = import_helper.import_module('_testcapi') |
| 1481 |
from _testcapi import err_writeunraisable |
| 1482 |
|
| 1483 |
def hook_func(*args): |
| 1484 |
raise Exception("hook_func failed") |
| 1485 |
|
| 1486 |
with test.support.captured_output("stderr") as stderr: |
| 1487 |
with test.support.swap_attr(sys, 'unraisablehook', hook_func): |
| 1488 |
err_writeunraisable(ValueError(42), "custom hook fail") |
| 1489 |
|
| 1490 |
err = stderr.getvalue() |
| 1491 |
self.assertIn(f'Exception ignored in sys.unraisablehook: ' |
| 1492 |
f'{hook_func!r}\n', |
| 1493 |
err) |
| 1494 |
self.assertIn('Traceback (most recent call last):\n', err) |
| 1495 |
self.assertIn('Exception: hook_func failed\n', err) |
| 1496 |
|
| 1497 |
|
| 1498 |
@test.support.cpython_only |
| 1499 |
class SizeofTest(unittest.TestCase): |
| 1500 |
|
| 1501 |
def setUp(self): |
| 1502 |
self.P = struct.calcsize('P') |
| 1503 |
self.longdigit = sys.int_info.sizeof_digit |
| 1504 |
_testinternalcapi = import_helper.import_module("_testinternalcapi") |
| 1505 |
self.gc_headsize = _testinternalcapi.SIZEOF_PYGC_HEAD |
| 1506 |
self.managed_pre_header_size = _testinternalcapi.SIZEOF_MANAGED_PRE_HEADER |
| 1507 |
|
| 1508 |
check_sizeof = test.support.check_sizeof |
| 1509 |
|
| 1510 |
def test_gc_head_size(self): |
| 1511 |
# Check that the gc header size is added to objects tracked by the gc. |
| 1512 |
vsize = test.support.calcvobjsize |
| 1513 |
gc_header_size = self.gc_headsize |
| 1514 |
# bool objects are not gc tracked |
| 1515 |
self.assertEqual(sys.getsizeof(True), vsize('') + self.longdigit) |
| 1516 |
# but lists are |
| 1517 |
self.assertEqual(sys.getsizeof([]), vsize('Pn') + gc_header_size) |
| 1518 |
|
| 1519 |
def test_errors(self): |
| 1520 |
class BadSizeof: |
| 1521 |
def __sizeof__(self): |
| 1522 |
raise ValueError |
| 1523 |
self.assertRaises(ValueError, sys.getsizeof, BadSizeof()) |
| 1524 |
|
| 1525 |
class InvalidSizeof: |
| 1526 |
def __sizeof__(self): |
| 1527 |
return None |
| 1528 |
self.assertRaises(TypeError, sys.getsizeof, InvalidSizeof()) |
| 1529 |
sentinel = ["sentinel"] |
| 1530 |
self.assertIs(sys.getsizeof(InvalidSizeof(), sentinel), sentinel) |
| 1531 |
|
| 1532 |
class FloatSizeof: |
| 1533 |
def __sizeof__(self): |
| 1534 |
return 4.5 |
| 1535 |
self.assertRaises(TypeError, sys.getsizeof, FloatSizeof()) |
| 1536 |
self.assertIs(sys.getsizeof(FloatSizeof(), sentinel), sentinel) |
| 1537 |
|
| 1538 |
class OverflowSizeof(int): |
| 1539 |
def __sizeof__(self): |
| 1540 |
return int(self) |
| 1541 |
self.assertEqual(sys.getsizeof(OverflowSizeof(sys.maxsize)), |
| 1542 |
sys.maxsize + self.gc_headsize + self.managed_pre_header_size) |
| 1543 |
with self.assertRaises(OverflowError): |
| 1544 |
sys.getsizeof(OverflowSizeof(sys.maxsize + 1)) |
| 1545 |
with self.assertRaises(ValueError): |
| 1546 |
sys.getsizeof(OverflowSizeof(-1)) |
| 1547 |
with self.assertRaises((ValueError, OverflowError)): |
| 1548 |
sys.getsizeof(OverflowSizeof(-sys.maxsize - 1)) |
| 1549 |
|
| 1550 |
def test_default(self): |
| 1551 |
size = test.support.calcvobjsize |
| 1552 |
self.assertEqual(sys.getsizeof(True), size('') + self.longdigit) |
| 1553 |
self.assertEqual(sys.getsizeof(True, -1), size('') + self.longdigit) |
| 1554 |
|
| 1555 |
def test_objecttypes(self): |
| 1556 |
# check all types defined in Objects/ |
| 1557 |
calcsize = struct.calcsize |
| 1558 |
size = test.support.calcobjsize |
| 1559 |
vsize = test.support.calcvobjsize |
| 1560 |
check = self.check_sizeof |
| 1561 |
# bool |
| 1562 |
check(True, vsize('') + self.longdigit) |
| 1563 |
check(False, vsize('') + self.longdigit) |
| 1564 |
# buffer |
| 1565 |
# XXX |
| 1566 |
# builtin_function_or_method |
| 1567 |
check(len, size('5P')) |
| 1568 |
# bytearray |
| 1569 |
samples = [b'', b'u'*100000] |
| 1570 |
for sample in samples: |
| 1571 |
x = bytearray(sample) |
| 1572 |
check(x, vsize('n2Pi') + x.__alloc__()) |
| 1573 |
# bytearray_iterator |
| 1574 |
check(iter(bytearray()), size('nP')) |
| 1575 |
# bytes |
| 1576 |
check(b'', vsize('n') + 1) |
| 1577 |
check(b'x' * 10, vsize('n') + 11) |
| 1578 |
# cell |
| 1579 |
def get_cell(): |
| 1580 |
x = 42 |
| 1581 |
def inner(): |
| 1582 |
return x |
| 1583 |
return inner |
| 1584 |
check(get_cell().__closure__[0], size('P')) |
| 1585 |
# code |
| 1586 |
def check_code_size(a, expected_size): |
| 1587 |
self.assertGreaterEqual(sys.getsizeof(a), expected_size) |
| 1588 |
check_code_size(get_cell().__code__, size('6i13P')) |
| 1589 |
check_code_size(get_cell.__code__, size('6i13P')) |
| 1590 |
def get_cell2(x): |
| 1591 |
def inner(): |
| 1592 |
return x |
| 1593 |
return inner |
| 1594 |
check_code_size(get_cell2.__code__, size('6i13P') + calcsize('n')) |
| 1595 |
# complex |
| 1596 |
check(complex(0,1), size('2d')) |
| 1597 |
# method_descriptor (descriptor object) |
| 1598 |
check(str.lower, size('3PPP')) |
| 1599 |
# classmethod_descriptor (descriptor object) |
| 1600 |
# XXX |
| 1601 |
# member_descriptor (descriptor object) |
| 1602 |
import datetime |
| 1603 |
check(datetime.timedelta.days, size('3PP')) |
| 1604 |
# getset_descriptor (descriptor object) |
| 1605 |
import collections |
| 1606 |
check(collections.defaultdict.default_factory, size('3PP')) |
| 1607 |
# wrapper_descriptor (descriptor object) |
| 1608 |
check(int.__add__, size('3P2P')) |
| 1609 |
# method-wrapper (descriptor object) |
| 1610 |
check({}.__iter__, size('2P')) |
| 1611 |
# empty dict |
| 1612 |
check({}, size('nQ2P')) |
| 1613 |
# dict (string key) |
| 1614 |
check({"a": 1}, size('nQ2P') + calcsize(DICT_KEY_STRUCT_FORMAT) + 8 + (8*2//3)*calcsize('2P')) |
| 1615 |
longdict = {str(i): i for i in range(8)} |
| 1616 |
check(longdict, size('nQ2P') + calcsize(DICT_KEY_STRUCT_FORMAT) + 16 + (16*2//3)*calcsize('2P')) |
| 1617 |
# dict (non-string key) |
| 1618 |
check({1: 1}, size('nQ2P') + calcsize(DICT_KEY_STRUCT_FORMAT) + 8 + (8*2//3)*calcsize('n2P')) |
| 1619 |
longdict = {1:1, 2:2, 3:3, 4:4, 5:5, 6:6, 7:7, 8:8} |
| 1620 |
check(longdict, size('nQ2P') + calcsize(DICT_KEY_STRUCT_FORMAT) + 16 + (16*2//3)*calcsize('n2P')) |
| 1621 |
# dictionary-keyview |
| 1622 |
check({}.keys(), size('P')) |
| 1623 |
# dictionary-valueview |
| 1624 |
check({}.values(), size('P')) |
| 1625 |
# dictionary-itemview |
| 1626 |
check({}.items(), size('P')) |
| 1627 |
# dictionary iterator |
| 1628 |
check(iter({}), size('P2nPn')) |
| 1629 |
# dictionary-keyiterator |
| 1630 |
check(iter({}.keys()), size('P2nPn')) |
| 1631 |
# dictionary-valueiterator |
| 1632 |
check(iter({}.values()), size('P2nPn')) |
| 1633 |
# dictionary-itemiterator |
| 1634 |
check(iter({}.items()), size('P2nPn')) |
| 1635 |
# dictproxy |
| 1636 |
class C(object): pass |
| 1637 |
check(C.__dict__, size('P')) |
| 1638 |
# BaseException |
| 1639 |
check(BaseException(), size('6Pb')) |
| 1640 |
# UnicodeEncodeError |
| 1641 |
check(UnicodeEncodeError("", "", 0, 0, ""), size('6Pb 2P2nP')) |
| 1642 |
# UnicodeDecodeError |
| 1643 |
check(UnicodeDecodeError("", b"", 0, 0, ""), size('6Pb 2P2nP')) |
| 1644 |
# UnicodeTranslateError |
| 1645 |
check(UnicodeTranslateError("", 0, 1, ""), size('6Pb 2P2nP')) |
| 1646 |
# ellipses |
| 1647 |
check(Ellipsis, size('')) |
| 1648 |
# EncodingMap |
| 1649 |
import codecs, encodings.iso8859_3 |
| 1650 |
x = codecs.charmap_build(encodings.iso8859_3.decoding_table) |
| 1651 |
check(x, size('32B2iB')) |
| 1652 |
# enumerate |
| 1653 |
check(enumerate([]), size('n4P')) |
| 1654 |
# reverse |
| 1655 |
check(reversed(''), size('nP')) |
| 1656 |
# float |
| 1657 |
check(float(0), size('d')) |
| 1658 |
# sys.floatinfo |
| 1659 |
check(sys.float_info, self.P + vsize('') + self.P * len(sys.float_info)) |
| 1660 |
# frame |
| 1661 |
def func(): |
| 1662 |
return sys._getframe() |
| 1663 |
x = func() |
| 1664 |
if support.Py_GIL_DISABLED: |
| 1665 |
INTERPRETER_FRAME = '9PihcP' |
| 1666 |
else: |
| 1667 |
INTERPRETER_FRAME = '9PhcP' |
| 1668 |
check(x, size('3PiccPPP' + INTERPRETER_FRAME + 'P')) |
| 1669 |
# function |
| 1670 |
def func(): pass |
| 1671 |
check(func, size('16Pi')) |
| 1672 |
class c(): |
| 1673 |
@staticmethod |
| 1674 |
def foo(): |
| 1675 |
pass |
| 1676 |
@classmethod |
| 1677 |
def bar(cls): |
| 1678 |
pass |
| 1679 |
# staticmethod |
| 1680 |
check(foo, size('PP')) |
| 1681 |
# classmethod |
| 1682 |
check(bar, size('PP')) |
| 1683 |
# generator |
| 1684 |
def get_gen(): yield 1 |
| 1685 |
check(get_gen(), size('6P4c' + INTERPRETER_FRAME + 'P')) |
| 1686 |
# iterator |
| 1687 |
check(iter('abc'), size('lP')) |
| 1688 |
# callable-iterator |
| 1689 |
import re |
| 1690 |
check(re.finditer('',''), size('2P')) |
| 1691 |
# list |
| 1692 |
check(list([]), vsize('Pn')) |
| 1693 |
check(list([1]), vsize('Pn') + 2*self.P) |
| 1694 |
check(list([1, 2]), vsize('Pn') + 2*self.P) |
| 1695 |
check(list([1, 2, 3]), vsize('Pn') + 4*self.P) |
| 1696 |
# sortwrapper (list) |
| 1697 |
# XXX |
| 1698 |
# cmpwrapper (list) |
| 1699 |
# XXX |
| 1700 |
# listiterator (list) |
| 1701 |
check(iter([]), size('lP')) |
| 1702 |
# listreverseiterator (list) |
| 1703 |
check(reversed([]), size('nP')) |
| 1704 |
# int |
| 1705 |
check(0, vsize('') + self.longdigit) |
| 1706 |
check(1, vsize('') + self.longdigit) |
| 1707 |
check(-1, vsize('') + self.longdigit) |
| 1708 |
PyLong_BASE = 2**sys.int_info.bits_per_digit |
| 1709 |
check(int(PyLong_BASE), vsize('') + 2*self.longdigit) |
| 1710 |
check(int(PyLong_BASE**2-1), vsize('') + 2*self.longdigit) |
| 1711 |
check(int(PyLong_BASE**2), vsize('') + 3*self.longdigit) |
| 1712 |
# module |
| 1713 |
if support.Py_GIL_DISABLED: |
| 1714 |
check(unittest, size('PPPPPP')) |
| 1715 |
else: |
| 1716 |
check(unittest, size('PPPPP')) |
| 1717 |
# None |
| 1718 |
check(None, size('')) |
| 1719 |
# NotImplementedType |
| 1720 |
check(NotImplemented, size('')) |
| 1721 |
# object |
| 1722 |
check(object(), size('')) |
| 1723 |
# property (descriptor object) |
| 1724 |
class C(object): |
| 1725 |
def getx(self): return self.__x |
| 1726 |
def setx(self, value): self.__x = value |
| 1727 |
def delx(self): del self.__x |
| 1728 |
x = property(getx, setx, delx, "") |
| 1729 |
check(x, size('5Pi')) |
| 1730 |
# PyCapsule |
| 1731 |
try: |
| 1732 |
import _datetime |
| 1733 |
except ModuleNotFoundError: |
| 1734 |
pass |
| 1735 |
else: |
| 1736 |
check(_datetime.datetime_CAPI, size('6P')) |
| 1737 |
# rangeiterator |
| 1738 |
check(iter(range(1)), size('3l')) |
| 1739 |
check(iter(range(2**65)), size('3P')) |
| 1740 |
# reverse |
| 1741 |
check(reversed(''), size('nP')) |
| 1742 |
# range |
| 1743 |
check(range(1), size('4P')) |
| 1744 |
check(range(66000), size('4P')) |
| 1745 |
# set |
| 1746 |
# frozenset |
| 1747 |
PySet_MINSIZE = 8 |
| 1748 |
samples = [[], range(10), range(50)] |
| 1749 |
s = size('3nP' + PySet_MINSIZE*'nP' + '2nP') |
| 1750 |
for sample in samples: |
| 1751 |
minused = len(sample) |
| 1752 |
if minused == 0: tmp = 1 |
| 1753 |
# the computation of minused is actually a bit more complicated |
| 1754 |
# but this suffices for the sizeof test |
| 1755 |
minused = minused*2 |
| 1756 |
newsize = PySet_MINSIZE |
| 1757 |
while newsize <= minused: |
| 1758 |
newsize = newsize << 1 |
| 1759 |
if newsize <= 8: |
| 1760 |
check(set(sample), s) |
| 1761 |
check(frozenset(sample), s) |
| 1762 |
else: |
| 1763 |
check(set(sample), s + newsize*calcsize('nP')) |
| 1764 |
check(frozenset(sample), s + newsize*calcsize('nP')) |
| 1765 |
# setiterator |
| 1766 |
check(iter(set()), size('P3n')) |
| 1767 |
# slice |
| 1768 |
check(slice(0), size('3P')) |
| 1769 |
# super |
| 1770 |
check(super(int), size('3P')) |
| 1771 |
# tuple |
| 1772 |
check((), vsize('') + self.P) |
| 1773 |
check((1,2,3), vsize('') + self.P + 3*self.P) |
| 1774 |
# type |
| 1775 |
# static type: PyTypeObject |
| 1776 |
fmt = 'P2nPI13Pl4Pn9Pn12PIPc' |
| 1777 |
s = vsize(fmt) |
| 1778 |
check(int, s) |
| 1779 |
typeid = 'n' if support.Py_GIL_DISABLED else '' |
| 1780 |
# class |
| 1781 |
s = vsize(fmt + # PyTypeObject |
| 1782 |
'4P' # PyAsyncMethods |
| 1783 |
'36P' # PyNumberMethods |
| 1784 |
'3P' # PyMappingMethods |
| 1785 |
'10P' # PySequenceMethods |
| 1786 |
'2P' # PyBufferProcs |
| 1787 |
'7P' |
| 1788 |
'1PIP' # Specializer cache |
| 1789 |
+ typeid # heap type id (free-threaded only) |
| 1790 |
) |
| 1791 |
class newstyleclass(object): pass |
| 1792 |
# Separate block for PyDictKeysObject with 8 keys and 5 entries |
| 1793 |
check(newstyleclass, s + calcsize(DICT_KEY_STRUCT_FORMAT) + 64 + 42*calcsize("2P")) |
| 1794 |
# dict with shared keys |
| 1795 |
[newstyleclass() for _ in range(100)] |
| 1796 |
check(newstyleclass().__dict__, size('nQ2P') + self.P) |
| 1797 |
o = newstyleclass() |
| 1798 |
o.a = o.b = o.c = o.d = o.e = o.f = o.g = o.h = 1 |
| 1799 |
# Separate block for PyDictKeysObject with 16 keys and 10 entries |
| 1800 |
check(newstyleclass, s + calcsize(DICT_KEY_STRUCT_FORMAT) + 64 + 42*calcsize("2P")) |
| 1801 |
# dict with shared keys |
| 1802 |
check(newstyleclass().__dict__, size('nQ2P') + self.P) |
| 1803 |
# unicode |
| 1804 |
# each tuple contains a string and its expected character size |
| 1805 |
# don't put any static strings here, as they may contain |
| 1806 |
# wchar_t or UTF-8 representations |
| 1807 |
samples = ['1'*100, '\xff'*50, |
| 1808 |
'\u0100'*40, '\uffff'*100, |
| 1809 |
'\U00010000'*30, '\U0010ffff'*100] |
| 1810 |
# also update field definitions in test_unicode.test_raiseMemError |
| 1811 |
asciifields = "nnb" |
| 1812 |
compactfields = asciifields + "nP" |
| 1813 |
unicodefields = compactfields + "P" |
| 1814 |
for s in samples: |
| 1815 |
maxchar = ord(max(s)) |
| 1816 |
if maxchar < 128: |
| 1817 |
L = size(asciifields) + len(s) + 1 |
| 1818 |
elif maxchar < 256: |
| 1819 |
L = size(compactfields) + len(s) + 1 |
| 1820 |
elif maxchar < 65536: |
| 1821 |
L = size(compactfields) + 2*(len(s) + 1) |
| 1822 |
else: |
| 1823 |
L = size(compactfields) + 4*(len(s) + 1) |
| 1824 |
check(s, L) |
| 1825 |
# verify that the UTF-8 size is accounted for |
| 1826 |
s = chr(0x4000) # 4 bytes canonical representation |
| 1827 |
check(s, size(compactfields) + 4) |
| 1828 |
# compile() will trigger the generation of the UTF-8 |
| 1829 |
# representation as a side effect |
| 1830 |
compile(s, "<stdin>", "eval") |
| 1831 |
check(s, size(compactfields) + 4 + 4) |
| 1832 |
# TODO: add check that forces the presence of wchar_t representation |
| 1833 |
# TODO: add check that forces layout of unicodefields |
| 1834 |
# weakref |
| 1835 |
import weakref |
| 1836 |
if support.Py_GIL_DISABLED: |
| 1837 |
expected = size('2Pn4P') |
| 1838 |
else: |
| 1839 |
expected = size('2Pn3P') |
| 1840 |
check(weakref.ref(int), expected) |
| 1841 |
# weakproxy |
| 1842 |
# XXX |
| 1843 |
# weakcallableproxy |
| 1844 |
check(weakref.proxy(int), expected) |
| 1845 |
|
| 1846 |
def check_slots(self, obj, base, extra): |
| 1847 |
expected = sys.getsizeof(base) + struct.calcsize(extra) |
| 1848 |
if gc.is_tracked(obj) and not gc.is_tracked(base): |
| 1849 |
expected += self.gc_headsize |
| 1850 |
self.assertEqual(sys.getsizeof(obj), expected) |
| 1851 |
|
| 1852 |
def test_slots(self): |
| 1853 |
# check all subclassable types defined in Objects/ that allow |
| 1854 |
# non-empty __slots__ |
| 1855 |
check = self.check_slots |
| 1856 |
class BA(bytearray): |
| 1857 |
__slots__ = 'a', 'b', 'c' |
| 1858 |
check(BA(), bytearray(), '3P') |
| 1859 |
class D(dict): |
| 1860 |
__slots__ = 'a', 'b', 'c' |
| 1861 |
check(D(x=[]), {'x': []}, '3P') |
| 1862 |
class L(list): |
| 1863 |
__slots__ = 'a', 'b', 'c' |
| 1864 |
check(L(), [], '3P') |
| 1865 |
class S(set): |
| 1866 |
__slots__ = 'a', 'b', 'c' |
| 1867 |
check(S(), set(), '3P') |
| 1868 |
class FS(frozenset): |
| 1869 |
__slots__ = 'a', 'b', 'c' |
| 1870 |
check(FS(), frozenset(), '3P') |
| 1871 |
from collections import OrderedDict |
| 1872 |
class OD(OrderedDict): |
| 1873 |
__slots__ = 'a', 'b', 'c' |
| 1874 |
check(OD(x=[]), OrderedDict(x=[]), '3P') |
| 1875 |
|
| 1876 |
def test_pythontypes(self): |
| 1877 |
# check all types defined in Python/ |
| 1878 |
size = test.support.calcobjsize |
| 1879 |
vsize = test.support.calcvobjsize |
| 1880 |
check = self.check_sizeof |
| 1881 |
# _ast.AST |
| 1882 |
import _ast |
| 1883 |
check(_ast.AST(), size('P')) |
| 1884 |
try: |
| 1885 |
raise TypeError |
| 1886 |
except TypeError as e: |
| 1887 |
tb = e.__traceback__ |
| 1888 |
# traceback |
| 1889 |
if tb is not None: |
| 1890 |
check(tb, size('2P2i')) |
| 1891 |
# symtable entry |
| 1892 |
# XXX |
| 1893 |
# sys.flags |
| 1894 |
# FIXME: The +3 is for the 'gil', 'thread_inherit_context' and |
| 1895 |
# 'context_aware_warnings' flags and will not be necessary once |
| 1896 |
# gh-122575 is fixed |
| 1897 |
check(sys.flags, vsize('') + self.P + self.P * (3 + len(sys.flags))) |
| 1898 |
|
| 1899 |
def test_asyncgen_hooks(self): |
| 1900 |
old = sys.get_asyncgen_hooks() |
| 1901 |
self.assertIsNone(old.firstiter) |
| 1902 |
self.assertIsNone(old.finalizer) |
| 1903 |
|
| 1904 |
firstiter = lambda *a: None |
| 1905 |
finalizer = lambda *a: None |
| 1906 |
|
| 1907 |
with self.assertRaises(TypeError): |
| 1908 |
sys.set_asyncgen_hooks(firstiter=firstiter, finalizer="invalid") |
| 1909 |
cur = sys.get_asyncgen_hooks() |
| 1910 |
self.assertIsNone(cur.firstiter) |
| 1911 |
self.assertIsNone(cur.finalizer) |
| 1912 |
|
| 1913 |
# gh-118473 |
| 1914 |
with self.assertRaises(TypeError): |
| 1915 |
sys.set_asyncgen_hooks(firstiter="invalid", finalizer=finalizer) |
| 1916 |
cur = sys.get_asyncgen_hooks() |
| 1917 |
self.assertIsNone(cur.firstiter) |
| 1918 |
self.assertIsNone(cur.finalizer) |
| 1919 |
|
| 1920 |
sys.set_asyncgen_hooks(firstiter=firstiter) |
| 1921 |
hooks = sys.get_asyncgen_hooks() |
| 1922 |
self.assertIs(hooks.firstiter, firstiter) |
| 1923 |
self.assertIs(hooks[0], firstiter) |
| 1924 |
self.assertIs(hooks.finalizer, None) |
| 1925 |
self.assertIs(hooks[1], None) |
| 1926 |
|
| 1927 |
sys.set_asyncgen_hooks(finalizer=finalizer) |
| 1928 |
hooks = sys.get_asyncgen_hooks() |
| 1929 |
self.assertIs(hooks.firstiter, firstiter) |
| 1930 |
self.assertIs(hooks[0], firstiter) |
| 1931 |
self.assertIs(hooks.finalizer, finalizer) |
| 1932 |
self.assertIs(hooks[1], finalizer) |
| 1933 |
|
| 1934 |
sys.set_asyncgen_hooks(*old) |
| 1935 |
cur = sys.get_asyncgen_hooks() |
| 1936 |
self.assertIsNone(cur.firstiter) |
| 1937 |
self.assertIsNone(cur.finalizer) |
| 1938 |
|
| 1939 |
def test_changing_sys_stderr_and_removing_reference(self): |
| 1940 |
# If the default displayhook doesn't take a strong reference |
| 1941 |
# to sys.stderr the following code can crash. See bpo-43660 |
| 1942 |
# for more details. |
| 1943 |
code = textwrap.dedent(''' |
| 1944 |
import sys |
| 1945 |
class MyStderr: |
| 1946 |
def write(self, s): |
| 1947 |
sys.stderr = None |
| 1948 |
sys.stderr = MyStderr() |
| 1949 |
1/0 |
| 1950 |
''') |
| 1951 |
rc, out, err = assert_python_failure('-c', code) |
| 1952 |
self.assertEqual(out, b"") |
| 1953 |
self.assertEqual(err, b"") |
| 1954 |
|
| 1955 |
@test.support.support_remote_exec_only |
| 1956 |
@test.support.cpython_only |
| 1957 |
class TestRemoteExec(unittest.TestCase): |
| 1958 |
def tearDown(self): |
| 1959 |
test.support.reap_children() |
| 1960 |
|
| 1961 |
def _run_remote_exec_test(self, script_code, python_args=None, env=None, |
| 1962 |
prologue='', |
| 1963 |
script_path=os_helper.TESTFN + '_remote.py'): |
| 1964 |
# Create the script that will be remotely executed |
| 1965 |
self.addCleanup(os_helper.unlink, script_path) |
| 1966 |
|
| 1967 |
with open(script_path, 'w') as f: |
| 1968 |
f.write(script_code) |
| 1969 |
|
| 1970 |
# Create and run the target process |
| 1971 |
target = os_helper.TESTFN + '_target.py' |
| 1972 |
self.addCleanup(os_helper.unlink, target) |
| 1973 |
|
| 1974 |
port = find_unused_port() |
| 1975 |
|
| 1976 |
with open(target, 'w') as f: |
| 1977 |
f.write(f''' |
| 1978 |
import sys |
| 1979 |
import time |
| 1980 |
import socket |
| 1981 |
|
| 1982 |
# Connect to the test process |
| 1983 |
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 1984 |
sock.connect(('localhost', {port})) |
| 1985 |
|
| 1986 |
{prologue} |
| 1987 |
|
| 1988 |
# Signal that the process is ready |
| 1989 |
sock.sendall(b"ready") |
| 1990 |
|
| 1991 |
print("Target process running...") |
| 1992 |
|
| 1993 |
# Wait for remote script to be executed |
| 1994 |
# (the execution will happen as the following |
| 1995 |
# code is processed as soon as the recv call |
| 1996 |
# unblocks) |
| 1997 |
sock.recv(1024) |
| 1998 |
|
| 1999 |
# Do a bunch of work to give the remote script time to run |
| 2000 |
x = 0 |
| 2001 |
for i in range(100): |
| 2002 |
x += i |
| 2003 |
|
| 2004 |
# Write confirmation back |
| 2005 |
sock.sendall(b"executed") |
| 2006 |
sock.close() |
| 2007 |
''') |
| 2008 |
|
| 2009 |
# Start the target process and capture its output |
| 2010 |
cmd = [sys.executable] |
| 2011 |
if python_args: |
| 2012 |
cmd.extend(python_args) |
| 2013 |
cmd.append(target) |
| 2014 |
|
| 2015 |
# Create a socket server to communicate with the target process |
| 2016 |
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 2017 |
server_socket.bind(('localhost', port)) |
| 2018 |
server_socket.settimeout(SHORT_TIMEOUT) |
| 2019 |
server_socket.listen(1) |
| 2020 |
|
| 2021 |
with subprocess.Popen(cmd, |
| 2022 |
stdout=subprocess.PIPE, |
| 2023 |
stderr=subprocess.PIPE, |
| 2024 |
env=env, |
| 2025 |
) as proc: |
| 2026 |
client_socket = None |
| 2027 |
try: |
| 2028 |
# Accept connection from target process |
| 2029 |
client_socket, _ = server_socket.accept() |
| 2030 |
server_socket.close() |
| 2031 |
|
| 2032 |
response = client_socket.recv(1024) |
| 2033 |
self.assertEqual(response, b"ready") |
| 2034 |
|
| 2035 |
# Try remote exec on the target process |
| 2036 |
sys.remote_exec(proc.pid, script_path) |
| 2037 |
|
| 2038 |
# Signal script to continue |
| 2039 |
client_socket.sendall(b"continue") |
| 2040 |
|
| 2041 |
# Wait for execution confirmation |
| 2042 |
response = client_socket.recv(1024) |
| 2043 |
self.assertEqual(response, b"executed") |
| 2044 |
|
| 2045 |
# Return output for test verification |
| 2046 |
stdout, stderr = proc.communicate(timeout=10.0) |
| 2047 |
return proc.returncode, stdout, stderr |
| 2048 |
except PermissionError: |
| 2049 |
self.skipTest("Insufficient permissions to execute code in remote process") |
| 2050 |
finally: |
| 2051 |
if client_socket is not None: |
| 2052 |
client_socket.close() |
| 2053 |
proc.kill() |
| 2054 |
proc.terminate() |
| 2055 |
proc.wait(timeout=SHORT_TIMEOUT) |
| 2056 |
|
| 2057 |
def test_remote_exec(self): |
| 2058 |
"""Test basic remote exec functionality""" |
| 2059 |
script = 'print("Remote script executed successfully!")' |
| 2060 |
returncode, stdout, stderr = self._run_remote_exec_test(script) |
| 2061 |
# self.assertEqual(returncode, 0) |
| 2062 |
self.assertIn(b"Remote script executed successfully!", stdout) |
| 2063 |
self.assertEqual(stderr, b"") |
| 2064 |
|
| 2065 |
def test_remote_exec_bytes(self): |
| 2066 |
script = 'print("Remote script executed successfully!")' |
| 2067 |
script_path = os.fsencode(os_helper.TESTFN) + b'_bytes_remote.py' |
| 2068 |
returncode, stdout, stderr = self._run_remote_exec_test(script, |
| 2069 |
script_path=script_path) |
| 2070 |
self.assertIn(b"Remote script executed successfully!", stdout) |
| 2071 |
self.assertEqual(stderr, b"") |
| 2072 |
|
| 2073 |
@unittest.skipUnless(os_helper.TESTFN_UNDECODABLE, 'requires undecodable path') |
| 2074 |
@unittest.skipIf(sys.platform == 'darwin', |
| 2075 |
'undecodable paths are not supported on macOS') |
| 2076 |
def test_remote_exec_undecodable(self): |
| 2077 |
script = 'print("Remote script executed successfully!")' |
| 2078 |
script_path = os_helper.TESTFN_UNDECODABLE + b'_undecodable_remote.py' |
| 2079 |
for script_path in [script_path, os.fsdecode(script_path)]: |
| 2080 |
returncode, stdout, stderr = self._run_remote_exec_test(script, |
| 2081 |
script_path=script_path) |
| 2082 |
self.assertIn(b"Remote script executed successfully!", stdout) |
| 2083 |
self.assertEqual(stderr, b"") |
| 2084 |
|
| 2085 |
def test_remote_exec_with_self_process(self): |
| 2086 |
"""Test remote exec with the target process being the same as the test process""" |
| 2087 |
|
| 2088 |
code = 'import sys;print("Remote script executed successfully!", file=sys.stderr)' |
| 2089 |
file = os_helper.TESTFN + '_remote_self.py' |
| 2090 |
with open(file, 'w') as f: |
| 2091 |
f.write(code) |
| 2092 |
self.addCleanup(os_helper.unlink, file) |
| 2093 |
with mock.patch('sys.stderr', new_callable=StringIO) as mock_stderr: |
| 2094 |
with mock.patch('sys.stdout', new_callable=StringIO) as mock_stdout: |
| 2095 |
sys.remote_exec(os.getpid(), os.path.abspath(file)) |
| 2096 |
print("Done") |
| 2097 |
self.assertEqual(mock_stderr.getvalue(), "Remote script executed successfully!\n") |
| 2098 |
self.assertEqual(mock_stdout.getvalue(), "Done\n") |
| 2099 |
|
| 2100 |
def test_remote_exec_raises_audit_event(self): |
| 2101 |
"""Test remote exec raises an audit event""" |
| 2102 |
prologue = '''\ |
| 2103 |
import sys |
| 2104 |
def audit_hook(event, arg): |
| 2105 |
print(f"Audit event: {event}, arg: {arg}".encode("ascii", errors="replace")) |
| 2106 |
sys.addaudithook(audit_hook) |
| 2107 |
''' |
| 2108 |
script = ''' |
| 2109 |
print("Remote script executed successfully!") |
| 2110 |
''' |
| 2111 |
returncode, stdout, stderr = self._run_remote_exec_test(script, prologue=prologue) |
| 2112 |
self.assertEqual(returncode, 0) |
| 2113 |
self.assertIn(b"Remote script executed successfully!", stdout) |
| 2114 |
self.assertIn(b"Audit event: cpython.remote_debugger_script, arg: ", stdout) |
| 2115 |
self.assertEqual(stderr, b"") |
| 2116 |
|
| 2117 |
def test_remote_exec_with_exception(self): |
| 2118 |
"""Test remote exec with an exception raised in the target process |
| 2119 |
|
| 2120 |
The exception should be raised in the main thread of the target process |
| 2121 |
but not crash the target process. |
| 2122 |
""" |
| 2123 |
script = ''' |
| 2124 |
raise Exception("Remote script exception") |
| 2125 |
''' |
| 2126 |
returncode, stdout, stderr = self._run_remote_exec_test(script) |
| 2127 |
self.assertEqual(returncode, 0) |
| 2128 |
self.assertIn(b"Remote script exception", stderr) |
| 2129 |
self.assertEqual(stdout.strip(), b"Target process running...") |
| 2130 |
|
| 2131 |
def test_new_namespace_for_each_remote_exec(self): |
| 2132 |
"""Test that each remote_exec call gets its own namespace.""" |
| 2133 |
script = textwrap.dedent( |
| 2134 |
""" |
| 2135 |
assert globals() is not __import__("__main__").__dict__ |
| 2136 |
print("Remote script executed successfully!") |
| 2137 |
""" |
| 2138 |
) |
| 2139 |
returncode, stdout, stderr = self._run_remote_exec_test(script) |
| 2140 |
self.assertEqual(returncode, 0) |
| 2141 |
self.assertEqual(stderr, b"") |
| 2142 |
self.assertIn(b"Remote script executed successfully", stdout) |
| 2143 |
|
| 2144 |
def test_remote_exec_disabled_by_env(self): |
| 2145 |
"""Test remote exec is disabled when PYTHON_DISABLE_REMOTE_DEBUG is set""" |
| 2146 |
env = os.environ.copy() |
| 2147 |
env['PYTHON_DISABLE_REMOTE_DEBUG'] = '1' |
| 2148 |
with self.assertRaisesRegex(RuntimeError, "Remote debugging is not enabled in the remote process"): |
| 2149 |
self._run_remote_exec_test("print('should not run')", env=env) |
| 2150 |
|
| 2151 |
def test_remote_exec_disabled_by_xoption(self): |
| 2152 |
"""Test remote exec is disabled with -Xdisable-remote-debug""" |
| 2153 |
with self.assertRaisesRegex(RuntimeError, "Remote debugging is not enabled in the remote process"): |
| 2154 |
self._run_remote_exec_test("print('should not run')", python_args=['-Xdisable-remote-debug']) |
| 2155 |
|
| 2156 |
def test_remote_exec_invalid_pid(self): |
| 2157 |
"""Test remote exec with invalid process ID""" |
| 2158 |
with self.assertRaises(OSError): |
| 2159 |
sys.remote_exec(99999, "print('should not run')") |
| 2160 |
|
| 2161 |
def test_remote_exec_invalid_script(self): |
| 2162 |
"""Test remote exec with invalid script type""" |
| 2163 |
with self.assertRaises(TypeError): |
| 2164 |
sys.remote_exec(0, None) |
| 2165 |
with self.assertRaises(TypeError): |
| 2166 |
sys.remote_exec(0, 123) |
| 2167 |
|
| 2168 |
def test_remote_exec_syntax_error(self): |
| 2169 |
"""Test remote exec with syntax error in script""" |
| 2170 |
script = ''' |
| 2171 |
this is invalid python code |
| 2172 |
''' |
| 2173 |
returncode, stdout, stderr = self._run_remote_exec_test(script) |
| 2174 |
self.assertEqual(returncode, 0) |
| 2175 |
self.assertIn(b"SyntaxError", stderr) |
| 2176 |
self.assertEqual(stdout.strip(), b"Target process running...") |
| 2177 |
|
| 2178 |
def test_remote_exec_invalid_script_path(self): |
| 2179 |
"""Test remote exec with invalid script path""" |
| 2180 |
with self.assertRaises(OSError): |
| 2181 |
sys.remote_exec(os.getpid(), "invalid_script_path") |
| 2182 |
|
| 2183 |
def test_remote_exec_in_process_without_debug_fails_envvar(self): |
| 2184 |
"""Test remote exec in a process without remote debugging enabled""" |
| 2185 |
script = os_helper.TESTFN + '_remote.py' |
| 2186 |
self.addCleanup(os_helper.unlink, script) |
| 2187 |
with open(script, 'w') as f: |
| 2188 |
f.write('print("Remote script executed successfully!")') |
| 2189 |
env = os.environ.copy() |
| 2190 |
env['PYTHON_DISABLE_REMOTE_DEBUG'] = '1' |
| 2191 |
|
| 2192 |
_, out, err = assert_python_failure('-c', f'import os, sys; sys.remote_exec(os.getpid(), "{script}")', **env) |
| 2193 |
self.assertIn(b"Remote debugging is not enabled", err) |
| 2194 |
self.assertEqual(out, b"") |
| 2195 |
|
| 2196 |
def test_remote_exec_in_process_without_debug_fails_xoption(self): |
| 2197 |
"""Test remote exec in a process without remote debugging enabled""" |
| 2198 |
script = os_helper.TESTFN + '_remote.py' |
| 2199 |
self.addCleanup(os_helper.unlink, script) |
| 2200 |
with open(script, 'w') as f: |
| 2201 |
f.write('print("Remote script executed successfully!")') |
| 2202 |
|
| 2203 |
_, out, err = assert_python_failure('-Xdisable-remote-debug', '-c', f'import os, sys; sys.remote_exec(os.getpid(), "{script}")') |
| 2204 |
self.assertIn(b"Remote debugging is not enabled", err) |
| 2205 |
self.assertEqual(out, b"") |
| 2206 |
|
| 2207 |
class TestSysJIT(unittest.TestCase): |
| 2208 |
|
| 2209 |
def test_jit_is_available(self): |
| 2210 |
available = sys._jit.is_available() |
| 2211 |
script = f"import sys; assert sys._jit.is_available() is {available}" |
| 2212 |
assert_python_ok("-c", script, PYTHON_JIT="0") |
| 2213 |
assert_python_ok("-c", script, PYTHON_JIT="1") |
| 2214 |
|
| 2215 |
def test_jit_is_enabled(self): |
| 2216 |
available = sys._jit.is_available() |
| 2217 |
script = "import sys; assert sys._jit.is_enabled() is {enabled}" |
| 2218 |
assert_python_ok("-c", script.format(enabled=False), PYTHON_JIT="0") |
| 2219 |
assert_python_ok("-c", script.format(enabled=available), PYTHON_JIT="1") |
| 2220 |
|
| 2221 |
def test_jit_is_active(self): |
| 2222 |
available = sys._jit.is_available() |
| 2223 |
script = textwrap.dedent( |
| 2224 |
""" |
| 2225 |
import _testcapi |
| 2226 |
import _testinternalcapi |
| 2227 |
import sys |
| 2228 |
|
| 2229 |
def frame_0_interpreter() -> None: |
| 2230 |
assert sys._jit.is_active() is False |
| 2231 |
|
| 2232 |
def frame_1_interpreter() -> None: |
| 2233 |
assert sys._jit.is_active() is False |
| 2234 |
frame_0_interpreter() |
| 2235 |
assert sys._jit.is_active() is False |
| 2236 |
|
| 2237 |
def frame_2_jit(expected: bool) -> None: |
| 2238 |
# Inlined into the last loop of frame_3_jit: |
| 2239 |
assert sys._jit.is_active() is expected |
| 2240 |
# Insert C frame: |
| 2241 |
_testcapi.pyobject_vectorcall(frame_1_interpreter, None, None) |
| 2242 |
assert sys._jit.is_active() is expected |
| 2243 |
|
| 2244 |
def frame_3_jit() -> None: |
| 2245 |
# JITs just before the last loop: |
| 2246 |
for i in range(_testinternalcapi.TIER2_THRESHOLD + 1): |
| 2247 |
# Careful, doing this in the reverse order breaks tracing: |
| 2248 |
expected = {enabled} and i == _testinternalcapi.TIER2_THRESHOLD |
| 2249 |
assert sys._jit.is_active() is expected |
| 2250 |
frame_2_jit(expected) |
| 2251 |
assert sys._jit.is_active() is expected |
| 2252 |
|
| 2253 |
def frame_4_interpreter() -> None: |
| 2254 |
assert sys._jit.is_active() is False |
| 2255 |
frame_3_jit() |
| 2256 |
assert sys._jit.is_active() is False |
| 2257 |
|
| 2258 |
assert sys._jit.is_active() is False |
| 2259 |
frame_4_interpreter() |
| 2260 |
assert sys._jit.is_active() is False |
| 2261 |
""" |
| 2262 |
) |
| 2263 |
assert_python_ok("-c", script.format(enabled=False), PYTHON_JIT="0") |
| 2264 |
assert_python_ok("-c", script.format(enabled=available), PYTHON_JIT="1") |
| 2265 |
|
| 2266 |
|
| 2267 |
if __name__ == "__main__": |
| 2268 |
unittest.main() |