@@ -569,8 +569,8 @@ def path(self):
569569 """Return the path this PEX was built at."""
570570 return self ._pex
571571
572- def execute (self ):
573- # type: () -> Any
572+ def execute (self , python_args = () ):
573+ # type: (Sequence[str] ) -> Any
574574 """Execute the PEX.
575575
576576 This function makes assumptions that it is the last function called by the interpreter.
@@ -607,14 +607,14 @@ def execute(self):
607607 V = 3 ,
608608 )
609609
610- result = self ._wrap_coverage (self ._wrap_profiling , self ._execute )
610+ result = self ._wrap_coverage (self ._wrap_profiling , self ._execute , python_args )
611611 if "PYTHONINSPECT" not in os .environ :
612612 sys .exit (0 if isinstance (result , Globals ) else result )
613613 else :
614614 return result
615615
616- def _execute (self ):
617- # type: () -> Any
616+ def _execute (self , python_args ):
617+ # type: (Sequence[str] ) -> Any
618618 force_interpreter = self ._vars .PEX_INTERPRETER
619619
620620 self ._clean_environment (strip_pex_env = self ._pex_info .strip_pex_env )
@@ -629,7 +629,7 @@ def _execute(self):
629629
630630 if force_interpreter :
631631 TRACER .log ("PEX_INTERPRETER specified, dropping into interpreter" )
632- return self .execute_interpreter ()
632+ return self .execute_interpreter (python_args )
633633
634634 if not any (
635635 (
@@ -640,7 +640,7 @@ def _execute(self):
640640 )
641641 ):
642642 TRACER .log ("No entry point specified, dropping into interpreter" )
643- return self .execute_interpreter ()
643+ return self .execute_interpreter (python_args )
644644
645645 if self ._pex_info_overrides .script and self ._pex_info_overrides .entry_point :
646646 return "Cannot specify both script and entry_point for a PEX!"
@@ -663,32 +663,35 @@ def _execute(self):
663663 assert self ._pex_info .entry_point
664664 return self .execute_entry (parse_entry_point (self ._pex_info .entry_point ))
665665
666- def execute_interpreter (self ):
667- # type: () -> Any
666+ def execute_interpreter (self , python_args ):
667+ # type: (Sequence[str] ) -> Any
668668
669669 # A Python interpreter always inserts the CWD at the head of the sys.path.
670670 # See https://docs.python.org/3/library/sys.html#sys.path
671671 sys .path .insert (0 , "" )
672672
673673 args = sys .argv [1 :]
674- python_options = []
674+ python_options = list (python_args )
675+ called_with_python_options = False
675676 for index , arg in enumerate (args ):
676677 # Check if the arg is an expected startup arg.
677678 if arg .startswith ("-" ) and arg not in ("-" , "-c" , "-m" ):
678- python_options .append (arg )
679+ # N.B.: In the face of short options that can be combined, this is a naive check.
680+ if arg not in python_options :
681+ python_options .append (arg )
682+ called_with_python_options = True
679683 else :
680684 args = args [index :]
681685 break
682686 else :
683687 # All the args were python options
684688 args = []
685689
686- # The pex was called with Python interpreter options
687- if python_options :
688- return self .execute_with_options (python_options , args )
690+ if called_with_python_options :
691+ return self .re_execute_with_options (python_options , args )
689692
690693 if args :
691- # NB: We take care here to setup sys.argv to match how CPython does it for each case.
694+ # NB: We take care here to set up sys.argv to match how CPython does it for each case.
692695 arg = args [0 ]
693696 if arg == "-c" :
694697 content = args [1 ]
@@ -734,7 +737,7 @@ def execute_interpreter(self):
734737 return Globals (pex_repl ())
735738
736739 @staticmethod
737- def execute_with_options (
740+ def re_execute_with_options (
738741 python_options , # type: List[str]
739742 args , # List[str]
740743 ):
@@ -749,7 +752,7 @@ def execute_with_options(
749752 return "Unable to resolve PEX __main__ module file: {}" .format (main )
750753
751754 python = sys .executable
752- cmdline = [python ] + python_options + [main .__file__ ] + args
755+ cmdline = [python ] + python_options + [os . path . dirname ( main .__file__ ) ] + args
753756 TRACER .log (
754757 "Re-executing with Python interpreter options: cmdline={cmdline!r}" .format (
755758 cmdline = " " .join (cmdline )
0 commit comments