@@ -87,31 +87,11 @@ def _make_test_zip_pkg(zip_dir, zip_basename, pkg_name, script_basename,
87
87
importlib .invalidate_caches ()
88
88
return to_return
89
89
90
- # There's no easy way to pass the script directory in to get
91
- # -m to work (avoiding that is the whole point of making
92
- # directories and zipfiles executable!)
93
- # So we fake it for testing purposes with a custom launch script
94
- launch_source = """\
95
- import sys, os.path, runpy
96
- sys.path.insert(0, %s)
97
- runpy._run_module_as_main(%r)
98
- """
99
-
100
- def _make_launch_script (script_dir , script_basename , module_name , path = None ):
101
- if path is None :
102
- path = "os.path.dirname(__file__)"
103
- else :
104
- path = repr (path )
105
- source = launch_source % (path , module_name )
106
- to_return = make_script (script_dir , script_basename , source )
107
- importlib .invalidate_caches ()
108
- return to_return
109
-
110
90
class CmdLineTest (unittest .TestCase ):
111
91
def _check_output (self , script_name , exit_code , data ,
112
92
expected_file , expected_argv0 ,
113
93
expected_path0 , expected_package ,
114
- expected_loader ):
94
+ expected_loader , expected_cwd = None ):
115
95
if verbose > 1 :
116
96
print ("Output from test script %r:" % script_name )
117
97
print (repr (data ))
@@ -121,7 +101,9 @@ def _check_output(self, script_name, exit_code, data,
121
101
printed_package = '__package__==%r' % expected_package
122
102
printed_argv0 = 'sys.argv[0]==%a' % expected_argv0
123
103
printed_path0 = 'sys.path[0]==%a' % expected_path0
124
- printed_cwd = 'cwd==%a' % os .getcwd ()
104
+ if expected_cwd is None :
105
+ expected_cwd = os .getcwd ()
106
+ printed_cwd = 'cwd==%a' % expected_cwd
125
107
if verbose > 1 :
126
108
print ('Expected output:' )
127
109
print (printed_file )
@@ -135,23 +117,33 @@ def _check_output(self, script_name, exit_code, data,
135
117
self .assertIn (printed_path0 .encode ('utf-8' ), data )
136
118
self .assertIn (printed_cwd .encode ('utf-8' ), data )
137
119
138
- def _check_script (self , script_name , expected_file ,
120
+ def _check_script (self , script_exec_args , expected_file ,
139
121
expected_argv0 , expected_path0 ,
140
122
expected_package , expected_loader ,
141
- * cmd_line_switches ):
123
+ * cmd_line_switches , cwd = None , ** env_vars ):
124
+ if isinstance (script_exec_args , str ):
125
+ script_exec_args = [script_exec_args ]
142
126
run_args = [* support .optim_args_from_interpreter_flags (),
143
- * cmd_line_switches , script_name , * example_args ]
144
- rc , out , err = assert_python_ok (* run_args , __isolated = False )
145
- self ._check_output (script_name , rc , out + err , expected_file ,
127
+ * cmd_line_switches , * script_exec_args , * example_args ]
128
+ rc , out , err = assert_python_ok (
129
+ * run_args , __isolated = False , __cwd = cwd , ** env_vars
130
+ )
131
+ self ._check_output (script_exec_args , rc , out + err , expected_file ,
146
132
expected_argv0 , expected_path0 ,
147
- expected_package , expected_loader )
133
+ expected_package , expected_loader , cwd )
148
134
149
- def _check_import_error (self , script_name , expected_msg ,
150
- * cmd_line_switches ):
151
- run_args = cmd_line_switches + (script_name ,)
152
- rc , out , err = assert_python_failure (* run_args )
135
+ def _check_import_error (self , script_exec_args , expected_msg ,
136
+ * cmd_line_switches , cwd = None , ** env_vars ):
137
+ if isinstance (script_exec_args , str ):
138
+ script_exec_args = (script_exec_args ,)
139
+ else :
140
+ script_exec_args = tuple (script_exec_args )
141
+ run_args = cmd_line_switches + script_exec_args
142
+ rc , out , err = assert_python_failure (
143
+ * run_args , __isolated = False , __cwd = cwd , ** env_vars
144
+ )
153
145
if verbose > 1 :
154
- print ('Output from test script %r:' % script_name )
146
+ print ('Output from test script %r:' % script_exec_args )
155
147
print (repr (err ))
156
148
print ('Expected output: %r' % expected_msg )
157
149
self .assertIn (expected_msg .encode ('utf-8' ), err )
@@ -287,35 +279,35 @@ def test_module_in_package(self):
287
279
pkg_dir = os .path .join (script_dir , 'test_pkg' )
288
280
make_pkg (pkg_dir )
289
281
script_name = _make_test_script (pkg_dir , 'script' )
290
- launch_name = _make_launch_script (script_dir , 'launch' , 'test_pkg.script' )
291
- self ._check_script (launch_name , script_name , script_name ,
282
+ self ._check_script (["-m" , "test_pkg.script" ], script_name , script_name ,
292
283
script_dir , 'test_pkg' ,
293
- importlib .machinery .SourceFileLoader )
284
+ importlib .machinery .SourceFileLoader ,
285
+ cwd = script_dir )
294
286
295
287
def test_module_in_package_in_zipfile (self ):
296
288
with support .temp_dir () as script_dir :
297
289
zip_name , run_name = _make_test_zip_pkg (script_dir , 'test_zip' , 'test_pkg' , 'script' )
298
- launch_name = _make_launch_script ( script_dir , 'launch' , ' test_pkg.script' , zip_name )
299
- self . _check_script ( launch_name , run_name , run_name ,
300
- zip_name , 'test_pkg' , zipimport . zipimporter )
290
+ self . _check_script ([ "-m" , " test_pkg.script" ], run_name , run_name ,
291
+ script_dir , 'test_pkg' , zipimport . zipimporter ,
292
+ PYTHONPATH = zip_name , cwd = script_dir )
301
293
302
294
def test_module_in_subpackage_in_zipfile (self ):
303
295
with support .temp_dir () as script_dir :
304
296
zip_name , run_name = _make_test_zip_pkg (script_dir , 'test_zip' , 'test_pkg' , 'script' , depth = 2 )
305
- launch_name = _make_launch_script ( script_dir , 'launch' , ' test_pkg.test_pkg.script' , zip_name )
306
- self . _check_script ( launch_name , run_name , run_name ,
307
- zip_name , 'test_pkg.test_pkg' ,
308
- zipimport . zipimporter )
297
+ self . _check_script ([ "-m" , " test_pkg.test_pkg.script" ], run_name , run_name ,
298
+ script_dir , 'test_pkg.test_pkg' ,
299
+ zipimport . zipimporter ,
300
+ PYTHONPATH = zip_name , cwd = script_dir )
309
301
310
302
def test_package (self ):
311
303
with support .temp_dir () as script_dir :
312
304
pkg_dir = os .path .join (script_dir , 'test_pkg' )
313
305
make_pkg (pkg_dir )
314
306
script_name = _make_test_script (pkg_dir , '__main__' )
315
- launch_name = _make_launch_script (script_dir , 'launch' , 'test_pkg' )
316
- self ._check_script (launch_name , script_name ,
307
+ self ._check_script (["-m" , "test_pkg" ], script_name ,
317
308
script_name , script_dir , 'test_pkg' ,
318
- importlib .machinery .SourceFileLoader )
309
+ importlib .machinery .SourceFileLoader ,
310
+ cwd = script_dir )
319
311
320
312
def test_package_compiled (self ):
321
313
with support .temp_dir () as script_dir :
@@ -325,19 +317,18 @@ def test_package_compiled(self):
325
317
compiled_name = py_compile .compile (script_name , doraise = True )
326
318
os .remove (script_name )
327
319
pyc_file = support .make_legacy_pyc (script_name )
328
- launch_name = _make_launch_script (script_dir , 'launch' , 'test_pkg' )
329
- self ._check_script (launch_name , pyc_file ,
320
+ self ._check_script (["-m" , "test_pkg" ], pyc_file ,
330
321
pyc_file , script_dir , 'test_pkg' ,
331
- importlib .machinery .SourcelessFileLoader )
322
+ importlib .machinery .SourcelessFileLoader ,
323
+ cwd = script_dir )
332
324
333
325
def test_package_error (self ):
334
326
with support .temp_dir () as script_dir :
335
327
pkg_dir = os .path .join (script_dir , 'test_pkg' )
336
328
make_pkg (pkg_dir )
337
329
msg = ("'test_pkg' is a package and cannot "
338
330
"be directly executed" )
339
- launch_name = _make_launch_script (script_dir , 'launch' , 'test_pkg' )
340
- self ._check_import_error (launch_name , msg )
331
+ self ._check_import_error (["-m" , "test_pkg" ], msg , cwd = script_dir )
341
332
342
333
def test_package_recursion (self ):
343
334
with support .temp_dir () as script_dir :
@@ -348,8 +339,7 @@ def test_package_recursion(self):
348
339
msg = ("Cannot use package as __main__ module; "
349
340
"'test_pkg' is a package and cannot "
350
341
"be directly executed" )
351
- launch_name = _make_launch_script (script_dir , 'launch' , 'test_pkg' )
352
- self ._check_import_error (launch_name , msg )
342
+ self ._check_import_error (["-m" , "test_pkg" ], msg , cwd = script_dir )
353
343
354
344
def test_issue8202 (self ):
355
345
# Make sure package __init__ modules see "-m" in sys.argv0 while
@@ -365,7 +355,7 @@ def test_issue8202(self):
365
355
expected = "init_argv0==%r" % '-m'
366
356
self .assertIn (expected .encode ('utf-8' ), out )
367
357
self ._check_output (script_name , rc , out ,
368
- script_name , script_name , '' , 'test_pkg' ,
358
+ script_name , script_name , script_dir , 'test_pkg' ,
369
359
importlib .machinery .SourceFileLoader )
370
360
371
361
def test_issue8202_dash_c_file_ignored (self ):
@@ -394,7 +384,7 @@ def test_issue8202_dash_m_file_ignored(self):
394
384
rc , out , err = assert_python_ok ('-m' , 'other' , * example_args ,
395
385
__isolated = False )
396
386
self ._check_output (script_name , rc , out ,
397
- script_name , script_name , '' , '' ,
387
+ script_name , script_name , script_dir , '' ,
398
388
importlib .machinery .SourceFileLoader )
399
389
400
390
@contextlib .contextmanager
@@ -627,7 +617,7 @@ def test_consistent_sys_path_for_module_execution(self):
627
617
# direct execution test cases
628
618
p = spawn_python ("-sm" , "script_pkg.__main__" , cwd = work_dir )
629
619
out_by_module = kill_python (p ).decode ().splitlines ()
630
- self .assertEqual (out_by_module [0 ], '' )
620
+ self .assertEqual (out_by_module [0 ], work_dir )
631
621
self .assertNotIn (script_dir , out_by_module )
632
622
# Package execution should give the same output
633
623
p = spawn_python ("-sm" , "script_pkg" , cwd = work_dir )
0 commit comments