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        self.assertIn(printed_path0.encode('utf-8'), data)
120        self.assertIn(printed_cwd.encode('utf-8'), data)
121
122    def _check_script(self, script_exec_args, expected_file,
123                            expected_argv0, expected_path0,
124                            expected_package, expected_loader,
125                            *cmd_line_switches, cwd=None, **env_vars):
126        if isinstance(script_exec_args, str):
127            script_exec_args = [script_exec_args]
128        run_args = [*support.optim_args_from_interpreter_flags(),
129                    *cmd_line_switches, *script_exec_args, *example_args]
130        rc, out, err = assert_python_ok(
131            *run_args, __isolated=False, __cwd=cwd, **env_vars
132        )
133        self._check_output(script_exec_args, rc, out + err, expected_file,
134                           expected_argv0, expected_path0,
135                           expected_package, expected_loader, cwd)
136
137    def _check_import_error(self, script_exec_args, expected_msg,
138                            *cmd_line_switches, cwd=None, **env_vars):
139        if isinstance(script_exec_args, str):
140            script_exec_args = (script_exec_args,)
141        else:
142            script_exec_args = tuple(script_exec_args)
143        run_args = cmd_line_switches + script_exec_args
144        rc, out, err = assert_python_failure(
145            *run_args, __isolated=False, __cwd=cwd, **env_vars
146        )
147        if verbose > 1:
148            print(f'Output from test script {script_exec_args!r:}')
149            print(repr(err))
150            print('Expected output: %r' % expected_msg)
151        self.assertIn(expected_msg.encode('utf-8'), err)
152
153    def test_dash_c_loader(self):
154        rc, out, err = assert_python_ok("-c", "print(__loader__)")
155        expected = repr(importlib.machinery.BuiltinImporter).encode("utf-8")
156        self.assertIn(expected, out)
157
158    def test_stdin_loader(self):
159        # Unfortunately, there's no way to automatically test the fully
160        # interactive REPL, since that code path only gets executed when
161        # stdin is an interactive tty.
162        p = spawn_python()
163        try:
164            p.stdin.write(b"print(__loader__)\n")
165            p.stdin.flush()
166        finally:
167            out = kill_python(p)
168        expected = repr(importlib.machinery.BuiltinImporter).encode("utf-8")
169        self.assertIn(expected, out)
170
171    @contextlib.contextmanager
172    def interactive_python(self, separate_stderr=False):
173        if separate_stderr:
174            p = spawn_python('-i', stderr=subprocess.PIPE)
175            stderr = p.stderr
176        else:
177            p = spawn_python('-i', stderr=subprocess.STDOUT)
178            stderr = p.stdout
179        try:
180            # Drain stderr until prompt
181            while True:
182                data = stderr.read(4)
183                if data == b">>> ":
184                    break
185                stderr.readline()
186            yield p
187        finally:
188            kill_python(p)
189            stderr.close()
190
191    def check_repl_stdout_flush(self, separate_stderr=False):
192        with self.interactive_python(separate_stderr) as p:
193            p.stdin.write(b"print('foo')\n")
194            p.stdin.flush()
195            self.assertEqual(b'foo', p.stdout.readline().strip())
196
197    def check_repl_stderr_flush(self, separate_stderr=False):
198        with self.interactive_python(separate_stderr) as p:
199            p.stdin.write(b"1/0\n")
200            p.stdin.flush()
201            stderr = p.stderr if separate_stderr else p.stdout
202            self.assertIn(b'Traceback ', stderr.readline())
203            self.assertIn(b'File "<stdin>"', stderr.readline())
204            self.assertIn(b'ZeroDivisionError', stderr.readline())
205
206    def test_repl_stdout_flush(self):
207        self.check_repl_stdout_flush()
208
209    def test_repl_stdout_flush_separate_stderr(self):
210        self.check_repl_stdout_flush(True)
211
212    def test_repl_stderr_flush(self):
213        self.check_repl_stderr_flush()
214
215    def test_repl_stderr_flush_separate_stderr(self):
216        self.check_repl_stderr_flush(True)
217
218    def test_basic_script(self):
219        with os_helper.temp_dir() as script_dir:
220            script_name = _make_test_script(script_dir, 'script')
221            self._check_script(script_name, script_name, script_name,
222                               script_dir, None,
223                               importlib.machinery.SourceFileLoader,
224                               expected_cwd=script_dir)
225
226    def test_script_abspath(self):
227        # pass the script using the relative path, expect the absolute path
228        # in __file__
229        with os_helper.temp_cwd() as script_dir:
230            self.assertTrue(os.path.isabs(script_dir), script_dir)
231
232            script_name = _make_test_script(script_dir, 'script')
233            relative_name = os.path.basename(script_name)
234            self._check_script(relative_name, script_name, relative_name,
235                               script_dir, None,
236                               importlib.machinery.SourceFileLoader)
237
238    def test_script_compiled(self):
239        with os_helper.temp_dir() as script_dir:
240            script_name = _make_test_script(script_dir, 'script')
241            py_compile.compile(script_name, doraise=True)
242            os.remove(script_name)
243            pyc_file = import_helper.make_legacy_pyc(script_name)
244            self._check_script(pyc_file, pyc_file,
245                               pyc_file, script_dir, None,
246                               importlib.machinery.SourcelessFileLoader)
247
248    def test_directory(self):
249        with os_helper.temp_dir() as script_dir:
250            script_name = _make_test_script(script_dir, '__main__')
251            self._check_script(script_dir, script_name, script_dir,
252                               script_dir, '',
253                               importlib.machinery.SourceFileLoader)
254
255    def test_directory_compiled(self):
256        with os_helper.temp_dir() as script_dir:
257            script_name = _make_test_script(script_dir, '__main__')
258            py_compile.compile(script_name, doraise=True)
259            os.remove(script_name)
260            pyc_file = import_helper.make_legacy_pyc(script_name)
261            self._check_script(script_dir, pyc_file, script_dir,
262                               script_dir, '',
263                               importlib.machinery.SourcelessFileLoader)
264
265    def test_directory_error(self):
266        with os_helper.temp_dir() as script_dir:
267            msg = "can't find '__main__' module in %r" % script_dir
268            self._check_import_error(script_dir, msg)
269
270    def test_zipfile(self):
271        with os_helper.temp_dir() as script_dir:
272            script_name = _make_test_script(script_dir, '__main__')
273            zip_name, run_name = make_zip_script(script_dir, 'test_zip', script_name)
274            self._check_script(zip_name, run_name, zip_name, zip_name, '',
275                               zipimport.zipimporter)
276
277    def test_zipfile_compiled_timestamp(self):
278        with os_helper.temp_dir() as script_dir:
279            script_name = _make_test_script(script_dir, '__main__')
280            compiled_name = py_compile.compile(
281                script_name, doraise=True,
282                invalidation_mode=py_compile.PycInvalidationMode.TIMESTAMP)
283            zip_name, run_name = make_zip_script(script_dir, 'test_zip', compiled_name)
284            self._check_script(zip_name, run_name, zip_name, zip_name, '',
285                               zipimport.zipimporter)
286
287    def test_zipfile_compiled_checked_hash(self):
288        with os_helper.temp_dir() as script_dir:
289            script_name = _make_test_script(script_dir, '__main__')
290            compiled_name = py_compile.compile(
291                script_name, doraise=True,
292                invalidation_mode=py_compile.PycInvalidationMode.CHECKED_HASH)
293            zip_name, run_name = make_zip_script(script_dir, 'test_zip', compiled_name)
294            self._check_script(zip_name, run_name, zip_name, zip_name, '',
295                               zipimport.zipimporter)
296
297    def test_zipfile_compiled_unchecked_hash(self):
298        with os_helper.temp_dir() as script_dir:
299            script_name = _make_test_script(script_dir, '__main__')
300            compiled_name = py_compile.compile(
301                script_name, doraise=True,
302                invalidation_mode=py_compile.PycInvalidationMode.UNCHECKED_HASH)
303            zip_name, run_name = make_zip_script(script_dir, 'test_zip', compiled_name)
304            self._check_script(zip_name, run_name, zip_name, zip_name, '',
305                               zipimport.zipimporter)
306
307    def test_zipfile_error(self):
308        with os_helper.temp_dir() as script_dir:
309            script_name = _make_test_script(script_dir, 'not_main')
310            zip_name, run_name = make_zip_script(script_dir, 'test_zip', script_name)
311            msg = "can't find '__main__' module in %r" % zip_name
312            self._check_import_error(zip_name, msg)
313
314    def test_module_in_package(self):
315        with os_helper.temp_dir() as script_dir:
316            pkg_dir = os.path.join(script_dir, 'test_pkg')
317            make_pkg(pkg_dir)
318            script_name = _make_test_script(pkg_dir, 'script')
319            self._check_script(["-m", "test_pkg.script"], script_name, script_name,
320                               script_dir, 'test_pkg',
321                               importlib.machinery.SourceFileLoader,
322                               cwd=script_dir)
323
324    def test_module_in_package_in_zipfile(self):
325        with os_helper.temp_dir() as script_dir:
326            zip_name, run_name = _make_test_zip_pkg(script_dir, 'test_zip', 'test_pkg', 'script')
327            self._check_script(["-m", "test_pkg.script"], run_name, run_name,
328                               script_dir, 'test_pkg', zipimport.zipimporter,
329                               PYTHONPATH=zip_name, cwd=script_dir)
330
331    def test_module_in_subpackage_in_zipfile(self):
332        with os_helper.temp_dir() as script_dir:
333            zip_name, run_name = _make_test_zip_pkg(script_dir, 'test_zip', 'test_pkg', 'script', depth=2)
334            self._check_script(["-m", "test_pkg.test_pkg.script"], run_name, run_name,
335                               script_dir, 'test_pkg.test_pkg',
336                               zipimport.zipimporter,
337                               PYTHONPATH=zip_name, cwd=script_dir)
338
339    def test_package(self):
340        with os_helper.temp_dir() as script_dir:
341            pkg_dir = os.path.join(script_dir, 'test_pkg')
342            make_pkg(pkg_dir)
343            script_name = _make_test_script(pkg_dir, '__main__')
344            self._check_script(["-m", "test_pkg"], script_name,
345                               script_name, script_dir, 'test_pkg',
346                               importlib.machinery.SourceFileLoader,
347                               cwd=script_dir)
348
349    def test_package_compiled(self):
350        with os_helper.temp_dir() as script_dir:
351            pkg_dir = os.path.join(script_dir, 'test_pkg')
352            make_pkg(pkg_dir)
353            script_name = _make_test_script(pkg_dir, '__main__')
354            compiled_name = py_compile.compile(script_name, doraise=True)
355            os.remove(script_name)
356            pyc_file = import_helper.make_legacy_pyc(script_name)
357            self._check_script(["-m", "test_pkg"], pyc_file,
358                               pyc_file, script_dir, 'test_pkg',
359                               importlib.machinery.SourcelessFileLoader,
360                               cwd=script_dir)
361
362    def test_package_error(self):
363        with os_helper.temp_dir() as script_dir:
364            pkg_dir = os.path.join(script_dir, 'test_pkg')
365            make_pkg(pkg_dir)
366            msg = ("'test_pkg' is a package and cannot "
367                   "be directly executed")
368            self._check_import_error(["-m", "test_pkg"], msg, cwd=script_dir)
369
370    def test_package_recursion(self):
371        with os_helper.temp_dir() as script_dir:
372            pkg_dir = os.path.join(script_dir, 'test_pkg')
373            make_pkg(pkg_dir)
374            main_dir = os.path.join(pkg_dir, '__main__')
375            make_pkg(main_dir)
376            msg = ("Cannot use package as __main__ module; "
377                   "'test_pkg' is a package and cannot "
378                   "be directly executed")
379            self._check_import_error(["-m", "test_pkg"], msg, cwd=script_dir)
380
381    def test_issue8202(self):
382        # Make sure package __init__ modules see "-m" in sys.argv0 while
383        # searching for the module to execute
384        with os_helper.temp_dir() as script_dir:
385            with os_helper.change_cwd(path=script_dir):
386                pkg_dir = os.path.join(script_dir, 'test_pkg')
387                make_pkg(pkg_dir, "import sys; print('init_argv0==%r' % sys.argv[0])")
388                script_name = _make_test_script(pkg_dir, 'script')
389                rc, out, err = assert_python_ok('-m', 'test_pkg.script', *example_args, __isolated=False)
390                if verbose > 1:
391                    print(repr(out))
392                expected = "init_argv0==%r" % '-m'
393                self.assertIn(expected.encode('utf-8'), out)
394                self._check_output(script_name, rc, out,
395                                   script_name, script_name, script_dir, 'test_pkg',
396                                   importlib.machinery.SourceFileLoader)
397
398    def test_issue8202_dash_c_file_ignored(self):
399        # Make sure a "-c" file in the current directory
400        # does not alter the value of sys.path[0]
401        with os_helper.temp_dir() as script_dir:
402            with os_helper.change_cwd(path=script_dir):
403                with open("-c", "w", encoding="utf-8") as f:
404                    f.write("data")
405                    rc, out, err = assert_python_ok('-c',
406                        'import sys; print("sys.path[0]==%r" % sys.path[0])',
407                        __isolated=False)
408                    if verbose > 1:
409                        print(repr(out))
410                    expected = "sys.path[0]==%r" % ''
411                    self.assertIn(expected.encode('utf-8'), out)
412
413    def test_issue8202_dash_m_file_ignored(self):
414        # Make sure a "-m" file in the current directory
415        # does not alter the value of sys.path[0]
416        with os_helper.temp_dir() as script_dir:
417            script_name = _make_test_script(script_dir, 'other')
418            with os_helper.change_cwd(path=script_dir):
419                with open("-m", "w", encoding="utf-8") as f:
420                    f.write("data")
421                    rc, out, err = assert_python_ok('-m', 'other', *example_args,
422                                                    __isolated=False)
423                    self._check_output(script_name, rc, out,
424                                      script_name, script_name, script_dir, '',
425                                      importlib.machinery.SourceFileLoader)
426
427    def test_issue20884(self):
428        # On Windows, script with encoding cookie and LF line ending
429        # will be failed.
430        with os_helper.temp_dir() as script_dir:
431            script_name = os.path.join(script_dir, "issue20884.py")
432            with open(script_name, "w", encoding="latin1", newline='\n') as f:
433                f.write("#coding: iso-8859-1\n")
434                f.write('"""\n')
435                for _ in range(30):
436                    f.write('x'*80 + '\n')
437                f.write('"""\n')
438
439            with os_helper.change_cwd(path=script_dir):
440                rc, out, err = assert_python_ok(script_name)
441            self.assertEqual(b"", out)
442            self.assertEqual(b"", err)
443
444    @contextlib.contextmanager
445    def setup_test_pkg(self, *args):
446        with os_helper.temp_dir() as script_dir, \
447                os_helper.change_cwd(path=script_dir):
448            pkg_dir = os.path.join(script_dir, 'test_pkg')
449            make_pkg(pkg_dir, *args)
450            yield pkg_dir
451
452    def check_dash_m_failure(self, *args):
453        rc, out, err = assert_python_failure('-m', *args, __isolated=False)
454        if verbose > 1:
455            print(repr(out))
456        self.assertEqual(rc, 1)
457        return err
458
459    def test_dash_m_error_code_is_one(self):
460        # If a module is invoked with the -m command line flag
461        # and results in an error that the return code to the
462        # shell is '1'
463        with self.setup_test_pkg() as pkg_dir:
464            script_name = _make_test_script(pkg_dir, 'other',
465                                            "if __name__ == '__main__': raise ValueError")
466            err = self.check_dash_m_failure('test_pkg.other', *example_args)
467            self.assertIn(b'ValueError', err)
468
469    def test_dash_m_errors(self):
470        # Exercise error reporting for various invalid package executions
471        tests = (
472            ('builtins', br'No code object available'),
473            ('builtins.x', br'Error while finding module specification.*'
474                br'ModuleNotFoundError'),
475            ('builtins.x.y', br'Error while finding module specification.*'
476                br'ModuleNotFoundError.*No module named.*not a package'),
477            ('importlib', br'No module named.*'
478                br'is a package and cannot be directly executed'),
479            ('importlib.nonexistent', br'No module named'),
480            ('.unittest', br'Relative module names not supported'),
481        )
482        for name, regex in tests:
483            with self.subTest(name):
484                rc, _, err = assert_python_failure('-m', name)
485                self.assertEqual(rc, 1)
486                self.assertRegex(err, regex)
487                self.assertNotIn(b'Traceback', err)
488
489    def test_dash_m_bad_pyc(self):
490        with os_helper.temp_dir() as script_dir, \
491                os_helper.change_cwd(path=script_dir):
492            os.mkdir('test_pkg')
493            # Create invalid *.pyc as empty file
494            with open('test_pkg/__init__.pyc', 'wb'):
495                pass
496            err = self.check_dash_m_failure('test_pkg')
497            self.assertRegex(err,
498                br'Error while finding module specification.*'
499                br'ImportError.*bad magic number')
500            self.assertNotIn(b'is a package', err)
501            self.assertNotIn(b'Traceback', err)
502
503    def test_hint_when_triying_to_import_a_py_file(self):
504        with os_helper.temp_dir() as script_dir, \
505                os_helper.change_cwd(path=script_dir):
506            # Create invalid *.pyc as empty file
507            with open('asyncio.py', 'wb'):
508                pass
509            err = self.check_dash_m_failure('asyncio.py')
510            self.assertIn(b"Try using 'asyncio' instead "
511                          b"of 'asyncio.py' as the module name", err)
512
513    def test_dash_m_init_traceback(self):
514        # These were wrapped in an ImportError and tracebacks were
515        # suppressed; see Issue 14285
516        exceptions = (ImportError, AttributeError, TypeError, ValueError)
517        for exception in exceptions:
518            exception = exception.__name__
519            init = "raise {0}('Exception in __init__.py')".format(exception)
520            with self.subTest(exception), \
521                    self.setup_test_pkg(init) as pkg_dir:
522                err = self.check_dash_m_failure('test_pkg')
523                self.assertIn(exception.encode('ascii'), err)
524                self.assertIn(b'Exception in __init__.py', err)
525                self.assertIn(b'Traceback', err)
526
527    def test_dash_m_main_traceback(self):
528        # Ensure that an ImportError's traceback is reported
529        with self.setup_test_pkg() as pkg_dir:
530            main = "raise ImportError('Exception in __main__ module')"
531            _make_test_script(pkg_dir, '__main__', main)
532            err = self.check_dash_m_failure('test_pkg')
533            self.assertIn(b'ImportError', err)
534            self.assertIn(b'Exception in __main__ module', err)
535            self.assertIn(b'Traceback', err)
536
537    def test_pep_409_verbiage(self):
538        # Make sure PEP 409 syntax properly suppresses
539        # the context of an exception
540        script = textwrap.dedent("""\
541            try:
542                raise ValueError
543            except:
544                raise NameError from None
545            """)
546        with os_helper.temp_dir() as script_dir:
547            script_name = _make_test_script(script_dir, 'script', script)
548            exitcode, stdout, stderr = assert_python_failure(script_name)
549            text = stderr.decode('ascii').split('\n')
550            self.assertEqual(len(text), 6)
551            self.assertTrue(text[0].startswith('Traceback'))
552            self.assertTrue(text[1].startswith('  File '))
553            self.assertTrue(text[4].startswith('NameError'))
554
555    def test_non_ascii(self):
556        # Mac OS X denies the creation of a file with an invalid UTF-8 name.
557        # Windows allows creating a name with an arbitrary bytes name, but
558        # Python cannot a undecodable bytes argument to a subprocess.
559        if (os_helper.TESTFN_UNDECODABLE
560        and sys.platform not in ('win32', 'darwin')):
561            name = os.fsdecode(os_helper.TESTFN_UNDECODABLE)
562        elif os_helper.TESTFN_NONASCII:
563            name = os_helper.TESTFN_NONASCII
564        else:
565            self.skipTest("need os_helper.TESTFN_NONASCII")
566
567        # Issue #16218
568        source = 'print(ascii(__file__))\n'
569        script_name = _make_test_script(os.getcwd(), name, source)
570        self.addCleanup(os_helper.unlink, script_name)
571        rc, stdout, stderr = assert_python_ok(script_name)
572        self.assertEqual(
573            ascii(script_name),
574            stdout.rstrip().decode('ascii'),
575            'stdout=%r stderr=%r' % (stdout, stderr))
576        self.assertEqual(0, rc)
577
578    def test_issue20500_exit_with_exception_value(self):
579        script = textwrap.dedent("""\
580            import sys
581            error = None
582            try:
583                raise ValueError('some text')
584            except ValueError as err:
585                error = err
586
587            if error:
588                sys.exit(error)
589            """)
590        with os_helper.temp_dir() as script_dir:
591            script_name = _make_test_script(script_dir, 'script', script)
592            exitcode, stdout, stderr = assert_python_failure(script_name)
593            text = stderr.decode('ascii')
594            self.assertEqual(text.rstrip(), "some text")
595
596    def test_syntaxerror_unindented_caret_position(self):
597        script = "1 + 1 = 2\n"
598        with os_helper.temp_dir() as script_dir:
599            script_name = _make_test_script(script_dir, 'script', script)
600            exitcode, stdout, stderr = assert_python_failure(script_name)
601            text = io.TextIOWrapper(io.BytesIO(stderr), 'ascii').read()
602            # Confirm that the caret is located under the '=' sign
603            self.assertIn("\n    ^^^^^\n", text)
604
605    def test_syntaxerror_indented_caret_position(self):
606        script = textwrap.dedent("""\
607            if True:
608                1 + 1 = 2
609            """)
610        with os_helper.temp_dir() as script_dir:
611            script_name = _make_test_script(script_dir, 'script', script)
612            exitcode, stdout, stderr = assert_python_failure(script_name)
613            text = io.TextIOWrapper(io.BytesIO(stderr), 'ascii').read()
614            # Confirm that the caret starts under the first 1 character
615            self.assertIn("\n    1 + 1 = 2\n    ^^^^^\n", text)
616
617            # Try the same with a form feed at the start of the indented line
618            script = (
619                "if True:\n"
620                "\f    1 + 1 = 2\n"
621            )
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            self.assertNotIn("\f", text)
626            self.assertIn("\n    1 + 1 = 2\n    ^^^^^\n", text)
627
628    def test_syntaxerror_multi_line_fstring(self):
629        script = 'foo = f"""{}\nfoo"""\n'
630        with os_helper.temp_dir() as script_dir:
631            script_name = _make_test_script(script_dir, 'script', script)
632            exitcode, stdout, stderr = assert_python_failure(script_name)
633            self.assertEqual(
634                stderr.splitlines()[-3:],
635                [
636                    b'    foo"""',
637                    b'          ^',
638                    b'SyntaxError: f-string: empty expression not allowed',
639                ],
640            )
641
642    def test_syntaxerror_invalid_escape_sequence_multi_line(self):
643        script = 'foo = """\\q"""\n'
644        with os_helper.temp_dir() as script_dir:
645            script_name = _make_test_script(script_dir, 'script', script)
646            exitcode, stdout, stderr = assert_python_failure(
647                '-Werror', script_name,
648            )
649            self.assertEqual(
650                stderr.splitlines()[-3:],
651                [   b'    foo = """\\q"""',
652                    b'          ^^^^^^^^',
653                    b'SyntaxError: invalid escape sequence \'\\q\''
654                ],
655            )
656
657    def test_consistent_sys_path_for_direct_execution(self):
658        # This test case ensures that the following all give the same
659        # sys.path configuration:
660        #
661        #    ./python -s script_dir/__main__.py
662        #    ./python -s script_dir
663        #    ./python -I script_dir
664        script = textwrap.dedent("""\
665            import sys
666            for entry in sys.path:
667                print(entry)
668            """)
669        # Always show full path diffs on errors
670        self.maxDiff = None
671        with os_helper.temp_dir() as work_dir, os_helper.temp_dir() as script_dir:
672            script_name = _make_test_script(script_dir, '__main__', script)
673            # Reference output comes from directly executing __main__.py
674            # We omit PYTHONPATH and user site to align with isolated mode
675            p = spawn_python("-Es", script_name, cwd=work_dir)
676            out_by_name = kill_python(p).decode().splitlines()
677            self.assertEqual(out_by_name[0], script_dir)
678            self.assertNotIn(work_dir, out_by_name)
679            # Directory execution should give the same output
680            p = spawn_python("-Es", script_dir, cwd=work_dir)
681            out_by_dir = kill_python(p).decode().splitlines()
682            self.assertEqual(out_by_dir, out_by_name)
683            # As should directory execution in isolated mode
684            p = spawn_python("-I", script_dir, cwd=work_dir)
685            out_by_dir_isolated = kill_python(p).decode().splitlines()
686            self.assertEqual(out_by_dir_isolated, out_by_dir, out_by_name)
687
688    def test_consistent_sys_path_for_module_execution(self):
689        # This test case ensures that the following both give the same
690        # sys.path configuration:
691        #    ./python -sm script_pkg.__main__
692        #    ./python -sm script_pkg
693        #
694        # And that this fails as unable to find the package:
695        #    ./python -Im script_pkg
696        script = textwrap.dedent("""\
697            import sys
698            for entry in sys.path:
699                print(entry)
700            """)
701        # Always show full path diffs on errors
702        self.maxDiff = None
703        with os_helper.temp_dir() as work_dir:
704            script_dir = os.path.join(work_dir, "script_pkg")
705            os.mkdir(script_dir)
706            script_name = _make_test_script(script_dir, '__main__', script)
707            # Reference output comes from `-m script_pkg.__main__`
708            # We omit PYTHONPATH and user site to better align with the
709            # direct execution test cases
710            p = spawn_python("-sm", "script_pkg.__main__", cwd=work_dir)
711            out_by_module = kill_python(p).decode().splitlines()
712            self.assertEqual(out_by_module[0], work_dir)
713            self.assertNotIn(script_dir, out_by_module)
714            # Package execution should give the same output
715            p = spawn_python("-sm", "script_pkg", cwd=work_dir)
716            out_by_package = kill_python(p).decode().splitlines()
717            self.assertEqual(out_by_package, out_by_module)
718            # Isolated mode should fail with an import error
719            exitcode, stdout, stderr = assert_python_failure(
720                "-Im", "script_pkg", cwd=work_dir
721            )
722            traceback_lines = stderr.decode().splitlines()
723            self.assertIn("No module named script_pkg", traceback_lines[-1])
724
725    def test_nonexisting_script(self):
726        # bpo-34783: "./python script.py" must not crash
727        # if the script file doesn't exist.
728        # (Skip test for macOS framework builds because sys.executable name
729        #  is not the actual Python executable file name.
730        script = 'nonexistingscript.py'
731        self.assertFalse(os.path.exists(script))
732
733        proc = spawn_python(script, text=True,
734                            stdout=subprocess.PIPE,
735                            stderr=subprocess.PIPE)
736        out, err = proc.communicate()
737        self.assertIn(": can't open file ", err)
738        self.assertNotEqual(proc.returncode, 0)
739
740
741def tearDownModule():
742    support.reap_children()
743
744
745if __name__ == '__main__':
746    unittest.main()
747