test_cmd_line.py 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965
  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. import os
  5. import subprocess
  6. import sys
  7. import tempfile
  8. import textwrap
  9. import unittest
  10. from test import support
  11. from test.support import os_helper
  12. from test.support.script_helper import (
  13. spawn_python, kill_python, assert_python_ok, assert_python_failure,
  14. interpreter_requires_environment
  15. )
  16. if not support.has_subprocess_support:
  17. raise unittest.SkipTest("test module requires subprocess")
  18. # Debug build?
  19. Py_DEBUG = hasattr(sys, "gettotalrefcount")
  20. # XXX (ncoghlan): Move to script_helper and make consistent with run_python
  21. def _kill_python_and_exit_code(p):
  22. data = kill_python(p)
  23. returncode = p.wait()
  24. return data, returncode
  25. class CmdLineTest(unittest.TestCase):
  26. def test_directories(self):
  27. assert_python_failure('.')
  28. assert_python_failure('< .')
  29. def verify_valid_flag(self, cmd_line):
  30. rc, out, err = assert_python_ok(cmd_line)
  31. self.assertTrue(out == b'' or out.endswith(b'\n'))
  32. self.assertNotIn(b'Traceback', out)
  33. self.assertNotIn(b'Traceback', err)
  34. return out
  35. def test_help(self):
  36. self.verify_valid_flag('-h')
  37. self.verify_valid_flag('-?')
  38. out = self.verify_valid_flag('--help')
  39. lines = out.splitlines()
  40. self.assertIn(b'usage', lines[0])
  41. self.assertNotIn(b'PYTHONHOME', out)
  42. self.assertNotIn(b'-X dev', out)
  43. self.assertLess(len(lines), 50)
  44. def test_help_env(self):
  45. out = self.verify_valid_flag('--help-env')
  46. self.assertIn(b'PYTHONHOME', out)
  47. def test_help_xoptions(self):
  48. out = self.verify_valid_flag('--help-xoptions')
  49. self.assertIn(b'-X dev', out)
  50. def test_help_all(self):
  51. out = self.verify_valid_flag('--help-all')
  52. lines = out.splitlines()
  53. self.assertIn(b'usage', lines[0])
  54. self.assertIn(b'PYTHONHOME', out)
  55. self.assertIn(b'-X dev', out)
  56. # The first line contains the program name,
  57. # but the rest should be ASCII-only
  58. b''.join(lines[1:]).decode('ascii')
  59. def test_optimize(self):
  60. self.verify_valid_flag('-O')
  61. self.verify_valid_flag('-OO')
  62. def test_site_flag(self):
  63. self.verify_valid_flag('-S')
  64. def test_version(self):
  65. version = ('Python %d.%d' % sys.version_info[:2]).encode("ascii")
  66. for switch in '-V', '--version', '-VV':
  67. rc, out, err = assert_python_ok(switch)
  68. self.assertFalse(err.startswith(version))
  69. self.assertTrue(out.startswith(version))
  70. def test_verbose(self):
  71. # -v causes imports to write to stderr. If the write to
  72. # stderr itself causes an import to happen (for the output
  73. # codec), a recursion loop can occur.
  74. rc, out, err = assert_python_ok('-v')
  75. self.assertNotIn(b'stack overflow', err)
  76. rc, out, err = assert_python_ok('-vv')
  77. self.assertNotIn(b'stack overflow', err)
  78. @unittest.skipIf(interpreter_requires_environment(),
  79. 'Cannot run -E tests when PYTHON env vars are required.')
  80. def test_xoptions(self):
  81. def get_xoptions(*args):
  82. # use subprocess module directly because test.support.script_helper adds
  83. # "-X faulthandler" to the command line
  84. args = (sys.executable, '-E') + args
  85. args += ('-c', 'import sys; print(sys._xoptions)')
  86. out = subprocess.check_output(args)
  87. opts = eval(out.splitlines()[0])
  88. return opts
  89. opts = get_xoptions()
  90. self.assertEqual(opts, {})
  91. opts = get_xoptions('-Xa', '-Xb=c,d=e')
  92. self.assertEqual(opts, {'a': True, 'b': 'c,d=e'})
  93. def test_showrefcount(self):
  94. def run_python(*args):
  95. # this is similar to assert_python_ok but doesn't strip
  96. # the refcount from stderr. It can be replaced once
  97. # assert_python_ok stops doing that.
  98. cmd = [sys.executable]
  99. cmd.extend(args)
  100. PIPE = subprocess.PIPE
  101. p = subprocess.Popen(cmd, stdout=PIPE, stderr=PIPE)
  102. out, err = p.communicate()
  103. p.stdout.close()
  104. p.stderr.close()
  105. rc = p.returncode
  106. self.assertEqual(rc, 0)
  107. return rc, out, err
  108. code = 'import sys; print(sys._xoptions)'
  109. # normally the refcount is hidden
  110. rc, out, err = run_python('-c', code)
  111. self.assertEqual(out.rstrip(), b'{}')
  112. self.assertEqual(err, b'')
  113. # "-X showrefcount" shows the refcount, but only in debug builds
  114. rc, out, err = run_python('-I', '-X', 'showrefcount', '-c', code)
  115. self.assertEqual(out.rstrip(), b"{'showrefcount': True}")
  116. if Py_DEBUG:
  117. # bpo-46417: Tolerate negative reference count which can occur
  118. # because of bugs in C extensions. This test is only about checking
  119. # the showrefcount feature.
  120. self.assertRegex(err, br'^\[-?\d+ refs, \d+ blocks\]')
  121. else:
  122. self.assertEqual(err, b'')
  123. def test_xoption_frozen_modules(self):
  124. tests = {
  125. ('=on', 'FrozenImporter'),
  126. ('=off', 'SourceFileLoader'),
  127. ('=', 'FrozenImporter'),
  128. ('', 'FrozenImporter'),
  129. }
  130. for raw, expected in tests:
  131. cmd = ['-X', f'frozen_modules{raw}',
  132. '-c', 'import os; print(os.__spec__.loader, end="")']
  133. with self.subTest(raw):
  134. res = assert_python_ok(*cmd)
  135. self.assertRegex(res.out.decode('utf-8'), expected)
  136. def test_run_module(self):
  137. # Test expected operation of the '-m' switch
  138. # Switch needs an argument
  139. assert_python_failure('-m')
  140. # Check we get an error for a nonexistent module
  141. assert_python_failure('-m', 'fnord43520xyz')
  142. # Check the runpy module also gives an error for
  143. # a nonexistent module
  144. assert_python_failure('-m', 'runpy', 'fnord43520xyz')
  145. # All good if module is located and run successfully
  146. assert_python_ok('-m', 'timeit', '-n', '1')
  147. def test_run_module_bug1764407(self):
  148. # -m and -i need to play well together
  149. # Runs the timeit module and checks the __main__
  150. # namespace has been populated appropriately
  151. p = spawn_python('-i', '-m', 'timeit', '-n', '1')
  152. p.stdin.write(b'Timer\n')
  153. p.stdin.write(b'exit()\n')
  154. data = kill_python(p)
  155. self.assertTrue(data.find(b'1 loop') != -1)
  156. self.assertTrue(data.find(b'__main__.Timer') != -1)
  157. def test_relativedir_bug46421(self):
  158. # Test `python -m unittest` with a relative directory beginning with ./
  159. # Note: We have to switch to the project's top module's directory, as per
  160. # the python unittest wiki. We will switch back when we are done.
  161. projectlibpath = os.path.dirname(__file__).removesuffix("test")
  162. with os_helper.change_cwd(projectlibpath):
  163. # Testing with and without ./
  164. assert_python_ok('-m', 'unittest', "test/test_longexp.py")
  165. assert_python_ok('-m', 'unittest', "./test/test_longexp.py")
  166. def test_run_code(self):
  167. # Test expected operation of the '-c' switch
  168. # Switch needs an argument
  169. assert_python_failure('-c')
  170. # Check we get an error for an uncaught exception
  171. assert_python_failure('-c', 'raise Exception')
  172. # All good if execution is successful
  173. assert_python_ok('-c', 'pass')
  174. @unittest.skipUnless(os_helper.FS_NONASCII, 'need os_helper.FS_NONASCII')
  175. def test_non_ascii(self):
  176. # Test handling of non-ascii data
  177. command = ("assert(ord(%r) == %s)"
  178. % (os_helper.FS_NONASCII, ord(os_helper.FS_NONASCII)))
  179. assert_python_ok('-c', command)
  180. @unittest.skipUnless(os_helper.FS_NONASCII, 'need os_helper.FS_NONASCII')
  181. def test_coding(self):
  182. # bpo-32381: the -c command ignores the coding cookie
  183. ch = os_helper.FS_NONASCII
  184. cmd = f"# coding: latin1\nprint(ascii('{ch}'))"
  185. res = assert_python_ok('-c', cmd)
  186. self.assertEqual(res.out.rstrip(), ascii(ch).encode('ascii'))
  187. # On Windows, pass bytes to subprocess doesn't test how Python decodes the
  188. # command line, but how subprocess does decode bytes to unicode. Python
  189. # doesn't decode the command line because Windows provides directly the
  190. # arguments as unicode (using wmain() instead of main()).
  191. @unittest.skipIf(sys.platform == 'win32',
  192. 'Windows has a native unicode API')
  193. def test_undecodable_code(self):
  194. undecodable = b"\xff"
  195. env = os.environ.copy()
  196. # Use C locale to get ascii for the locale encoding
  197. env['LC_ALL'] = 'C'
  198. env['PYTHONCOERCECLOCALE'] = '0'
  199. code = (
  200. b'import locale; '
  201. b'print(ascii("' + undecodable + b'"), '
  202. b'locale.getencoding())')
  203. p = subprocess.Popen(
  204. [sys.executable, "-c", code],
  205. stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
  206. env=env)
  207. stdout, stderr = p.communicate()
  208. if p.returncode == 1:
  209. # _Py_char2wchar() decoded b'\xff' as '\udcff' (b'\xff' is not
  210. # decodable from ASCII) and run_command() failed on
  211. # PyUnicode_AsUTF8String(). This is the expected behaviour on
  212. # Linux.
  213. pattern = b"Unable to decode the command from the command line:"
  214. elif p.returncode == 0:
  215. # _Py_char2wchar() decoded b'\xff' as '\xff' even if the locale is
  216. # C and the locale encoding is ASCII. It occurs on FreeBSD, Solaris
  217. # and Mac OS X.
  218. pattern = b"'\\xff' "
  219. # The output is followed by the encoding name, an alias to ASCII.
  220. # Examples: "US-ASCII" or "646" (ISO 646, on Solaris).
  221. else:
  222. raise AssertionError("Unknown exit code: %s, output=%a" % (p.returncode, stdout))
  223. if not stdout.startswith(pattern):
  224. raise AssertionError("%a doesn't start with %a" % (stdout, pattern))
  225. @unittest.skipIf(sys.platform == 'win32',
  226. 'Windows has a native unicode API')
  227. def test_invalid_utf8_arg(self):
  228. # bpo-35883: Py_DecodeLocale() must escape b'\xfd\xbf\xbf\xbb\xba\xba'
  229. # byte sequence with surrogateescape rather than decoding it as the
  230. # U+7fffbeba character which is outside the [U+0000; U+10ffff] range of
  231. # Python Unicode characters.
  232. #
  233. # Test with default config, in the C locale, in the Python UTF-8 Mode.
  234. code = 'import sys, os; s=os.fsencode(sys.argv[1]); print(ascii(s))'
  235. def run_default(arg):
  236. cmd = [sys.executable, '-c', code, arg]
  237. return subprocess.run(cmd, stdout=subprocess.PIPE, text=True)
  238. def run_c_locale(arg):
  239. cmd = [sys.executable, '-c', code, arg]
  240. env = dict(os.environ)
  241. env['LC_ALL'] = 'C'
  242. return subprocess.run(cmd, stdout=subprocess.PIPE,
  243. text=True, env=env)
  244. def run_utf8_mode(arg):
  245. cmd = [sys.executable, '-X', 'utf8', '-c', code, arg]
  246. return subprocess.run(cmd, stdout=subprocess.PIPE, text=True)
  247. valid_utf8 = 'e:\xe9, euro:\u20ac, non-bmp:\U0010ffff'.encode('utf-8')
  248. # invalid UTF-8 byte sequences with a valid UTF-8 sequence
  249. # in the middle.
  250. invalid_utf8 = (
  251. b'\xff' # invalid byte
  252. b'\xc3\xff' # invalid byte sequence
  253. b'\xc3\xa9' # valid utf-8: U+00E9 character
  254. b'\xed\xa0\x80' # lone surrogate character (invalid)
  255. b'\xfd\xbf\xbf\xbb\xba\xba' # character outside [U+0000; U+10ffff]
  256. )
  257. test_args = [valid_utf8, invalid_utf8]
  258. for run_cmd in (run_default, run_c_locale, run_utf8_mode):
  259. with self.subTest(run_cmd=run_cmd):
  260. for arg in test_args:
  261. proc = run_cmd(arg)
  262. self.assertEqual(proc.stdout.rstrip(), ascii(arg))
  263. @unittest.skipUnless((sys.platform == 'darwin' or
  264. support.is_android), 'test specific to Mac OS X and Android')
  265. def test_osx_android_utf8(self):
  266. text = 'e:\xe9, euro:\u20ac, non-bmp:\U0010ffff'.encode('utf-8')
  267. code = "import sys; print(ascii(sys.argv[1]))"
  268. decoded = text.decode('utf-8', 'surrogateescape')
  269. expected = ascii(decoded).encode('ascii') + b'\n'
  270. env = os.environ.copy()
  271. # C locale gives ASCII locale encoding, but Python uses UTF-8
  272. # to parse the command line arguments on Mac OS X and Android.
  273. env['LC_ALL'] = 'C'
  274. p = subprocess.Popen(
  275. (sys.executable, "-c", code, text),
  276. stdout=subprocess.PIPE,
  277. env=env)
  278. stdout, stderr = p.communicate()
  279. self.assertEqual(stdout, expected)
  280. self.assertEqual(p.returncode, 0)
  281. def test_non_interactive_output_buffering(self):
  282. code = textwrap.dedent("""
  283. import sys
  284. out = sys.stdout
  285. print(out.isatty(), out.write_through, out.line_buffering)
  286. err = sys.stderr
  287. print(err.isatty(), err.write_through, err.line_buffering)
  288. """)
  289. args = [sys.executable, '-c', code]
  290. proc = subprocess.run(args, stdout=subprocess.PIPE,
  291. stderr=subprocess.PIPE, text=True, check=True)
  292. self.assertEqual(proc.stdout,
  293. 'False False False\n'
  294. 'False False True\n')
  295. def test_unbuffered_output(self):
  296. # Test expected operation of the '-u' switch
  297. for stream in ('stdout', 'stderr'):
  298. # Binary is unbuffered
  299. code = ("import os, sys; sys.%s.buffer.write(b'x'); os._exit(0)"
  300. % stream)
  301. rc, out, err = assert_python_ok('-u', '-c', code)
  302. data = err if stream == 'stderr' else out
  303. self.assertEqual(data, b'x', "binary %s not unbuffered" % stream)
  304. # Text is unbuffered
  305. code = ("import os, sys; sys.%s.write('x'); os._exit(0)"
  306. % stream)
  307. rc, out, err = assert_python_ok('-u', '-c', code)
  308. data = err if stream == 'stderr' else out
  309. self.assertEqual(data, b'x', "text %s not unbuffered" % stream)
  310. def test_unbuffered_input(self):
  311. # sys.stdin still works with '-u'
  312. code = ("import sys; sys.stdout.write(sys.stdin.read(1))")
  313. p = spawn_python('-u', '-c', code)
  314. p.stdin.write(b'x')
  315. p.stdin.flush()
  316. data, rc = _kill_python_and_exit_code(p)
  317. self.assertEqual(rc, 0)
  318. self.assertTrue(data.startswith(b'x'), data)
  319. def test_large_PYTHONPATH(self):
  320. path1 = "ABCDE" * 100
  321. path2 = "FGHIJ" * 100
  322. path = path1 + os.pathsep + path2
  323. code = """if 1:
  324. import sys
  325. path = ":".join(sys.path)
  326. path = path.encode("ascii", "backslashreplace")
  327. sys.stdout.buffer.write(path)"""
  328. rc, out, err = assert_python_ok('-S', '-c', code,
  329. PYTHONPATH=path)
  330. self.assertIn(path1.encode('ascii'), out)
  331. self.assertIn(path2.encode('ascii'), out)
  332. @unittest.skipIf(sys.flags.safe_path,
  333. 'PYTHONSAFEPATH changes default sys.path')
  334. def test_empty_PYTHONPATH_issue16309(self):
  335. # On Posix, it is documented that setting PATH to the
  336. # empty string is equivalent to not setting PATH at all,
  337. # which is an exception to the rule that in a string like
  338. # "/bin::/usr/bin" the empty string in the middle gets
  339. # interpreted as '.'
  340. code = """if 1:
  341. import sys
  342. path = ":".join(sys.path)
  343. path = path.encode("ascii", "backslashreplace")
  344. sys.stdout.buffer.write(path)"""
  345. rc1, out1, err1 = assert_python_ok('-c', code, PYTHONPATH="")
  346. rc2, out2, err2 = assert_python_ok('-c', code, __isolated=False)
  347. # regarding to Posix specification, outputs should be equal
  348. # for empty and unset PYTHONPATH
  349. self.assertEqual(out1, out2)
  350. def test_displayhook_unencodable(self):
  351. for encoding in ('ascii', 'latin-1', 'utf-8'):
  352. env = os.environ.copy()
  353. env['PYTHONIOENCODING'] = encoding
  354. p = subprocess.Popen(
  355. [sys.executable, '-i'],
  356. stdin=subprocess.PIPE,
  357. stdout=subprocess.PIPE,
  358. stderr=subprocess.STDOUT,
  359. env=env)
  360. # non-ascii, surrogate, non-BMP printable, non-BMP unprintable
  361. text = "a=\xe9 b=\uDC80 c=\U00010000 d=\U0010FFFF"
  362. p.stdin.write(ascii(text).encode('ascii') + b"\n")
  363. p.stdin.write(b'exit()\n')
  364. data = kill_python(p)
  365. escaped = repr(text).encode(encoding, 'backslashreplace')
  366. self.assertIn(escaped, data)
  367. def check_input(self, code, expected):
  368. with tempfile.NamedTemporaryFile("wb+") as stdin:
  369. sep = os.linesep.encode('ASCII')
  370. stdin.write(sep.join((b'abc', b'def')))
  371. stdin.flush()
  372. stdin.seek(0)
  373. with subprocess.Popen(
  374. (sys.executable, "-c", code),
  375. stdin=stdin, stdout=subprocess.PIPE) as proc:
  376. stdout, stderr = proc.communicate()
  377. self.assertEqual(stdout.rstrip(), expected)
  378. def test_stdin_readline(self):
  379. # Issue #11272: check that sys.stdin.readline() replaces '\r\n' by '\n'
  380. # on Windows (sys.stdin is opened in binary mode)
  381. self.check_input(
  382. "import sys; print(repr(sys.stdin.readline()))",
  383. b"'abc\\n'")
  384. def test_builtin_input(self):
  385. # Issue #11272: check that input() strips newlines ('\n' or '\r\n')
  386. self.check_input(
  387. "print(repr(input()))",
  388. b"'abc'")
  389. def test_output_newline(self):
  390. # Issue 13119 Newline for print() should be \r\n on Windows.
  391. code = """if 1:
  392. import sys
  393. print(1)
  394. print(2)
  395. print(3, file=sys.stderr)
  396. print(4, file=sys.stderr)"""
  397. rc, out, err = assert_python_ok('-c', code)
  398. if sys.platform == 'win32':
  399. self.assertEqual(b'1\r\n2\r\n', out)
  400. self.assertEqual(b'3\r\n4\r\n', err)
  401. else:
  402. self.assertEqual(b'1\n2\n', out)
  403. self.assertEqual(b'3\n4\n', err)
  404. def test_unmached_quote(self):
  405. # Issue #10206: python program starting with unmatched quote
  406. # spewed spaces to stdout
  407. rc, out, err = assert_python_failure('-c', "'")
  408. self.assertRegex(err.decode('ascii', 'ignore'), 'SyntaxError')
  409. self.assertEqual(b'', out)
  410. def test_stdout_flush_at_shutdown(self):
  411. # Issue #5319: if stdout.flush() fails at shutdown, an error should
  412. # be printed out.
  413. code = """if 1:
  414. import os, sys, test.support
  415. test.support.SuppressCrashReport().__enter__()
  416. sys.stdout.write('x')
  417. os.close(sys.stdout.fileno())"""
  418. rc, out, err = assert_python_failure('-c', code)
  419. self.assertEqual(b'', out)
  420. self.assertEqual(120, rc)
  421. self.assertRegex(err.decode('ascii', 'ignore'),
  422. 'Exception ignored in.*\nOSError: .*')
  423. def test_closed_stdout(self):
  424. # Issue #13444: if stdout has been explicitly closed, we should
  425. # not attempt to flush it at shutdown.
  426. code = "import sys; sys.stdout.close()"
  427. rc, out, err = assert_python_ok('-c', code)
  428. self.assertEqual(b'', err)
  429. # Issue #7111: Python should work without standard streams
  430. @unittest.skipIf(os.name != 'posix', "test needs POSIX semantics")
  431. @unittest.skipIf(sys.platform == "vxworks",
  432. "test needs preexec support in subprocess.Popen")
  433. def _test_no_stdio(self, streams):
  434. code = """if 1:
  435. import os, sys
  436. for i, s in enumerate({streams}):
  437. if getattr(sys, s) is not None:
  438. os._exit(i + 1)
  439. os._exit(42)""".format(streams=streams)
  440. def preexec():
  441. if 'stdin' in streams:
  442. os.close(0)
  443. if 'stdout' in streams:
  444. os.close(1)
  445. if 'stderr' in streams:
  446. os.close(2)
  447. p = subprocess.Popen(
  448. [sys.executable, "-E", "-c", code],
  449. stdin=subprocess.PIPE,
  450. stdout=subprocess.PIPE,
  451. stderr=subprocess.PIPE,
  452. preexec_fn=preexec)
  453. out, err = p.communicate()
  454. self.assertEqual(err, b'')
  455. self.assertEqual(p.returncode, 42)
  456. def test_no_stdin(self):
  457. self._test_no_stdio(['stdin'])
  458. def test_no_stdout(self):
  459. self._test_no_stdio(['stdout'])
  460. def test_no_stderr(self):
  461. self._test_no_stdio(['stderr'])
  462. def test_no_std_streams(self):
  463. self._test_no_stdio(['stdin', 'stdout', 'stderr'])
  464. def test_hash_randomization(self):
  465. # Verify that -R enables hash randomization:
  466. self.verify_valid_flag('-R')
  467. hashes = []
  468. if os.environ.get('PYTHONHASHSEED', 'random') != 'random':
  469. env = dict(os.environ) # copy
  470. # We need to test that it is enabled by default without
  471. # the environment variable enabling it for us.
  472. del env['PYTHONHASHSEED']
  473. env['__cleanenv'] = '1' # consumed by assert_python_ok()
  474. else:
  475. env = {}
  476. for i in range(3):
  477. code = 'print(hash("spam"))'
  478. rc, out, err = assert_python_ok('-c', code, **env)
  479. self.assertEqual(rc, 0)
  480. hashes.append(out)
  481. hashes = sorted(set(hashes)) # uniq
  482. # Rare chance of failure due to 3 random seeds honestly being equal.
  483. self.assertGreater(len(hashes), 1,
  484. msg='3 runs produced an identical random hash '
  485. ' for "spam": {}'.format(hashes))
  486. # Verify that sys.flags contains hash_randomization
  487. code = 'import sys; print("random is", sys.flags.hash_randomization)'
  488. rc, out, err = assert_python_ok('-c', code, PYTHONHASHSEED='')
  489. self.assertIn(b'random is 1', out)
  490. rc, out, err = assert_python_ok('-c', code, PYTHONHASHSEED='random')
  491. self.assertIn(b'random is 1', out)
  492. rc, out, err = assert_python_ok('-c', code, PYTHONHASHSEED='0')
  493. self.assertIn(b'random is 0', out)
  494. rc, out, err = assert_python_ok('-R', '-c', code, PYTHONHASHSEED='0')
  495. self.assertIn(b'random is 1', out)
  496. def test_del___main__(self):
  497. # Issue #15001: PyRun_SimpleFileExFlags() did crash because it kept a
  498. # borrowed reference to the dict of __main__ module and later modify
  499. # the dict whereas the module was destroyed
  500. filename = os_helper.TESTFN
  501. self.addCleanup(os_helper.unlink, filename)
  502. with open(filename, "w", encoding="utf-8") as script:
  503. print("import sys", file=script)
  504. print("del sys.modules['__main__']", file=script)
  505. assert_python_ok(filename)
  506. def test_unknown_options(self):
  507. rc, out, err = assert_python_failure('-E', '-z')
  508. self.assertIn(b'Unknown option: -z', err)
  509. self.assertEqual(err.splitlines().count(b'Unknown option: -z'), 1)
  510. self.assertEqual(b'', out)
  511. # Add "without='-E'" to prevent _assert_python to append -E
  512. # to env_vars and change the output of stderr
  513. rc, out, err = assert_python_failure('-z', without='-E')
  514. self.assertIn(b'Unknown option: -z', err)
  515. self.assertEqual(err.splitlines().count(b'Unknown option: -z'), 1)
  516. self.assertEqual(b'', out)
  517. rc, out, err = assert_python_failure('-a', '-z', without='-E')
  518. self.assertIn(b'Unknown option: -a', err)
  519. # only the first unknown option is reported
  520. self.assertNotIn(b'Unknown option: -z', err)
  521. self.assertEqual(err.splitlines().count(b'Unknown option: -a'), 1)
  522. self.assertEqual(b'', out)
  523. @unittest.skipIf(interpreter_requires_environment(),
  524. 'Cannot run -I tests when PYTHON env vars are required.')
  525. def test_isolatedmode(self):
  526. self.verify_valid_flag('-I')
  527. self.verify_valid_flag('-IEPs')
  528. rc, out, err = assert_python_ok('-I', '-c',
  529. 'from sys import flags as f; '
  530. 'print(f.no_user_site, f.ignore_environment, f.isolated, f.safe_path)',
  531. # dummyvar to prevent extraneous -E
  532. dummyvar="")
  533. self.assertEqual(out.strip(), b'1 1 1 True')
  534. with os_helper.temp_cwd() as tmpdir:
  535. fake = os.path.join(tmpdir, "uuid.py")
  536. main = os.path.join(tmpdir, "main.py")
  537. with open(fake, "w", encoding="utf-8") as f:
  538. f.write("raise RuntimeError('isolated mode test')\n")
  539. with open(main, "w", encoding="utf-8") as f:
  540. f.write("import uuid\n")
  541. f.write("print('ok')\n")
  542. # Use -E to ignore PYTHONSAFEPATH env var
  543. self.assertRaises(subprocess.CalledProcessError,
  544. subprocess.check_output,
  545. [sys.executable, '-E', main], cwd=tmpdir,
  546. stderr=subprocess.DEVNULL)
  547. out = subprocess.check_output([sys.executable, "-I", main],
  548. cwd=tmpdir)
  549. self.assertEqual(out.strip(), b"ok")
  550. def test_sys_flags_set(self):
  551. # Issue 31845: a startup refactoring broke reading flags from env vars
  552. for value, expected in (("", 0), ("1", 1), ("text", 1), ("2", 2)):
  553. env_vars = dict(
  554. PYTHONDEBUG=value,
  555. PYTHONOPTIMIZE=value,
  556. PYTHONDONTWRITEBYTECODE=value,
  557. PYTHONVERBOSE=value,
  558. )
  559. dont_write_bytecode = int(bool(value))
  560. code = (
  561. "import sys; "
  562. "sys.stderr.write(str(sys.flags)); "
  563. f"""sys.exit(not (
  564. sys.flags.debug == sys.flags.optimize ==
  565. sys.flags.verbose ==
  566. {expected}
  567. and sys.flags.dont_write_bytecode == {dont_write_bytecode}
  568. ))"""
  569. )
  570. with self.subTest(envar_value=value):
  571. assert_python_ok('-c', code, **env_vars)
  572. def test_set_pycache_prefix(self):
  573. # sys.pycache_prefix can be set from either -X pycache_prefix or
  574. # PYTHONPYCACHEPREFIX env var, with the former taking precedence.
  575. NO_VALUE = object() # `-X pycache_prefix` with no `=PATH`
  576. cases = [
  577. # (PYTHONPYCACHEPREFIX, -X pycache_prefix, sys.pycache_prefix)
  578. (None, None, None),
  579. ('foo', None, 'foo'),
  580. (None, 'bar', 'bar'),
  581. ('foo', 'bar', 'bar'),
  582. ('foo', '', None),
  583. ('foo', NO_VALUE, None),
  584. ]
  585. for envval, opt, expected in cases:
  586. exp_clause = "is None" if expected is None else f'== "{expected}"'
  587. code = f"import sys; sys.exit(not sys.pycache_prefix {exp_clause})"
  588. args = ['-c', code]
  589. env = {} if envval is None else {'PYTHONPYCACHEPREFIX': envval}
  590. if opt is NO_VALUE:
  591. args[:0] = ['-X', 'pycache_prefix']
  592. elif opt is not None:
  593. args[:0] = ['-X', f'pycache_prefix={opt}']
  594. with self.subTest(envval=envval, opt=opt):
  595. with os_helper.temp_cwd():
  596. assert_python_ok(*args, **env)
  597. def run_xdev(self, *args, check_exitcode=True, xdev=True):
  598. env = dict(os.environ)
  599. env.pop('PYTHONWARNINGS', None)
  600. env.pop('PYTHONDEVMODE', None)
  601. env.pop('PYTHONMALLOC', None)
  602. if xdev:
  603. args = (sys.executable, '-X', 'dev', *args)
  604. else:
  605. args = (sys.executable, *args)
  606. proc = subprocess.run(args,
  607. stdout=subprocess.PIPE,
  608. stderr=subprocess.STDOUT,
  609. universal_newlines=True,
  610. env=env)
  611. if check_exitcode:
  612. self.assertEqual(proc.returncode, 0, proc)
  613. return proc.stdout.rstrip()
  614. def test_xdev(self):
  615. # sys.flags.dev_mode
  616. code = "import sys; print(sys.flags.dev_mode)"
  617. out = self.run_xdev("-c", code, xdev=False)
  618. self.assertEqual(out, "False")
  619. out = self.run_xdev("-c", code)
  620. self.assertEqual(out, "True")
  621. # Warnings
  622. code = ("import warnings; "
  623. "print(' '.join('%s::%s' % (f[0], f[2].__name__) "
  624. "for f in warnings.filters))")
  625. if Py_DEBUG:
  626. expected_filters = "default::Warning"
  627. else:
  628. expected_filters = ("default::Warning "
  629. "default::DeprecationWarning "
  630. "ignore::DeprecationWarning "
  631. "ignore::PendingDeprecationWarning "
  632. "ignore::ImportWarning "
  633. "ignore::ResourceWarning")
  634. out = self.run_xdev("-c", code)
  635. self.assertEqual(out, expected_filters)
  636. out = self.run_xdev("-b", "-c", code)
  637. self.assertEqual(out, f"default::BytesWarning {expected_filters}")
  638. out = self.run_xdev("-bb", "-c", code)
  639. self.assertEqual(out, f"error::BytesWarning {expected_filters}")
  640. out = self.run_xdev("-Werror", "-c", code)
  641. self.assertEqual(out, f"error::Warning {expected_filters}")
  642. # Memory allocator debug hooks
  643. try:
  644. import _testcapi
  645. except ImportError:
  646. pass
  647. else:
  648. code = "import _testcapi; print(_testcapi.pymem_getallocatorsname())"
  649. with support.SuppressCrashReport():
  650. out = self.run_xdev("-c", code, check_exitcode=False)
  651. if support.with_pymalloc():
  652. alloc_name = "pymalloc_debug"
  653. else:
  654. alloc_name = "malloc_debug"
  655. self.assertEqual(out, alloc_name)
  656. # Faulthandler
  657. try:
  658. import faulthandler
  659. except ImportError:
  660. pass
  661. else:
  662. code = "import faulthandler; print(faulthandler.is_enabled())"
  663. out = self.run_xdev("-c", code)
  664. self.assertEqual(out, "True")
  665. def check_warnings_filters(self, cmdline_option, envvar, use_pywarning=False):
  666. if use_pywarning:
  667. code = ("import sys; from test.support.import_helper import "
  668. "import_fresh_module; "
  669. "warnings = import_fresh_module('warnings', blocked=['_warnings']); ")
  670. else:
  671. code = "import sys, warnings; "
  672. code += ("print(' '.join('%s::%s' % (f[0], f[2].__name__) "
  673. "for f in warnings.filters))")
  674. args = (sys.executable, '-W', cmdline_option, '-bb', '-c', code)
  675. env = dict(os.environ)
  676. env.pop('PYTHONDEVMODE', None)
  677. env["PYTHONWARNINGS"] = envvar
  678. proc = subprocess.run(args,
  679. stdout=subprocess.PIPE,
  680. stderr=subprocess.STDOUT,
  681. universal_newlines=True,
  682. env=env)
  683. self.assertEqual(proc.returncode, 0, proc)
  684. return proc.stdout.rstrip()
  685. def test_warnings_filter_precedence(self):
  686. expected_filters = ("error::BytesWarning "
  687. "once::UserWarning "
  688. "always::UserWarning")
  689. if not Py_DEBUG:
  690. expected_filters += (" "
  691. "default::DeprecationWarning "
  692. "ignore::DeprecationWarning "
  693. "ignore::PendingDeprecationWarning "
  694. "ignore::ImportWarning "
  695. "ignore::ResourceWarning")
  696. out = self.check_warnings_filters("once::UserWarning",
  697. "always::UserWarning")
  698. self.assertEqual(out, expected_filters)
  699. out = self.check_warnings_filters("once::UserWarning",
  700. "always::UserWarning",
  701. use_pywarning=True)
  702. self.assertEqual(out, expected_filters)
  703. def check_pythonmalloc(self, env_var, name):
  704. code = 'import _testcapi; print(_testcapi.pymem_getallocatorsname())'
  705. env = dict(os.environ)
  706. env.pop('PYTHONDEVMODE', None)
  707. if env_var is not None:
  708. env['PYTHONMALLOC'] = env_var
  709. else:
  710. env.pop('PYTHONMALLOC', None)
  711. args = (sys.executable, '-c', code)
  712. proc = subprocess.run(args,
  713. stdout=subprocess.PIPE,
  714. stderr=subprocess.STDOUT,
  715. universal_newlines=True,
  716. env=env)
  717. self.assertEqual(proc.stdout.rstrip(), name)
  718. self.assertEqual(proc.returncode, 0)
  719. def test_pythonmalloc(self):
  720. # Test the PYTHONMALLOC environment variable
  721. pymalloc = support.with_pymalloc()
  722. if pymalloc:
  723. default_name = 'pymalloc_debug' if Py_DEBUG else 'pymalloc'
  724. default_name_debug = 'pymalloc_debug'
  725. else:
  726. default_name = 'malloc_debug' if Py_DEBUG else 'malloc'
  727. default_name_debug = 'malloc_debug'
  728. tests = [
  729. (None, default_name),
  730. ('debug', default_name_debug),
  731. ('malloc', 'malloc'),
  732. ('malloc_debug', 'malloc_debug'),
  733. ]
  734. if pymalloc:
  735. tests.extend((
  736. ('pymalloc', 'pymalloc'),
  737. ('pymalloc_debug', 'pymalloc_debug'),
  738. ))
  739. for env_var, name in tests:
  740. with self.subTest(env_var=env_var, name=name):
  741. self.check_pythonmalloc(env_var, name)
  742. def test_pythondevmode_env(self):
  743. # Test the PYTHONDEVMODE environment variable
  744. code = "import sys; print(sys.flags.dev_mode)"
  745. env = dict(os.environ)
  746. env.pop('PYTHONDEVMODE', None)
  747. args = (sys.executable, '-c', code)
  748. proc = subprocess.run(args, stdout=subprocess.PIPE,
  749. universal_newlines=True, env=env)
  750. self.assertEqual(proc.stdout.rstrip(), 'False')
  751. self.assertEqual(proc.returncode, 0, proc)
  752. env['PYTHONDEVMODE'] = '1'
  753. proc = subprocess.run(args, stdout=subprocess.PIPE,
  754. universal_newlines=True, env=env)
  755. self.assertEqual(proc.stdout.rstrip(), 'True')
  756. self.assertEqual(proc.returncode, 0, proc)
  757. @unittest.skipUnless(sys.platform == 'win32',
  758. 'bpo-32457 only applies on Windows')
  759. def test_argv0_normalization(self):
  760. args = sys.executable, '-c', 'print(0)'
  761. prefix, exe = os.path.split(sys.executable)
  762. executable = prefix + '\\.\\.\\.\\' + exe
  763. proc = subprocess.run(args, stdout=subprocess.PIPE,
  764. executable=executable)
  765. self.assertEqual(proc.returncode, 0, proc)
  766. self.assertEqual(proc.stdout.strip(), b'0')
  767. def test_parsing_error(self):
  768. args = [sys.executable, '-I', '--unknown-option']
  769. proc = subprocess.run(args,
  770. stdout=subprocess.PIPE,
  771. stderr=subprocess.PIPE,
  772. text=True)
  773. err_msg = "unknown option --unknown-option\nusage: "
  774. self.assertTrue(proc.stderr.startswith(err_msg), proc.stderr)
  775. self.assertNotEqual(proc.returncode, 0)
  776. def test_int_max_str_digits(self):
  777. code = "import sys; print(sys.flags.int_max_str_digits, sys.get_int_max_str_digits())"
  778. assert_python_failure('-X', 'int_max_str_digits', '-c', code)
  779. assert_python_failure('-X', 'int_max_str_digits=foo', '-c', code)
  780. assert_python_failure('-X', 'int_max_str_digits=100', '-c', code)
  781. assert_python_failure('-X', 'int_max_str_digits', '-c', code,
  782. PYTHONINTMAXSTRDIGITS='4000')
  783. assert_python_failure('-c', code, PYTHONINTMAXSTRDIGITS='foo')
  784. assert_python_failure('-c', code, PYTHONINTMAXSTRDIGITS='100')
  785. def res2int(res):
  786. out = res.out.strip().decode("utf-8")
  787. return tuple(int(i) for i in out.split())
  788. res = assert_python_ok('-c', code)
  789. self.assertEqual(res2int(res), (-1, sys.get_int_max_str_digits()))
  790. res = assert_python_ok('-X', 'int_max_str_digits=0', '-c', code)
  791. self.assertEqual(res2int(res), (0, 0))
  792. res = assert_python_ok('-X', 'int_max_str_digits=4000', '-c', code)
  793. self.assertEqual(res2int(res), (4000, 4000))
  794. res = assert_python_ok('-X', 'int_max_str_digits=100000', '-c', code)
  795. self.assertEqual(res2int(res), (100000, 100000))
  796. res = assert_python_ok('-c', code, PYTHONINTMAXSTRDIGITS='0')
  797. self.assertEqual(res2int(res), (0, 0))
  798. res = assert_python_ok('-c', code, PYTHONINTMAXSTRDIGITS='4000')
  799. self.assertEqual(res2int(res), (4000, 4000))
  800. res = assert_python_ok(
  801. '-X', 'int_max_str_digits=6000', '-c', code,
  802. PYTHONINTMAXSTRDIGITS='4000'
  803. )
  804. self.assertEqual(res2int(res), (6000, 6000))
  805. @unittest.skipIf(interpreter_requires_environment(),
  806. 'Cannot run -I tests when PYTHON env vars are required.')
  807. class IgnoreEnvironmentTest(unittest.TestCase):
  808. def run_ignoring_vars(self, predicate, **env_vars):
  809. # Runs a subprocess with -E set, even though we're passing
  810. # specific environment variables
  811. # Logical inversion to match predicate check to a zero return
  812. # code indicating success
  813. code = "import sys; sys.stderr.write(str(sys.flags)); sys.exit(not ({}))".format(predicate)
  814. return assert_python_ok('-E', '-c', code, **env_vars)
  815. def test_ignore_PYTHONPATH(self):
  816. path = "should_be_ignored"
  817. self.run_ignoring_vars("'{}' not in sys.path".format(path),
  818. PYTHONPATH=path)
  819. def test_ignore_PYTHONHASHSEED(self):
  820. self.run_ignoring_vars("sys.flags.hash_randomization == 1",
  821. PYTHONHASHSEED="0")
  822. def test_sys_flags_not_set(self):
  823. # Issue 31845: a startup refactoring broke reading flags from env vars
  824. expected_outcome = """
  825. (sys.flags.debug == sys.flags.optimize ==
  826. sys.flags.dont_write_bytecode ==
  827. sys.flags.verbose == sys.flags.safe_path == 0)
  828. """
  829. self.run_ignoring_vars(
  830. expected_outcome,
  831. PYTHONDEBUG="1",
  832. PYTHONOPTIMIZE="1",
  833. PYTHONDONTWRITEBYTECODE="1",
  834. PYTHONVERBOSE="1",
  835. PYTHONSAFEPATH="1",
  836. )
  837. class SyntaxErrorTests(unittest.TestCase):
  838. def check_string(self, code):
  839. proc = subprocess.run([sys.executable, "-"], input=code,
  840. stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  841. self.assertNotEqual(proc.returncode, 0)
  842. self.assertNotEqual(proc.stderr, None)
  843. self.assertIn(b"\nSyntaxError", proc.stderr)
  844. def test_tokenizer_error_with_stdin(self):
  845. self.check_string(b"(1+2+3")
  846. def test_decoding_error_at_the_end_of_the_line(self):
  847. self.check_string(br"'\u1f'")
  848. def tearDownModule():
  849. support.reap_children()
  850. if __name__ == "__main__":
  851. unittest.main()