test_cmd_line_script.py 23.4 KB
Newer Older
Barry Warsaw's avatar
Barry Warsaw committed
1
# tests command line execution of scripts
2

3
import contextlib
4
import importlib
5 6
import importlib.machinery
import zipimport
7
import unittest
8
import sys
9 10
import os
import os.path
Barry Warsaw's avatar
Barry Warsaw committed
11
import py_compile
12
import subprocess
Barry Warsaw's avatar
Barry Warsaw committed
13

14
import textwrap
15
from test import support
16
from test.support.script_helper import (
17
    make_pkg, make_script, make_zip_pkg, make_zip_script,
18
    assert_python_ok, assert_python_failure, spawn_python, kill_python)
19

20
verbose = support.verbose
21

22 23
example_args = ['test1', 'test2', 'test3']

24
test_source = """\
25 26 27 28
# Script may be run with optimisation enabled, so don't rely on assert
# statements being executed
def assertEqual(lhs, rhs):
    if lhs != rhs:
29
        raise AssertionError('%r != %r' % (lhs, rhs))
30 31
def assertIdentical(lhs, rhs):
    if lhs is not rhs:
32
        raise AssertionError('%r is not %r' % (lhs, rhs))
33 34 35 36 37 38 39 40
# Check basic code execution
result = ['Top level assignment']
def f():
    result.append('Lower level reference')
f()
assertEqual(result, ['Top level assignment', 'Lower level reference'])
# Check population of magic variables
assertEqual(__name__, '__main__')
41 42
from importlib.machinery import BuiltinImporter
_loader = __loader__ if __loader__ is BuiltinImporter else type(__loader__)
43
print('__loader__==%a' % _loader)
44
print('__file__==%a' % __file__)
45
print('__cached__==%a' % __cached__)
46
print('__package__==%r' % __package__)
47 48 49 50 51 52 53 54 55 56 57 58 59 60
# Check PEP 451 details
import os.path
if __package__ is not None:
    print('__main__ was located through the import system')
    assertIdentical(__spec__.loader, __loader__)
    expected_spec_name = os.path.splitext(os.path.basename(__file__))[0]
    if __package__:
        expected_spec_name = __package__ + "." + expected_spec_name
    assertEqual(__spec__.name, expected_spec_name)
    assertEqual(__spec__.parent, __package__)
    assertIdentical(__spec__.submodule_search_locations, None)
    assertEqual(__spec__.origin, __file__)
    if __spec__.cached is not None:
        assertEqual(__spec__.cached, __cached__)
61 62 63
# Check the sys module
import sys
assertIdentical(globals(), sys.modules[__name__].__dict__)
64 65 66
if __spec__ is not None:
    # XXX: We're not currently making __main__ available under its real name
    pass # assertIdentical(globals(), sys.modules[__spec__.name].__dict__)
67 68 69
from test import test_cmd_line_script
example_args_list = test_cmd_line_script.example_args
assertEqual(sys.argv[1:], example_args_list)
70 71
print('sys.argv[0]==%a' % sys.argv[0])
print('sys.path[0]==%a' % sys.path[0])
72 73
# Check the working directory
import os
74
print('cwd==%a' % os.getcwd())
75
"""
76

77
def _make_test_script(script_dir, script_basename, source=test_source):
78 79 80
    to_return = make_script(script_dir, script_basename, source)
    importlib.invalidate_caches()
    return to_return
81

82 83
def _make_test_zip_pkg(zip_dir, zip_basename, pkg_name, script_basename,
                       source=test_source, depth=1):
84 85 86 87
    to_return = make_zip_pkg(zip_dir, zip_basename, pkg_name, script_basename,
                             source, depth)
    importlib.invalidate_caches()
    return to_return
88

89 90 91 92 93 94
# There's no easy way to pass the script directory in to get
# -m to work (avoiding that is the whole point of making
# directories and zipfiles executable!)
# So we fake it for testing purposes with a custom launch script
launch_source = """\
import sys, os.path, runpy
95
sys.path.insert(0, %s)
96 97 98
runpy._run_module_as_main(%r)
"""

99 100 101 102 103 104
def _make_launch_script(script_dir, script_basename, module_name, path=None):
    if path is None:
        path = "os.path.dirname(__file__)"
    else:
        path = repr(path)
    source = launch_source % (path, module_name)
105 106 107
    to_return = make_script(script_dir, script_basename, source)
    importlib.invalidate_caches()
    return to_return
108

109
class CmdLineTest(unittest.TestCase):
110 111
    def _check_output(self, script_name, exit_code, data,
                             expected_file, expected_argv0,
112 113
                             expected_path0, expected_package,
                             expected_loader):
114
        if verbose > 1:
115
            print("Output from test script %r:" % script_name)
116
            print(repr(data))
117
        self.assertEqual(exit_code, 0)
118
        printed_loader = '__loader__==%a' % expected_loader
119
        printed_file = '__file__==%a' % expected_file
120
        printed_package = '__package__==%r' % expected_package
121 122 123
        printed_argv0 = 'sys.argv[0]==%a' % expected_argv0
        printed_path0 = 'sys.path[0]==%a' % expected_path0
        printed_cwd = 'cwd==%a' % os.getcwd()
124
        if verbose > 1:
125 126 127 128
            print('Expected output:')
            print(printed_file)
            print(printed_package)
            print(printed_argv0)
129
            print(printed_cwd)
130
        self.assertIn(printed_loader.encode('utf-8'), data)
131 132 133
        self.assertIn(printed_file.encode('utf-8'), data)
        self.assertIn(printed_package.encode('utf-8'), data)
        self.assertIn(printed_argv0.encode('utf-8'), data)
134 135 136 137 138
        self.assertIn(printed_path0.encode('utf-8'), data)
        self.assertIn(printed_cwd.encode('utf-8'), data)

    def _check_script(self, script_name, expected_file,
                            expected_argv0, expected_path0,
139
                            expected_package, expected_loader,
140
                            *cmd_line_switches):
141 142
        run_args = [*support.optim_args_from_interpreter_flags(),
                    *cmd_line_switches, script_name, *example_args]
143
        rc, out, err = assert_python_ok(*run_args, __isolated=False)
144
        self._check_output(script_name, rc, out + err, expected_file,
145 146
                           expected_argv0, expected_path0,
                           expected_package, expected_loader)
147

148 149 150
    def _check_import_error(self, script_name, expected_msg,
                            *cmd_line_switches):
        run_args = cmd_line_switches + (script_name,)
151
        rc, out, err = assert_python_failure(*run_args)
152
        if verbose > 1:
153
            print('Output from test script %r:' % script_name)
154
            print(repr(err))
155
            print('Expected output: %r' % expected_msg)
156
        self.assertIn(expected_msg.encode('utf-8'), err)
157

158 159 160 161 162 163
    def test_dash_c_loader(self):
        rc, out, err = assert_python_ok("-c", "print(__loader__)")
        expected = repr(importlib.machinery.BuiltinImporter).encode("utf-8")
        self.assertIn(expected, out)

    def test_stdin_loader(self):
164 165
        # Unfortunately, there's no way to automatically test the fully
        # interactive REPL, since that code path only gets executed when
166
        # stdin is an interactive tty.
167 168 169 170 171 172 173 174 175
        p = spawn_python()
        try:
            p.stdin.write(b"print(__loader__)\n")
            p.stdin.flush()
        finally:
            out = kill_python(p)
        expected = repr(importlib.machinery.BuiltinImporter).encode("utf-8")
        self.assertIn(expected, out)

176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
    @contextlib.contextmanager
    def interactive_python(self, separate_stderr=False):
        if separate_stderr:
            p = spawn_python('-i', bufsize=1, stderr=subprocess.PIPE)
            stderr = p.stderr
        else:
            p = spawn_python('-i', bufsize=1, stderr=subprocess.STDOUT)
            stderr = p.stdout
        try:
            # Drain stderr until prompt
            while True:
                data = stderr.read(4)
                if data == b">>> ":
                    break
                stderr.readline()
            yield p
        finally:
            kill_python(p)
            stderr.close()

    def check_repl_stdout_flush(self, separate_stderr=False):
        with self.interactive_python(separate_stderr) as p:
            p.stdin.write(b"print('foo')\n")
            p.stdin.flush()
            self.assertEqual(b'foo', p.stdout.readline().strip())

    def check_repl_stderr_flush(self, separate_stderr=False):
        with self.interactive_python(separate_stderr) as p:
            p.stdin.write(b"1/0\n")
            p.stdin.flush()
            stderr = p.stderr if separate_stderr else p.stdout
            self.assertIn(b'Traceback ', stderr.readline())
            self.assertIn(b'File "<stdin>"', stderr.readline())
            self.assertIn(b'ZeroDivisionError', stderr.readline())

    def test_repl_stdout_flush(self):
        self.check_repl_stdout_flush()

    def test_repl_stdout_flush_separate_stderr(self):
        self.check_repl_stdout_flush(True)

    def test_repl_stderr_flush(self):
        self.check_repl_stderr_flush()

    def test_repl_stderr_flush_separate_stderr(self):
        self.check_repl_stderr_flush(True)

223
    def test_basic_script(self):
224
        with support.temp_dir() as script_dir:
225
            script_name = _make_test_script(script_dir, 'script')
226
            self._check_script(script_name, script_name, script_name,
227 228
                               script_dir, None,
                               importlib.machinery.SourceFileLoader)
229 230

    def test_script_compiled(self):
231
        with support.temp_dir() as script_dir:
232
            script_name = _make_test_script(script_dir, 'script')
233
            py_compile.compile(script_name, doraise=True)
234
            os.remove(script_name)
235 236
            pyc_file = support.make_legacy_pyc(script_name)
            self._check_script(pyc_file, pyc_file,
237 238
                               pyc_file, script_dir, None,
                               importlib.machinery.SourcelessFileLoader)
239 240

    def test_directory(self):
241
        with support.temp_dir() as script_dir:
242
            script_name = _make_test_script(script_dir, '__main__')
243
            self._check_script(script_dir, script_name, script_dir,
244 245
                               script_dir, '',
                               importlib.machinery.SourceFileLoader)
246 247

    def test_directory_compiled(self):
248
        with support.temp_dir() as script_dir:
249
            script_name = _make_test_script(script_dir, '__main__')
250
            py_compile.compile(script_name, doraise=True)
251
            os.remove(script_name)
252 253
            pyc_file = support.make_legacy_pyc(script_name)
            self._check_script(script_dir, pyc_file, script_dir,
254 255
                               script_dir, '',
                               importlib.machinery.SourcelessFileLoader)
256

257
    def test_directory_error(self):
258
        with support.temp_dir() as script_dir:
Nick Coghlan's avatar
Nick Coghlan committed
259
            msg = "can't find '__main__' module in %r" % script_dir
260 261
            self._check_import_error(script_dir, msg)

262
    def test_zipfile(self):
263
        with support.temp_dir() as script_dir:
264
            script_name = _make_test_script(script_dir, '__main__')
Nick Coghlan's avatar
Nick Coghlan committed
265
            zip_name, run_name = make_zip_script(script_dir, 'test_zip', script_name)
266 267
            self._check_script(zip_name, run_name, zip_name, zip_name, '',
                               zipimport.zipimporter)
268 269

    def test_zipfile_compiled(self):
270
        with support.temp_dir() as script_dir:
271
            script_name = _make_test_script(script_dir, '__main__')
Barry Warsaw's avatar
Barry Warsaw committed
272
            compiled_name = py_compile.compile(script_name, doraise=True)
Nick Coghlan's avatar
Nick Coghlan committed
273
            zip_name, run_name = make_zip_script(script_dir, 'test_zip', compiled_name)
274 275
            self._check_script(zip_name, run_name, zip_name, zip_name, '',
                               zipimport.zipimporter)
276

277
    def test_zipfile_error(self):
278
        with support.temp_dir() as script_dir:
279
            script_name = _make_test_script(script_dir, 'not_main')
Nick Coghlan's avatar
Nick Coghlan committed
280 281
            zip_name, run_name = make_zip_script(script_dir, 'test_zip', script_name)
            msg = "can't find '__main__' module in %r" % zip_name
282 283
            self._check_import_error(zip_name, msg)

284
    def test_module_in_package(self):
285
        with support.temp_dir() as script_dir:
286
            pkg_dir = os.path.join(script_dir, 'test_pkg')
Nick Coghlan's avatar
Nick Coghlan committed
287
            make_pkg(pkg_dir)
288 289
            script_name = _make_test_script(pkg_dir, 'script')
            launch_name = _make_launch_script(script_dir, 'launch', 'test_pkg.script')
290 291 292
            self._check_script(launch_name, script_name, script_name,
                               script_dir, 'test_pkg',
                               importlib.machinery.SourceFileLoader)
293 294

    def test_module_in_package_in_zipfile(self):
295
        with support.temp_dir() as script_dir:
296 297
            zip_name, run_name = _make_test_zip_pkg(script_dir, 'test_zip', 'test_pkg', 'script')
            launch_name = _make_launch_script(script_dir, 'launch', 'test_pkg.script', zip_name)
298 299
            self._check_script(launch_name, run_name, run_name,
                               zip_name, 'test_pkg', zipimport.zipimporter)
300 301

    def test_module_in_subpackage_in_zipfile(self):
302
        with support.temp_dir() as script_dir:
303 304
            zip_name, run_name = _make_test_zip_pkg(script_dir, 'test_zip', 'test_pkg', 'script', depth=2)
            launch_name = _make_launch_script(script_dir, 'launch', 'test_pkg.test_pkg.script', zip_name)
305 306 307
            self._check_script(launch_name, run_name, run_name,
                               zip_name, 'test_pkg.test_pkg',
                               zipimport.zipimporter)
308

309
    def test_package(self):
310
        with support.temp_dir() as script_dir:
311
            pkg_dir = os.path.join(script_dir, 'test_pkg')
Nick Coghlan's avatar
Nick Coghlan committed
312
            make_pkg(pkg_dir)
313 314 315
            script_name = _make_test_script(pkg_dir, '__main__')
            launch_name = _make_launch_script(script_dir, 'launch', 'test_pkg')
            self._check_script(launch_name, script_name,
316 317
                               script_name, script_dir, 'test_pkg',
                               importlib.machinery.SourceFileLoader)
318 319

    def test_package_compiled(self):
320
        with support.temp_dir() as script_dir:
321
            pkg_dir = os.path.join(script_dir, 'test_pkg')
Nick Coghlan's avatar
Nick Coghlan committed
322
            make_pkg(pkg_dir)
323
            script_name = _make_test_script(pkg_dir, '__main__')
Barry Warsaw's avatar
Barry Warsaw committed
324
            compiled_name = py_compile.compile(script_name, doraise=True)
325
            os.remove(script_name)
326
            pyc_file = support.make_legacy_pyc(script_name)
327
            launch_name = _make_launch_script(script_dir, 'launch', 'test_pkg')
Barry Warsaw's avatar
Barry Warsaw committed
328
            self._check_script(launch_name, pyc_file,
329 330
                               pyc_file, script_dir, 'test_pkg',
                               importlib.machinery.SourcelessFileLoader)
331 332

    def test_package_error(self):
333
        with support.temp_dir() as script_dir:
334
            pkg_dir = os.path.join(script_dir, 'test_pkg')
Nick Coghlan's avatar
Nick Coghlan committed
335
            make_pkg(pkg_dir)
336 337 338 339 340 341
            msg = ("'test_pkg' is a package and cannot "
                   "be directly executed")
            launch_name = _make_launch_script(script_dir, 'launch', 'test_pkg')
            self._check_import_error(launch_name, msg)

    def test_package_recursion(self):
342
        with support.temp_dir() as script_dir:
343
            pkg_dir = os.path.join(script_dir, 'test_pkg')
Nick Coghlan's avatar
Nick Coghlan committed
344
            make_pkg(pkg_dir)
345
            main_dir = os.path.join(pkg_dir, '__main__')
Nick Coghlan's avatar
Nick Coghlan committed
346
            make_pkg(main_dir)
347 348 349 350 351 352
            msg = ("Cannot use package as __main__ module; "
                   "'test_pkg' is a package and cannot "
                   "be directly executed")
            launch_name = _make_launch_script(script_dir, 'launch', 'test_pkg')
            self._check_import_error(launch_name, msg)

353 354 355
    def test_issue8202(self):
        # Make sure package __init__ modules see "-m" in sys.argv0 while
        # searching for the module to execute
356
        with support.temp_dir() as script_dir:
357
            with support.change_cwd(path=script_dir):
358 359 360
                pkg_dir = os.path.join(script_dir, 'test_pkg')
                make_pkg(pkg_dir, "import sys; print('init_argv0==%r' % sys.argv[0])")
                script_name = _make_test_script(pkg_dir, 'script')
361
                rc, out, err = assert_python_ok('-m', 'test_pkg.script', *example_args, __isolated=False)
362
                if verbose > 1:
363
                    print(repr(out))
364
                expected = "init_argv0==%r" % '-m'
365 366
                self.assertIn(expected.encode('utf-8'), out)
                self._check_output(script_name, rc, out,
367 368
                                   script_name, script_name, '', 'test_pkg',
                                   importlib.machinery.SourceFileLoader)
369 370 371 372

    def test_issue8202_dash_c_file_ignored(self):
        # Make sure a "-c" file in the current directory
        # does not alter the value of sys.path[0]
373
        with support.temp_dir() as script_dir:
374
            with support.change_cwd(path=script_dir):
375 376
                with open("-c", "w") as f:
                    f.write("data")
377
                    rc, out, err = assert_python_ok('-c',
378 379
                        'import sys; print("sys.path[0]==%r" % sys.path[0])',
                        __isolated=False)
380
                    if verbose > 1:
381
                        print(repr(out))
382
                    expected = "sys.path[0]==%r" % ''
383
                    self.assertIn(expected.encode('utf-8'), out)
384 385 386 387

    def test_issue8202_dash_m_file_ignored(self):
        # Make sure a "-m" file in the current directory
        # does not alter the value of sys.path[0]
388
        with support.temp_dir() as script_dir:
389
            script_name = _make_test_script(script_dir, 'other')
390
            with support.change_cwd(path=script_dir):
391 392
                with open("-m", "w") as f:
                    f.write("data")
393 394
                    rc, out, err = assert_python_ok('-m', 'other', *example_args,
                                                    __isolated=False)
395
                    self._check_output(script_name, rc, out,
396 397
                                      script_name, script_name, '', '',
                                      importlib.machinery.SourceFileLoader)
398

399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
    @contextlib.contextmanager
    def setup_test_pkg(self, *args):
        with support.temp_dir() as script_dir, \
                support.change_cwd(path=script_dir):
            pkg_dir = os.path.join(script_dir, 'test_pkg')
            make_pkg(pkg_dir, *args)
            yield pkg_dir

    def check_dash_m_failure(self, *args):
        rc, out, err = assert_python_failure('-m', *args, __isolated=False)
        if verbose > 1:
            print(repr(out))
        self.assertEqual(rc, 1)
        return err

414 415 416 417
    def test_dash_m_error_code_is_one(self):
        # If a module is invoked with the -m command line flag
        # and results in an error that the return code to the
        # shell is '1'
418 419 420 421 422 423 424 425 426 427
        with self.setup_test_pkg() as pkg_dir:
            script_name = _make_test_script(pkg_dir, 'other',
                                            "if __name__ == '__main__': raise ValueError")
            err = self.check_dash_m_failure('test_pkg.other', *example_args)
            self.assertIn(b'ValueError', err)

    def test_dash_m_errors(self):
        # Exercise error reporting for various invalid package executions
        tests = (
            ('builtins', br'No code object available'),
428 429 430
            ('builtins.x', br'Error while finding module specification.*'
                br'AttributeError'),
            ('builtins.x.y', br'Error while finding module specification.*'
431 432 433 434 435
                br'ImportError.*No module named.*not a package'),
            ('os.path', br'loader.*cannot handle'),
            ('importlib', br'No module named.*'
                br'is a package and cannot be directly executed'),
            ('importlib.nonexistant', br'No module named'),
436
            ('.unittest', br'Relative module names not supported'),
437 438 439 440
        )
        for name, regex in tests:
            with self.subTest(name):
                rc, _, err = assert_python_failure('-m', name)
441
                self.assertEqual(rc, 1)
442 443 444
                self.assertRegex(err, regex)
                self.assertNotIn(b'Traceback', err)

445 446 447 448 449 450 451 452
    def test_dash_m_bad_pyc(self):
        with support.temp_dir() as script_dir, \
                support.change_cwd(path=script_dir):
            os.mkdir('test_pkg')
            # Create invalid *.pyc as empty file
            with open('test_pkg/__init__.pyc', 'wb'):
                pass
            err = self.check_dash_m_failure('test_pkg')
453 454
            self.assertRegex(err,
                br'Error while finding module specification.*'
455 456 457 458
                br'ImportError.*bad magic number')
            self.assertNotIn(b'is a package', err)
            self.assertNotIn(b'Traceback', err)

459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
    def test_dash_m_init_traceback(self):
        # These were wrapped in an ImportError and tracebacks were
        # suppressed; see Issue 14285
        exceptions = (ImportError, AttributeError, TypeError, ValueError)
        for exception in exceptions:
            exception = exception.__name__
            init = "raise {0}('Exception in __init__.py')".format(exception)
            with self.subTest(exception), \
                    self.setup_test_pkg(init) as pkg_dir:
                err = self.check_dash_m_failure('test_pkg')
                self.assertIn(exception.encode('ascii'), err)
                self.assertIn(b'Exception in __init__.py', err)
                self.assertIn(b'Traceback', err)

    def test_dash_m_main_traceback(self):
        # Ensure that an ImportError's traceback is reported
        with self.setup_test_pkg() as pkg_dir:
            main = "raise ImportError('Exception in __main__ module')"
            _make_test_script(pkg_dir, '__main__', main)
            err = self.check_dash_m_failure('test_pkg')
            self.assertIn(b'ImportError', err)
            self.assertIn(b'Exception in __main__ module', err)
            self.assertIn(b'Traceback', err)
482

483 484 485 486 487 488 489 490 491
    def test_pep_409_verbiage(self):
        # Make sure PEP 409 syntax properly suppresses
        # the context of an exception
        script = textwrap.dedent("""\
            try:
                raise ValueError
            except:
                raise NameError from None
            """)
492
        with support.temp_dir() as script_dir:
493 494 495 496 497 498 499 500
            script_name = _make_test_script(script_dir, 'script', script)
            exitcode, stdout, stderr = assert_python_failure(script_name)
            text = stderr.decode('ascii').split('\n')
            self.assertEqual(len(text), 4)
            self.assertTrue(text[0].startswith('Traceback'))
            self.assertTrue(text[1].startswith('  File '))
            self.assertTrue(text[3].startswith('NameError'))

501 502
    def test_non_ascii(self):
        # Mac OS X denies the creation of a file with an invalid UTF-8 name.
503
        # Windows allows creating a name with an arbitrary bytes name, but
504
        # Python cannot a undecodable bytes argument to a subprocess.
505 506 507 508
        if (support.TESTFN_UNDECODABLE
        and sys.platform not in ('win32', 'darwin')):
            name = os.fsdecode(support.TESTFN_UNDECODABLE)
        elif support.TESTFN_NONASCII:
509 510 511 512
            name = support.TESTFN_NONASCII
        else:
            self.skipTest("need support.TESTFN_NONASCII")

513
        # Issue #16218
514 515 516 517 518 519 520 521 522 523
        source = 'print(ascii(__file__))\n'
        script_name = _make_test_script(os.curdir, name, source)
        self.addCleanup(support.unlink, script_name)
        rc, stdout, stderr = assert_python_ok(script_name)
        self.assertEqual(
            ascii(script_name),
            stdout.rstrip().decode('ascii'),
            'stdout=%r stderr=%r' % (stdout, stderr))
        self.assertEqual(0, rc)

524 525 526 527 528 529 530 531 532 533 534 535
    def test_issue20500_exit_with_exception_value(self):
        script = textwrap.dedent("""\
            import sys
            error = None
            try:
                raise ValueError('some text')
            except ValueError as err:
                error = err

            if error:
                sys.exit(error)
            """)
536
        with support.temp_dir() as script_dir:
537 538 539 540 541
            script_name = _make_test_script(script_dir, 'script', script)
            exitcode, stdout, stderr = assert_python_failure(script_name)
            text = stderr.decode('ascii')
            self.assertEqual(text, "some text")

542

543
def test_main():
544 545
    support.run_unittest(CmdLineTest)
    support.reap_children()
546

547
if __name__ == '__main__':
548
    test_main()