@@ -188,16 +188,21 @@ def find_py(cls):
188
188
)
189
189
return py_exe
190
190
191
+ def get_py_exe (self ):
192
+ if not self .py_exe :
193
+ self .py_exe = self .find_py ()
194
+ return self .py_exe
195
+
191
196
def run_py (self , args , env = None , allow_fail = False , expect_returncode = 0 , argv = None ):
192
197
if not self .py_exe :
193
198
self .py_exe = self .find_py ()
194
199
195
200
ignore = {"VIRTUAL_ENV" , "PY_PYTHON" , "PY_PYTHON2" , "PY_PYTHON3" }
196
201
env = {
197
202
** {k .upper (): v for k , v in os .environ .items () if k .upper () not in ignore },
198
- ** {k .upper (): v for k , v in (env or {}).items ()},
199
203
"PYLAUNCHER_DEBUG" : "1" ,
200
204
"PYLAUNCHER_DRYRUN" : "1" ,
205
+ ** {k .upper (): v for k , v in (env or {}).items ()},
201
206
}
202
207
if not argv :
203
208
argv = [self .py_exe , * args ]
@@ -497,61 +502,93 @@ def test_virtualenv_with_env(self):
497
502
498
503
def test_py_shebang (self ):
499
504
with self .py_ini (TEST_PY_COMMANDS ):
500
- with self .script ("#! /usr/bin/env python -prearg" ) as script :
505
+ with self .script ("#! /usr/bin/python -prearg" ) as script :
501
506
data = self .run_py ([script , "-postarg" ])
502
507
self .assertEqual ("PythonTestSuite" , data ["SearchInfo.company" ])
503
508
self .assertEqual ("3.100" , data ["SearchInfo.tag" ])
504
509
self .assertEqual (f"X.Y.exe -prearg { script } -postarg" , data ["stdout" ].strip ())
505
510
506
511
def test_py2_shebang (self ):
507
512
with self .py_ini (TEST_PY_COMMANDS ):
508
- with self .script ("#! /usr/bin/env python2 -prearg" ) as script :
513
+ with self .script ("#! /usr/bin/python2 -prearg" ) as script :
509
514
data = self .run_py ([script , "-postarg" ])
510
515
self .assertEqual ("PythonTestSuite" , data ["SearchInfo.company" ])
511
516
self .assertEqual ("3.100-32" , data ["SearchInfo.tag" ])
512
517
self .assertEqual (f"X.Y-32.exe -prearg { script } -postarg" , data ["stdout" ].strip ())
513
518
514
519
def test_py3_shebang (self ):
515
520
with self .py_ini (TEST_PY_COMMANDS ):
516
- with self .script ("#! /usr/bin/env python3 -prearg" ) as script :
521
+ with self .script ("#! /usr/bin/python3 -prearg" ) as script :
517
522
data = self .run_py ([script , "-postarg" ])
518
523
self .assertEqual ("PythonTestSuite" , data ["SearchInfo.company" ])
519
524
self .assertEqual ("3.100-arm64" , data ["SearchInfo.tag" ])
520
525
self .assertEqual (f"X.Y-arm64.exe -X fake_arg_for_test -prearg { script } -postarg" , data ["stdout" ].strip ())
521
526
522
527
def test_py_shebang_nl (self ):
523
528
with self .py_ini (TEST_PY_COMMANDS ):
524
- with self .script ("#! /usr/bin/env python -prearg\n " ) as script :
529
+ with self .script ("#! /usr/bin/python -prearg\n " ) as script :
525
530
data = self .run_py ([script , "-postarg" ])
526
531
self .assertEqual ("PythonTestSuite" , data ["SearchInfo.company" ])
527
532
self .assertEqual ("3.100" , data ["SearchInfo.tag" ])
528
533
self .assertEqual (f"X.Y.exe -prearg { script } -postarg" , data ["stdout" ].strip ())
529
534
530
535
def test_py2_shebang_nl (self ):
531
536
with self .py_ini (TEST_PY_COMMANDS ):
532
- with self .script ("#! /usr/bin/env python2 -prearg\n " ) as script :
537
+ with self .script ("#! /usr/bin/python2 -prearg\n " ) as script :
533
538
data = self .run_py ([script , "-postarg" ])
534
539
self .assertEqual ("PythonTestSuite" , data ["SearchInfo.company" ])
535
540
self .assertEqual ("3.100-32" , data ["SearchInfo.tag" ])
536
541
self .assertEqual (f"X.Y-32.exe -prearg { script } -postarg" , data ["stdout" ].strip ())
537
542
538
543
def test_py3_shebang_nl (self ):
539
544
with self .py_ini (TEST_PY_COMMANDS ):
540
- with self .script ("#! /usr/bin/env python3 -prearg\n " ) as script :
545
+ with self .script ("#! /usr/bin/python3 -prearg\n " ) as script :
541
546
data = self .run_py ([script , "-postarg" ])
542
547
self .assertEqual ("PythonTestSuite" , data ["SearchInfo.company" ])
543
548
self .assertEqual ("3.100-arm64" , data ["SearchInfo.tag" ])
544
549
self .assertEqual (f"X.Y-arm64.exe -X fake_arg_for_test -prearg { script } -postarg" , data ["stdout" ].strip ())
545
550
546
551
def test_py_shebang_short_argv0 (self ):
547
552
with self .py_ini (TEST_PY_COMMANDS ):
548
- with self .script ("#! /usr/bin/env python -prearg" ) as script :
553
+ with self .script ("#! /usr/bin/python -prearg" ) as script :
549
554
# Override argv to only pass "py.exe" as the command
550
555
data = self .run_py ([script , "-postarg" ], argv = f'"py.exe" "{ script } " -postarg' )
551
556
self .assertEqual ("PythonTestSuite" , data ["SearchInfo.company" ])
552
557
self .assertEqual ("3.100" , data ["SearchInfo.tag" ])
553
558
self .assertEqual (f'X.Y.exe -prearg "{ script } " -postarg' , data ["stdout" ].strip ())
554
559
560
+ def test_search_path (self ):
561
+ stem = Path (sys .executable ).stem
562
+ with self .py_ini (TEST_PY_COMMANDS ):
563
+ with self .script (f"#! /usr/bin/env { stem } -prearg" ) as script :
564
+ data = self .run_py (
565
+ [script , "-postarg" ],
566
+ env = {"PATH" : f"{ Path (sys .executable ).parent } ;{ os .getenv ('PATH' )} " },
567
+ )
568
+ self .assertEqual (f"{ sys .executable } -prearg { script } -postarg" , data ["stdout" ].strip ())
569
+
570
+ def test_search_path_exe (self ):
571
+ # Leave the .exe on the name to ensure we don't add it a second time
572
+ name = Path (sys .executable ).name
573
+ with self .py_ini (TEST_PY_COMMANDS ):
574
+ with self .script (f"#! /usr/bin/env { name } -prearg" ) as script :
575
+ data = self .run_py (
576
+ [script , "-postarg" ],
577
+ env = {"PATH" : f"{ Path (sys .executable ).parent } ;{ os .getenv ('PATH' )} " },
578
+ )
579
+ self .assertEqual (f"{ sys .executable } -prearg { script } -postarg" , data ["stdout" ].strip ())
580
+
581
+ def test_recursive_search_path (self ):
582
+ stem = self .get_py_exe ().stem
583
+ with self .py_ini (TEST_PY_COMMANDS ):
584
+ with self .script (f"#! /usr/bin/env { stem } " ) as script :
585
+ data = self .run_py (
586
+ [script ],
587
+ env = {"PATH" : f"{ self .get_py_exe ().parent } ;{ os .getenv ('PATH' )} " },
588
+ )
589
+ # The recursive search is ignored and we get normal "py" behavior
590
+ self .assertEqual (f"X.Y.exe { script } " , data ["stdout" ].strip ())
591
+
555
592
def test_install (self ):
556
593
data = self .run_py (["-V:3.10" ], env = {"PYLAUNCHER_ALWAYS_INSTALL" : "1" }, expect_returncode = 111 )
557
594
cmd = data ["stdout" ].strip ()
0 commit comments