Debian Codegraph @

Lib/test/test_cmd_line_script.py

raw
1 # tests command line execution of scripts
2
3 import contextlib
4 import importlib
5 import importlib.machinery
6 import zipimport
7 import unittest
8 import sys
9 import os
10 import os.path
11 import py_compile
12 import subprocess
13 import io
14
15 import textwrap
16 from test import support
17 from test.support import import_helper, is_apple, os_helper
18 from test.support.script_helper import (
19 make_pkg, make_script, make_zip_pkg, make_zip_script,
20 assert_python_ok, assert_python_failure, spawn_python, kill_python)
21
22 verbose = support.verbose
23
24 example_args = ['test1', 'test2', 'test3']
25
26 test_source = """\
27 # Script may be run with optimisation enabled, so don't rely on assert
28 # statements being executed
29 def assertEqual(lhs, rhs):
30 if lhs != rhs:
31 raise AssertionError('%r != %r' % (lhs, rhs))
32 def assertIdentical(lhs, rhs):
33 if lhs is not rhs:
34 raise AssertionError('%r is not %r' % (lhs, rhs))
35 # Check basic code execution
36 result = ['Top level assignment']
37 def f():
38 result.append('Lower level reference')
39 f()
40 assertEqual(result, ['Top level assignment', 'Lower level reference'])
41 # Check population of magic variables
42 assertEqual(__name__, '__main__')
43 from importlib.machinery import BuiltinImporter
44 _loader = __loader__ if __loader__ is BuiltinImporter else type(__loader__)
45 print('__loader__==%a' % _loader)
46 print('__file__==%a' % __file__)
47 print('__cached__==%a' % __cached__)
48 print('__package__==%r' % __package__)
49 # Check PEP 451 details
50 import os.path
51 if __package__ is not None:
52 print('__main__ was located through the import system')
53 assertIdentical(__spec__.loader, __loader__)
54 expected_spec_name = os.path.splitext(os.path.basename(__file__))[0]
55 if __package__:
56 expected_spec_name = __package__ + "." + expected_spec_name
57 assertEqual(__spec__.name, expected_spec_name)
58 assertEqual(__spec__.parent, __package__)
59 assertIdentical(__spec__.submodule_search_locations, None)
60 assertEqual(__spec__.origin, __file__)
61 if __spec__.cached is not None:
62 assertEqual(__spec__.cached, __cached__)
63 # Check the sys module
64 import sys
65 assertIdentical(globals(), sys.modules[__name__].__dict__)
66 if __spec__ is not None:
67 # XXX: We're not currently making __main__ available under its real name
68 pass # assertIdentical(globals(), sys.modules[__spec__.name].__dict__)
69 from test import test_cmd_line_script
70 example_args_list = test_cmd_line_script.example_args
71 assertEqual(sys.argv[1:], example_args_list)
72 print('sys.argv[0]==%a' % sys.argv[0])
73 print('sys.path[0]==%a' % sys.path[0])
74 # Check the working directory
75 import os
76 print('cwd==%a' % os.getcwd())
77 """
78
79 def _make_test_script(script_dir, script_basename, source=test_source):
80 to_return = make_script(script_dir, script_basename, source)
81 importlib.invalidate_caches()
82 return to_return
83
84 def _make_test_zip_pkg(zip_dir, zip_basename, pkg_name, script_basename,
85 source=test_source, depth=1):
86 to_return = make_zip_pkg(zip_dir, zip_basename, pkg_name, script_basename,
87 source, depth)
88 importlib.invalidate_caches()
89 return to_return
90
91
92 @support.force_not_colorized_test_class
93 class CmdLineTest(unittest.TestCase):
94 def _check_output(self, script_name, exit_code, data,
95 expected_file, expected_argv0,
96 expected_path0, expected_package,
97 expected_loader, expected_cwd=None):
98 if verbose > 1:
99 print("Output from test script %r:" % script_name)
100 print(repr(data))
101 self.assertEqual(exit_code, 0)
102 printed_loader = '__loader__==%a' % expected_loader
103 printed_file = '__file__==%a' % expected_file
104 printed_package = '__package__==%r' % expected_package
105 printed_argv0 = 'sys.argv[0]==%a' % expected_argv0
106 printed_path0 = 'sys.path[0]==%a' % expected_path0
107 if expected_cwd is None:
108 expected_cwd = os.getcwd()
109 printed_cwd = 'cwd==%a' % expected_cwd
110 if verbose > 1:
111 print('Expected output:')
112 print(printed_file)
113 print(printed_package)
114 print(printed_argv0)
115 print(printed_cwd)
116 self.assertIn(printed_loader.encode('utf-8'), data)
117 self.assertIn(printed_file.encode('utf-8'), data)
118 self.assertIn(printed_package.encode('utf-8'), data)
119 self.assertIn(printed_argv0.encode('utf-8'), data)
120 # PYTHONSAFEPATH=1 changes the default sys.path[0]
121 if not sys.flags.safe_path:
122 self.assertIn(printed_path0.encode('utf-8'), data)
123 self.assertIn(printed_cwd.encode('utf-8'), data)
124
125 def _check_script(self, script_exec_args, expected_file,
126 expected_argv0, expected_path0,
127 expected_package, expected_loader,
128 *cmd_line_switches, cwd=None, **env_vars):
129 if isinstance(script_exec_args, str):
130 script_exec_args = [script_exec_args]
131 run_args = [*support.optim_args_from_interpreter_flags(),
132 *cmd_line_switches, *script_exec_args, *example_args]
133 rc, out, err = assert_python_ok(
134 *run_args, __isolated=False, __cwd=cwd, **env_vars
135 )
136 self._check_output(script_exec_args, rc, out + err, expected_file,
137 expected_argv0, expected_path0,
138 expected_package, expected_loader, cwd)
139
140 def _check_import_error(self, script_exec_args, expected_msg,
141 *cmd_line_switches, cwd=None, **env_vars):
142 if isinstance(script_exec_args, str):
143 script_exec_args = (script_exec_args,)
144 else:
145 script_exec_args = tuple(script_exec_args)
146 run_args = cmd_line_switches + script_exec_args
147 rc, out, err = assert_python_failure(
148 *run_args, __isolated=False, __cwd=cwd, **env_vars
149 )
150 if verbose > 1:
151 print(f'Output from test script {script_exec_args!r:}')
152 print(repr(err))
153 print('Expected output: %r' % expected_msg)
154 self.assertIn(expected_msg.encode('utf-8'), err)
155
156 def test_dash_c_loader(self):
157 rc, out, err = assert_python_ok("-c", "print(__loader__)")
158 expected = repr(importlib.machinery.BuiltinImporter).encode("utf-8")
159 self.assertIn(expected, out)
160
161 def test_stdin_loader(self):
162 # Unfortunately, there's no way to automatically test the fully
163 # interactive REPL, since that code path only gets executed when
164 # stdin is an interactive tty.
165 p = spawn_python()
166 try:
167 p.stdin.write(b"print(__loader__)\n")
168 p.stdin.flush()
169 finally:
170 out = kill_python(p)
171 expected = repr(importlib.machinery.BuiltinImporter).encode("utf-8")
172 self.assertIn(expected, out)
173
174 @contextlib.contextmanager
175 def interactive_python(self, separate_stderr=False):
176 if separate_stderr:
177 p = spawn_python('-i', stderr=subprocess.PIPE)
178 stderr = p.stderr
179 else:
180 p = spawn_python('-i', stderr=subprocess.STDOUT)
181 stderr = p.stdout
182 try:
183 # Drain stderr until prompt
184 while True:
185 data = stderr.read(4)
186 if data == b">>> ":
187 break
188 stderr.readline()
189 yield p
190 finally:
191 kill_python(p)
192 stderr.close()
193
194 def check_repl_stdout_flush(self, separate_stderr=False):
195 with self.interactive_python(separate_stderr) as p:
196 p.stdin.write(b"print('foo')\n")
197 p.stdin.flush()
198 self.assertEqual(b'foo', p.stdout.readline().strip())
199
200 def check_repl_stderr_flush(self, separate_stderr=False):
201 with self.interactive_python(separate_stderr) as p:
202 p.stdin.write(b"1/0\n")
203 p.stdin.flush()
204 stderr = p.stderr if separate_stderr else p.stdout
205 self.assertIn(b'Traceback ', stderr.readline())
206 self.assertIn(b'File "<stdin>"', stderr.readline())
207 self.assertIn(b'1/0', stderr.readline())
208 self.assertIn(b' ~^~', stderr.readline())
209 self.assertIn(b'ZeroDivisionError', stderr.readline())
210
211 def test_repl_stdout_flush(self):
212 self.check_repl_stdout_flush()
213
214 def test_repl_stdout_flush_separate_stderr(self):
215 self.check_repl_stdout_flush(True)
216
217 def test_repl_stderr_flush(self):
218 self.check_repl_stderr_flush()
219
220 def test_repl_stderr_flush_separate_stderr(self):
221 self.check_repl_stderr_flush(True)
222
223 def test_basic_script(self):
224 with os_helper.temp_dir() as script_dir:
225 script_name = _make_test_script(script_dir, 'script')
226 self._check_script(script_name, script_name, script_name,
227 script_dir, None,
228 importlib.machinery.SourceFileLoader,
229 expected_cwd=script_dir)
230
231 def test_script_abspath(self):
232 # pass the script using the relative path, expect the absolute path
233 # in __file__
234 with os_helper.temp_cwd() as script_dir:
235 self.assertTrue(os.path.isabs(script_dir), script_dir)
236
237 script_name = _make_test_script(script_dir, 'script')
238 relative_name = os.path.basename(script_name)
239 self._check_script(relative_name, script_name, relative_name,
240 script_dir, None,
241 importlib.machinery.SourceFileLoader)
242
243 def test_script_compiled(self):
244 with os_helper.temp_dir() as script_dir:
245 script_name = _make_test_script(script_dir, 'script')
246 py_compile.compile(script_name, doraise=True)
247 os.remove(script_name)
248 pyc_file = import_helper.make_legacy_pyc(script_name)
249 self._check_script(pyc_file, pyc_file,
250 pyc_file, script_dir, None,
251 importlib.machinery.SourcelessFileLoader)
252
253 def test_directory(self):
254 with os_helper.temp_dir() as script_dir:
255 script_name = _make_test_script(script_dir, '__main__')
256 self._check_script(script_dir, script_name, script_dir,
257 script_dir, '',
258 importlib.machinery.SourceFileLoader)
259
260 def test_directory_compiled(self):
261 with os_helper.temp_dir() as script_dir:
262 script_name = _make_test_script(script_dir, '__main__')
263 py_compile.compile(script_name, doraise=True)
264 os.remove(script_name)
265 pyc_file = import_helper.make_legacy_pyc(script_name)
266 self._check_script(script_dir, pyc_file, script_dir,
267 script_dir, '',
268 importlib.machinery.SourcelessFileLoader)
269
270 def test_directory_error(self):
271 with os_helper.temp_dir() as script_dir:
272 msg = "can't find '__main__' module in %r" % script_dir
273 self._check_import_error(script_dir, msg)
274
275 def test_zipfile(self):
276 with os_helper.temp_dir() as script_dir:
277 script_name = _make_test_script(script_dir, '__main__')
278 zip_name, run_name = make_zip_script(script_dir, 'test_zip', script_name)
279 self._check_script(zip_name, run_name, zip_name, zip_name, '',
280 zipimport.zipimporter)
281
282 def test_zipfile_compiled_timestamp(self):
283 with os_helper.temp_dir() as script_dir:
284 script_name = _make_test_script(script_dir, '__main__')
285 compiled_name = py_compile.compile(
286 script_name, doraise=True,
287 invalidation_mode=py_compile.PycInvalidationMode.TIMESTAMP)
288 zip_name, run_name = make_zip_script(script_dir, 'test_zip', compiled_name)
289 self._check_script(zip_name, run_name, zip_name, zip_name, '',
290 zipimport.zipimporter)
291
292 def test_zipfile_compiled_checked_hash(self):
293 with os_helper.temp_dir() as script_dir:
294 script_name = _make_test_script(script_dir, '__main__')
295 compiled_name = py_compile.compile(
296 script_name, doraise=True,
297 invalidation_mode=py_compile.PycInvalidationMode.CHECKED_HASH)
298 zip_name, run_name = make_zip_script(script_dir, 'test_zip', compiled_name)
299 self._check_script(zip_name, run_name, zip_name, zip_name, '',
300 zipimport.zipimporter)
301
302 def test_zipfile_compiled_unchecked_hash(self):
303 with os_helper.temp_dir() as script_dir:
304 script_name = _make_test_script(script_dir, '__main__')
305 compiled_name = py_compile.compile(
306 script_name, doraise=True,
307 invalidation_mode=py_compile.PycInvalidationMode.UNCHECKED_HASH)
308 zip_name, run_name = make_zip_script(script_dir, 'test_zip', compiled_name)
309 self._check_script(zip_name, run_name, zip_name, zip_name, '',
310 zipimport.zipimporter)
311
312 def test_zipfile_error(self):
313 with os_helper.temp_dir() as script_dir:
314 script_name = _make_test_script(script_dir, 'not_main')
315 zip_name, run_name = make_zip_script(script_dir, 'test_zip', script_name)
316 msg = "can't find '__main__' module in %r" % zip_name
317 self._check_import_error(zip_name, msg)
318
319 def test_module_in_package(self):
320 with os_helper.temp_dir() as script_dir:
321 pkg_dir = os.path.join(script_dir, 'test_pkg')
322 make_pkg(pkg_dir)
323 script_name = _make_test_script(pkg_dir, 'script')
324 self._check_script(["-m", "test_pkg.script"], script_name, script_name,
325 script_dir, 'test_pkg',
326 importlib.machinery.SourceFileLoader,
327 cwd=script_dir)
328
329 def test_module_in_package_in_zipfile(self):
330 with os_helper.temp_dir() as script_dir:
331 zip_name, run_name = _make_test_zip_pkg(script_dir, 'test_zip', 'test_pkg', 'script')
332 self._check_script(["-m", "test_pkg.script"], run_name, run_name,
333 script_dir, 'test_pkg', zipimport.zipimporter,
334 PYTHONPATH=zip_name, cwd=script_dir)
335
336 def test_module_in_subpackage_in_zipfile(self):
337 with os_helper.temp_dir() as script_dir:
338 zip_name, run_name = _make_test_zip_pkg(script_dir, 'test_zip', 'test_pkg', 'script', depth=2)
339 self._check_script(["-m", "test_pkg.test_pkg.script"], run_name, run_name,
340 script_dir, 'test_pkg.test_pkg',
341 zipimport.zipimporter,
342 PYTHONPATH=zip_name, cwd=script_dir)
343
344 def test_package(self):
345 with os_helper.temp_dir() as script_dir:
346 pkg_dir = os.path.join(script_dir, 'test_pkg')
347 make_pkg(pkg_dir)
348 script_name = _make_test_script(pkg_dir, '__main__')
349 self._check_script(["-m", "test_pkg"], script_name,
350 script_name, script_dir, 'test_pkg',
351 importlib.machinery.SourceFileLoader,
352 cwd=script_dir)
353
354 def test_package_compiled(self):
355 with os_helper.temp_dir() as script_dir:
356 pkg_dir = os.path.join(script_dir, 'test_pkg')
357 make_pkg(pkg_dir)
358 script_name = _make_test_script(pkg_dir, '__main__')
359 compiled_name = py_compile.compile(script_name, doraise=True)
360 os.remove(script_name)
361 pyc_file = import_helper.make_legacy_pyc(script_name)
362 self._check_script(["-m", "test_pkg"], pyc_file,
363 pyc_file, script_dir, 'test_pkg',
364 importlib.machinery.SourcelessFileLoader,
365 cwd=script_dir)
366
367 def test_package_error(self):
368 with os_helper.temp_dir() as script_dir:
369 pkg_dir = os.path.join(script_dir, 'test_pkg')
370 make_pkg(pkg_dir)
371 msg = ("'test_pkg' is a package and cannot "
372 "be directly executed")
373 self._check_import_error(["-m", "test_pkg"], msg, cwd=script_dir)
374
375 def test_package_recursion(self):
376 with os_helper.temp_dir() as script_dir:
377 pkg_dir = os.path.join(script_dir, 'test_pkg')
378 make_pkg(pkg_dir)
379 main_dir = os.path.join(pkg_dir, '__main__')
380 make_pkg(main_dir)
381 msg = ("Cannot use package as __main__ module; "
382 "'test_pkg' is a package and cannot "
383 "be directly executed")
384 self._check_import_error(["-m", "test_pkg"], msg, cwd=script_dir)
385
386 def test_issue8202(self):
387 # Make sure package __init__ modules see "-m" in sys.argv0 while
388 # searching for the module to execute
389 with os_helper.temp_dir() as script_dir:
390 with os_helper.change_cwd(path=script_dir):
391 pkg_dir = os.path.join(script_dir, 'test_pkg')
392 make_pkg(pkg_dir, "import sys; print('init_argv0==%r' % sys.argv[0])")
393 script_name = _make_test_script(pkg_dir, 'script')
394 rc, out, err = assert_python_ok('-m', 'test_pkg.script', *example_args, __isolated=False)
395 if verbose > 1:
396 print(repr(out))
397 expected = "init_argv0==%r" % '-m'
398 self.assertIn(expected.encode('utf-8'), out)
399 self._check_output(script_name, rc, out,
400 script_name, script_name, script_dir, 'test_pkg',
401 importlib.machinery.SourceFileLoader)
402
403 def test_issue8202_dash_c_file_ignored(self):
404 # Make sure a "-c" file in the current directory
405 # does not alter the value of sys.path[0]
406 with os_helper.temp_dir() as script_dir:
407 with os_helper.change_cwd(path=script_dir):
408 with open("-c", "w", encoding="utf-8") as f:
409 f.write("data")
410 rc, out, err = assert_python_ok('-c',
411 'import sys; print("sys.path[0]==%r" % sys.path[0])',
412 __isolated=False)
413 if verbose > 1:
414 print(repr(out))
415 expected = "sys.path[0]==%r" % ''
416 self.assertIn(expected.encode('utf-8'), out)
417
418 def test_issue8202_dash_m_file_ignored(self):
419 # Make sure a "-m" file in the current directory
420 # does not alter the value of sys.path[0]
421 with os_helper.temp_dir() as script_dir:
422 script_name = _make_test_script(script_dir, 'other')
423 with os_helper.change_cwd(path=script_dir):
424 with open("-m", "w", encoding="utf-8") as f:
425 f.write("data")
426 rc, out, err = assert_python_ok('-m', 'other', *example_args,
427 __isolated=False)
428 self._check_output(script_name, rc, out,
429 script_name, script_name, script_dir, '',
430 importlib.machinery.SourceFileLoader)
431
432 def test_issue20884(self):
433 # On Windows, script with encoding cookie and LF line ending
434 # will be failed.
435 with os_helper.temp_dir() as script_dir:
436 script_name = os.path.join(script_dir, "issue20884.py")
437 with open(script_name, "w", encoding="latin1", newline='\n') as f:
438 f.write("#coding: iso-8859-1\n")
439 f.write('"""\n')
440 for _ in range(30):
441 f.write('x'*80 + '\n')
442 f.write('"""\n')
443
444 with os_helper.change_cwd(path=script_dir):
445 rc, out, err = assert_python_ok(script_name)
446 self.assertEqual(b"", out)
447 self.assertEqual(b"", err)
448
449 @contextlib.contextmanager
450 def setup_test_pkg(self, *args):
451 with os_helper.temp_dir() as script_dir, \
452 os_helper.change_cwd(path=script_dir):
453 pkg_dir = os.path.join(script_dir, 'test_pkg')
454 make_pkg(pkg_dir, *args)
455 yield pkg_dir
456
457 def check_dash_m_failure(self, *args):
458 rc, out, err = assert_python_failure('-m', *args, __isolated=False)
459 if verbose > 1:
460 print(repr(out))
461 self.assertEqual(rc, 1)
462 return err
463
464 def test_dash_m_error_code_is_one(self):
465 # If a module is invoked with the -m command line flag
466 # and results in an error that the return code to the
467 # shell is '1'
468 with self.setup_test_pkg() as pkg_dir:
469 script_name = _make_test_script(pkg_dir, 'other',
470 "if __name__ == '__main__': raise ValueError")
471 err = self.check_dash_m_failure('test_pkg.other', *example_args)
472 self.assertIn(b'ValueError', err)
473
474 def test_dash_m_errors(self):
475 # Exercise error reporting for various invalid package executions
476 tests = (
477 ('builtins', br'No code object available'),
478 ('builtins.x', br'Error while finding module specification.*'
479 br'ModuleNotFoundError'),
480 ('builtins.x.y', br'Error while finding module specification.*'
481 br'ModuleNotFoundError.*No module named.*not a package'),
482 ('importlib', br'No module named.*'
483 br'is a package and cannot be directly executed'),
484 ('importlib.nonexistent', br'No module named'),
485 ('.unittest', br'Relative module names not supported'),
486 )
487 for name, regex in tests:
488 with self.subTest(name):
489 rc, _, err = assert_python_failure('-m', name)
490 self.assertEqual(rc, 1)
491 self.assertRegex(err, regex)
492 self.assertNotIn(b'Traceback', err)
493
494 def test_dash_m_bad_pyc(self):
495 with os_helper.temp_dir() as script_dir, \
496 os_helper.change_cwd(path=script_dir):
497 os.mkdir('test_pkg')
498 # Create invalid *.pyc as empty file
499 with open('test_pkg/__init__.pyc', 'wb'):
500 pass
501 err = self.check_dash_m_failure('test_pkg')
502 self.assertRegex(err,
503 br'Error while finding module specification.*'
504 br'ImportError.*bad magic number')
505 self.assertNotIn(b'is a package', err)
506 self.assertNotIn(b'Traceback', err)
507
508 def test_hint_when_triying_to_import_a_py_file(self):
509 with os_helper.temp_dir() as script_dir, \
510 os_helper.change_cwd(path=script_dir):
511 # Create invalid *.pyc as empty file
512 with open('asyncio.py', 'wb'):
513 pass
514 err = self.check_dash_m_failure('asyncio.py')
515 self.assertIn(b"Try using 'asyncio' instead "
516 b"of 'asyncio.py' as the module name", err)
517
518 def test_dash_m_init_traceback(self):
519 # These were wrapped in an ImportError and tracebacks were
520 # suppressed; see Issue 14285
521 exceptions = (ImportError, AttributeError, TypeError, ValueError)
522 for exception in exceptions:
523 exception = exception.__name__
524 init = "raise {0}('Exception in __init__.py')".format(exception)
525 with self.subTest(exception), \
526 self.setup_test_pkg(init) as pkg_dir:
527 err = self.check_dash_m_failure('test_pkg')
528 self.assertIn(exception.encode('ascii'), err)
529 self.assertIn(b'Exception in __init__.py', err)
530 self.assertIn(b'Traceback', err)
531
532 def test_dash_m_main_traceback(self):
533 # Ensure that an ImportError's traceback is reported
534 with self.setup_test_pkg() as pkg_dir:
535 main = "raise ImportError('Exception in __main__ module')"
536 _make_test_script(pkg_dir, '__main__', main)
537 err = self.check_dash_m_failure('test_pkg')
538 self.assertIn(b'ImportError', err)
539 self.assertIn(b'Exception in __main__ module', err)
540 self.assertIn(b'Traceback', err)
541
542 def test_pep_409_verbiage(self):
543 # Make sure PEP 409 syntax properly suppresses
544 # the context of an exception
545 script = textwrap.dedent("""\
546 try:
547 raise ValueError
548 except ValueError:
549 raise NameError from None
550 """)
551 with os_helper.temp_dir() as script_dir:
552 script_name = _make_test_script(script_dir, 'script', script)
553 exitcode, stdout, stderr = assert_python_failure(script_name)
554 text = stderr.decode('ascii').split('\n')
555 self.assertEqual(len(text), 5)
556 self.assertStartsWith(text[0], 'Traceback')
557 self.assertStartsWith(text[1], ' File ')
558 self.assertStartsWith(text[3], 'NameError')
559
560 def test_non_ascii(self):
561 # Apple platforms deny the creation of a file with an invalid UTF-8 name.
562 # Windows allows creating a name with an arbitrary bytes name, but
563 # Python cannot a undecodable bytes argument to a subprocess.
564 # Emscripten/WASI does not permit invalid UTF-8 names.
565 if (
566 os_helper.TESTFN_UNDECODABLE
567 and sys.platform not in {
568 "win32", "emscripten", "wasi"
569 }
570 and not is_apple
571 ):
572 name = os.fsdecode(os_helper.TESTFN_UNDECODABLE)
573 elif os_helper.TESTFN_NONASCII:
574 name = os_helper.TESTFN_NONASCII
575 else:
576 self.skipTest("need os_helper.TESTFN_NONASCII")
577
578 # Issue #16218
579 source = 'print(ascii(__file__))\n'
580 script_name = _make_test_script(os.getcwd(), name, source)
581 self.addCleanup(os_helper.unlink, script_name)
582 rc, stdout, stderr = assert_python_ok(script_name)
583 self.assertEqual(
584 ascii(script_name),
585 stdout.rstrip().decode('ascii'),
586 'stdout=%r stderr=%r' % (stdout, stderr))
587 self.assertEqual(0, rc)
588
589 def test_issue20500_exit_with_exception_value(self):
590 script = textwrap.dedent("""\
591 import sys
592 error = None
593 try:
594 raise ValueError('some text')
595 except ValueError as err:
596 error = err
597
598 if error:
599 sys.exit(error)
600 """)
601 with os_helper.temp_dir() as script_dir:
602 script_name = _make_test_script(script_dir, 'script', script)
603 exitcode, stdout, stderr = assert_python_failure(script_name)
604 text = stderr.decode('ascii')
605 self.assertEqual(text.rstrip(), "some text")
606
607 def test_syntaxerror_unindented_caret_position(self):
608 script = "1 + 1 = 2\n"
609 with os_helper.temp_dir() as script_dir:
610 script_name = _make_test_script(script_dir, 'script', script)
611 exitcode, stdout, stderr = assert_python_failure(script_name)
612 text = io.TextIOWrapper(io.BytesIO(stderr), 'ascii').read()
613 # Confirm that the caret is located under the '=' sign
614 self.assertIn("\n ^^^^^\n", text)
615
616 def test_syntaxerror_indented_caret_position(self):
617 script = textwrap.dedent("""\
618 if True:
619 1 + 1 = 2
620 """)
621 with os_helper.temp_dir() as script_dir:
622 script_name = _make_test_script(script_dir, 'script', script)
623 exitcode, stdout, stderr = assert_python_failure(script_name)
624 text = io.TextIOWrapper(io.BytesIO(stderr), 'ascii').read()
625 # Confirm that the caret starts under the first 1 character
626 self.assertIn("\n 1 + 1 = 2\n ^^^^^\n", text)
627
628 # Try the same with a form feed at the start of the indented line
629 script = (
630 "if True:\n"
631 "\f 1 + 1 = 2\n"
632 )
633 script_name = _make_test_script(script_dir, "script", script)
634 exitcode, stdout, stderr = assert_python_failure(script_name)
635 text = io.TextIOWrapper(io.BytesIO(stderr), "ascii").read()
636 self.assertNotIn("\f", text)
637 self.assertIn("\n 1 + 1 = 2\n ^^^^^\n", text)
638
639 def test_syntaxerror_multi_line_fstring(self):
640 script = 'foo = f"""{}\nfoo"""\n'
641 with os_helper.temp_dir() as script_dir:
642 script_name = _make_test_script(script_dir, 'script', script)
643 exitcode, stdout, stderr = assert_python_failure(script_name)
644 self.assertEqual(
645 stderr.splitlines()[-3:],
646 [
647 b' foo = f"""{}',
648 b' ^',
649 b'SyntaxError: f-string: valid expression required before \'}\'',
650 ],
651 )
652
653 def test_syntaxerror_invalid_escape_sequence_multi_line(self):
654 script = 'foo = """\\q"""\n'
655 with os_helper.temp_dir() as script_dir:
656 script_name = _make_test_script(script_dir, 'script', script)
657 exitcode, stdout, stderr = assert_python_failure(
658 '-Werror', script_name,
659 )
660 self.assertEqual(
661 stderr.splitlines()[-3:],
662 [ b' foo = """\\q"""',
663 b' ^^',
664 b'SyntaxError: "\\q" is an invalid escape sequence. '
665 b'Did you mean "\\\\q"? A raw string is also an option.'
666 ],
667 )
668
669 def test_syntaxerror_null_bytes(self):
670 script = "x = '\0' nothing to see here\n';import os;os.system('echo pwnd')\n"
671 with os_helper.temp_dir() as script_dir:
672 script_name = _make_test_script(script_dir, 'script', script)
673 exitcode, stdout, stderr = assert_python_failure(script_name)
674 self.assertEqual(
675 stderr.splitlines()[-2:],
676 [ b" x = '",
677 b'SyntaxError: source code cannot contain null bytes'
678 ],
679 )
680
681 def test_syntaxerror_null_bytes_in_multiline_string(self):
682 scripts = ["\n'''\nmultilinestring\0\n'''", "\nf'''\nmultilinestring\0\n'''"] # Both normal and f-strings
683 with os_helper.temp_dir() as script_dir:
684 for script in scripts:
685 script_name = _make_test_script(script_dir, 'script', script)
686 _, _, stderr = assert_python_failure(script_name)
687 self.assertEqual(
688 stderr.splitlines()[-2:],
689 [ b" multilinestring",
690 b'SyntaxError: source code cannot contain null bytes'
691 ]
692 )
693
694 def test_source_lines_are_shown_when_running_source(self):
695 _, _, stderr = assert_python_failure("-c", "1/0")
696 expected_lines = [
697 b'Traceback (most recent call last):',
698 b' File "<string>", line 1, in <module>',
699 b' 1/0',
700 b' ~^~',
701 b'ZeroDivisionError: division by zero']
702 self.assertEqual(stderr.splitlines(), expected_lines)
703
704 def test_syntaxerror_does_not_crash(self):
705 script = "nonlocal x\n"
706 with os_helper.temp_dir() as script_dir:
707 script_name = _make_test_script(script_dir, 'script', script)
708 exitcode, stdout, stderr = assert_python_failure(script_name)
709 text = io.TextIOWrapper(io.BytesIO(stderr), 'ascii').read()
710 # It used to crash in https://github.com/python/cpython/issues/111132
711 self.assertEndsWith(text,
712 'SyntaxError: nonlocal declaration not allowed at module level\n')
713
714 def test_consistent_sys_path_for_direct_execution(self):
715 # This test case ensures that the following all give the same
716 # sys.path configuration:
717 #
718 # ./python -s script_dir/__main__.py
719 # ./python -s script_dir
720 # ./python -I script_dir
721 script = textwrap.dedent("""\
722 import sys
723 for entry in sys.path:
724 print(entry)
725 """)
726 # Always show full path diffs on errors
727 self.maxDiff = None
728 with os_helper.temp_dir() as work_dir, os_helper.temp_dir() as script_dir:
729 script_name = _make_test_script(script_dir, '__main__', script)
730 # Reference output comes from directly executing __main__.py
731 # We omit PYTHONPATH and user site to align with isolated mode
732 p = spawn_python("-Es", script_name, cwd=work_dir)
733 out_by_name = kill_python(p).decode().splitlines()
734 self.assertEqual(out_by_name[0], script_dir)
735 self.assertNotIn(work_dir, out_by_name)
736 # Directory execution should give the same output
737 p = spawn_python("-Es", script_dir, cwd=work_dir)
738 out_by_dir = kill_python(p).decode().splitlines()
739 self.assertEqual(out_by_dir, out_by_name)
740 # As should directory execution in isolated mode
741 p = spawn_python("-I", script_dir, cwd=work_dir)
742 out_by_dir_isolated = kill_python(p).decode().splitlines()
743 self.assertEqual(out_by_dir_isolated, out_by_dir, out_by_name)
744
745 def test_consistent_sys_path_for_module_execution(self):
746 # This test case ensures that the following both give the same
747 # sys.path configuration:
748 # ./python -sm script_pkg.__main__
749 # ./python -sm script_pkg
750 #
751 # And that this fails as unable to find the package:
752 # ./python -Im script_pkg
753 script = textwrap.dedent("""\
754 import sys
755 for entry in sys.path:
756 print(entry)
757 """)
758 # Always show full path diffs on errors
759 self.maxDiff = None
760 with os_helper.temp_dir() as work_dir:
761 script_dir = os.path.join(work_dir, "script_pkg")
762 os.mkdir(script_dir)
763 script_name = _make_test_script(script_dir, '__main__', script)
764 # Reference output comes from `-m script_pkg.__main__`
765 # We omit PYTHONPATH and user site to better align with the
766 # direct execution test cases
767 p = spawn_python("-sm", "script_pkg.__main__", cwd=work_dir)
768 out_by_module = kill_python(p).decode().splitlines()
769 self.assertEqual(out_by_module[0], work_dir)
770 self.assertNotIn(script_dir, out_by_module)
771 # Package execution should give the same output
772 p = spawn_python("-sm", "script_pkg", cwd=work_dir)
773 out_by_package = kill_python(p).decode().splitlines()
774 self.assertEqual(out_by_package, out_by_module)
775 # Isolated mode should fail with an import error
776 exitcode, stdout, stderr = assert_python_failure(
777 "-Im", "script_pkg", cwd=work_dir
778 )
779 traceback_lines = stderr.decode().splitlines()
780 self.assertIn("No module named script_pkg", traceback_lines[-1])
781
782 def test_nonexisting_script(self):
783 # bpo-34783: "./python script.py" must not crash
784 # if the script file doesn't exist.
785 # (Skip test for macOS framework builds because sys.executable name
786 # is not the actual Python executable file name.
787 script = 'nonexistingscript.py'
788 self.assertFalse(os.path.exists(script))
789
790 proc = spawn_python(script, text=True,
791 stdout=subprocess.PIPE,
792 stderr=subprocess.PIPE)
793 out, err = proc.communicate()
794 self.assertIn(": can't open file ", err)
795 self.assertNotEqual(proc.returncode, 0)
796
797 @unittest.skipUnless(os.path.exists('/dev/fd/0'), 'requires /dev/fd platform')
798 @unittest.skipIf(sys.platform.startswith("freebsd") and
799 os.stat("/dev").st_dev == os.stat("/dev/fd").st_dev,
800 "Requires fdescfs mounted on /dev/fd on FreeBSD")
801 def test_script_as_dev_fd(self):
802 # GH-87235: On macOS passing a non-trivial script to /dev/fd/N can cause
803 # problems because all open /dev/fd/N file descriptors share the same
804 # offset.
805 script = 'print("12345678912345678912345")'
806 with os_helper.temp_dir() as work_dir:
807 script_name = _make_test_script(work_dir, 'script.py', script)
808 with open(script_name, "r") as fp:
809 p = spawn_python(f"/dev/fd/{fp.fileno()}", close_fds=True, pass_fds=(0,1,2,fp.fileno()))
810 out, err = p.communicate()
811 self.assertEqual(out, b"12345678912345678912345\n")
812
813
814
815 def tearDownModule():
816 support.reap_children()
817
818
819 if __name__ == '__main__':
820 unittest.main()