test_cmd_line_script.py 20.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
        if not __debug__:
            cmd_line_switches += ('-' + 'O' * sys.flags.optimize,)
143
        run_args = cmd_line_switches + (script_name,) + tuple(example_args)
144
        rc, out, err = assert_python_ok(*run_args, __isolated=False)
145
        self._check_output(script_name, rc, out + err, expected_file,
146 147
                           expected_argv0, expected_path0,
                           expected_package, expected_loader)
148

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

159 160 161 162 163 164
    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):
165 166
        # Unfortunately, there's no way to automatically test the fully
        # interactive REPL, since that code path only gets executed when
167
        # stdin is an interactive tty.
168 169 170 171 172 173 174 175 176
        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)

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 223
    @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)

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

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

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

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

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

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

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

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

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

    def test_module_in_package_in_zipfile(self):
296
        with support.temp_dir() as script_dir:
297 298
            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)
299 300
            self._check_script(launch_name, run_name, run_name,
                               zip_name, 'test_pkg', zipimport.zipimporter)
301 302

    def test_module_in_subpackage_in_zipfile(self):
303
        with support.temp_dir() as script_dir:
304 305
            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)
306 307 308
            self._check_script(launch_name, run_name, run_name,
                               zip_name, 'test_pkg.test_pkg',
                               zipimport.zipimporter)
309

310
    def test_package(self):
311
        with support.temp_dir() as script_dir:
312
            pkg_dir = os.path.join(script_dir, 'test_pkg')
Nick Coghlan's avatar
Nick Coghlan committed
313
            make_pkg(pkg_dir)
314 315 316
            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,
317 318
                               script_name, script_dir, 'test_pkg',
                               importlib.machinery.SourceFileLoader)
319 320

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

    def test_package_error(self):
334
        with support.temp_dir() as script_dir:
335
            pkg_dir = os.path.join(script_dir, 'test_pkg')
Nick Coghlan's avatar
Nick Coghlan committed
336
            make_pkg(pkg_dir)
337 338 339 340 341 342
            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):
343
        with support.temp_dir() as script_dir:
344
            pkg_dir = os.path.join(script_dir, 'test_pkg')
Nick Coghlan's avatar
Nick Coghlan committed
345
            make_pkg(pkg_dir)
346
            main_dir = os.path.join(pkg_dir, '__main__')
Nick Coghlan's avatar
Nick Coghlan committed
347
            make_pkg(main_dir)
348 349 350 351 352 353
            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)

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

    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]
374
        with support.temp_dir() as script_dir:
375
            with support.change_cwd(path=script_dir):
376 377
                with open("-c", "w") as f:
                    f.write("data")
378
                    rc, out, err = assert_python_ok('-c',
379 380
                        'import sys; print("sys.path[0]==%r" % sys.path[0])',
                        __isolated=False)
381
                    if verbose > 1:
382
                        print(repr(out))
383
                    expected = "sys.path[0]==%r" % ''
384
                    self.assertIn(expected.encode('utf-8'), out)
385 386 387 388

    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]
389
        with support.temp_dir() as script_dir:
390
            script_name = _make_test_script(script_dir, 'other')
391
            with support.change_cwd(path=script_dir):
392 393
                with open("-m", "w") as f:
                    f.write("data")
394 395
                    rc, out, err = assert_python_ok('-m', 'other', *example_args,
                                                    __isolated=False)
396
                    self._check_output(script_name, rc, out,
397 398
                                      script_name, script_name, '', '',
                                      importlib.machinery.SourceFileLoader)
399

400 401 402 403
    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'
404
        with support.temp_dir() as script_dir:
405
            with support.change_cwd(path=script_dir):
406 407 408 409 410 411
                pkg_dir = os.path.join(script_dir, 'test_pkg')
                make_pkg(pkg_dir)
                script_name = _make_test_script(pkg_dir, 'other',
                                                "if __name__ == '__main__': raise ValueError")
                rc, out, err = assert_python_failure('-m', 'test_pkg.other', *example_args)
                if verbose > 1:
412
                    print(repr(out))
413 414
                self.assertEqual(rc, 1)

415 416 417 418 419 420 421 422 423
    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
            """)
424
        with support.temp_dir() as script_dir:
425 426 427 428 429 430 431 432
            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'))

433 434 435 436
    def test_non_ascii(self):
        # Mac OS X denies the creation of a file with an invalid UTF-8 name.
        # Windows allows to create a name with an arbitrary bytes name, but
        # Python cannot a undecodable bytes argument to a subprocess.
437 438 439 440
        if (support.TESTFN_UNDECODABLE
        and sys.platform not in ('win32', 'darwin')):
            name = os.fsdecode(support.TESTFN_UNDECODABLE)
        elif support.TESTFN_NONASCII:
441 442 443 444
            name = support.TESTFN_NONASCII
        else:
            self.skipTest("need support.TESTFN_NONASCII")

445
        # Issue #16218
446 447 448 449 450 451 452 453 454 455
        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)

456 457 458 459 460 461 462 463 464 465 466 467
    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)
            """)
468
        with support.temp_dir() as script_dir:
469 470 471 472 473
            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")

474

475
def test_main():
476 477
    support.run_unittest(CmdLineTest)
    support.reap_children()
478

479
if __name__ == '__main__':
480
    test_main()