| 1 |
# Tests invocation of the interpreter with various command line arguments |
| 2 |
# Most tests are executed with environment variables ignored |
| 3 |
# See test_cmd_line_script.py for testing of script execution |
| 4 |
|
| 5 |
import os |
| 6 |
import re |
| 7 |
import subprocess |
| 8 |
import sys |
| 9 |
import sysconfig |
| 10 |
import tempfile |
| 11 |
import textwrap |
| 12 |
import unittest |
| 13 |
from test import support |
| 14 |
from test.support import os_helper |
| 15 |
from test.support import force_not_colorized |
| 16 |
from test.support import threading_helper |
| 17 |
from test.support.script_helper import ( |
| 18 |
spawn_python, kill_python, assert_python_ok, assert_python_failure, |
| 19 |
interpreter_requires_environment |
| 20 |
) |
| 21 |
from textwrap import dedent |
| 22 |
|
| 23 |
|
| 24 |
if not support.has_subprocess_support: |
| 25 |
raise unittest.SkipTest("test module requires subprocess") |
| 26 |
|
| 27 |
|
| 28 |
# XXX (ncoghlan): Move to script_helper and make consistent with run_python |
| 29 |
def _kill_python_and_exit_code(p): |
| 30 |
data = kill_python(p) |
| 31 |
returncode = p.wait() |
| 32 |
return data, returncode |
| 33 |
|
| 34 |
|
| 35 |
class CmdLineTest(unittest.TestCase): |
| 36 |
def test_directories(self): |
| 37 |
assert_python_failure('.') |
| 38 |
assert_python_failure('< .') |
| 39 |
|
| 40 |
def verify_valid_flag(self, cmd_line): |
| 41 |
rc, out, err = assert_python_ok(cmd_line) |
| 42 |
if out != b'': |
| 43 |
self.assertEndsWith(out, b'\n') |
| 44 |
self.assertNotIn(b'Traceback', out) |
| 45 |
self.assertNotIn(b'Traceback', err) |
| 46 |
return out |
| 47 |
|
| 48 |
@support.cpython_only |
| 49 |
def test_help(self): |
| 50 |
self.verify_valid_flag('-h') |
| 51 |
self.verify_valid_flag('-?') |
| 52 |
out = self.verify_valid_flag('--help') |
| 53 |
lines = out.splitlines() |
| 54 |
self.assertIn(b'usage', lines[0]) |
| 55 |
self.assertNotIn(b'PYTHONHOME', out) |
| 56 |
self.assertNotIn(b'-X dev', out) |
| 57 |
self.assertLess(len(lines), 50) |
| 58 |
|
| 59 |
@support.cpython_only |
| 60 |
def test_help_env(self): |
| 61 |
out = self.verify_valid_flag('--help-env') |
| 62 |
self.assertIn(b'PYTHONHOME', out) |
| 63 |
# Env vars in each section should be sorted alphabetically |
| 64 |
# (ignoring underscores so PYTHON_FOO and PYTHONFOO intermix naturally) |
| 65 |
sort_key = lambda name: name.replace(b'_', b'').lower() |
| 66 |
sections = out.split(b'These variables have equivalent') |
| 67 |
for section in sections: |
| 68 |
envvars = re.findall(rb'^(PYTHON\w+)', section, re.MULTILINE) |
| 69 |
self.assertEqual(envvars, sorted(envvars, key=sort_key), |
| 70 |
"env vars should be sorted alphabetically") |
| 71 |
|
| 72 |
@support.cpython_only |
| 73 |
def test_help_xoptions(self): |
| 74 |
out = self.verify_valid_flag('--help-xoptions') |
| 75 |
self.assertIn(b'-X dev', out) |
| 76 |
options = re.findall(rb'^-X (\w+)', out, re.MULTILINE) |
| 77 |
self.assertEqual(options, sorted(options), |
| 78 |
"options should be sorted alphabetically") |
| 79 |
|
| 80 |
@support.cpython_only |
| 81 |
def test_help_all(self): |
| 82 |
out = self.verify_valid_flag('--help-all') |
| 83 |
lines = out.splitlines() |
| 84 |
self.assertIn(b'usage', lines[0]) |
| 85 |
self.assertIn(b'PYTHONHOME', out) |
| 86 |
self.assertIn(b'-X dev', out) |
| 87 |
|
| 88 |
# The first line contains the program name, |
| 89 |
# but the rest should be ASCII-only |
| 90 |
b''.join(lines[1:]).decode('ascii') |
| 91 |
|
| 92 |
def test_optimize(self): |
| 93 |
self.verify_valid_flag('-O') |
| 94 |
self.verify_valid_flag('-OO') |
| 95 |
|
| 96 |
def test_site_flag(self): |
| 97 |
self.verify_valid_flag('-S') |
| 98 |
|
| 99 |
@support.cpython_only |
| 100 |
def test_version(self): |
| 101 |
version = ('Python %d.%d' % sys.version_info[:2]).encode("ascii") |
| 102 |
for switch in '-V', '--version', '-VV': |
| 103 |
rc, out, err = assert_python_ok(switch) |
| 104 |
self.assertNotStartsWith(err, version) |
| 105 |
self.assertStartsWith(out, version) |
| 106 |
|
| 107 |
def test_verbose(self): |
| 108 |
# -v causes imports to write to stderr. If the write to |
| 109 |
# stderr itself causes an import to happen (for the output |
| 110 |
# codec), a recursion loop can occur. |
| 111 |
rc, out, err = assert_python_ok('-v') |
| 112 |
self.assertNotIn(b'stack overflow', err) |
| 113 |
rc, out, err = assert_python_ok('-vv') |
| 114 |
self.assertNotIn(b'stack overflow', err) |
| 115 |
|
| 116 |
@unittest.skipIf(interpreter_requires_environment(), |
| 117 |
'Cannot run -E tests when PYTHON env vars are required.') |
| 118 |
def test_xoptions(self): |
| 119 |
def get_xoptions(*args): |
| 120 |
# use subprocess module directly because test.support.script_helper adds |
| 121 |
# "-X faulthandler" to the command line |
| 122 |
args = (sys.executable, '-E') + args |
| 123 |
args += ('-c', 'import sys; print(sys._xoptions)') |
| 124 |
out = subprocess.check_output(args) |
| 125 |
opts = eval(out.splitlines()[0]) |
| 126 |
return opts |
| 127 |
|
| 128 |
opts = get_xoptions() |
| 129 |
self.assertEqual(opts, {}) |
| 130 |
|
| 131 |
opts = get_xoptions('-Xa', '-Xb=c,d=e') |
| 132 |
self.assertEqual(opts, {'a': True, 'b': 'c,d=e'}) |
| 133 |
|
| 134 |
def test_showrefcount(self): |
| 135 |
def run_python(*args): |
| 136 |
# this is similar to assert_python_ok but doesn't strip |
| 137 |
# the refcount from stderr. It can be replaced once |
| 138 |
# assert_python_ok stops doing that. |
| 139 |
cmd = [sys.executable] |
| 140 |
cmd.extend(args) |
| 141 |
PIPE = subprocess.PIPE |
| 142 |
p = subprocess.Popen(cmd, stdout=PIPE, stderr=PIPE) |
| 143 |
out, err = p.communicate() |
| 144 |
p.stdout.close() |
| 145 |
p.stderr.close() |
| 146 |
rc = p.returncode |
| 147 |
self.assertEqual(rc, 0) |
| 148 |
return rc, out, err |
| 149 |
code = 'import sys; print(sys._xoptions)' |
| 150 |
# normally the refcount is hidden |
| 151 |
rc, out, err = run_python('-c', code) |
| 152 |
self.assertEqual(out.rstrip(), b'{}') |
| 153 |
self.assertEqual(err, b'') |
| 154 |
# "-X showrefcount" shows the refcount, but only in debug builds |
| 155 |
rc, out, err = run_python('-I', '-X', 'showrefcount', '-c', code) |
| 156 |
self.assertEqual(out.rstrip(), b"{'showrefcount': True}") |
| 157 |
if support.Py_DEBUG: |
| 158 |
# bpo-46417: Tolerate negative reference count which can occur |
| 159 |
# because of bugs in C extensions. This test is only about checking |
| 160 |
# the showrefcount feature. |
| 161 |
self.assertRegex(err, br'^\[-?\d+ refs, \d+ blocks\]') |
| 162 |
else: |
| 163 |
self.assertEqual(err, b'') |
| 164 |
|
| 165 |
@support.cpython_only |
| 166 |
def test_xoption_frozen_modules(self): |
| 167 |
tests = { |
| 168 |
('=on', 'FrozenImporter'), |
| 169 |
('=off', 'SourceFileLoader'), |
| 170 |
('=', 'FrozenImporter'), |
| 171 |
('', 'FrozenImporter'), |
| 172 |
} |
| 173 |
for raw, expected in tests: |
| 174 |
cmd = ['-X', f'frozen_modules{raw}', |
| 175 |
'-c', 'import os; print(os.__spec__.loader, end="")'] |
| 176 |
with self.subTest(raw): |
| 177 |
res = assert_python_ok(*cmd) |
| 178 |
self.assertRegex(res.out.decode('utf-8'), expected) |
| 179 |
|
| 180 |
@support.cpython_only |
| 181 |
def test_env_var_frozen_modules(self): |
| 182 |
tests = { |
| 183 |
('on', 'FrozenImporter'), |
| 184 |
('off', 'SourceFileLoader'), |
| 185 |
} |
| 186 |
for raw, expected in tests: |
| 187 |
cmd = ['-c', 'import os; print(os.__spec__.loader, end="")'] |
| 188 |
with self.subTest(raw): |
| 189 |
res = assert_python_ok(*cmd, PYTHON_FROZEN_MODULES=raw) |
| 190 |
self.assertRegex(res.out.decode('utf-8'), expected) |
| 191 |
|
| 192 |
def test_run_module(self): |
| 193 |
# Test expected operation of the '-m' switch |
| 194 |
# Switch needs an argument |
| 195 |
assert_python_failure('-m') |
| 196 |
# Check we get an error for a nonexistent module |
| 197 |
assert_python_failure('-m', 'fnord43520xyz') |
| 198 |
# Check the runpy module also gives an error for |
| 199 |
# a nonexistent module |
| 200 |
assert_python_failure('-m', 'runpy', 'fnord43520xyz') |
| 201 |
# All good if module is located and run successfully |
| 202 |
assert_python_ok('-m', 'timeit', '-n', '1') |
| 203 |
|
| 204 |
def test_run_module_bug1764407(self): |
| 205 |
# -m and -i need to play well together |
| 206 |
# Runs the timeit module and checks the __main__ |
| 207 |
# namespace has been populated appropriately |
| 208 |
p = spawn_python('-i', '-m', 'timeit', '-n', '1') |
| 209 |
p.stdin.write(b'Timer\n') |
| 210 |
p.stdin.write(b'exit()\n') |
| 211 |
data = kill_python(p) |
| 212 |
self.assertTrue(data.find(b'1 loop') != -1) |
| 213 |
self.assertTrue(data.find(b'__main__.Timer') != -1) |
| 214 |
|
| 215 |
@support.cpython_only |
| 216 |
def test_null_byte_in_interactive_mode(self): |
| 217 |
# gh-140594: Fix an out of bounds read when a single NUL character |
| 218 |
# is read from the standard input in interactive mode. |
| 219 |
proc = spawn_python('-i') |
| 220 |
proc.communicate(b'\x00', timeout=support.SHORT_TIMEOUT) |
| 221 |
self.assertEqual(proc.returncode, 0) |
| 222 |
|
| 223 |
def test_relativedir_bug46421(self): |
| 224 |
# Test `python -m unittest` with a relative directory beginning with ./ |
| 225 |
# Note: We have to switch to the project's top module's directory, as per |
| 226 |
# the python unittest wiki. We will switch back when we are done. |
| 227 |
projectlibpath = os.path.dirname(__file__).removesuffix("test") |
| 228 |
with os_helper.change_cwd(projectlibpath): |
| 229 |
# Testing with and without ./ |
| 230 |
assert_python_ok('-m', 'unittest', "test/test_longexp.py") |
| 231 |
assert_python_ok('-m', 'unittest', "./test/test_longexp.py") |
| 232 |
|
| 233 |
def test_run_code(self): |
| 234 |
# Test expected operation of the '-c' switch |
| 235 |
# Switch needs an argument |
| 236 |
assert_python_failure('-c') |
| 237 |
# Check we get an error for an uncaught exception |
| 238 |
assert_python_failure('-c', 'raise Exception') |
| 239 |
# All good if execution is successful |
| 240 |
assert_python_ok('-c', 'pass') |
| 241 |
|
| 242 |
@unittest.skipUnless(os_helper.FS_NONASCII, 'need os_helper.FS_NONASCII') |
| 243 |
def test_non_ascii(self): |
| 244 |
# Test handling of non-ascii data |
| 245 |
command = ("assert(ord(%r) == %s)" |
| 246 |
% (os_helper.FS_NONASCII, ord(os_helper.FS_NONASCII))) |
| 247 |
assert_python_ok('-c', command) |
| 248 |
|
| 249 |
@unittest.skipUnless(os_helper.FS_NONASCII, 'need os_helper.FS_NONASCII') |
| 250 |
def test_coding(self): |
| 251 |
# bpo-32381: the -c command ignores the coding cookie |
| 252 |
ch = os_helper.FS_NONASCII |
| 253 |
cmd = f"# coding: latin1\nprint(ascii('{ch}'))" |
| 254 |
res = assert_python_ok('-c', cmd) |
| 255 |
self.assertEqual(res.out.rstrip(), ascii(ch).encode('ascii')) |
| 256 |
|
| 257 |
# On Windows, pass bytes to subprocess doesn't test how Python decodes the |
| 258 |
# command line, but how subprocess does decode bytes to unicode. Python |
| 259 |
# doesn't decode the command line because Windows provides directly the |
| 260 |
# arguments as unicode (using wmain() instead of main()). |
| 261 |
@unittest.skipIf(sys.platform == 'win32', |
| 262 |
'Windows has a native unicode API') |
| 263 |
def test_undecodable_code(self): |
| 264 |
undecodable = b"\xff" |
| 265 |
env = os.environ.copy() |
| 266 |
# Use C locale to get ascii for the locale encoding |
| 267 |
env['LC_ALL'] = 'C' |
| 268 |
env['PYTHONCOERCECLOCALE'] = '0' |
| 269 |
code = ( |
| 270 |
b'import locale; ' |
| 271 |
b'print(ascii("' + undecodable + b'"), ' |
| 272 |
b'locale.getencoding())') |
| 273 |
p = subprocess.Popen( |
| 274 |
[sys.executable, "-c", code], |
| 275 |
stdout=subprocess.PIPE, stderr=subprocess.STDOUT, |
| 276 |
env=env) |
| 277 |
stdout, stderr = p.communicate() |
| 278 |
if p.returncode == 1: |
| 279 |
# _Py_char2wchar() decoded b'\xff' as '\udcff' (b'\xff' is not |
| 280 |
# decodable from ASCII) and run_command() failed on |
| 281 |
# PyUnicode_AsUTF8String(). This is the expected behaviour on |
| 282 |
# Linux. |
| 283 |
pattern = b"Unable to decode the command from the command line:" |
| 284 |
elif p.returncode == 0: |
| 285 |
# _Py_char2wchar() decoded b'\xff' as '\xff' even if the locale is |
| 286 |
# C and the locale encoding is ASCII. It occurs on FreeBSD, Solaris |
| 287 |
# and Mac OS X. |
| 288 |
pattern = b"'\\xff' " |
| 289 |
# The output is followed by the encoding name, an alias to ASCII. |
| 290 |
# Examples: "US-ASCII" or "646" (ISO 646, on Solaris). |
| 291 |
else: |
| 292 |
raise AssertionError("Unknown exit code: %s, output=%a" % (p.returncode, stdout)) |
| 293 |
if not stdout.startswith(pattern): |
| 294 |
raise AssertionError("%a doesn't start with %a" % (stdout, pattern)) |
| 295 |
|
| 296 |
@unittest.skipIf(sys.platform == 'win32', |
| 297 |
'Windows has a native unicode API') |
| 298 |
def test_invalid_utf8_arg(self): |
| 299 |
# bpo-35883: Py_DecodeLocale() must escape b'\xfd\xbf\xbf\xbb\xba\xba' |
| 300 |
# byte sequence with surrogateescape rather than decoding it as the |
| 301 |
# U+7fffbeba character which is outside the [U+0000; U+10ffff] range of |
| 302 |
# Python Unicode characters. |
| 303 |
# |
| 304 |
# Test with default config, in the C locale, in the Python UTF-8 Mode. |
| 305 |
code = 'import sys, os; s=os.fsencode(sys.argv[1]); print(ascii(s))' |
| 306 |
|
| 307 |
def run_default(arg): |
| 308 |
cmd = [sys.executable, '-c', code, arg] |
| 309 |
return subprocess.run(cmd, stdout=subprocess.PIPE, text=True) |
| 310 |
|
| 311 |
def run_c_locale(arg): |
| 312 |
cmd = [sys.executable, '-c', code, arg] |
| 313 |
env = dict(os.environ) |
| 314 |
env['LC_ALL'] = 'C' |
| 315 |
return subprocess.run(cmd, stdout=subprocess.PIPE, |
| 316 |
text=True, env=env) |
| 317 |
|
| 318 |
def run_utf8_mode(arg): |
| 319 |
cmd = [sys.executable, '-X', 'utf8', '-c', code, arg] |
| 320 |
return subprocess.run(cmd, stdout=subprocess.PIPE, text=True) |
| 321 |
|
| 322 |
valid_utf8 = 'e:\xe9, euro:\u20ac, non-bmp:\U0010ffff'.encode('utf-8') |
| 323 |
# invalid UTF-8 byte sequences with a valid UTF-8 sequence |
| 324 |
# in the middle. |
| 325 |
invalid_utf8 = ( |
| 326 |
b'\xff' # invalid byte |
| 327 |
b'\xc3\xff' # invalid byte sequence |
| 328 |
b'\xc3\xa9' # valid utf-8: U+00E9 character |
| 329 |
b'\xed\xa0\x80' # lone surrogate character (invalid) |
| 330 |
b'\xfd\xbf\xbf\xbb\xba\xba' # character outside [U+0000; U+10ffff] |
| 331 |
) |
| 332 |
test_args = [valid_utf8, invalid_utf8] |
| 333 |
|
| 334 |
for run_cmd in (run_default, run_c_locale, run_utf8_mode): |
| 335 |
with self.subTest(run_cmd=run_cmd): |
| 336 |
for arg in test_args: |
| 337 |
proc = run_cmd(arg) |
| 338 |
self.assertEqual(proc.stdout.rstrip(), ascii(arg)) |
| 339 |
|
| 340 |
@unittest.skipUnless((sys.platform == 'darwin' or |
| 341 |
support.is_android), 'test specific to Mac OS X and Android') |
| 342 |
def test_osx_android_utf8(self): |
| 343 |
text = 'e:\xe9, euro:\u20ac, non-bmp:\U0010ffff'.encode('utf-8') |
| 344 |
code = "import sys; print(ascii(sys.argv[1]))" |
| 345 |
|
| 346 |
decoded = text.decode('utf-8', 'surrogateescape') |
| 347 |
expected = ascii(decoded).encode('ascii') + b'\n' |
| 348 |
|
| 349 |
env = os.environ.copy() |
| 350 |
# C locale gives ASCII locale encoding, but Python uses UTF-8 |
| 351 |
# to parse the command line arguments on Mac OS X and Android. |
| 352 |
env['LC_ALL'] = 'C' |
| 353 |
|
| 354 |
p = subprocess.Popen( |
| 355 |
(sys.executable, "-c", code, text), |
| 356 |
stdout=subprocess.PIPE, |
| 357 |
env=env) |
| 358 |
stdout, stderr = p.communicate() |
| 359 |
self.assertEqual(stdout, expected) |
| 360 |
self.assertEqual(p.returncode, 0) |
| 361 |
|
| 362 |
@unittest.skipIf(os.environ.get("PYTHONUNBUFFERED", "0") != "0", |
| 363 |
"Python stdio buffering is disabled.") |
| 364 |
def test_non_interactive_output_buffering(self): |
| 365 |
code = textwrap.dedent(""" |
| 366 |
import sys |
| 367 |
out = sys.stdout |
| 368 |
print(out.isatty(), out.write_through, out.line_buffering) |
| 369 |
err = sys.stderr |
| 370 |
print(err.isatty(), err.write_through, err.line_buffering) |
| 371 |
""") |
| 372 |
args = [sys.executable, '-c', code] |
| 373 |
proc = subprocess.run(args, stdout=subprocess.PIPE, |
| 374 |
stderr=subprocess.PIPE, text=True, check=True) |
| 375 |
self.assertEqual(proc.stdout, |
| 376 |
'False False False\n' |
| 377 |
'False False True\n') |
| 378 |
|
| 379 |
def test_unbuffered_output(self): |
| 380 |
# Test expected operation of the '-u' switch |
| 381 |
for stream in ('stdout', 'stderr'): |
| 382 |
# Binary is unbuffered |
| 383 |
code = ("import os, sys; sys.%s.buffer.write(b'x'); os._exit(0)" |
| 384 |
% stream) |
| 385 |
rc, out, err = assert_python_ok('-u', '-c', code) |
| 386 |
data = err if stream == 'stderr' else out |
| 387 |
self.assertEqual(data, b'x', "binary %s not unbuffered" % stream) |
| 388 |
# Text is unbuffered |
| 389 |
code = ("import os, sys; sys.%s.write('x'); os._exit(0)" |
| 390 |
% stream) |
| 391 |
rc, out, err = assert_python_ok('-u', '-c', code) |
| 392 |
data = err if stream == 'stderr' else out |
| 393 |
self.assertEqual(data, b'x', "text %s not unbuffered" % stream) |
| 394 |
|
| 395 |
def test_unbuffered_input(self): |
| 396 |
# sys.stdin still works with '-u' |
| 397 |
code = ("import sys; sys.stdout.write(sys.stdin.read(1))") |
| 398 |
p = spawn_python('-u', '-c', code) |
| 399 |
p.stdin.write(b'x') |
| 400 |
p.stdin.flush() |
| 401 |
data, rc = _kill_python_and_exit_code(p) |
| 402 |
self.assertEqual(rc, 0) |
| 403 |
self.assertStartsWith(data, b'x') |
| 404 |
|
| 405 |
def test_large_PYTHONPATH(self): |
| 406 |
path1 = "ABCDE" * 100 |
| 407 |
path2 = "FGHIJ" * 100 |
| 408 |
path = path1 + os.pathsep + path2 |
| 409 |
|
| 410 |
code = """if 1: |
| 411 |
import sys |
| 412 |
path = ":".join(sys.path) |
| 413 |
path = path.encode("ascii", "backslashreplace") |
| 414 |
sys.stdout.buffer.write(path)""" |
| 415 |
rc, out, err = assert_python_ok('-S', '-c', code, |
| 416 |
PYTHONPATH=path) |
| 417 |
self.assertIn(path1.encode('ascii'), out) |
| 418 |
self.assertIn(path2.encode('ascii'), out) |
| 419 |
|
| 420 |
@unittest.skipIf(sys.flags.safe_path, |
| 421 |
'PYTHONSAFEPATH changes default sys.path') |
| 422 |
def test_empty_PYTHONPATH_issue16309(self): |
| 423 |
# On Posix, it is documented that setting PATH to the |
| 424 |
# empty string is equivalent to not setting PATH at all, |
| 425 |
# which is an exception to the rule that in a string like |
| 426 |
# "/bin::/usr/bin" the empty string in the middle gets |
| 427 |
# interpreted as '.' |
| 428 |
code = """if 1: |
| 429 |
import sys |
| 430 |
path = ":".join(sys.path) |
| 431 |
path = path.encode("ascii", "backslashreplace") |
| 432 |
sys.stdout.buffer.write(path)""" |
| 433 |
rc1, out1, err1 = assert_python_ok('-c', code, PYTHONPATH="") |
| 434 |
rc2, out2, err2 = assert_python_ok('-c', code, __isolated=False) |
| 435 |
# regarding to Posix specification, outputs should be equal |
| 436 |
# for empty and unset PYTHONPATH |
| 437 |
self.assertEqual(out1, out2) |
| 438 |
|
| 439 |
def test_displayhook_unencodable(self): |
| 440 |
for encoding in ('ascii', 'latin-1', 'utf-8'): |
| 441 |
env = os.environ.copy() |
| 442 |
env['PYTHONIOENCODING'] = encoding |
| 443 |
p = subprocess.Popen( |
| 444 |
[sys.executable, '-i'], |
| 445 |
stdin=subprocess.PIPE, |
| 446 |
stdout=subprocess.PIPE, |
| 447 |
stderr=subprocess.STDOUT, |
| 448 |
env=env) |
| 449 |
# non-ascii, surrogate, non-BMP printable, non-BMP unprintable |
| 450 |
text = "a=\xe9 b=\uDC80 c=\U00010000 d=\U0010FFFF" |
| 451 |
p.stdin.write(ascii(text).encode('ascii') + b"\n") |
| 452 |
p.stdin.write(b'exit()\n') |
| 453 |
data = kill_python(p) |
| 454 |
escaped = repr(text).encode(encoding, 'backslashreplace') |
| 455 |
self.assertIn(escaped, data) |
| 456 |
|
| 457 |
def check_input(self, code, expected): |
| 458 |
with tempfile.NamedTemporaryFile("wb+") as stdin: |
| 459 |
sep = os.linesep.encode('ASCII') |
| 460 |
stdin.write(sep.join((b'abc', b'def'))) |
| 461 |
stdin.flush() |
| 462 |
stdin.seek(0) |
| 463 |
with subprocess.Popen( |
| 464 |
(sys.executable, "-c", code), |
| 465 |
stdin=stdin, stdout=subprocess.PIPE) as proc: |
| 466 |
stdout, stderr = proc.communicate() |
| 467 |
self.assertEqual(stdout.rstrip(), expected) |
| 468 |
|
| 469 |
def test_stdin_readline(self): |
| 470 |
# Issue #11272: check that sys.stdin.readline() replaces '\r\n' by '\n' |
| 471 |
# on Windows (sys.stdin is opened in binary mode) |
| 472 |
self.check_input( |
| 473 |
"import sys; print(repr(sys.stdin.readline()))", |
| 474 |
b"'abc\\n'") |
| 475 |
|
| 476 |
def test_builtin_input(self): |
| 477 |
# Issue #11272: check that input() strips newlines ('\n' or '\r\n') |
| 478 |
self.check_input( |
| 479 |
"print(repr(input()))", |
| 480 |
b"'abc'") |
| 481 |
|
| 482 |
def test_output_newline(self): |
| 483 |
# Issue 13119 Newline for print() should be \r\n on Windows. |
| 484 |
code = """if 1: |
| 485 |
import sys |
| 486 |
print(1) |
| 487 |
print(2) |
| 488 |
print(3, file=sys.stderr) |
| 489 |
print(4, file=sys.stderr)""" |
| 490 |
rc, out, err = assert_python_ok('-c', code) |
| 491 |
|
| 492 |
if sys.platform == 'win32': |
| 493 |
self.assertEqual(b'1\r\n2\r\n', out) |
| 494 |
self.assertEqual(b'3\r\n4\r\n', err) |
| 495 |
else: |
| 496 |
self.assertEqual(b'1\n2\n', out) |
| 497 |
self.assertEqual(b'3\n4\n', err) |
| 498 |
|
| 499 |
def test_unmached_quote(self): |
| 500 |
# Issue #10206: python program starting with unmatched quote |
| 501 |
# spewed spaces to stdout |
| 502 |
rc, out, err = assert_python_failure('-c', "'") |
| 503 |
self.assertRegex(err.decode('ascii', 'ignore'), 'SyntaxError') |
| 504 |
self.assertEqual(b'', out) |
| 505 |
|
| 506 |
def test_stdout_flush_at_shutdown(self): |
| 507 |
# Issue #5319: if stdout.flush() fails at shutdown, an error should |
| 508 |
# be printed out. |
| 509 |
code = """if 1: |
| 510 |
import os, sys, test.support |
| 511 |
test.support.SuppressCrashReport().__enter__() |
| 512 |
sys.stdout.write('x') |
| 513 |
os.close(sys.stdout.fileno())""" |
| 514 |
rc, out, err = assert_python_failure('-c', code) |
| 515 |
self.assertEqual(b'', out) |
| 516 |
self.assertEqual(120, rc) |
| 517 |
self.assertIn(b'Exception ignored while flushing sys.stdout:\n' |
| 518 |
b'OSError: '.replace(b'\n', os.linesep.encode()), |
| 519 |
err) |
| 520 |
|
| 521 |
def test_closed_stdout(self): |
| 522 |
# Issue #13444: if stdout has been explicitly closed, we should |
| 523 |
# not attempt to flush it at shutdown. |
| 524 |
code = "import sys; sys.stdout.close()" |
| 525 |
rc, out, err = assert_python_ok('-c', code) |
| 526 |
self.assertEqual(b'', err) |
| 527 |
|
| 528 |
# Issue #7111: Python should work without standard streams |
| 529 |
|
| 530 |
@unittest.skipIf(os.name != 'posix', "test needs POSIX semantics") |
| 531 |
@unittest.skipIf(sys.platform == "vxworks", |
| 532 |
"test needs preexec support in subprocess.Popen") |
| 533 |
def _test_no_stdio(self, streams): |
| 534 |
code = """if 1: |
| 535 |
import os, sys |
| 536 |
for i, s in enumerate({streams}): |
| 537 |
if getattr(sys, s) is not None: |
| 538 |
os._exit(i + 1) |
| 539 |
os._exit(42)""".format(streams=streams) |
| 540 |
def preexec(): |
| 541 |
if 'stdin' in streams: |
| 542 |
os.close(0) |
| 543 |
if 'stdout' in streams: |
| 544 |
os.close(1) |
| 545 |
if 'stderr' in streams: |
| 546 |
os.close(2) |
| 547 |
p = subprocess.Popen( |
| 548 |
[sys.executable, "-E", "-c", code], |
| 549 |
stdin=subprocess.PIPE, |
| 550 |
stdout=subprocess.PIPE, |
| 551 |
stderr=subprocess.PIPE, |
| 552 |
preexec_fn=preexec) |
| 553 |
out, err = p.communicate() |
| 554 |
self.assertEqual(err, b'') |
| 555 |
self.assertEqual(p.returncode, 42) |
| 556 |
|
| 557 |
def test_no_stdin(self): |
| 558 |
self._test_no_stdio(['stdin']) |
| 559 |
|
| 560 |
def test_no_stdout(self): |
| 561 |
self._test_no_stdio(['stdout']) |
| 562 |
|
| 563 |
def test_no_stderr(self): |
| 564 |
self._test_no_stdio(['stderr']) |
| 565 |
|
| 566 |
def test_no_std_streams(self): |
| 567 |
self._test_no_stdio(['stdin', 'stdout', 'stderr']) |
| 568 |
|
| 569 |
def test_hash_randomization(self): |
| 570 |
# Verify that -R enables hash randomization: |
| 571 |
self.verify_valid_flag('-R') |
| 572 |
hashes = [] |
| 573 |
if os.environ.get('PYTHONHASHSEED', 'random') != 'random': |
| 574 |
env = dict(os.environ) # copy |
| 575 |
# We need to test that it is enabled by default without |
| 576 |
# the environment variable enabling it for us. |
| 577 |
del env['PYTHONHASHSEED'] |
| 578 |
env['__cleanenv'] = '1' # consumed by assert_python_ok() |
| 579 |
else: |
| 580 |
env = {} |
| 581 |
for i in range(3): |
| 582 |
code = 'print(hash("spam"))' |
| 583 |
rc, out, err = assert_python_ok('-c', code, **env) |
| 584 |
self.assertEqual(rc, 0) |
| 585 |
hashes.append(out) |
| 586 |
hashes = sorted(set(hashes)) # uniq |
| 587 |
# Rare chance of failure due to 3 random seeds honestly being equal. |
| 588 |
self.assertGreater(len(hashes), 1, |
| 589 |
msg='3 runs produced an identical random hash ' |
| 590 |
' for "spam": {}'.format(hashes)) |
| 591 |
|
| 592 |
# Verify that sys.flags contains hash_randomization |
| 593 |
code = 'import sys; print("random is", sys.flags.hash_randomization)' |
| 594 |
rc, out, err = assert_python_ok('-c', code, PYTHONHASHSEED='') |
| 595 |
self.assertIn(b'random is 1', out) |
| 596 |
|
| 597 |
rc, out, err = assert_python_ok('-c', code, PYTHONHASHSEED='random') |
| 598 |
self.assertIn(b'random is 1', out) |
| 599 |
|
| 600 |
rc, out, err = assert_python_ok('-c', code, PYTHONHASHSEED='0') |
| 601 |
self.assertIn(b'random is 0', out) |
| 602 |
|
| 603 |
rc, out, err = assert_python_ok('-R', '-c', code, PYTHONHASHSEED='0') |
| 604 |
self.assertIn(b'random is 1', out) |
| 605 |
|
| 606 |
def test_del___main__(self): |
| 607 |
# Issue #15001: PyRun_SimpleFileExFlags() did crash because it kept a |
| 608 |
# borrowed reference to the dict of __main__ module and later modify |
| 609 |
# the dict whereas the module was destroyed |
| 610 |
filename = os_helper.TESTFN |
| 611 |
self.addCleanup(os_helper.unlink, filename) |
| 612 |
with open(filename, "w", encoding="utf-8") as script: |
| 613 |
print("import sys", file=script) |
| 614 |
print("del sys.modules['__main__']", file=script) |
| 615 |
assert_python_ok(filename) |
| 616 |
|
| 617 |
@support.cpython_only |
| 618 |
def test_unknown_options(self): |
| 619 |
rc, out, err = assert_python_failure('-E', '-z') |
| 620 |
self.assertIn(b'Unknown option: -z', err) |
| 621 |
self.assertEqual(err.splitlines().count(b'Unknown option: -z'), 1) |
| 622 |
self.assertEqual(b'', out) |
| 623 |
# Add "without='-E'" to prevent _assert_python to append -E |
| 624 |
# to env_vars and change the output of stderr |
| 625 |
rc, out, err = assert_python_failure('-z', without='-E') |
| 626 |
self.assertIn(b'Unknown option: -z', err) |
| 627 |
self.assertEqual(err.splitlines().count(b'Unknown option: -z'), 1) |
| 628 |
self.assertEqual(b'', out) |
| 629 |
rc, out, err = assert_python_failure('-a', '-z', without='-E') |
| 630 |
self.assertIn(b'Unknown option: -a', err) |
| 631 |
# only the first unknown option is reported |
| 632 |
self.assertNotIn(b'Unknown option: -z', err) |
| 633 |
self.assertEqual(err.splitlines().count(b'Unknown option: -a'), 1) |
| 634 |
self.assertEqual(b'', out) |
| 635 |
|
| 636 |
@unittest.skipIf(interpreter_requires_environment(), |
| 637 |
'Cannot run -I tests when PYTHON env vars are required.') |
| 638 |
def test_isolatedmode(self): |
| 639 |
self.verify_valid_flag('-I') |
| 640 |
self.verify_valid_flag('-IEPs') |
| 641 |
rc, out, err = assert_python_ok('-I', '-c', |
| 642 |
'from sys import flags as f; ' |
| 643 |
'print(f.no_user_site, f.ignore_environment, f.isolated, f.safe_path)', |
| 644 |
# dummyvar to prevent extraneous -E |
| 645 |
dummyvar="") |
| 646 |
self.assertEqual(out.strip(), b'1 1 1 True') |
| 647 |
with os_helper.temp_cwd() as tmpdir: |
| 648 |
fake = os.path.join(tmpdir, "uuid.py") |
| 649 |
main = os.path.join(tmpdir, "main.py") |
| 650 |
with open(fake, "w", encoding="utf-8") as f: |
| 651 |
f.write("raise RuntimeError('isolated mode test')\n") |
| 652 |
with open(main, "w", encoding="utf-8") as f: |
| 653 |
f.write("import uuid\n") |
| 654 |
f.write("print('ok')\n") |
| 655 |
# Use -E to ignore PYTHONSAFEPATH env var |
| 656 |
self.assertRaises(subprocess.CalledProcessError, |
| 657 |
subprocess.check_output, |
| 658 |
[sys.executable, '-E', main], cwd=tmpdir, |
| 659 |
stderr=subprocess.DEVNULL) |
| 660 |
out = subprocess.check_output([sys.executable, "-I", main], |
| 661 |
cwd=tmpdir) |
| 662 |
self.assertEqual(out.strip(), b"ok") |
| 663 |
|
| 664 |
def test_sys_flags_set(self): |
| 665 |
# Issue 31845: a startup refactoring broke reading flags from env vars |
| 666 |
for value, expected in (("", 0), ("1", 1), ("text", 1), ("2", 2)): |
| 667 |
env_vars = dict( |
| 668 |
PYTHONDEBUG=value, |
| 669 |
PYTHONOPTIMIZE=value, |
| 670 |
PYTHONDONTWRITEBYTECODE=value, |
| 671 |
PYTHONVERBOSE=value, |
| 672 |
) |
| 673 |
expected_bool = int(bool(value)) |
| 674 |
code = ( |
| 675 |
"import sys; " |
| 676 |
"sys.stderr.write(str(sys.flags)); " |
| 677 |
f"""sys.exit(not ( |
| 678 |
sys.flags.optimize == sys.flags.verbose == {expected} |
| 679 |
and sys.flags.debug == sys.flags.dont_write_bytecode == {expected_bool} |
| 680 |
))""" |
| 681 |
) |
| 682 |
with self.subTest(envar_value=value): |
| 683 |
assert_python_ok('-c', code, **env_vars) |
| 684 |
|
| 685 |
def test_set_pycache_prefix(self): |
| 686 |
# sys.pycache_prefix can be set from either -X pycache_prefix or |
| 687 |
# PYTHONPYCACHEPREFIX env var, with the former taking precedence. |
| 688 |
NO_VALUE = object() # `-X pycache_prefix` with no `=PATH` |
| 689 |
cases = [ |
| 690 |
# (PYTHONPYCACHEPREFIX, -X pycache_prefix, sys.pycache_prefix) |
| 691 |
(None, None, None), |
| 692 |
('foo', None, 'foo'), |
| 693 |
(None, 'bar', 'bar'), |
| 694 |
('foo', 'bar', 'bar'), |
| 695 |
('foo', '', None), |
| 696 |
('foo', NO_VALUE, None), |
| 697 |
] |
| 698 |
for envval, opt, expected in cases: |
| 699 |
exp_clause = "is None" if expected is None else f'== "{expected}"' |
| 700 |
code = f"import sys; sys.exit(not sys.pycache_prefix {exp_clause})" |
| 701 |
args = ['-c', code] |
| 702 |
env = {} if envval is None else {'PYTHONPYCACHEPREFIX': envval} |
| 703 |
if opt is NO_VALUE: |
| 704 |
args[:0] = ['-X', 'pycache_prefix'] |
| 705 |
elif opt is not None: |
| 706 |
args[:0] = ['-X', f'pycache_prefix={opt}'] |
| 707 |
with self.subTest(envval=envval, opt=opt): |
| 708 |
with os_helper.temp_cwd(): |
| 709 |
assert_python_ok(*args, **env) |
| 710 |
|
| 711 |
def run_xdev(self, *args, check_exitcode=True, xdev=True): |
| 712 |
env = dict(os.environ) |
| 713 |
env.pop('PYTHONWARNINGS', None) |
| 714 |
env.pop('PYTHONDEVMODE', None) |
| 715 |
env.pop('PYTHONMALLOC', None) |
| 716 |
|
| 717 |
if xdev: |
| 718 |
args = (sys.executable, '-X', 'dev', *args) |
| 719 |
else: |
| 720 |
args = (sys.executable, *args) |
| 721 |
proc = subprocess.run(args, |
| 722 |
stdout=subprocess.PIPE, |
| 723 |
stderr=subprocess.STDOUT, |
| 724 |
universal_newlines=True, |
| 725 |
env=env) |
| 726 |
if check_exitcode: |
| 727 |
self.assertEqual(proc.returncode, 0, proc) |
| 728 |
return proc.stdout.rstrip() |
| 729 |
|
| 730 |
@support.cpython_only |
| 731 |
def test_xdev(self): |
| 732 |
# sys.flags.dev_mode |
| 733 |
code = "import sys; print(sys.flags.dev_mode)" |
| 734 |
out = self.run_xdev("-c", code, xdev=False) |
| 735 |
self.assertEqual(out, "False") |
| 736 |
out = self.run_xdev("-c", code) |
| 737 |
self.assertEqual(out, "True") |
| 738 |
|
| 739 |
# Warnings |
| 740 |
code = ("import warnings; " |
| 741 |
"print(' '.join('%s::%s' % (f[0], f[2].__name__) " |
| 742 |
"for f in warnings.filters))") |
| 743 |
if support.Py_DEBUG: |
| 744 |
expected_filters = "default::Warning" |
| 745 |
else: |
| 746 |
expected_filters = ("default::Warning " |
| 747 |
"default::DeprecationWarning " |
| 748 |
"ignore::DeprecationWarning " |
| 749 |
"ignore::PendingDeprecationWarning " |
| 750 |
"ignore::ImportWarning " |
| 751 |
"ignore::ResourceWarning") |
| 752 |
|
| 753 |
out = self.run_xdev("-c", code) |
| 754 |
self.assertEqual(out, expected_filters) |
| 755 |
|
| 756 |
out = self.run_xdev("-b", "-c", code) |
| 757 |
self.assertEqual(out, f"default::BytesWarning {expected_filters}") |
| 758 |
|
| 759 |
out = self.run_xdev("-bb", "-c", code) |
| 760 |
self.assertEqual(out, f"error::BytesWarning {expected_filters}") |
| 761 |
|
| 762 |
out = self.run_xdev("-Werror", "-c", code) |
| 763 |
self.assertEqual(out, f"error::Warning {expected_filters}") |
| 764 |
|
| 765 |
# Memory allocator debug hooks |
| 766 |
try: |
| 767 |
import _testinternalcapi # noqa: F401 |
| 768 |
except ImportError: |
| 769 |
pass |
| 770 |
else: |
| 771 |
code = "import _testinternalcapi; print(_testinternalcapi.pymem_getallocatorsname())" |
| 772 |
with support.SuppressCrashReport(): |
| 773 |
out = self.run_xdev("-c", code, check_exitcode=False) |
| 774 |
if support.with_pymalloc(): |
| 775 |
alloc_name = "pymalloc_debug" |
| 776 |
elif support.Py_GIL_DISABLED: |
| 777 |
alloc_name = "mimalloc_debug" |
| 778 |
else: |
| 779 |
alloc_name = "malloc_debug" |
| 780 |
self.assertEqual(out, alloc_name) |
| 781 |
|
| 782 |
# Faulthandler |
| 783 |
try: |
| 784 |
import faulthandler # noqa: F401 |
| 785 |
except ImportError: |
| 786 |
pass |
| 787 |
else: |
| 788 |
code = "import faulthandler; print(faulthandler.is_enabled())" |
| 789 |
out = self.run_xdev("-c", code) |
| 790 |
self.assertEqual(out, "True") |
| 791 |
|
| 792 |
def check_warnings_filters(self, cmdline_option, envvar, use_pywarning=False): |
| 793 |
if use_pywarning: |
| 794 |
code = ("import sys; from test.support.import_helper import " |
| 795 |
"import_fresh_module; " |
| 796 |
"warnings = import_fresh_module('warnings', blocked=['_warnings']); ") |
| 797 |
else: |
| 798 |
code = "import sys, warnings; " |
| 799 |
code += ("print(' '.join('%s::%s' % (f[0], f[2].__name__) " |
| 800 |
"for f in warnings.filters))") |
| 801 |
args = (sys.executable, '-W', cmdline_option, '-bb', '-c', code) |
| 802 |
env = dict(os.environ) |
| 803 |
env.pop('PYTHONDEVMODE', None) |
| 804 |
env["PYTHONWARNINGS"] = envvar |
| 805 |
proc = subprocess.run(args, |
| 806 |
stdout=subprocess.PIPE, |
| 807 |
stderr=subprocess.STDOUT, |
| 808 |
universal_newlines=True, |
| 809 |
env=env) |
| 810 |
self.assertEqual(proc.returncode, 0, proc) |
| 811 |
return proc.stdout.rstrip() |
| 812 |
|
| 813 |
def test_warnings_filter_precedence(self): |
| 814 |
expected_filters = ("error::BytesWarning " |
| 815 |
"once::UserWarning " |
| 816 |
"always::UserWarning") |
| 817 |
if not support.Py_DEBUG: |
| 818 |
expected_filters += (" " |
| 819 |
"default::DeprecationWarning " |
| 820 |
"ignore::DeprecationWarning " |
| 821 |
"ignore::PendingDeprecationWarning " |
| 822 |
"ignore::ImportWarning " |
| 823 |
"ignore::ResourceWarning") |
| 824 |
|
| 825 |
out = self.check_warnings_filters("once::UserWarning", |
| 826 |
"always::UserWarning") |
| 827 |
self.assertEqual(out, expected_filters) |
| 828 |
|
| 829 |
out = self.check_warnings_filters("once::UserWarning", |
| 830 |
"always::UserWarning", |
| 831 |
use_pywarning=True) |
| 832 |
self.assertEqual(out, expected_filters) |
| 833 |
|
| 834 |
def check_pythonmalloc(self, env_var, name): |
| 835 |
code = 'import _testinternalcapi; print(_testinternalcapi.pymem_getallocatorsname())' |
| 836 |
env = dict(os.environ) |
| 837 |
env.pop('PYTHONDEVMODE', None) |
| 838 |
if env_var is not None: |
| 839 |
env['PYTHONMALLOC'] = env_var |
| 840 |
else: |
| 841 |
env.pop('PYTHONMALLOC', None) |
| 842 |
args = (sys.executable, '-c', code) |
| 843 |
proc = subprocess.run(args, |
| 844 |
stdout=subprocess.PIPE, |
| 845 |
stderr=subprocess.STDOUT, |
| 846 |
universal_newlines=True, |
| 847 |
env=env) |
| 848 |
self.assertEqual(proc.stdout.rstrip(), name) |
| 849 |
self.assertEqual(proc.returncode, 0) |
| 850 |
|
| 851 |
@support.cpython_only |
| 852 |
def test_pythonmalloc(self): |
| 853 |
# Test the PYTHONMALLOC environment variable |
| 854 |
malloc = not support.Py_GIL_DISABLED |
| 855 |
pymalloc = support.with_pymalloc() |
| 856 |
mimalloc = support.with_mimalloc() |
| 857 |
if support.Py_GIL_DISABLED: |
| 858 |
default_name = 'mimalloc_debug' if support.Py_DEBUG else 'mimalloc' |
| 859 |
default_name_debug = 'mimalloc_debug' |
| 860 |
elif pymalloc: |
| 861 |
default_name = 'pymalloc_debug' if support.Py_DEBUG else 'pymalloc' |
| 862 |
default_name_debug = 'pymalloc_debug' |
| 863 |
else: |
| 864 |
default_name = 'malloc_debug' if support.Py_DEBUG else 'malloc' |
| 865 |
default_name_debug = 'malloc_debug' |
| 866 |
|
| 867 |
tests = [ |
| 868 |
(None, default_name), |
| 869 |
('debug', default_name_debug), |
| 870 |
] |
| 871 |
if malloc: |
| 872 |
tests.extend([ |
| 873 |
('malloc', 'malloc'), |
| 874 |
('malloc_debug', 'malloc_debug'), |
| 875 |
]) |
| 876 |
if pymalloc: |
| 877 |
tests.extend(( |
| 878 |
('pymalloc', 'pymalloc'), |
| 879 |
('pymalloc_debug', 'pymalloc_debug'), |
| 880 |
)) |
| 881 |
if mimalloc: |
| 882 |
tests.extend(( |
| 883 |
('mimalloc', 'mimalloc'), |
| 884 |
('mimalloc_debug', 'mimalloc_debug'), |
| 885 |
)) |
| 886 |
|
| 887 |
for env_var, name in tests: |
| 888 |
with self.subTest(env_var=env_var, name=name): |
| 889 |
self.check_pythonmalloc(env_var, name) |
| 890 |
|
| 891 |
def test_pythondevmode_env(self): |
| 892 |
# Test the PYTHONDEVMODE environment variable |
| 893 |
code = "import sys; print(sys.flags.dev_mode)" |
| 894 |
env = dict(os.environ) |
| 895 |
env.pop('PYTHONDEVMODE', None) |
| 896 |
args = (sys.executable, '-c', code) |
| 897 |
|
| 898 |
proc = subprocess.run(args, stdout=subprocess.PIPE, |
| 899 |
universal_newlines=True, env=env) |
| 900 |
self.assertEqual(proc.stdout.rstrip(), 'False') |
| 901 |
self.assertEqual(proc.returncode, 0, proc) |
| 902 |
|
| 903 |
env['PYTHONDEVMODE'] = '1' |
| 904 |
proc = subprocess.run(args, stdout=subprocess.PIPE, |
| 905 |
universal_newlines=True, env=env) |
| 906 |
self.assertEqual(proc.stdout.rstrip(), 'True') |
| 907 |
self.assertEqual(proc.returncode, 0, proc) |
| 908 |
|
| 909 |
def test_python_gil(self): |
| 910 |
cases = [ |
| 911 |
# (env, opt, expected, msg) |
| 912 |
('1', None, '1', "PYTHON_GIL=1"), |
| 913 |
(None, '1', '1', "-X gil=1"), |
| 914 |
] |
| 915 |
|
| 916 |
if support.Py_GIL_DISABLED: |
| 917 |
cases.extend( |
| 918 |
[ |
| 919 |
(None, None, 'None', "no options set"), |
| 920 |
('0', None, '0', "PYTHON_GIL=0"), |
| 921 |
('1', '0', '0', "-X gil=0 overrides PYTHON_GIL=1"), |
| 922 |
(None, '0', '0', "-X gil=0"), |
| 923 |
] |
| 924 |
) |
| 925 |
else: |
| 926 |
cases.extend( |
| 927 |
[ |
| 928 |
(None, None, '1', '-X gil=0 (unsupported by this build)'), |
| 929 |
('1', None, '1', 'PYTHON_GIL=0 (unsupported by this build)'), |
| 930 |
] |
| 931 |
) |
| 932 |
code = "import sys; print(sys.flags.gil)" |
| 933 |
environ = dict(os.environ) |
| 934 |
|
| 935 |
for env, opt, expected, msg in cases: |
| 936 |
with self.subTest(msg, env=env, opt=opt): |
| 937 |
environ.pop('PYTHON_GIL', None) |
| 938 |
if env is not None: |
| 939 |
environ['PYTHON_GIL'] = env |
| 940 |
extra_args = [] |
| 941 |
if opt is not None: |
| 942 |
extra_args = ['-X', f'gil={opt}'] |
| 943 |
|
| 944 |
proc = subprocess.run([sys.executable, *extra_args, '-c', code], |
| 945 |
stdout=subprocess.PIPE, |
| 946 |
stderr=subprocess.PIPE, |
| 947 |
text=True, env=environ) |
| 948 |
self.assertEqual(proc.returncode, 0, proc) |
| 949 |
self.assertEqual(proc.stdout.rstrip(), expected) |
| 950 |
self.assertEqual(proc.stderr, '') |
| 951 |
|
| 952 |
def test_python_asyncio_debug(self): |
| 953 |
code = "import asyncio; print(asyncio.new_event_loop().get_debug())" |
| 954 |
rc, out, err = assert_python_ok('-c', code, PYTHONASYNCIODEBUG='1') |
| 955 |
self.assertIn(b'True', out) |
| 956 |
|
| 957 |
@unittest.skipUnless(sysconfig.get_config_var('Py_TRACE_REFS'), "Requires --with-trace-refs build option") |
| 958 |
def test_python_dump_refs(self): |
| 959 |
code = 'import sys; sys._clear_internal_caches()' |
| 960 |
rc, out, err = assert_python_ok('-c', code, PYTHONDUMPREFS='1') |
| 961 |
self.assertEqual(rc, 0) |
| 962 |
|
| 963 |
@unittest.skipUnless(sysconfig.get_config_var('Py_TRACE_REFS'), "Requires --with-trace-refs build option") |
| 964 |
def test_python_dump_refs_file(self): |
| 965 |
with tempfile.NamedTemporaryFile() as dump_file: |
| 966 |
code = 'import sys; sys._clear_internal_caches()' |
| 967 |
rc, out, err = assert_python_ok('-c', code, PYTHONDUMPREFSFILE=dump_file.name) |
| 968 |
self.assertEqual(rc, 0) |
| 969 |
with open(dump_file.name, 'r') as file: |
| 970 |
contents = file.read() |
| 971 |
self.assertIn('Remaining objects', contents) |
| 972 |
|
| 973 |
@unittest.skipUnless(sys.platform == 'darwin', 'PYTHONEXECUTABLE only works on macOS') |
| 974 |
def test_python_executable(self): |
| 975 |
code = 'import sys; print(sys.executable)' |
| 976 |
expected = "/busr/bbin/bpython" |
| 977 |
rc, out, err = assert_python_ok('-c', code, PYTHONEXECUTABLE=expected) |
| 978 |
self.assertIn(expected.encode(), out) |
| 979 |
|
| 980 |
@unittest.skipUnless(support.MS_WINDOWS, 'Test only applicable on Windows') |
| 981 |
def test_python_legacy_windows_fs_encoding(self): |
| 982 |
code = "import sys; print(sys.getfilesystemencoding())" |
| 983 |
expected = 'mbcs' |
| 984 |
rc, out, err = assert_python_ok('-c', code, PYTHONLEGACYWINDOWSFSENCODING='1') |
| 985 |
self.assertIn(expected.encode(), out) |
| 986 |
|
| 987 |
@unittest.skipUnless(support.MS_WINDOWS, 'Test only applicable on Windows') |
| 988 |
def test_python_legacy_windows_stdio(self): |
| 989 |
# Test that _WindowsConsoleIO is used when PYTHONLEGACYWINDOWSSTDIO |
| 990 |
# is not set. |
| 991 |
# We cannot use PIPE becase it prevents creating new console. |
| 992 |
# So we use exit code. |
| 993 |
code = "import sys; sys.exit(type(sys.stdout.buffer.raw).__name__ != '_WindowsConsoleIO')" |
| 994 |
env = os.environ.copy() |
| 995 |
env["PYTHONLEGACYWINDOWSSTDIO"] = "" |
| 996 |
p = subprocess.run([sys.executable, "-c", code], |
| 997 |
creationflags=subprocess.CREATE_NEW_CONSOLE, |
| 998 |
env=env) |
| 999 |
self.assertEqual(p.returncode, 0) |
| 1000 |
|
| 1001 |
# Then test that FIleIO is used when PYTHONLEGACYWINDOWSSTDIO is set. |
| 1002 |
code = "import sys; sys.exit(type(sys.stdout.buffer.raw).__name__ != 'FileIO')" |
| 1003 |
env["PYTHONLEGACYWINDOWSSTDIO"] = "1" |
| 1004 |
p = subprocess.run([sys.executable, "-c", code], |
| 1005 |
creationflags=subprocess.CREATE_NEW_CONSOLE, |
| 1006 |
env=env) |
| 1007 |
self.assertEqual(p.returncode, 0) |
| 1008 |
|
| 1009 |
@unittest.skipIf("-fsanitize" in sysconfig.get_config_vars().get('PY_CFLAGS', ()), |
| 1010 |
"PYTHONMALLOCSTATS doesn't work with ASAN") |
| 1011 |
def test_python_malloc_stats(self): |
| 1012 |
code = "pass" |
| 1013 |
rc, out, err = assert_python_ok('-c', code, PYTHONMALLOCSTATS='1') |
| 1014 |
self.assertIn(b'Small block threshold', err) |
| 1015 |
|
| 1016 |
def test_python_user_base(self): |
| 1017 |
code = "import site; print(site.USER_BASE)" |
| 1018 |
expected = "/custom/userbase" |
| 1019 |
rc, out, err = assert_python_ok('-c', code, PYTHONUSERBASE=expected) |
| 1020 |
self.assertIn(expected.encode(), out) |
| 1021 |
|
| 1022 |
def test_python_basic_repl(self): |
| 1023 |
# Currently this only tests that the env var is set. See test_pyrepl.test_python_basic_repl. |
| 1024 |
code = "import os; print('PYTHON_BASIC_REPL' in os.environ)" |
| 1025 |
expected = "True" |
| 1026 |
rc, out, err = assert_python_ok('-c', code, PYTHON_BASIC_REPL='1') |
| 1027 |
self.assertIn(expected.encode(), out) |
| 1028 |
|
| 1029 |
@unittest.skipUnless(sysconfig.get_config_var('HAVE_PERF_TRAMPOLINE'), "Requires HAVE_PERF_TRAMPOLINE support") |
| 1030 |
def test_python_perf_jit_support(self): |
| 1031 |
code = "import sys; print(sys.is_stack_trampoline_active())" |
| 1032 |
expected = "True" |
| 1033 |
rc, out, err = assert_python_ok('-c', code, PYTHON_PERF_JIT_SUPPORT='1') |
| 1034 |
self.assertIn(expected.encode(), out) |
| 1035 |
|
| 1036 |
@unittest.skipUnless(sys.platform == 'win32', |
| 1037 |
'bpo-32457 only applies on Windows') |
| 1038 |
def test_argv0_normalization(self): |
| 1039 |
args = sys.executable, '-c', 'print(0)' |
| 1040 |
prefix, exe = os.path.split(sys.executable) |
| 1041 |
executable = prefix + '\\.\\.\\.\\' + exe |
| 1042 |
|
| 1043 |
proc = subprocess.run(args, stdout=subprocess.PIPE, |
| 1044 |
executable=executable) |
| 1045 |
self.assertEqual(proc.returncode, 0, proc) |
| 1046 |
self.assertEqual(proc.stdout.strip(), b'0') |
| 1047 |
|
| 1048 |
@support.cpython_only |
| 1049 |
def test_parsing_error(self): |
| 1050 |
args = [sys.executable, '-I', '--unknown-option'] |
| 1051 |
proc = subprocess.run(args, |
| 1052 |
stdout=subprocess.PIPE, |
| 1053 |
stderr=subprocess.PIPE, |
| 1054 |
text=True) |
| 1055 |
err_msg = "Unknown option: --unknown-option\nusage: " |
| 1056 |
self.assertStartsWith(proc.stderr, err_msg) |
| 1057 |
self.assertNotEqual(proc.returncode, 0) |
| 1058 |
|
| 1059 |
def test_int_max_str_digits(self): |
| 1060 |
code = "import sys; print(sys.flags.int_max_str_digits, sys.get_int_max_str_digits())" |
| 1061 |
|
| 1062 |
assert_python_failure('-X', 'int_max_str_digits', '-c', code) |
| 1063 |
assert_python_failure('-X', 'int_max_str_digits=foo', '-c', code) |
| 1064 |
assert_python_failure('-X', 'int_max_str_digits=100', '-c', code) |
| 1065 |
assert_python_failure('-X', 'int_max_str_digits', '-c', code, |
| 1066 |
PYTHONINTMAXSTRDIGITS='4000') |
| 1067 |
|
| 1068 |
assert_python_failure('-c', code, PYTHONINTMAXSTRDIGITS='foo') |
| 1069 |
assert_python_failure('-c', code, PYTHONINTMAXSTRDIGITS='100') |
| 1070 |
|
| 1071 |
res = assert_python_ok('-c', code) |
| 1072 |
res2int = self.res2int |
| 1073 |
current_max = sys.get_int_max_str_digits() |
| 1074 |
self.assertEqual(res2int(res), (current_max, current_max)) |
| 1075 |
res = assert_python_ok('-X', 'int_max_str_digits=0', '-c', code) |
| 1076 |
self.assertEqual(res2int(res), (0, 0)) |
| 1077 |
res = assert_python_ok('-X', 'int_max_str_digits=4000', '-c', code) |
| 1078 |
self.assertEqual(res2int(res), (4000, 4000)) |
| 1079 |
res = assert_python_ok('-X', 'int_max_str_digits=100000', '-c', code) |
| 1080 |
self.assertEqual(res2int(res), (100000, 100000)) |
| 1081 |
|
| 1082 |
res = assert_python_ok('-c', code, PYTHONINTMAXSTRDIGITS='0') |
| 1083 |
self.assertEqual(res2int(res), (0, 0)) |
| 1084 |
res = assert_python_ok('-c', code, PYTHONINTMAXSTRDIGITS='4000') |
| 1085 |
self.assertEqual(res2int(res), (4000, 4000)) |
| 1086 |
res = assert_python_ok( |
| 1087 |
'-X', 'int_max_str_digits=6000', '-c', code, |
| 1088 |
PYTHONINTMAXSTRDIGITS='4000' |
| 1089 |
) |
| 1090 |
self.assertEqual(res2int(res), (6000, 6000)) |
| 1091 |
|
| 1092 |
def test_cmd_dedent(self): |
| 1093 |
# test that -c auto-dedents its arguments |
| 1094 |
test_cases = [ |
| 1095 |
( |
| 1096 |
""" |
| 1097 |
print('space-auto-dedent') |
| 1098 |
""", |
| 1099 |
"space-auto-dedent", |
| 1100 |
), |
| 1101 |
( |
| 1102 |
dedent( |
| 1103 |
""" |
| 1104 |
^^^print('tab-auto-dedent') |
| 1105 |
""" |
| 1106 |
).replace("^", "\t"), |
| 1107 |
"tab-auto-dedent", |
| 1108 |
), |
| 1109 |
( |
| 1110 |
dedent( |
| 1111 |
""" |
| 1112 |
^^if 1: |
| 1113 |
^^^^print('mixed-auto-dedent-1') |
| 1114 |
^^print('mixed-auto-dedent-2') |
| 1115 |
""" |
| 1116 |
).replace("^", "\t \t"), |
| 1117 |
"mixed-auto-dedent-1\nmixed-auto-dedent-2", |
| 1118 |
), |
| 1119 |
( |
| 1120 |
''' |
| 1121 |
data = """$ |
| 1122 |
|
| 1123 |
this data has an empty newline above and a newline with spaces below $ |
| 1124 |
$ |
| 1125 |
"""$ |
| 1126 |
if 1: $ |
| 1127 |
print(repr(data))$ |
| 1128 |
'''.replace( |
| 1129 |
"$", "" |
| 1130 |
), |
| 1131 |
# Note: entirely blank lines are normalized to \n, even if they |
| 1132 |
# are part of a data string. This is consistent with |
| 1133 |
# textwrap.dedent behavior, but might not be intuitive. |
| 1134 |
"'\\n\\nthis data has an empty newline above and a newline with spaces below \\n\\n'", |
| 1135 |
), |
| 1136 |
( |
| 1137 |
'', |
| 1138 |
'', |
| 1139 |
), |
| 1140 |
( |
| 1141 |
' \t\n\t\n \t\t\t \t\t \t\n\t\t \n\n\n\t\t\t ', |
| 1142 |
'', |
| 1143 |
), |
| 1144 |
] |
| 1145 |
for code, expected in test_cases: |
| 1146 |
# Run the auto-dedent case |
| 1147 |
args1 = sys.executable, '-c', code |
| 1148 |
proc1 = subprocess.run(args1, stdout=subprocess.PIPE) |
| 1149 |
self.assertEqual(proc1.returncode, 0, proc1) |
| 1150 |
output1 = proc1.stdout.strip().decode(encoding='utf-8') |
| 1151 |
|
| 1152 |
# Manually dedent beforehand, check the result is the same. |
| 1153 |
args2 = sys.executable, '-c', dedent(code) |
| 1154 |
proc2 = subprocess.run(args2, stdout=subprocess.PIPE) |
| 1155 |
self.assertEqual(proc2.returncode, 0, proc2) |
| 1156 |
output2 = proc2.stdout.strip().decode(encoding='utf-8') |
| 1157 |
|
| 1158 |
self.assertEqual(output1, output2) |
| 1159 |
self.assertEqual(output1.replace('\r\n', '\n'), expected) |
| 1160 |
|
| 1161 |
def test_cmd_dedent_failcase(self): |
| 1162 |
# Mixing tabs and spaces is not allowed |
| 1163 |
from textwrap import dedent |
| 1164 |
template = dedent( |
| 1165 |
''' |
| 1166 |
-+if 1: |
| 1167 |
+-++ print('will fail') |
| 1168 |
''') |
| 1169 |
code = template.replace('-', ' ').replace('+', '\t') |
| 1170 |
assert_python_failure('-c', code) |
| 1171 |
code = template.replace('-', '\t').replace('+', ' ') |
| 1172 |
assert_python_failure('-c', code) |
| 1173 |
|
| 1174 |
def test_cpu_count(self): |
| 1175 |
code = "import os; print(os.cpu_count(), os.process_cpu_count())" |
| 1176 |
res = assert_python_ok('-X', 'cpu_count=4321', '-c', code) |
| 1177 |
self.assertEqual(self.res2int(res), (4321, 4321)) |
| 1178 |
res = assert_python_ok('-c', code, PYTHON_CPU_COUNT='1234') |
| 1179 |
self.assertEqual(self.res2int(res), (1234, 1234)) |
| 1180 |
|
| 1181 |
def test_cpu_count_default(self): |
| 1182 |
code = "import os; print(os.cpu_count(), os.process_cpu_count())" |
| 1183 |
res = assert_python_ok('-X', 'cpu_count=default', '-c', code) |
| 1184 |
self.assertEqual(self.res2int(res), (os.cpu_count(), os.process_cpu_count())) |
| 1185 |
res = assert_python_ok('-X', 'cpu_count=default', '-c', code, PYTHON_CPU_COUNT='1234') |
| 1186 |
self.assertEqual(self.res2int(res), (os.cpu_count(), os.process_cpu_count())) |
| 1187 |
res = assert_python_ok('-c', code, PYTHON_CPU_COUNT='default') |
| 1188 |
self.assertEqual(self.res2int(res), (os.cpu_count(), os.process_cpu_count())) |
| 1189 |
|
| 1190 |
def test_import_time(self): |
| 1191 |
# os is not imported at startup |
| 1192 |
code = 'import os; import os' |
| 1193 |
|
| 1194 |
for case in 'importtime', 'importtime=1', 'importtime=true': |
| 1195 |
res = assert_python_ok('-X', case, '-c', code) |
| 1196 |
res_err = res.err.decode('utf-8') |
| 1197 |
self.assertRegex(res_err, r'import time: \s*\d+ \| \s*\d+ \| \s*os') |
| 1198 |
self.assertNotRegex(res_err, r'import time: cached\s* \| cached\s* \| os') |
| 1199 |
|
| 1200 |
res = assert_python_ok('-X', 'importtime=2', '-c', code) |
| 1201 |
res_err = res.err.decode('utf-8') |
| 1202 |
self.assertRegex(res_err, r'import time: \s*\d+ \| \s*\d+ \| \s*os') |
| 1203 |
self.assertRegex(res_err, r'import time: cached\s* \| cached\s* \| os') |
| 1204 |
|
| 1205 |
assert_python_failure('-X', 'importtime=-1', '-c', code) |
| 1206 |
assert_python_failure('-X', 'importtime=3', '-c', code) |
| 1207 |
|
| 1208 |
def res2int(self, res): |
| 1209 |
out = res.out.strip().decode("utf-8") |
| 1210 |
return tuple(int(i) for i in out.split()) |
| 1211 |
|
| 1212 |
@unittest.skipUnless(support.Py_GIL_DISABLED, |
| 1213 |
"PYTHON_TLBC and -X tlbc" |
| 1214 |
" only supported in Py_GIL_DISABLED builds") |
| 1215 |
@threading_helper.requires_working_threading() |
| 1216 |
def test_disable_thread_local_bytecode(self): |
| 1217 |
code = """if 1: |
| 1218 |
import threading |
| 1219 |
def test(x, y): |
| 1220 |
return x + y |
| 1221 |
t = threading.Thread(target=test, args=(1,2)) |
| 1222 |
t.start() |
| 1223 |
t.join()""" |
| 1224 |
assert_python_ok("-W", "always", "-X", "tlbc=0", "-c", code) |
| 1225 |
assert_python_ok("-W", "always", "-c", code, PYTHON_TLBC="0") |
| 1226 |
|
| 1227 |
@unittest.skipUnless(support.Py_GIL_DISABLED, |
| 1228 |
"PYTHON_TLBC and -X tlbc" |
| 1229 |
" only supported in Py_GIL_DISABLED builds") |
| 1230 |
@threading_helper.requires_working_threading() |
| 1231 |
def test_enable_thread_local_bytecode(self): |
| 1232 |
code = """if 1: |
| 1233 |
import threading |
| 1234 |
def test(x, y): |
| 1235 |
return x + y |
| 1236 |
t = threading.Thread(target=test, args=(1,2)) |
| 1237 |
t.start() |
| 1238 |
t.join()""" |
| 1239 |
# The functionality of thread-local bytecode is tested more extensively |
| 1240 |
# in test_thread_local_bytecode |
| 1241 |
assert_python_ok("-W", "always", "-X", "tlbc=1", "-c", code) |
| 1242 |
assert_python_ok("-W", "always", "-c", code, PYTHON_TLBC="1") |
| 1243 |
|
| 1244 |
@unittest.skipUnless(support.Py_GIL_DISABLED, |
| 1245 |
"PYTHON_TLBC and -X tlbc" |
| 1246 |
" only supported in Py_GIL_DISABLED builds") |
| 1247 |
def test_invalid_thread_local_bytecode(self): |
| 1248 |
rc, out, err = assert_python_failure("-X", "tlbc") |
| 1249 |
self.assertIn(b"tlbc=n: n is missing or invalid", err) |
| 1250 |
rc, out, err = assert_python_failure("-X", "tlbc=foo") |
| 1251 |
self.assertIn(b"tlbc=n: n is missing or invalid", err) |
| 1252 |
rc, out, err = assert_python_failure("-X", "tlbc=-1") |
| 1253 |
self.assertIn(b"tlbc=n: n is missing or invalid", err) |
| 1254 |
rc, out, err = assert_python_failure("-X", "tlbc=2") |
| 1255 |
self.assertIn(b"tlbc=n: n is missing or invalid", err) |
| 1256 |
rc, out, err = assert_python_failure(PYTHON_TLBC="foo") |
| 1257 |
self.assertIn(b"PYTHON_TLBC=N: N is missing or invalid", err) |
| 1258 |
rc, out, err = assert_python_failure(PYTHON_TLBC="-1") |
| 1259 |
self.assertIn(b"PYTHON_TLBC=N: N is missing or invalid", err) |
| 1260 |
rc, out, err = assert_python_failure(PYTHON_TLBC="2") |
| 1261 |
self.assertIn(b"PYTHON_TLBC=N: N is missing or invalid", err) |
| 1262 |
|
| 1263 |
|
| 1264 |
@unittest.skipIf(interpreter_requires_environment(), |
| 1265 |
'Cannot run -I tests when PYTHON env vars are required.') |
| 1266 |
class IgnoreEnvironmentTest(unittest.TestCase): |
| 1267 |
|
| 1268 |
def run_ignoring_vars(self, predicate, **env_vars): |
| 1269 |
# Runs a subprocess with -E set, even though we're passing |
| 1270 |
# specific environment variables |
| 1271 |
# Logical inversion to match predicate check to a zero return |
| 1272 |
# code indicating success |
| 1273 |
code = "import sys; sys.stderr.write(str(sys.flags)); sys.exit(not ({}))".format(predicate) |
| 1274 |
return assert_python_ok('-E', '-c', code, **env_vars) |
| 1275 |
|
| 1276 |
def test_ignore_PYTHONPATH(self): |
| 1277 |
path = "should_be_ignored" |
| 1278 |
self.run_ignoring_vars("'{}' not in sys.path".format(path), |
| 1279 |
PYTHONPATH=path) |
| 1280 |
|
| 1281 |
def test_ignore_PYTHONHASHSEED(self): |
| 1282 |
self.run_ignoring_vars("sys.flags.hash_randomization == 1", |
| 1283 |
PYTHONHASHSEED="0") |
| 1284 |
|
| 1285 |
def test_sys_flags_not_set(self): |
| 1286 |
# Issue 31845: a startup refactoring broke reading flags from env vars |
| 1287 |
expected_outcome = """ |
| 1288 |
(sys.flags.debug == sys.flags.optimize == |
| 1289 |
sys.flags.dont_write_bytecode == |
| 1290 |
sys.flags.verbose == sys.flags.safe_path == 0) |
| 1291 |
""" |
| 1292 |
self.run_ignoring_vars( |
| 1293 |
expected_outcome, |
| 1294 |
PYTHONDEBUG="1", |
| 1295 |
PYTHONOPTIMIZE="1", |
| 1296 |
PYTHONDONTWRITEBYTECODE="1", |
| 1297 |
PYTHONVERBOSE="1", |
| 1298 |
PYTHONSAFEPATH="1", |
| 1299 |
) |
| 1300 |
|
| 1301 |
|
| 1302 |
class SyntaxErrorTests(unittest.TestCase): |
| 1303 |
@force_not_colorized |
| 1304 |
def check_string(self, code): |
| 1305 |
proc = subprocess.run([sys.executable, "-"], input=code, |
| 1306 |
stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 1307 |
self.assertNotEqual(proc.returncode, 0) |
| 1308 |
self.assertNotEqual(proc.stderr, None) |
| 1309 |
self.assertIn(b"\nSyntaxError", proc.stderr) |
| 1310 |
|
| 1311 |
def test_tokenizer_error_with_stdin(self): |
| 1312 |
self.check_string(b"(1+2+3") |
| 1313 |
|
| 1314 |
def test_decoding_error_at_the_end_of_the_line(self): |
| 1315 |
self.check_string(br"'\u1f'") |
| 1316 |
|
| 1317 |
|
| 1318 |
def tearDownModule(): |
| 1319 |
support.reap_children() |
| 1320 |
|
| 1321 |
|
| 1322 |
if __name__ == "__main__": |
| 1323 |
unittest.main() |