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