Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_cmd_line_script.py @ 70817:dd775b1561ba
Fix markup.
| author | Georg Brandl <georg@python.org> |
|---|---|
| date | Tue, 14 Jun 2011 21:09:55 +0200 |
| parents | 3c9232a684ae |
| children | c4c67c2d8ffc |
| rev | line source |
|---|---|
| 60648 | 1 # tests command line execution of scripts |
|
43897
41f7c13ef161
Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
diff
changeset
|
2 |
|
41f7c13ef161
Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
diff
changeset
|
3 import unittest |
|
65300
660afdaeb2d6
#9964: fix running test_cmd_line_script under -O and -OO.
Georg Brandl <georg@python.org>
parents:
65182
diff
changeset
|
4 import sys |
|
43897
41f7c13ef161
Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
diff
changeset
|
5 import os |
|
41f7c13ef161
Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
diff
changeset
|
6 import os.path |
|
41f7c13ef161
Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
diff
changeset
|
7 import py_compile |
|
41f7c13ef161
Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
diff
changeset
|
8 |
|
64110
212a16f906a6
Issue #8202: Set sys.argv[0] to -m rather than -c while searching for the module to execute. Also updates all the cmd_line_script tests to validate the setting of sys.path[0] and the current working directory
Nick Coghlan <ncoghlan@gmail.com>
parents:
60648
diff
changeset
|
9 from test import support |
| 60648 | 10 from test.script_helper import ( |
|
65182
b1fb0539bf98
Better Python spawning primitives in test.script_helper, for
Antoine Pitrou <solipsis@pitrou.net>
parents:
64110
diff
changeset
|
11 make_pkg, make_script, make_zip_pkg, make_zip_script, |
|
b1fb0539bf98
Better Python spawning primitives in test.script_helper, for
Antoine Pitrou <solipsis@pitrou.net>
parents:
64110
diff
changeset
|
12 assert_python_ok, assert_python_failure, temp_dir) |
|
43897
41f7c13ef161
Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
diff
changeset
|
13 |
|
64110
212a16f906a6
Issue #8202: Set sys.argv[0] to -m rather than -c while searching for the module to execute. Also updates all the cmd_line_script tests to validate the setting of sys.path[0] and the current working directory
Nick Coghlan <ncoghlan@gmail.com>
parents:
60648
diff
changeset
|
14 verbose = support.verbose |
|
43897
41f7c13ef161
Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
diff
changeset
|
15 |
|
44129
b637349336fd
Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43912
diff
changeset
|
16 test_source = """\ |
|
43897
41f7c13ef161
Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
diff
changeset
|
17 # Script may be run with optimisation enabled, so don't rely on assert |
|
41f7c13ef161
Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
diff
changeset
|
18 # statements being executed |
|
41f7c13ef161
Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
diff
changeset
|
19 def assertEqual(lhs, rhs): |
|
41f7c13ef161
Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
diff
changeset
|
20 if lhs != rhs: |
|
44129
b637349336fd
Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43912
diff
changeset
|
21 raise AssertionError('%r != %r' % (lhs, rhs)) |
|
43897
41f7c13ef161
Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
diff
changeset
|
22 def assertIdentical(lhs, rhs): |
|
41f7c13ef161
Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
diff
changeset
|
23 if lhs is not rhs: |
|
44129
b637349336fd
Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43912
diff
changeset
|
24 raise AssertionError('%r is not %r' % (lhs, rhs)) |
|
43897
41f7c13ef161
Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
diff
changeset
|
25 # Check basic code execution |
|
41f7c13ef161
Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
diff
changeset
|
26 result = ['Top level assignment'] |
|
41f7c13ef161
Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
diff
changeset
|
27 def f(): |
|
41f7c13ef161
Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
diff
changeset
|
28 result.append('Lower level reference') |
|
41f7c13ef161
Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
diff
changeset
|
29 f() |
|
41f7c13ef161
Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
diff
changeset
|
30 assertEqual(result, ['Top level assignment', 'Lower level reference']) |
|
41f7c13ef161
Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
diff
changeset
|
31 # Check population of magic variables |
|
41f7c13ef161
Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
diff
changeset
|
32 assertEqual(__name__, '__main__') |
|
65424
0505eba62347
test_cmd_line_script: format paths with ascii() instead of repr()
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65300
diff
changeset
|
33 print('__file__==%a' % __file__) |
| 60648 | 34 assertEqual(__cached__, None) |
|
44129
b637349336fd
Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43912
diff
changeset
|
35 print('__package__==%r' % __package__) |
|
43897
41f7c13ef161
Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
diff
changeset
|
36 # Check the sys module |
|
41f7c13ef161
Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
diff
changeset
|
37 import sys |
|
41f7c13ef161
Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
diff
changeset
|
38 assertIdentical(globals(), sys.modules[__name__].__dict__) |
|
65424
0505eba62347
test_cmd_line_script: format paths with ascii() instead of repr()
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65300
diff
changeset
|
39 print('sys.argv[0]==%a' % sys.argv[0]) |
|
0505eba62347
test_cmd_line_script: format paths with ascii() instead of repr()
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65300
diff
changeset
|
40 print('sys.path[0]==%a' % sys.path[0]) |
|
64110
212a16f906a6
Issue #8202: Set sys.argv[0] to -m rather than -c while searching for the module to execute. Also updates all the cmd_line_script tests to validate the setting of sys.path[0] and the current working directory
Nick Coghlan <ncoghlan@gmail.com>
parents:
60648
diff
changeset
|
41 # Check the working directory |
|
212a16f906a6
Issue #8202: Set sys.argv[0] to -m rather than -c while searching for the module to execute. Also updates all the cmd_line_script tests to validate the setting of sys.path[0] and the current working directory
Nick Coghlan <ncoghlan@gmail.com>
parents:
60648
diff
changeset
|
42 import os |
|
65424
0505eba62347
test_cmd_line_script: format paths with ascii() instead of repr()
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65300
diff
changeset
|
43 print('cwd==%a' % os.getcwd()) |
|
44129
b637349336fd
Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43912
diff
changeset
|
44 """ |
|
43897
41f7c13ef161
Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
diff
changeset
|
45 |
|
44129
b637349336fd
Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43912
diff
changeset
|
46 def _make_test_script(script_dir, script_basename, source=test_source): |
|
57291
fd417cd64341
Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
55593
diff
changeset
|
47 return make_script(script_dir, script_basename, source) |
|
44129
b637349336fd
Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43912
diff
changeset
|
48 |
|
50164
252927a2f32d
Merged revisions 67753 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
47201
diff
changeset
|
49 def _make_test_zip_pkg(zip_dir, zip_basename, pkg_name, script_basename, |
|
252927a2f32d
Merged revisions 67753 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
47201
diff
changeset
|
50 source=test_source, depth=1): |
|
57291
fd417cd64341
Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
55593
diff
changeset
|
51 return make_zip_pkg(zip_dir, zip_basename, pkg_name, script_basename, |
|
fd417cd64341
Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
55593
diff
changeset
|
52 source, depth) |
|
50164
252927a2f32d
Merged revisions 67753 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
47201
diff
changeset
|
53 |
|
44129
b637349336fd
Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43912
diff
changeset
|
54 # There's no easy way to pass the script directory in to get |
|
b637349336fd
Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43912
diff
changeset
|
55 # -m to work (avoiding that is the whole point of making |
|
b637349336fd
Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43912
diff
changeset
|
56 # directories and zipfiles executable!) |
|
b637349336fd
Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43912
diff
changeset
|
57 # So we fake it for testing purposes with a custom launch script |
|
b637349336fd
Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43912
diff
changeset
|
58 launch_source = """\ |
|
b637349336fd
Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43912
diff
changeset
|
59 import sys, os.path, runpy |
|
50164
252927a2f32d
Merged revisions 67753 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
47201
diff
changeset
|
60 sys.path.insert(0, %s) |
|
44129
b637349336fd
Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43912
diff
changeset
|
61 runpy._run_module_as_main(%r) |
|
b637349336fd
Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43912
diff
changeset
|
62 """ |
|
b637349336fd
Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43912
diff
changeset
|
63 |
|
50164
252927a2f32d
Merged revisions 67753 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
47201
diff
changeset
|
64 def _make_launch_script(script_dir, script_basename, module_name, path=None): |
|
252927a2f32d
Merged revisions 67753 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
47201
diff
changeset
|
65 if path is None: |
|
252927a2f32d
Merged revisions 67753 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
47201
diff
changeset
|
66 path = "os.path.dirname(__file__)" |
|
252927a2f32d
Merged revisions 67753 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
47201
diff
changeset
|
67 else: |
|
252927a2f32d
Merged revisions 67753 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
47201
diff
changeset
|
68 path = repr(path) |
|
252927a2f32d
Merged revisions 67753 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
47201
diff
changeset
|
69 source = launch_source % (path, module_name) |
|
57291
fd417cd64341
Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
55593
diff
changeset
|
70 return make_script(script_dir, script_basename, source) |
|
44129
b637349336fd
Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43912
diff
changeset
|
71 |
|
43897
41f7c13ef161
Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
diff
changeset
|
72 class CmdLineTest(unittest.TestCase): |
|
64110
212a16f906a6
Issue #8202: Set sys.argv[0] to -m rather than -c while searching for the module to execute. Also updates all the cmd_line_script tests to validate the setting of sys.path[0] and the current working directory
Nick Coghlan <ncoghlan@gmail.com>
parents:
60648
diff
changeset
|
73 def _check_output(self, script_name, exit_code, data, |
|
212a16f906a6
Issue #8202: Set sys.argv[0] to -m rather than -c while searching for the module to execute. Also updates all the cmd_line_script tests to validate the setting of sys.path[0] and the current working directory
Nick Coghlan <ncoghlan@gmail.com>
parents:
60648
diff
changeset
|
74 expected_file, expected_argv0, |
|
212a16f906a6
Issue #8202: Set sys.argv[0] to -m rather than -c while searching for the module to execute. Also updates all the cmd_line_script tests to validate the setting of sys.path[0] and the current working directory
Nick Coghlan <ncoghlan@gmail.com>
parents:
60648
diff
changeset
|
75 expected_path0, expected_package): |
|
212a16f906a6
Issue #8202: Set sys.argv[0] to -m rather than -c while searching for the module to execute. Also updates all the cmd_line_script tests to validate the setting of sys.path[0] and the current working directory
Nick Coghlan <ncoghlan@gmail.com>
parents:
60648
diff
changeset
|
76 if verbose > 1: |
|
43912
4747ef9640ae
Merged revisions 59041-59055 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
43908
diff
changeset
|
77 print("Output from test script %r:" % script_name) |
|
4747ef9640ae
Merged revisions 59041-59055 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
43908
diff
changeset
|
78 print(data) |
|
44129
b637349336fd
Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43912
diff
changeset
|
79 self.assertEqual(exit_code, 0) |
|
65424
0505eba62347
test_cmd_line_script: format paths with ascii() instead of repr()
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65300
diff
changeset
|
80 printed_file = '__file__==%a' % expected_file |
|
44129
b637349336fd
Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43912
diff
changeset
|
81 printed_package = '__package__==%r' % expected_package |
|
65424
0505eba62347
test_cmd_line_script: format paths with ascii() instead of repr()
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65300
diff
changeset
|
82 printed_argv0 = 'sys.argv[0]==%a' % expected_argv0 |
|
0505eba62347
test_cmd_line_script: format paths with ascii() instead of repr()
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65300
diff
changeset
|
83 printed_path0 = 'sys.path[0]==%a' % expected_path0 |
|
0505eba62347
test_cmd_line_script: format paths with ascii() instead of repr()
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65300
diff
changeset
|
84 printed_cwd = 'cwd==%a' % os.getcwd() |
|
64110
212a16f906a6
Issue #8202: Set sys.argv[0] to -m rather than -c while searching for the module to execute. Also updates all the cmd_line_script tests to validate the setting of sys.path[0] and the current working directory
Nick Coghlan <ncoghlan@gmail.com>
parents:
60648
diff
changeset
|
85 if verbose > 1: |
|
44129
b637349336fd
Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43912
diff
changeset
|
86 print('Expected output:') |
|
b637349336fd
Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43912
diff
changeset
|
87 print(printed_file) |
|
b637349336fd
Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43912
diff
changeset
|
88 print(printed_package) |
|
b637349336fd
Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43912
diff
changeset
|
89 print(printed_argv0) |
|
64110
212a16f906a6
Issue #8202: Set sys.argv[0] to -m rather than -c while searching for the module to execute. Also updates all the cmd_line_script tests to validate the setting of sys.path[0] and the current working directory
Nick Coghlan <ncoghlan@gmail.com>
parents:
60648
diff
changeset
|
90 print(printed_cwd) |
|
58415
f6a626f34840
use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents:
57291
diff
changeset
|
91 self.assertIn(printed_file.encode('utf-8'), data) |
|
f6a626f34840
use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents:
57291
diff
changeset
|
92 self.assertIn(printed_package.encode('utf-8'), data) |
|
f6a626f34840
use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents:
57291
diff
changeset
|
93 self.assertIn(printed_argv0.encode('utf-8'), data) |
|
64110
212a16f906a6
Issue #8202: Set sys.argv[0] to -m rather than -c while searching for the module to execute. Also updates all the cmd_line_script tests to validate the setting of sys.path[0] and the current working directory
Nick Coghlan <ncoghlan@gmail.com>
parents:
60648
diff
changeset
|
94 self.assertIn(printed_path0.encode('utf-8'), data) |
|
212a16f906a6
Issue #8202: Set sys.argv[0] to -m rather than -c while searching for the module to execute. Also updates all the cmd_line_script tests to validate the setting of sys.path[0] and the current working directory
Nick Coghlan <ncoghlan@gmail.com>
parents:
60648
diff
changeset
|
95 self.assertIn(printed_cwd.encode('utf-8'), data) |
|
212a16f906a6
Issue #8202: Set sys.argv[0] to -m rather than -c while searching for the module to execute. Also updates all the cmd_line_script tests to validate the setting of sys.path[0] and the current working directory
Nick Coghlan <ncoghlan@gmail.com>
parents:
60648
diff
changeset
|
96 |
|
212a16f906a6
Issue #8202: Set sys.argv[0] to -m rather than -c while searching for the module to execute. Also updates all the cmd_line_script tests to validate the setting of sys.path[0] and the current working directory
Nick Coghlan <ncoghlan@gmail.com>
parents:
60648
diff
changeset
|
97 def _check_script(self, script_name, expected_file, |
|
212a16f906a6
Issue #8202: Set sys.argv[0] to -m rather than -c while searching for the module to execute. Also updates all the cmd_line_script tests to validate the setting of sys.path[0] and the current working directory
Nick Coghlan <ncoghlan@gmail.com>
parents:
60648
diff
changeset
|
98 expected_argv0, expected_path0, |
|
212a16f906a6
Issue #8202: Set sys.argv[0] to -m rather than -c while searching for the module to execute. Also updates all the cmd_line_script tests to validate the setting of sys.path[0] and the current working directory
Nick Coghlan <ncoghlan@gmail.com>
parents:
60648
diff
changeset
|
99 expected_package, |
|
212a16f906a6
Issue #8202: Set sys.argv[0] to -m rather than -c while searching for the module to execute. Also updates all the cmd_line_script tests to validate the setting of sys.path[0] and the current working directory
Nick Coghlan <ncoghlan@gmail.com>
parents:
60648
diff
changeset
|
100 *cmd_line_switches): |
|
65300
660afdaeb2d6
#9964: fix running test_cmd_line_script under -O and -OO.
Georg Brandl <georg@python.org>
parents:
65182
diff
changeset
|
101 if not __debug__: |
|
660afdaeb2d6
#9964: fix running test_cmd_line_script under -O and -OO.
Georg Brandl <georg@python.org>
parents:
65182
diff
changeset
|
102 cmd_line_switches += ('-' + 'O' * sys.flags.optimize,) |
|
64110
212a16f906a6
Issue #8202: Set sys.argv[0] to -m rather than -c while searching for the module to execute. Also updates all the cmd_line_script tests to validate the setting of sys.path[0] and the current working directory
Nick Coghlan <ncoghlan@gmail.com>
parents:
60648
diff
changeset
|
103 run_args = cmd_line_switches + (script_name,) |
|
65182
b1fb0539bf98
Better Python spawning primitives in test.script_helper, for
Antoine Pitrou <solipsis@pitrou.net>
parents:
64110
diff
changeset
|
104 rc, out, err = assert_python_ok(*run_args) |
|
b1fb0539bf98
Better Python spawning primitives in test.script_helper, for
Antoine Pitrou <solipsis@pitrou.net>
parents:
64110
diff
changeset
|
105 self._check_output(script_name, rc, out + err, expected_file, |
|
64110
212a16f906a6
Issue #8202: Set sys.argv[0] to -m rather than -c while searching for the module to execute. Also updates all the cmd_line_script tests to validate the setting of sys.path[0] and the current working directory
Nick Coghlan <ncoghlan@gmail.com>
parents:
60648
diff
changeset
|
106 expected_argv0, expected_path0, expected_package) |
|
43897
41f7c13ef161
Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
diff
changeset
|
107 |
|
51452
2d354f4d77bc
Merged revisions 69419-69420 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
50164
diff
changeset
|
108 def _check_import_error(self, script_name, expected_msg, |
|
2d354f4d77bc
Merged revisions 69419-69420 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
50164
diff
changeset
|
109 *cmd_line_switches): |
|
2d354f4d77bc
Merged revisions 69419-69420 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
50164
diff
changeset
|
110 run_args = cmd_line_switches + (script_name,) |
|
65182
b1fb0539bf98
Better Python spawning primitives in test.script_helper, for
Antoine Pitrou <solipsis@pitrou.net>
parents:
64110
diff
changeset
|
111 rc, out, err = assert_python_failure(*run_args) |
|
64110
212a16f906a6
Issue #8202: Set sys.argv[0] to -m rather than -c while searching for the module to execute. Also updates all the cmd_line_script tests to validate the setting of sys.path[0] and the current working directory
Nick Coghlan <ncoghlan@gmail.com>
parents:
60648
diff
changeset
|
112 if verbose > 1: |
|
51452
2d354f4d77bc
Merged revisions 69419-69420 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
50164
diff
changeset
|
113 print('Output from test script %r:' % script_name) |
|
65182
b1fb0539bf98
Better Python spawning primitives in test.script_helper, for
Antoine Pitrou <solipsis@pitrou.net>
parents:
64110
diff
changeset
|
114 print(err) |
|
51452
2d354f4d77bc
Merged revisions 69419-69420 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
50164
diff
changeset
|
115 print('Expected output: %r' % expected_msg) |
|
65182
b1fb0539bf98
Better Python spawning primitives in test.script_helper, for
Antoine Pitrou <solipsis@pitrou.net>
parents:
64110
diff
changeset
|
116 self.assertIn(expected_msg.encode('utf-8'), err) |
|
51452
2d354f4d77bc
Merged revisions 69419-69420 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
50164
diff
changeset
|
117 |
|
43897
41f7c13ef161
Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
diff
changeset
|
118 def test_basic_script(self): |
|
41f7c13ef161
Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
diff
changeset
|
119 with temp_dir() as script_dir: |
|
44129
b637349336fd
Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43912
diff
changeset
|
120 script_name = _make_test_script(script_dir, 'script') |
|
64110
212a16f906a6
Issue #8202: Set sys.argv[0] to -m rather than -c while searching for the module to execute. Also updates all the cmd_line_script tests to validate the setting of sys.path[0] and the current working directory
Nick Coghlan <ncoghlan@gmail.com>
parents:
60648
diff
changeset
|
121 self._check_script(script_name, script_name, script_name, |
|
212a16f906a6
Issue #8202: Set sys.argv[0] to -m rather than -c while searching for the module to execute. Also updates all the cmd_line_script tests to validate the setting of sys.path[0] and the current working directory
Nick Coghlan <ncoghlan@gmail.com>
parents:
60648
diff
changeset
|
122 script_dir, None) |
|
43897
41f7c13ef161
Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
diff
changeset
|
123 |
|
41f7c13ef161
Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
diff
changeset
|
124 def test_script_compiled(self): |
|
41f7c13ef161
Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
diff
changeset
|
125 with temp_dir() as script_dir: |
|
44129
b637349336fd
Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43912
diff
changeset
|
126 script_name = _make_test_script(script_dir, 'script') |
|
64110
212a16f906a6
Issue #8202: Set sys.argv[0] to -m rather than -c while searching for the module to execute. Also updates all the cmd_line_script tests to validate the setting of sys.path[0] and the current working directory
Nick Coghlan <ncoghlan@gmail.com>
parents:
60648
diff
changeset
|
127 py_compile.compile(script_name, doraise=True) |
|
43897
41f7c13ef161
Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
diff
changeset
|
128 os.remove(script_name) |
|
64110
212a16f906a6
Issue #8202: Set sys.argv[0] to -m rather than -c while searching for the module to execute. Also updates all the cmd_line_script tests to validate the setting of sys.path[0] and the current working directory
Nick Coghlan <ncoghlan@gmail.com>
parents:
60648
diff
changeset
|
129 pyc_file = support.make_legacy_pyc(script_name) |
|
212a16f906a6
Issue #8202: Set sys.argv[0] to -m rather than -c while searching for the module to execute. Also updates all the cmd_line_script tests to validate the setting of sys.path[0] and the current working directory
Nick Coghlan <ncoghlan@gmail.com>
parents:
60648
diff
changeset
|
130 self._check_script(pyc_file, pyc_file, |
|
212a16f906a6
Issue #8202: Set sys.argv[0] to -m rather than -c while searching for the module to execute. Also updates all the cmd_line_script tests to validate the setting of sys.path[0] and the current working directory
Nick Coghlan <ncoghlan@gmail.com>
parents:
60648
diff
changeset
|
131 pyc_file, script_dir, None) |
|
43897
41f7c13ef161
Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
diff
changeset
|
132 |
|
41f7c13ef161
Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
diff
changeset
|
133 def test_directory(self): |
|
41f7c13ef161
Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
diff
changeset
|
134 with temp_dir() as script_dir: |
|
44129
b637349336fd
Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43912
diff
changeset
|
135 script_name = _make_test_script(script_dir, '__main__') |
|
64110
212a16f906a6
Issue #8202: Set sys.argv[0] to -m rather than -c while searching for the module to execute. Also updates all the cmd_line_script tests to validate the setting of sys.path[0] and the current working directory
Nick Coghlan <ncoghlan@gmail.com>
parents:
60648
diff
changeset
|
136 self._check_script(script_dir, script_name, script_dir, |
|
212a16f906a6
Issue #8202: Set sys.argv[0] to -m rather than -c while searching for the module to execute. Also updates all the cmd_line_script tests to validate the setting of sys.path[0] and the current working directory
Nick Coghlan <ncoghlan@gmail.com>
parents:
60648
diff
changeset
|
137 script_dir, '') |
|
43897
41f7c13ef161
Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
diff
changeset
|
138 |
|
41f7c13ef161
Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
diff
changeset
|
139 def test_directory_compiled(self): |
|
41f7c13ef161
Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
diff
changeset
|
140 with temp_dir() as script_dir: |
|
44129
b637349336fd
Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43912
diff
changeset
|
141 script_name = _make_test_script(script_dir, '__main__') |
|
64110
212a16f906a6
Issue #8202: Set sys.argv[0] to -m rather than -c while searching for the module to execute. Also updates all the cmd_line_script tests to validate the setting of sys.path[0] and the current working directory
Nick Coghlan <ncoghlan@gmail.com>
parents:
60648
diff
changeset
|
142 py_compile.compile(script_name, doraise=True) |
|
43897
41f7c13ef161
Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
diff
changeset
|
143 os.remove(script_name) |
|
64110
212a16f906a6
Issue #8202: Set sys.argv[0] to -m rather than -c while searching for the module to execute. Also updates all the cmd_line_script tests to validate the setting of sys.path[0] and the current working directory
Nick Coghlan <ncoghlan@gmail.com>
parents:
60648
diff
changeset
|
144 pyc_file = support.make_legacy_pyc(script_name) |
|
212a16f906a6
Issue #8202: Set sys.argv[0] to -m rather than -c while searching for the module to execute. Also updates all the cmd_line_script tests to validate the setting of sys.path[0] and the current working directory
Nick Coghlan <ncoghlan@gmail.com>
parents:
60648
diff
changeset
|
145 self._check_script(script_dir, pyc_file, script_dir, |
|
212a16f906a6
Issue #8202: Set sys.argv[0] to -m rather than -c while searching for the module to execute. Also updates all the cmd_line_script tests to validate the setting of sys.path[0] and the current working directory
Nick Coghlan <ncoghlan@gmail.com>
parents:
60648
diff
changeset
|
146 script_dir, '') |
|
43897
41f7c13ef161
Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
diff
changeset
|
147 |
|
51452
2d354f4d77bc
Merged revisions 69419-69420 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
50164
diff
changeset
|
148 def test_directory_error(self): |
|
2d354f4d77bc
Merged revisions 69419-69420 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
50164
diff
changeset
|
149 with temp_dir() as script_dir: |
|
57291
fd417cd64341
Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
55593
diff
changeset
|
150 msg = "can't find '__main__' module in %r" % script_dir |
|
51452
2d354f4d77bc
Merged revisions 69419-69420 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
50164
diff
changeset
|
151 self._check_import_error(script_dir, msg) |
|
2d354f4d77bc
Merged revisions 69419-69420 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
50164
diff
changeset
|
152 |
|
43897
41f7c13ef161
Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
diff
changeset
|
153 def test_zipfile(self): |
|
41f7c13ef161
Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
diff
changeset
|
154 with temp_dir() as script_dir: |
|
44129
b637349336fd
Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43912
diff
changeset
|
155 script_name = _make_test_script(script_dir, '__main__') |
|
57291
fd417cd64341
Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
55593
diff
changeset
|
156 zip_name, run_name = make_zip_script(script_dir, 'test_zip', script_name) |
|
64110
212a16f906a6
Issue #8202: Set sys.argv[0] to -m rather than -c while searching for the module to execute. Also updates all the cmd_line_script tests to validate the setting of sys.path[0] and the current working directory
Nick Coghlan <ncoghlan@gmail.com>
parents:
60648
diff
changeset
|
157 self._check_script(zip_name, run_name, zip_name, zip_name, '') |
|
43897
41f7c13ef161
Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
diff
changeset
|
158 |
|
41f7c13ef161
Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
diff
changeset
|
159 def test_zipfile_compiled(self): |
|
41f7c13ef161
Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
diff
changeset
|
160 with temp_dir() as script_dir: |
|
44129
b637349336fd
Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43912
diff
changeset
|
161 script_name = _make_test_script(script_dir, '__main__') |
| 60648 | 162 compiled_name = py_compile.compile(script_name, doraise=True) |
|
57291
fd417cd64341
Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
55593
diff
changeset
|
163 zip_name, run_name = make_zip_script(script_dir, 'test_zip', compiled_name) |
|
64110
212a16f906a6
Issue #8202: Set sys.argv[0] to -m rather than -c while searching for the module to execute. Also updates all the cmd_line_script tests to validate the setting of sys.path[0] and the current working directory
Nick Coghlan <ncoghlan@gmail.com>
parents:
60648
diff
changeset
|
164 self._check_script(zip_name, run_name, zip_name, zip_name, '') |
|
44129
b637349336fd
Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43912
diff
changeset
|
165 |
|
51452
2d354f4d77bc
Merged revisions 69419-69420 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
50164
diff
changeset
|
166 def test_zipfile_error(self): |
|
2d354f4d77bc
Merged revisions 69419-69420 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
50164
diff
changeset
|
167 with temp_dir() as script_dir: |
|
2d354f4d77bc
Merged revisions 69419-69420 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
50164
diff
changeset
|
168 script_name = _make_test_script(script_dir, 'not_main') |
|
57291
fd417cd64341
Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
55593
diff
changeset
|
169 zip_name, run_name = make_zip_script(script_dir, 'test_zip', script_name) |
|
fd417cd64341
Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
55593
diff
changeset
|
170 msg = "can't find '__main__' module in %r" % zip_name |
|
51452
2d354f4d77bc
Merged revisions 69419-69420 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
50164
diff
changeset
|
171 self._check_import_error(zip_name, msg) |
|
2d354f4d77bc
Merged revisions 69419-69420 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
50164
diff
changeset
|
172 |
|
44129
b637349336fd
Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43912
diff
changeset
|
173 def test_module_in_package(self): |
|
b637349336fd
Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43912
diff
changeset
|
174 with temp_dir() as script_dir: |
|
b637349336fd
Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43912
diff
changeset
|
175 pkg_dir = os.path.join(script_dir, 'test_pkg') |
|
57291
fd417cd64341
Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
55593
diff
changeset
|
176 make_pkg(pkg_dir) |
|
44129
b637349336fd
Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43912
diff
changeset
|
177 script_name = _make_test_script(pkg_dir, 'script') |
|
b637349336fd
Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43912
diff
changeset
|
178 launch_name = _make_launch_script(script_dir, 'launch', 'test_pkg.script') |
|
64110
212a16f906a6
Issue #8202: Set sys.argv[0] to -m rather than -c while searching for the module to execute. Also updates all the cmd_line_script tests to validate the setting of sys.path[0] and the current working directory
Nick Coghlan <ncoghlan@gmail.com>
parents:
60648
diff
changeset
|
179 self._check_script(launch_name, script_name, script_name, script_dir, 'test_pkg') |
|
50164
252927a2f32d
Merged revisions 67753 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
47201
diff
changeset
|
180 |
|
252927a2f32d
Merged revisions 67753 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
47201
diff
changeset
|
181 def test_module_in_package_in_zipfile(self): |
|
252927a2f32d
Merged revisions 67753 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
47201
diff
changeset
|
182 with temp_dir() as script_dir: |
|
252927a2f32d
Merged revisions 67753 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
47201
diff
changeset
|
183 zip_name, run_name = _make_test_zip_pkg(script_dir, 'test_zip', 'test_pkg', 'script') |
|
252927a2f32d
Merged revisions 67753 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
47201
diff
changeset
|
184 launch_name = _make_launch_script(script_dir, 'launch', 'test_pkg.script', zip_name) |
|
64110
212a16f906a6
Issue #8202: Set sys.argv[0] to -m rather than -c while searching for the module to execute. Also updates all the cmd_line_script tests to validate the setting of sys.path[0] and the current working directory
Nick Coghlan <ncoghlan@gmail.com>
parents:
60648
diff
changeset
|
185 self._check_script(launch_name, run_name, run_name, zip_name, 'test_pkg') |
|
50164
252927a2f32d
Merged revisions 67753 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
47201
diff
changeset
|
186 |
|
252927a2f32d
Merged revisions 67753 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
47201
diff
changeset
|
187 def test_module_in_subpackage_in_zipfile(self): |
|
252927a2f32d
Merged revisions 67753 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
47201
diff
changeset
|
188 with temp_dir() as script_dir: |
|
252927a2f32d
Merged revisions 67753 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
47201
diff
changeset
|
189 zip_name, run_name = _make_test_zip_pkg(script_dir, 'test_zip', 'test_pkg', 'script', depth=2) |
|
252927a2f32d
Merged revisions 67753 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
47201
diff
changeset
|
190 launch_name = _make_launch_script(script_dir, 'launch', 'test_pkg.test_pkg.script', zip_name) |
|
64110
212a16f906a6
Issue #8202: Set sys.argv[0] to -m rather than -c while searching for the module to execute. Also updates all the cmd_line_script tests to validate the setting of sys.path[0] and the current working directory
Nick Coghlan <ncoghlan@gmail.com>
parents:
60648
diff
changeset
|
191 self._check_script(launch_name, run_name, run_name, zip_name, 'test_pkg.test_pkg') |
|
43897
41f7c13ef161
Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
diff
changeset
|
192 |
|
51452
2d354f4d77bc
Merged revisions 69419-69420 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
50164
diff
changeset
|
193 def test_package(self): |
|
2d354f4d77bc
Merged revisions 69419-69420 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
50164
diff
changeset
|
194 with temp_dir() as script_dir: |
|
2d354f4d77bc
Merged revisions 69419-69420 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
50164
diff
changeset
|
195 pkg_dir = os.path.join(script_dir, 'test_pkg') |
|
57291
fd417cd64341
Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
55593
diff
changeset
|
196 make_pkg(pkg_dir) |
|
51452
2d354f4d77bc
Merged revisions 69419-69420 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
50164
diff
changeset
|
197 script_name = _make_test_script(pkg_dir, '__main__') |
|
2d354f4d77bc
Merged revisions 69419-69420 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
50164
diff
changeset
|
198 launch_name = _make_launch_script(script_dir, 'launch', 'test_pkg') |
|
2d354f4d77bc
Merged revisions 69419-69420 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
50164
diff
changeset
|
199 self._check_script(launch_name, script_name, |
|
64110
212a16f906a6
Issue #8202: Set sys.argv[0] to -m rather than -c while searching for the module to execute. Also updates all the cmd_line_script tests to validate the setting of sys.path[0] and the current working directory
Nick Coghlan <ncoghlan@gmail.com>
parents:
60648
diff
changeset
|
200 script_name, script_dir, 'test_pkg') |
|
51452
2d354f4d77bc
Merged revisions 69419-69420 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
50164
diff
changeset
|
201 |
|
2d354f4d77bc
Merged revisions 69419-69420 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
50164
diff
changeset
|
202 def test_package_compiled(self): |
|
2d354f4d77bc
Merged revisions 69419-69420 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
50164
diff
changeset
|
203 with temp_dir() as script_dir: |
|
2d354f4d77bc
Merged revisions 69419-69420 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
50164
diff
changeset
|
204 pkg_dir = os.path.join(script_dir, 'test_pkg') |
|
57291
fd417cd64341
Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
55593
diff
changeset
|
205 make_pkg(pkg_dir) |
|
51452
2d354f4d77bc
Merged revisions 69419-69420 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
50164
diff
changeset
|
206 script_name = _make_test_script(pkg_dir, '__main__') |
| 60648 | 207 compiled_name = py_compile.compile(script_name, doraise=True) |
|
51452
2d354f4d77bc
Merged revisions 69419-69420 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
50164
diff
changeset
|
208 os.remove(script_name) |
|
64110
212a16f906a6
Issue #8202: Set sys.argv[0] to -m rather than -c while searching for the module to execute. Also updates all the cmd_line_script tests to validate the setting of sys.path[0] and the current working directory
Nick Coghlan <ncoghlan@gmail.com>
parents:
60648
diff
changeset
|
209 pyc_file = support.make_legacy_pyc(script_name) |
|
51452
2d354f4d77bc
Merged revisions 69419-69420 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
50164
diff
changeset
|
210 launch_name = _make_launch_script(script_dir, 'launch', 'test_pkg') |
| 60648 | 211 self._check_script(launch_name, pyc_file, |
|
64110
212a16f906a6
Issue #8202: Set sys.argv[0] to -m rather than -c while searching for the module to execute. Also updates all the cmd_line_script tests to validate the setting of sys.path[0] and the current working directory
Nick Coghlan <ncoghlan@gmail.com>
parents:
60648
diff
changeset
|
212 pyc_file, script_dir, 'test_pkg') |
|
51452
2d354f4d77bc
Merged revisions 69419-69420 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
50164
diff
changeset
|
213 |
|
2d354f4d77bc
Merged revisions 69419-69420 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
50164
diff
changeset
|
214 def test_package_error(self): |
|
2d354f4d77bc
Merged revisions 69419-69420 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
50164
diff
changeset
|
215 with temp_dir() as script_dir: |
|
2d354f4d77bc
Merged revisions 69419-69420 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
50164
diff
changeset
|
216 pkg_dir = os.path.join(script_dir, 'test_pkg') |
|
57291
fd417cd64341
Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
55593
diff
changeset
|
217 make_pkg(pkg_dir) |
|
51452
2d354f4d77bc
Merged revisions 69419-69420 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
50164
diff
changeset
|
218 msg = ("'test_pkg' is a package and cannot " |
|
2d354f4d77bc
Merged revisions 69419-69420 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
50164
diff
changeset
|
219 "be directly executed") |
|
2d354f4d77bc
Merged revisions 69419-69420 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
50164
diff
changeset
|
220 launch_name = _make_launch_script(script_dir, 'launch', 'test_pkg') |
|
2d354f4d77bc
Merged revisions 69419-69420 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
50164
diff
changeset
|
221 self._check_import_error(launch_name, msg) |
|
2d354f4d77bc
Merged revisions 69419-69420 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
50164
diff
changeset
|
222 |
|
2d354f4d77bc
Merged revisions 69419-69420 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
50164
diff
changeset
|
223 def test_package_recursion(self): |
|
2d354f4d77bc
Merged revisions 69419-69420 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
50164
diff
changeset
|
224 with temp_dir() as script_dir: |
|
2d354f4d77bc
Merged revisions 69419-69420 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
50164
diff
changeset
|
225 pkg_dir = os.path.join(script_dir, 'test_pkg') |
|
57291
fd417cd64341
Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
55593
diff
changeset
|
226 make_pkg(pkg_dir) |
|
51452
2d354f4d77bc
Merged revisions 69419-69420 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
50164
diff
changeset
|
227 main_dir = os.path.join(pkg_dir, '__main__') |
|
57291
fd417cd64341
Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
55593
diff
changeset
|
228 make_pkg(main_dir) |
|
51452
2d354f4d77bc
Merged revisions 69419-69420 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
50164
diff
changeset
|
229 msg = ("Cannot use package as __main__ module; " |
|
2d354f4d77bc
Merged revisions 69419-69420 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
50164
diff
changeset
|
230 "'test_pkg' is a package and cannot " |
|
2d354f4d77bc
Merged revisions 69419-69420 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
50164
diff
changeset
|
231 "be directly executed") |
|
2d354f4d77bc
Merged revisions 69419-69420 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
50164
diff
changeset
|
232 launch_name = _make_launch_script(script_dir, 'launch', 'test_pkg') |
|
2d354f4d77bc
Merged revisions 69419-69420 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
50164
diff
changeset
|
233 self._check_import_error(launch_name, msg) |
|
2d354f4d77bc
Merged revisions 69419-69420 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
50164
diff
changeset
|
234 |
|
64110
212a16f906a6
Issue #8202: Set sys.argv[0] to -m rather than -c while searching for the module to execute. Also updates all the cmd_line_script tests to validate the setting of sys.path[0] and the current working directory
Nick Coghlan <ncoghlan@gmail.com>
parents:
60648
diff
changeset
|
235 def test_issue8202(self): |
|
212a16f906a6
Issue #8202: Set sys.argv[0] to -m rather than -c while searching for the module to execute. Also updates all the cmd_line_script tests to validate the setting of sys.path[0] and the current working directory
Nick Coghlan <ncoghlan@gmail.com>
parents:
60648
diff
changeset
|
236 # Make sure package __init__ modules see "-m" in sys.argv0 while |
|
212a16f906a6
Issue #8202: Set sys.argv[0] to -m rather than -c while searching for the module to execute. Also updates all the cmd_line_script tests to validate the setting of sys.path[0] and the current working directory
Nick Coghlan <ncoghlan@gmail.com>
parents:
60648
diff
changeset
|
237 # searching for the module to execute |
|
212a16f906a6
Issue #8202: Set sys.argv[0] to -m rather than -c while searching for the module to execute. Also updates all the cmd_line_script tests to validate the setting of sys.path[0] and the current working directory
Nick Coghlan <ncoghlan@gmail.com>
parents:
60648
diff
changeset
|
238 with temp_dir() as script_dir: |
|
212a16f906a6
Issue #8202: Set sys.argv[0] to -m rather than -c while searching for the module to execute. Also updates all the cmd_line_script tests to validate the setting of sys.path[0] and the current working directory
Nick Coghlan <ncoghlan@gmail.com>
parents:
60648
diff
changeset
|
239 with support.temp_cwd(path=script_dir): |
|
212a16f906a6
Issue #8202: Set sys.argv[0] to -m rather than -c while searching for the module to execute. Also updates all the cmd_line_script tests to validate the setting of sys.path[0] and the current working directory
Nick Coghlan <ncoghlan@gmail.com>
parents:
60648
diff
changeset
|
240 pkg_dir = os.path.join(script_dir, 'test_pkg') |
|
212a16f906a6
Issue #8202: Set sys.argv[0] to -m rather than -c while searching for the module to execute. Also updates all the cmd_line_script tests to validate the setting of sys.path[0] and the current working directory
Nick Coghlan <ncoghlan@gmail.com>
parents:
60648
diff
changeset
|
241 make_pkg(pkg_dir, "import sys; print('init_argv0==%r' % sys.argv[0])") |
|
212a16f906a6
Issue #8202: Set sys.argv[0] to -m rather than -c while searching for the module to execute. Also updates all the cmd_line_script tests to validate the setting of sys.path[0] and the current working directory
Nick Coghlan <ncoghlan@gmail.com>
parents:
60648
diff
changeset
|
242 script_name = _make_test_script(pkg_dir, 'script') |
|
65182
b1fb0539bf98
Better Python spawning primitives in test.script_helper, for
Antoine Pitrou <solipsis@pitrou.net>
parents:
64110
diff
changeset
|
243 rc, out, err = assert_python_ok('-m', 'test_pkg.script') |
|
64110
212a16f906a6
Issue #8202: Set sys.argv[0] to -m rather than -c while searching for the module to execute. Also updates all the cmd_line_script tests to validate the setting of sys.path[0] and the current working directory
Nick Coghlan <ncoghlan@gmail.com>
parents:
60648
diff
changeset
|
244 if verbose > 1: |
|
212a16f906a6
Issue #8202: Set sys.argv[0] to -m rather than -c while searching for the module to execute. Also updates all the cmd_line_script tests to validate the setting of sys.path[0] and the current working directory
Nick Coghlan <ncoghlan@gmail.com>
parents:
60648
diff
changeset
|
245 print(data) |
|
212a16f906a6
Issue #8202: Set sys.argv[0] to -m rather than -c while searching for the module to execute. Also updates all the cmd_line_script tests to validate the setting of sys.path[0] and the current working directory
Nick Coghlan <ncoghlan@gmail.com>
parents:
60648
diff
changeset
|
246 expected = "init_argv0==%r" % '-m' |
|
65182
b1fb0539bf98
Better Python spawning primitives in test.script_helper, for
Antoine Pitrou <solipsis@pitrou.net>
parents:
64110
diff
changeset
|
247 self.assertIn(expected.encode('utf-8'), out) |
|
b1fb0539bf98
Better Python spawning primitives in test.script_helper, for
Antoine Pitrou <solipsis@pitrou.net>
parents:
64110
diff
changeset
|
248 self._check_output(script_name, rc, out, |
|
64110
212a16f906a6
Issue #8202: Set sys.argv[0] to -m rather than -c while searching for the module to execute. Also updates all the cmd_line_script tests to validate the setting of sys.path[0] and the current working directory
Nick Coghlan <ncoghlan@gmail.com>
parents:
60648
diff
changeset
|
249 script_name, script_name, '', 'test_pkg') |
|
212a16f906a6
Issue #8202: Set sys.argv[0] to -m rather than -c while searching for the module to execute. Also updates all the cmd_line_script tests to validate the setting of sys.path[0] and the current working directory
Nick Coghlan <ncoghlan@gmail.com>
parents:
60648
diff
changeset
|
250 |
|
212a16f906a6
Issue #8202: Set sys.argv[0] to -m rather than -c while searching for the module to execute. Also updates all the cmd_line_script tests to validate the setting of sys.path[0] and the current working directory
Nick Coghlan <ncoghlan@gmail.com>
parents:
60648
diff
changeset
|
251 def test_issue8202_dash_c_file_ignored(self): |
|
212a16f906a6
Issue #8202: Set sys.argv[0] to -m rather than -c while searching for the module to execute. Also updates all the cmd_line_script tests to validate the setting of sys.path[0] and the current working directory
Nick Coghlan <ncoghlan@gmail.com>
parents:
60648
diff
changeset
|
252 # Make sure a "-c" file in the current directory |
|
212a16f906a6
Issue #8202: Set sys.argv[0] to -m rather than -c while searching for the module to execute. Also updates all the cmd_line_script tests to validate the setting of sys.path[0] and the current working directory
Nick Coghlan <ncoghlan@gmail.com>
parents:
60648
diff
changeset
|
253 # does not alter the value of sys.path[0] |
|
212a16f906a6
Issue #8202: Set sys.argv[0] to -m rather than -c while searching for the module to execute. Also updates all the cmd_line_script tests to validate the setting of sys.path[0] and the current working directory
Nick Coghlan <ncoghlan@gmail.com>
parents:
60648
diff
changeset
|
254 with temp_dir() as script_dir: |
|
212a16f906a6
Issue #8202: Set sys.argv[0] to -m rather than -c while searching for the module to execute. Also updates all the cmd_line_script tests to validate the setting of sys.path[0] and the current working directory
Nick Coghlan <ncoghlan@gmail.com>
parents:
60648
diff
changeset
|
255 with support.temp_cwd(path=script_dir): |
|
212a16f906a6
Issue #8202: Set sys.argv[0] to -m rather than -c while searching for the module to execute. Also updates all the cmd_line_script tests to validate the setting of sys.path[0] and the current working directory
Nick Coghlan <ncoghlan@gmail.com>
parents:
60648
diff
changeset
|
256 with open("-c", "w") as f: |
|
212a16f906a6
Issue #8202: Set sys.argv[0] to -m rather than -c while searching for the module to execute. Also updates all the cmd_line_script tests to validate the setting of sys.path[0] and the current working directory
Nick Coghlan <ncoghlan@gmail.com>
parents:
60648
diff
changeset
|
257 f.write("data") |
|
65182
b1fb0539bf98
Better Python spawning primitives in test.script_helper, for
Antoine Pitrou <solipsis@pitrou.net>
parents:
64110
diff
changeset
|
258 rc, out, err = assert_python_ok('-c', |
|
64110
212a16f906a6
Issue #8202: Set sys.argv[0] to -m rather than -c while searching for the module to execute. Also updates all the cmd_line_script tests to validate the setting of sys.path[0] and the current working directory
Nick Coghlan <ncoghlan@gmail.com>
parents:
60648
diff
changeset
|
259 'import sys; print("sys.path[0]==%r" % sys.path[0])') |
|
212a16f906a6
Issue #8202: Set sys.argv[0] to -m rather than -c while searching for the module to execute. Also updates all the cmd_line_script tests to validate the setting of sys.path[0] and the current working directory
Nick Coghlan <ncoghlan@gmail.com>
parents:
60648
diff
changeset
|
260 if verbose > 1: |
|
65182
b1fb0539bf98
Better Python spawning primitives in test.script_helper, for
Antoine Pitrou <solipsis@pitrou.net>
parents:
64110
diff
changeset
|
261 print(out) |
|
64110
212a16f906a6
Issue #8202: Set sys.argv[0] to -m rather than -c while searching for the module to execute. Also updates all the cmd_line_script tests to validate the setting of sys.path[0] and the current working directory
Nick Coghlan <ncoghlan@gmail.com>
parents:
60648
diff
changeset
|
262 expected = "sys.path[0]==%r" % '' |
|
65182
b1fb0539bf98
Better Python spawning primitives in test.script_helper, for
Antoine Pitrou <solipsis@pitrou.net>
parents:
64110
diff
changeset
|
263 self.assertIn(expected.encode('utf-8'), out) |
|
64110
212a16f906a6
Issue #8202: Set sys.argv[0] to -m rather than -c while searching for the module to execute. Also updates all the cmd_line_script tests to validate the setting of sys.path[0] and the current working directory
Nick Coghlan <ncoghlan@gmail.com>
parents:
60648
diff
changeset
|
264 |
|
212a16f906a6
Issue #8202: Set sys.argv[0] to -m rather than -c while searching for the module to execute. Also updates all the cmd_line_script tests to validate the setting of sys.path[0] and the current working directory
Nick Coghlan <ncoghlan@gmail.com>
parents:
60648
diff
changeset
|
265 def test_issue8202_dash_m_file_ignored(self): |
|
212a16f906a6
Issue #8202: Set sys.argv[0] to -m rather than -c while searching for the module to execute. Also updates all the cmd_line_script tests to validate the setting of sys.path[0] and the current working directory
Nick Coghlan <ncoghlan@gmail.com>
parents:
60648
diff
changeset
|
266 # Make sure a "-m" file in the current directory |
|
212a16f906a6
Issue #8202: Set sys.argv[0] to -m rather than -c while searching for the module to execute. Also updates all the cmd_line_script tests to validate the setting of sys.path[0] and the current working directory
Nick Coghlan <ncoghlan@gmail.com>
parents:
60648
diff
changeset
|
267 # does not alter the value of sys.path[0] |
|
212a16f906a6
Issue #8202: Set sys.argv[0] to -m rather than -c while searching for the module to execute. Also updates all the cmd_line_script tests to validate the setting of sys.path[0] and the current working directory
Nick Coghlan <ncoghlan@gmail.com>
parents:
60648
diff
changeset
|
268 with temp_dir() as script_dir: |
|
212a16f906a6
Issue #8202: Set sys.argv[0] to -m rather than -c while searching for the module to execute. Also updates all the cmd_line_script tests to validate the setting of sys.path[0] and the current working directory
Nick Coghlan <ncoghlan@gmail.com>
parents:
60648
diff
changeset
|
269 script_name = _make_test_script(script_dir, 'other') |
|
212a16f906a6
Issue #8202: Set sys.argv[0] to -m rather than -c while searching for the module to execute. Also updates all the cmd_line_script tests to validate the setting of sys.path[0] and the current working directory
Nick Coghlan <ncoghlan@gmail.com>
parents:
60648
diff
changeset
|
270 with support.temp_cwd(path=script_dir): |
|
212a16f906a6
Issue #8202: Set sys.argv[0] to -m rather than -c while searching for the module to execute. Also updates all the cmd_line_script tests to validate the setting of sys.path[0] and the current working directory
Nick Coghlan <ncoghlan@gmail.com>
parents:
60648
diff
changeset
|
271 with open("-m", "w") as f: |
|
212a16f906a6
Issue #8202: Set sys.argv[0] to -m rather than -c while searching for the module to execute. Also updates all the cmd_line_script tests to validate the setting of sys.path[0] and the current working directory
Nick Coghlan <ncoghlan@gmail.com>
parents:
60648
diff
changeset
|
272 f.write("data") |
|
65182
b1fb0539bf98
Better Python spawning primitives in test.script_helper, for
Antoine Pitrou <solipsis@pitrou.net>
parents:
64110
diff
changeset
|
273 rc, out, err = assert_python_ok('-m', 'other') |
|
b1fb0539bf98
Better Python spawning primitives in test.script_helper, for
Antoine Pitrou <solipsis@pitrou.net>
parents:
64110
diff
changeset
|
274 self._check_output(script_name, rc, out, |
|
64110
212a16f906a6
Issue #8202: Set sys.argv[0] to -m rather than -c while searching for the module to execute. Also updates all the cmd_line_script tests to validate the setting of sys.path[0] and the current working directory
Nick Coghlan <ncoghlan@gmail.com>
parents:
60648
diff
changeset
|
275 script_name, script_name, '', '') |
|
43897
41f7c13ef161
Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
diff
changeset
|
276 |
|
41f7c13ef161
Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
diff
changeset
|
277 def test_main(): |
|
64110
212a16f906a6
Issue #8202: Set sys.argv[0] to -m rather than -c while searching for the module to execute. Also updates all the cmd_line_script tests to validate the setting of sys.path[0] and the current working directory
Nick Coghlan <ncoghlan@gmail.com>
parents:
60648
diff
changeset
|
278 support.run_unittest(CmdLineTest) |
|
212a16f906a6
Issue #8202: Set sys.argv[0] to -m rather than -c while searching for the module to execute. Also updates all the cmd_line_script tests to validate the setting of sys.path[0] and the current working directory
Nick Coghlan <ncoghlan@gmail.com>
parents:
60648
diff
changeset
|
279 support.reap_children() |
|
43897
41f7c13ef161
Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
diff
changeset
|
280 |
|
44129
b637349336fd
Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43912
diff
changeset
|
281 if __name__ == '__main__': |
|
43897
41f7c13ef161
Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
diff
changeset
|
282 test_main() |
