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