@@ -48,7 +48,7 @@ class Regrtest:
4848 directly to set the values that would normally be set by flags
4949 on the command line.
5050 """
51- def __init__ (self , ns : Namespace , reexec : bool = False ):
51+ def __init__ (self , ns : Namespace , _add_python_opts : bool = False ):
5252 # Log verbosity
5353 self .verbose : int = int (ns .verbose )
5454 self .quiet : bool = ns .quiet
@@ -70,7 +70,11 @@ def __init__(self, ns: Namespace, reexec: bool = False):
7070 self .want_cleanup : bool = ns .cleanup
7171 self .want_rerun : bool = ns .rerun
7272 self .want_run_leaks : bool = ns .runleaks
73- self .want_reexec : bool = (reexec and not ns .no_reexec )
73+
74+ ci_mode = (ns .fast_ci or ns .slow_ci )
75+ self .want_add_python_opts : bool = (_add_python_opts
76+ and ns ._add_python_opts
77+ and ci_mode )
7478
7579 # Select tests
7680 if ns .match_tests :
@@ -97,7 +101,6 @@ def __init__(self, ns: Namespace, reexec: bool = False):
97101 self .worker_json : StrJSON | None = ns .worker_json
98102
99103 # Options to run tests
100- self .ci_mode : bool = (ns .fast_ci or ns .slow_ci )
101104 self .fail_fast : bool = ns .failfast
102105 self .fail_env_changed : bool = ns .fail_env_changed
103106 self .fail_rerun : bool = ns .fail_rerun
@@ -486,32 +489,48 @@ def run_tests(self, selected: TestTuple, tests: TestList | None) -> int:
486489 # processes.
487490 return self ._run_tests (selected , tests )
488491
489- def _reexecute_python (self ):
490- if self .python_cmd :
491- # Do nothing if --python=cmd option is used
492- return
492+ def _add_python_opts (self ):
493+ python_opts = []
494+
495+ # Unbuffered stdout and stderr
496+ if not sys .stdout .write_through :
497+ python_opts .append ('-u' )
498+
499+ # Add warnings filter 'default'
500+ if 'default' not in sys .warnoptions :
501+ python_opts .extend (('-W' , 'default' ))
493502
494- python_opts = [
495- '-u' , # Unbuffered stdout and stderr
496- '-W' , 'default' , # Add warnings filter 'default'
497- '-bb' , # Error on bytes/str comparison
498- '-E' , # Ignore PYTHON* environment variables
499- ]
503+ # Error on bytes/str comparison
504+ if sys .flags .bytes_warning < 2 :
505+ python_opts .append ('-bb' )
500506
501- cmd = [* sys .orig_argv , "--no-reexec" ]
507+ # Ignore PYTHON* environment variables
508+ if not sys .flags .ignore_environment :
509+ python_opts .append ('-E' )
510+
511+ if not python_opts :
512+ return
513+
514+ cmd = [* sys .orig_argv , "--dont-add-python-opts" ]
502515 cmd [1 :1 ] = python_opts
503516
504517 # Make sure that messages before execv() are logged
505518 sys .stdout .flush ()
506519 sys .stderr .flush ()
507520
521+ cmd_text = shlex .join (cmd )
508522 try :
509- os .execv (cmd [0 ], cmd )
510- # execv() do no return and so we don't get to this line on success
511- except OSError as exc :
512- cmd_text = shlex .join (cmd )
513- print_warning (f"Failed to reexecute Python: { exc !r} \n "
523+ if hasattr (os , 'execv' ) and not MS_WINDOWS :
524+ os .execv (cmd [0 ], cmd )
525+ # execv() do no return and so we don't get to this line on success
526+ else :
527+ import subprocess
528+ proc = subprocess .run (cmd )
529+ sys .exit (proc .returncode )
530+ except Exception as exc :
531+ print_warning (f"Failed to change Python options: { exc !r} \n "
514532 f"Command: { cmd_text } " )
533+ # continue executing main()
515534
516535 def _init (self ):
517536 # Set sys.stdout encoder error handler to backslashreplace,
@@ -527,8 +546,8 @@ def _init(self):
527546 self .tmp_dir = get_temp_dir (self .tmp_dir )
528547
529548 def main (self , tests : TestList | None = None ):
530- if self .want_reexec and self . ci_mode :
531- self ._reexecute_python ()
549+ if self .want_add_python_opts :
550+ self ._add_python_opts ()
532551
533552 self ._init ()
534553
@@ -556,7 +575,7 @@ def main(self, tests: TestList | None = None):
556575 sys .exit (exitcode )
557576
558577
559- def main (tests = None , reexec = False , ** kwargs ):
578+ def main (tests = None , _add_python_opts = False , ** kwargs ):
560579 """Run the Python suite."""
561580 ns = _parse_args (sys .argv [1 :], ** kwargs )
562- Regrtest (ns , reexec = reexec ).main (tests = tests )
581+ Regrtest (ns , _add_python_opts = _add_python_opts ).main (tests = tests )
0 commit comments