annotate Lib/test/test_cmd_line_script.py @ 78069:a8aa77458e12 3.2

Add c_size_t and c_ssize_t to the ctypes summary table.
author Antoine Pitrou <solipsis@pitrou.net>
date Thu, 12 Jul 2012 20:31:50 +0200
parents fcbd3bda7c0f
children 41658a4fb3cc 1186d68715cc
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
60648
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59615
diff changeset
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
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59615
diff changeset
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
76469
22f0044ea366 Close issue #14026 by better testing sys.argv handling in test_cmd_line_script (patch by Jason Yeo)
Nick Coghlan <ncoghlan@gmail.com>
parents: 76416
diff changeset
16 example_args = ['test1', 'test2', 'test3']
22f0044ea366 Close issue #14026 by better testing sys.argv handling in test_cmd_line_script (patch by Jason Yeo)
Nick Coghlan <ncoghlan@gmail.com>
parents: 76416
diff changeset
17
44129
b637349336fd Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43912
diff changeset
18 test_source = """\
43897
41f7c13ef161 Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
diff changeset
19 # 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
20 # statements being executed
41f7c13ef161 Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
diff changeset
21 def assertEqual(lhs, rhs):
41f7c13ef161 Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
diff changeset
22 if lhs != rhs:
44129
b637349336fd Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43912
diff changeset
23 raise AssertionError('%r != %r' % (lhs, rhs))
43897
41f7c13ef161 Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
diff changeset
24 def assertIdentical(lhs, rhs):
41f7c13ef161 Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
diff changeset
25 if lhs is not rhs:
44129
b637349336fd Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43912
diff changeset
26 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
27 # Check basic code execution
41f7c13ef161 Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
diff changeset
28 result = ['Top level assignment']
41f7c13ef161 Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
diff changeset
29 def f():
41f7c13ef161 Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
diff changeset
30 result.append('Lower level reference')
41f7c13ef161 Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
diff changeset
31 f()
41f7c13ef161 Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
diff changeset
32 assertEqual(result, ['Top level assignment', 'Lower level reference'])
41f7c13ef161 Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
diff changeset
33 # Check population of magic variables
41f7c13ef161 Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
diff changeset
34 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
35 print('__file__==%a' % __file__)
60648
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59615
diff changeset
36 assertEqual(__cached__, None)
44129
b637349336fd Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43912
diff changeset
37 print('__package__==%r' % __package__)
43897
41f7c13ef161 Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
diff changeset
38 # Check the sys module
41f7c13ef161 Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
diff changeset
39 import sys
41f7c13ef161 Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
diff changeset
40 assertIdentical(globals(), sys.modules[__name__].__dict__)
76469
22f0044ea366 Close issue #14026 by better testing sys.argv handling in test_cmd_line_script (patch by Jason Yeo)
Nick Coghlan <ncoghlan@gmail.com>
parents: 76416
diff changeset
41 from test import test_cmd_line_script
22f0044ea366 Close issue #14026 by better testing sys.argv handling in test_cmd_line_script (patch by Jason Yeo)
Nick Coghlan <ncoghlan@gmail.com>
parents: 76416
diff changeset
42 example_args_list = test_cmd_line_script.example_args
22f0044ea366 Close issue #14026 by better testing sys.argv handling in test_cmd_line_script (patch by Jason Yeo)
Nick Coghlan <ncoghlan@gmail.com>
parents: 76416
diff changeset
43 assertEqual(sys.argv[1:], example_args_list)
65424
0505eba62347 test_cmd_line_script: format paths with ascii() instead of repr()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65300
diff changeset
44 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
45 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
46 # 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
47 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
48 print('cwd==%a' % os.getcwd())
44129
b637349336fd Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43912
diff changeset
49 """
43897
41f7c13ef161 Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
diff changeset
50
44129
b637349336fd Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43912
diff changeset
51 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
52 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
53
50164
252927a2f32d Merged revisions 67753 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 47201
diff changeset
54 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
55 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
56 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
57 source, depth)
50164
252927a2f32d Merged revisions 67753 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 47201
diff changeset
58
44129
b637349336fd Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43912
diff changeset
59 # 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
60 # -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
61 # directories and zipfiles executable!)
b637349336fd Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43912
diff changeset
62 # 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
63 launch_source = """\
b637349336fd Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43912
diff changeset
64 import sys, os.path, runpy
50164
252927a2f32d Merged revisions 67753 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 47201
diff changeset
65 sys.path.insert(0, %s)
44129
b637349336fd Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43912
diff changeset
66 runpy._run_module_as_main(%r)
b637349336fd Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43912
diff changeset
67 """
b637349336fd Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43912
diff changeset
68
50164
252927a2f32d Merged revisions 67753 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 47201
diff changeset
69 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
70 if path is None:
252927a2f32d Merged revisions 67753 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 47201
diff changeset
71 path = "os.path.dirname(__file__)"
252927a2f32d Merged revisions 67753 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 47201
diff changeset
72 else:
252927a2f32d Merged revisions 67753 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 47201
diff changeset
73 path = repr(path)
252927a2f32d Merged revisions 67753 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 47201
diff changeset
74 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
75 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
76
43897
41f7c13ef161 Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
diff changeset
77 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
78 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
79 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
80 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
81 if verbose > 1:
43912
4747ef9640ae Merged revisions 59041-59055 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43908
diff changeset
82 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
83 print(data)
44129
b637349336fd Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43912
diff changeset
84 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
85 printed_file = '__file__==%a' % expected_file
44129
b637349336fd Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43912
diff changeset
86 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
87 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
88 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
89 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
90 if verbose > 1:
44129
b637349336fd Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43912
diff changeset
91 print('Expected output:')
b637349336fd Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43912
diff changeset
92 print(printed_file)
b637349336fd Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43912
diff changeset
93 print(printed_package)
b637349336fd Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43912
diff changeset
94 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
95 print(printed_cwd)
58415
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 57291
diff changeset
96 self.assertIn(printed_file.encode('utf-8'), data)
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 57291
diff changeset
97 self.assertIn(printed_package.encode('utf-8'), data)
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 57291
diff changeset
98 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
99 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
100 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
101
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
102 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
103 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
104 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
105 *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
106 if not __debug__:
660afdaeb2d6 #9964: fix running test_cmd_line_script under -O and -OO.
Georg Brandl <georg@python.org>
parents: 65182
diff changeset
107 cmd_line_switches += ('-' + 'O' * sys.flags.optimize,)
76469
22f0044ea366 Close issue #14026 by better testing sys.argv handling in test_cmd_line_script (patch by Jason Yeo)
Nick Coghlan <ncoghlan@gmail.com>
parents: 76416
diff changeset
108 run_args = cmd_line_switches + (script_name,) + tuple(example_args)
65182
b1fb0539bf98 Better Python spawning primitives in test.script_helper, for
Antoine Pitrou <solipsis@pitrou.net>
parents: 64110
diff changeset
109 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
110 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
111 expected_argv0, expected_path0, expected_package)
43897
41f7c13ef161 Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
diff changeset
112
51452
2d354f4d77bc Merged revisions 69419-69420 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 50164
diff changeset
113 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
114 *cmd_line_switches):
2d354f4d77bc Merged revisions 69419-69420 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 50164
diff changeset
115 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
116 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
117 if verbose > 1:
51452
2d354f4d77bc Merged revisions 69419-69420 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 50164
diff changeset
118 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
119 print(err)
51452
2d354f4d77bc Merged revisions 69419-69420 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 50164
diff changeset
120 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
121 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
122
43897
41f7c13ef161 Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
diff changeset
123 def test_basic_script(self):
41f7c13ef161 Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
diff changeset
124 with temp_dir() as script_dir:
44129
b637349336fd Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43912
diff changeset
125 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
126 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
127 script_dir, None)
43897
41f7c13ef161 Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
diff changeset
128
41f7c13ef161 Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
diff changeset
129 def test_script_compiled(self):
41f7c13ef161 Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
diff changeset
130 with temp_dir() as script_dir:
44129
b637349336fd Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43912
diff changeset
131 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
132 py_compile.compile(script_name, doraise=True)
43897
41f7c13ef161 Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
diff changeset
133 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
134 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
135 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
136 pyc_file, script_dir, None)
43897
41f7c13ef161 Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
diff changeset
137
41f7c13ef161 Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
diff changeset
138 def test_directory(self):
41f7c13ef161 Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
diff changeset
139 with temp_dir() as script_dir:
44129
b637349336fd Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43912
diff changeset
140 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
141 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
142 script_dir, '')
43897
41f7c13ef161 Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
diff changeset
143
41f7c13ef161 Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
diff changeset
144 def test_directory_compiled(self):
41f7c13ef161 Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
diff changeset
145 with temp_dir() as script_dir:
44129
b637349336fd Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43912
diff changeset
146 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
147 py_compile.compile(script_name, doraise=True)
43897
41f7c13ef161 Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
diff changeset
148 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
149 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
150 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
151 script_dir, '')
43897
41f7c13ef161 Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
diff changeset
152
51452
2d354f4d77bc Merged revisions 69419-69420 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 50164
diff changeset
153 def test_directory_error(self):
2d354f4d77bc Merged revisions 69419-69420 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 50164
diff changeset
154 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
155 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
156 self._check_import_error(script_dir, msg)
2d354f4d77bc Merged revisions 69419-69420 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 50164
diff changeset
157
43897
41f7c13ef161 Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
diff changeset
158 def test_zipfile(self):
41f7c13ef161 Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
diff changeset
159 with temp_dir() as script_dir:
44129
b637349336fd Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43912
diff changeset
160 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
161 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
162 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
163
41f7c13ef161 Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
diff changeset
164 def test_zipfile_compiled(self):
41f7c13ef161 Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
diff changeset
165 with temp_dir() as script_dir:
44129
b637349336fd Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43912
diff changeset
166 script_name = _make_test_script(script_dir, '__main__')
60648
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59615
diff changeset
167 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
168 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
169 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
170
51452
2d354f4d77bc Merged revisions 69419-69420 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 50164
diff changeset
171 def test_zipfile_error(self):
2d354f4d77bc Merged revisions 69419-69420 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 50164
diff changeset
172 with temp_dir() as script_dir:
2d354f4d77bc Merged revisions 69419-69420 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 50164
diff changeset
173 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
174 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
175 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
176 self._check_import_error(zip_name, msg)
2d354f4d77bc Merged revisions 69419-69420 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 50164
diff changeset
177
44129
b637349336fd Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43912
diff changeset
178 def test_module_in_package(self):
b637349336fd Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43912
diff changeset
179 with temp_dir() as script_dir:
b637349336fd Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43912
diff changeset
180 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
181 make_pkg(pkg_dir)
44129
b637349336fd Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43912
diff changeset
182 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
183 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
184 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
185
252927a2f32d Merged revisions 67753 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 47201
diff changeset
186 def test_module_in_package_in_zipfile(self):
252927a2f32d Merged revisions 67753 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 47201
diff changeset
187 with temp_dir() as script_dir:
252927a2f32d Merged revisions 67753 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 47201
diff changeset
188 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
189 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
190 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
191
252927a2f32d Merged revisions 67753 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 47201
diff changeset
192 def test_module_in_subpackage_in_zipfile(self):
252927a2f32d Merged revisions 67753 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 47201
diff changeset
193 with temp_dir() as script_dir:
252927a2f32d Merged revisions 67753 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 47201
diff changeset
194 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
195 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
196 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
197
51452
2d354f4d77bc Merged revisions 69419-69420 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 50164
diff changeset
198 def test_package(self):
2d354f4d77bc Merged revisions 69419-69420 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 50164
diff changeset
199 with temp_dir() as script_dir:
2d354f4d77bc Merged revisions 69419-69420 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 50164
diff changeset
200 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
201 make_pkg(pkg_dir)
51452
2d354f4d77bc Merged revisions 69419-69420 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 50164
diff changeset
202 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
203 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
204 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
205 script_name, script_dir, 'test_pkg')
51452
2d354f4d77bc Merged revisions 69419-69420 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 50164
diff changeset
206
2d354f4d77bc Merged revisions 69419-69420 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 50164
diff changeset
207 def test_package_compiled(self):
2d354f4d77bc Merged revisions 69419-69420 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 50164
diff changeset
208 with temp_dir() as script_dir:
2d354f4d77bc Merged revisions 69419-69420 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 50164
diff changeset
209 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
210 make_pkg(pkg_dir)
51452
2d354f4d77bc Merged revisions 69419-69420 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 50164
diff changeset
211 script_name = _make_test_script(pkg_dir, '__main__')
60648
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59615
diff changeset
212 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
213 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
214 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
215 launch_name = _make_launch_script(script_dir, 'launch', 'test_pkg')
60648
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59615
diff changeset
216 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
217 pyc_file, script_dir, 'test_pkg')
51452
2d354f4d77bc Merged revisions 69419-69420 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 50164
diff changeset
218
2d354f4d77bc Merged revisions 69419-69420 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 50164
diff changeset
219 def test_package_error(self):
2d354f4d77bc Merged revisions 69419-69420 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 50164
diff changeset
220 with temp_dir() as script_dir:
2d354f4d77bc Merged revisions 69419-69420 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 50164
diff changeset
221 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
222 make_pkg(pkg_dir)
51452
2d354f4d77bc Merged revisions 69419-69420 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 50164
diff changeset
223 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
224 "be directly executed")
2d354f4d77bc Merged revisions 69419-69420 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 50164
diff changeset
225 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
226 self._check_import_error(launch_name, msg)
2d354f4d77bc Merged revisions 69419-69420 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 50164
diff changeset
227
2d354f4d77bc Merged revisions 69419-69420 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 50164
diff changeset
228 def test_package_recursion(self):
2d354f4d77bc Merged revisions 69419-69420 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 50164
diff changeset
229 with temp_dir() as script_dir:
2d354f4d77bc Merged revisions 69419-69420 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 50164
diff changeset
230 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
231 make_pkg(pkg_dir)
51452
2d354f4d77bc Merged revisions 69419-69420 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 50164
diff changeset
232 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
233 make_pkg(main_dir)
51452
2d354f4d77bc Merged revisions 69419-69420 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 50164
diff changeset
234 msg = ("Cannot use package as __main__ module; "
2d354f4d77bc Merged revisions 69419-69420 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 50164
diff changeset
235 "'test_pkg' is a package and cannot "
2d354f4d77bc Merged revisions 69419-69420 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 50164
diff changeset
236 "be directly executed")
2d354f4d77bc Merged revisions 69419-69420 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 50164
diff changeset
237 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
238 self._check_import_error(launch_name, msg)
2d354f4d77bc Merged revisions 69419-69420 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 50164
diff changeset
239
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
240 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
241 # 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
242 # 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
243 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
244 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
245 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
246 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
247 script_name = _make_test_script(pkg_dir, 'script')
76469
22f0044ea366 Close issue #14026 by better testing sys.argv handling in test_cmd_line_script (patch by Jason Yeo)
Nick Coghlan <ncoghlan@gmail.com>
parents: 76416
diff changeset
248 rc, out, err = assert_python_ok('-m', 'test_pkg.script', *example_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
249 if verbose > 1:
76416
c4c67c2d8ffc Close #14032: fix incorrect variable reference in test_cmd_line_script
Nick Coghlan <ncoghlan@gmail.com>
parents: 68257
diff changeset
250 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
251 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
252 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
253 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
254 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
255
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 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
257 # 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
258 # 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
259 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
260 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
261 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
262 f.write("data")
65182
b1fb0539bf98 Better Python spawning primitives in test.script_helper, for
Antoine Pitrou <solipsis@pitrou.net>
parents: 64110
diff changeset
263 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
264 '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
265 if verbose > 1:
65182
b1fb0539bf98 Better Python spawning primitives in test.script_helper, for
Antoine Pitrou <solipsis@pitrou.net>
parents: 64110
diff changeset
266 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
267 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
268 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
269
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 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
271 # 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
272 # 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
273 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
274 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
275 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
276 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
277 f.write("data")
76469
22f0044ea366 Close issue #14026 by better testing sys.argv handling in test_cmd_line_script (patch by Jason Yeo)
Nick Coghlan <ncoghlan@gmail.com>
parents: 76416
diff changeset
278 rc, out, err = assert_python_ok('-m', 'other', *example_args)
65182
b1fb0539bf98 Better Python spawning primitives in test.script_helper, for
Antoine Pitrou <solipsis@pitrou.net>
parents: 64110
diff changeset
279 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
280 script_name, script_name, '', '')
43897
41f7c13ef161 Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
diff changeset
281
77946
fcbd3bda7c0f Fix issue # 15033 - Return the proper exitcode for failure when modules are invoked using -m switch. Patch contributed by Jeff Knupp
Senthil Kumaran <senthil@uthcode.com>
parents: 76469
diff changeset
282 def test_dash_m_error_code_is_one(self):
fcbd3bda7c0f Fix issue # 15033 - Return the proper exitcode for failure when modules are invoked using -m switch. Patch contributed by Jeff Knupp
Senthil Kumaran <senthil@uthcode.com>
parents: 76469
diff changeset
283 # If a module is invoked with the -m command line flag
fcbd3bda7c0f Fix issue # 15033 - Return the proper exitcode for failure when modules are invoked using -m switch. Patch contributed by Jeff Knupp
Senthil Kumaran <senthil@uthcode.com>
parents: 76469
diff changeset
284 # and results in an error that the return code to the
fcbd3bda7c0f Fix issue # 15033 - Return the proper exitcode for failure when modules are invoked using -m switch. Patch contributed by Jeff Knupp
Senthil Kumaran <senthil@uthcode.com>
parents: 76469
diff changeset
285 # shell is '1'
fcbd3bda7c0f Fix issue # 15033 - Return the proper exitcode for failure when modules are invoked using -m switch. Patch contributed by Jeff Knupp
Senthil Kumaran <senthil@uthcode.com>
parents: 76469
diff changeset
286 with temp_dir() as script_dir:
fcbd3bda7c0f Fix issue # 15033 - Return the proper exitcode for failure when modules are invoked using -m switch. Patch contributed by Jeff Knupp
Senthil Kumaran <senthil@uthcode.com>
parents: 76469
diff changeset
287 with support.temp_cwd(path=script_dir):
fcbd3bda7c0f Fix issue # 15033 - Return the proper exitcode for failure when modules are invoked using -m switch. Patch contributed by Jeff Knupp
Senthil Kumaran <senthil@uthcode.com>
parents: 76469
diff changeset
288 pkg_dir = os.path.join(script_dir, 'test_pkg')
fcbd3bda7c0f Fix issue # 15033 - Return the proper exitcode for failure when modules are invoked using -m switch. Patch contributed by Jeff Knupp
Senthil Kumaran <senthil@uthcode.com>
parents: 76469
diff changeset
289 make_pkg(pkg_dir)
fcbd3bda7c0f Fix issue # 15033 - Return the proper exitcode for failure when modules are invoked using -m switch. Patch contributed by Jeff Knupp
Senthil Kumaran <senthil@uthcode.com>
parents: 76469
diff changeset
290 script_name = _make_test_script(pkg_dir, 'other',
fcbd3bda7c0f Fix issue # 15033 - Return the proper exitcode for failure when modules are invoked using -m switch. Patch contributed by Jeff Knupp
Senthil Kumaran <senthil@uthcode.com>
parents: 76469
diff changeset
291 "if __name__ == '__main__': raise ValueError")
fcbd3bda7c0f Fix issue # 15033 - Return the proper exitcode for failure when modules are invoked using -m switch. Patch contributed by Jeff Knupp
Senthil Kumaran <senthil@uthcode.com>
parents: 76469
diff changeset
292 rc, out, err = assert_python_failure('-m', 'test_pkg.other', *example_args)
fcbd3bda7c0f Fix issue # 15033 - Return the proper exitcode for failure when modules are invoked using -m switch. Patch contributed by Jeff Knupp
Senthil Kumaran <senthil@uthcode.com>
parents: 76469
diff changeset
293 if verbose > 1:
fcbd3bda7c0f Fix issue # 15033 - Return the proper exitcode for failure when modules are invoked using -m switch. Patch contributed by Jeff Knupp
Senthil Kumaran <senthil@uthcode.com>
parents: 76469
diff changeset
294 print(out)
fcbd3bda7c0f Fix issue # 15033 - Return the proper exitcode for failure when modules are invoked using -m switch. Patch contributed by Jeff Knupp
Senthil Kumaran <senthil@uthcode.com>
parents: 76469
diff changeset
295 self.assertEqual(rc, 1)
fcbd3bda7c0f Fix issue # 15033 - Return the proper exitcode for failure when modules are invoked using -m switch. Patch contributed by Jeff Knupp
Senthil Kumaran <senthil@uthcode.com>
parents: 76469
diff changeset
296
43897
41f7c13ef161 Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
diff changeset
297 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
298 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
299 support.reap_children()
43897
41f7c13ef161 Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
diff changeset
300
44129
b637349336fd Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43912
diff changeset
301 if __name__ == '__main__':
43897
41f7c13ef161 Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
diff changeset
302 test_main()