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