• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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            ('os.path', br'loader.*cannot handle'),
478            ('importlib', br'No module named.*'
479                br'is a package and cannot be directly executed'),
480            ('importlib.nonexistent', br'No module named'),
481            ('.unittest', br'Relative module names not supported'),
482        )
483        for name, regex in tests:
484            with self.subTest(name):
485                rc, _, err = assert_python_failure('-m', name)
486                self.assertEqual(rc, 1)
487                self.assertRegex(err, regex)
488                self.assertNotIn(b'Traceback', err)
489
490    def test_dash_m_bad_pyc(self):
491        with os_helper.temp_dir() as script_dir, \
492                os_helper.change_cwd(path=script_dir):
493            os.mkdir('test_pkg')
494            # Create invalid *.pyc as empty file
495            with open('test_pkg/__init__.pyc', 'wb'):
496                pass
497            err = self.check_dash_m_failure('test_pkg')
498            self.assertRegex(err,
499                br'Error while finding module specification.*'
500                br'ImportError.*bad magic number')
501            self.assertNotIn(b'is a package', err)
502            self.assertNotIn(b'Traceback', err)
503
504    def test_hint_when_triying_to_import_a_py_file(self):
505        with os_helper.temp_dir() as script_dir, \
506                os_helper.change_cwd(path=script_dir):
507            # Create invalid *.pyc as empty file
508            with open('asyncio.py', 'wb'):
509                pass
510            err = self.check_dash_m_failure('asyncio.py')
511            self.assertIn(b"Try using 'asyncio' instead "
512                          b"of 'asyncio.py' as the module name", err)
513
514    def test_dash_m_init_traceback(self):
515        # These were wrapped in an ImportError and tracebacks were
516        # suppressed; see Issue 14285
517        exceptions = (ImportError, AttributeError, TypeError, ValueError)
518        for exception in exceptions:
519            exception = exception.__name__
520            init = "raise {0}('Exception in __init__.py')".format(exception)
521            with self.subTest(exception), \
522                    self.setup_test_pkg(init) as pkg_dir:
523                err = self.check_dash_m_failure('test_pkg')
524                self.assertIn(exception.encode('ascii'), err)
525                self.assertIn(b'Exception in __init__.py', err)
526                self.assertIn(b'Traceback', err)
527
528    def test_dash_m_main_traceback(self):
529        # Ensure that an ImportError's traceback is reported
530        with self.setup_test_pkg() as pkg_dir:
531            main = "raise ImportError('Exception in __main__ module')"
532            _make_test_script(pkg_dir, '__main__', main)
533            err = self.check_dash_m_failure('test_pkg')
534            self.assertIn(b'ImportError', err)
535            self.assertIn(b'Exception in __main__ module', err)
536            self.assertIn(b'Traceback', err)
537
538    def test_pep_409_verbiage(self):
539        # Make sure PEP 409 syntax properly suppresses
540        # the context of an exception
541        script = textwrap.dedent("""\
542            try:
543                raise ValueError
544            except:
545                raise NameError from None
546            """)
547        with os_helper.temp_dir() as script_dir:
548            script_name = _make_test_script(script_dir, 'script', script)
549            exitcode, stdout, stderr = assert_python_failure(script_name)
550            text = stderr.decode('ascii').split('\n')
551            self.assertEqual(len(text), 5)
552            self.assertTrue(text[0].startswith('Traceback'))
553            self.assertTrue(text[1].startswith('  File '))
554            self.assertTrue(text[3].startswith('NameError'))
555
556    def test_non_ascii(self):
557        # Mac OS X denies the creation of a file with an invalid UTF-8 name.
558        # Windows allows creating a name with an arbitrary bytes name, but
559        # Python cannot a undecodable bytes argument to a subprocess.
560        if (os_helper.TESTFN_UNDECODABLE
561        and sys.platform not in ('win32', 'darwin')):
562            name = os.fsdecode(os_helper.TESTFN_UNDECODABLE)
563        elif os_helper.TESTFN_NONASCII:
564            name = os_helper.TESTFN_NONASCII
565        else:
566            self.skipTest("need os_helper.TESTFN_NONASCII")
567
568        # Issue #16218
569        source = 'print(ascii(__file__))\n'
570        script_name = _make_test_script(os.getcwd(), name, source)
571        self.addCleanup(os_helper.unlink, script_name)
572        rc, stdout, stderr = assert_python_ok(script_name)
573        self.assertEqual(
574            ascii(script_name),
575            stdout.rstrip().decode('ascii'),
576            'stdout=%r stderr=%r' % (stdout, stderr))
577        self.assertEqual(0, rc)
578
579    def test_issue20500_exit_with_exception_value(self):
580        script = textwrap.dedent("""\
581            import sys
582            error = None
583            try:
584                raise ValueError('some text')
585            except ValueError as err:
586                error = err
587
588            if error:
589                sys.exit(error)
590            """)
591        with os_helper.temp_dir() as script_dir:
592            script_name = _make_test_script(script_dir, 'script', script)
593            exitcode, stdout, stderr = assert_python_failure(script_name)
594            text = stderr.decode('ascii')
595            self.assertEqual(text.rstrip(), "some text")
596
597    def test_syntaxerror_unindented_caret_position(self):
598        script = "1 + 1 = 2\n"
599        with os_helper.temp_dir() as script_dir:
600            script_name = _make_test_script(script_dir, 'script', script)
601            exitcode, stdout, stderr = assert_python_failure(script_name)
602            text = io.TextIOWrapper(io.BytesIO(stderr), 'ascii').read()
603            # Confirm that the caret is located under the '=' sign
604            self.assertIn("\n    ^^^^^\n", text)
605
606    def test_syntaxerror_indented_caret_position(self):
607        script = textwrap.dedent("""\
608            if True:
609                1 + 1 = 2
610            """)
611        with os_helper.temp_dir() as script_dir:
612            script_name = _make_test_script(script_dir, 'script', script)
613            exitcode, stdout, stderr = assert_python_failure(script_name)
614            text = io.TextIOWrapper(io.BytesIO(stderr), 'ascii').read()
615            # Confirm that the caret starts under the first 1 character
616            self.assertIn("\n    1 + 1 = 2\n    ^^^^^\n", text)
617
618            # Try the same with a form feed at the start of the indented line
619            script = (
620                "if True:\n"
621                "\f    1 + 1 = 2\n"
622            )
623            script_name = _make_test_script(script_dir, "script", script)
624            exitcode, stdout, stderr = assert_python_failure(script_name)
625            text = io.TextIOWrapper(io.BytesIO(stderr), "ascii").read()
626            self.assertNotIn("\f", text)
627            self.assertIn("\n    1 + 1 = 2\n    ^^^^^\n", text)
628
629    def test_syntaxerror_multi_line_fstring(self):
630        script = 'foo = f"""{}\nfoo"""\n'
631        with os_helper.temp_dir() as script_dir:
632            script_name = _make_test_script(script_dir, 'script', script)
633            exitcode, stdout, stderr = assert_python_failure(script_name)
634            self.assertEqual(
635                stderr.splitlines()[-3:],
636                [
637                    b'    foo"""',
638                    b'          ^',
639                    b'SyntaxError: f-string: empty expression not allowed',
640                ],
641            )
642
643    def test_syntaxerror_invalid_escape_sequence_multi_line(self):
644        script = 'foo = """\\q"""\n'
645        with os_helper.temp_dir() as script_dir:
646            script_name = _make_test_script(script_dir, 'script', script)
647            exitcode, stdout, stderr = assert_python_failure(
648                '-Werror', script_name,
649            )
650            self.assertEqual(
651                stderr.splitlines()[-3:],
652                [   b'    foo = """\\q"""',
653                    b'          ^^^^^^^^',
654                    b'SyntaxError: invalid escape sequence \'\\q\''
655                ],
656            )
657
658    def test_consistent_sys_path_for_direct_execution(self):
659        # This test case ensures that the following all give the same
660        # sys.path configuration:
661        #
662        #    ./python -s script_dir/__main__.py
663        #    ./python -s script_dir
664        #    ./python -I script_dir
665        script = textwrap.dedent("""\
666            import sys
667            for entry in sys.path:
668                print(entry)
669            """)
670        # Always show full path diffs on errors
671        self.maxDiff = None
672        with os_helper.temp_dir() as work_dir, os_helper.temp_dir() as script_dir:
673            script_name = _make_test_script(script_dir, '__main__', script)
674            # Reference output comes from directly executing __main__.py
675            # We omit PYTHONPATH and user site to align with isolated mode
676            p = spawn_python("-Es", script_name, cwd=work_dir)
677            out_by_name = kill_python(p).decode().splitlines()
678            self.assertEqual(out_by_name[0], script_dir)
679            self.assertNotIn(work_dir, out_by_name)
680            # Directory execution should give the same output
681            p = spawn_python("-Es", script_dir, cwd=work_dir)
682            out_by_dir = kill_python(p).decode().splitlines()
683            self.assertEqual(out_by_dir, out_by_name)
684            # As should directory execution in isolated mode
685            p = spawn_python("-I", script_dir, cwd=work_dir)
686            out_by_dir_isolated = kill_python(p).decode().splitlines()
687            self.assertEqual(out_by_dir_isolated, out_by_dir, out_by_name)
688
689    def test_consistent_sys_path_for_module_execution(self):
690        # This test case ensures that the following both give the same
691        # sys.path configuration:
692        #    ./python -sm script_pkg.__main__
693        #    ./python -sm script_pkg
694        #
695        # And that this fails as unable to find the package:
696        #    ./python -Im script_pkg
697        script = textwrap.dedent("""\
698            import sys
699            for entry in sys.path:
700                print(entry)
701            """)
702        # Always show full path diffs on errors
703        self.maxDiff = None
704        with os_helper.temp_dir() as work_dir:
705            script_dir = os.path.join(work_dir, "script_pkg")
706            os.mkdir(script_dir)
707            script_name = _make_test_script(script_dir, '__main__', script)
708            # Reference output comes from `-m script_pkg.__main__`
709            # We omit PYTHONPATH and user site to better align with the
710            # direct execution test cases
711            p = spawn_python("-sm", "script_pkg.__main__", cwd=work_dir)
712            out_by_module = kill_python(p).decode().splitlines()
713            self.assertEqual(out_by_module[0], work_dir)
714            self.assertNotIn(script_dir, out_by_module)
715            # Package execution should give the same output
716            p = spawn_python("-sm", "script_pkg", cwd=work_dir)
717            out_by_package = kill_python(p).decode().splitlines()
718            self.assertEqual(out_by_package, out_by_module)
719            # Isolated mode should fail with an import error
720            exitcode, stdout, stderr = assert_python_failure(
721                "-Im", "script_pkg", cwd=work_dir
722            )
723            traceback_lines = stderr.decode().splitlines()
724            self.assertIn("No module named script_pkg", traceback_lines[-1])
725
726    def test_nonexisting_script(self):
727        # bpo-34783: "./python script.py" must not crash
728        # if the script file doesn't exist.
729        # (Skip test for macOS framework builds because sys.executable name
730        #  is not the actual Python executable file name.
731        script = 'nonexistingscript.py'
732        self.assertFalse(os.path.exists(script))
733
734        proc = spawn_python(script, text=True,
735                            stdout=subprocess.PIPE,
736                            stderr=subprocess.PIPE)
737        out, err = proc.communicate()
738        self.assertIn(": can't open file ", err)
739        self.assertNotEqual(proc.returncode, 0)
740
741
742def tearDownModule():
743    support.reap_children()
744
745
746if __name__ == '__main__':
747    unittest.main()
748