@@ -48,7 +48,7 @@ class Regrtest:
48
48
directly to set the values that would normally be set by flags
49
49
on the command line.
50
50
"""
51
- def __init__ (self , ns : Namespace , reexec : bool = False ):
51
+ def __init__ (self , ns : Namespace , _add_python_opts : bool = False ):
52
52
# Log verbosity
53
53
self .verbose : int = int (ns .verbose )
54
54
self .quiet : bool = ns .quiet
@@ -70,7 +70,11 @@ def __init__(self, ns: Namespace, reexec: bool = False):
70
70
self .want_cleanup : bool = ns .cleanup
71
71
self .want_rerun : bool = ns .rerun
72
72
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 )
74
78
75
79
# Select tests
76
80
if ns .match_tests :
@@ -97,7 +101,6 @@ def __init__(self, ns: Namespace, reexec: bool = False):
97
101
self .worker_json : StrJSON | None = ns .worker_json
98
102
99
103
# Options to run tests
100
- self .ci_mode : bool = (ns .fast_ci or ns .slow_ci )
101
104
self .fail_fast : bool = ns .failfast
102
105
self .fail_env_changed : bool = ns .fail_env_changed
103
106
self .fail_rerun : bool = ns .fail_rerun
@@ -486,32 +489,48 @@ def run_tests(self, selected: TestTuple, tests: TestList | None) -> int:
486
489
# processes.
487
490
return self ._run_tests (selected , tests )
488
491
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' ))
493
502
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' )
500
506
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" ]
502
515
cmd [1 :1 ] = python_opts
503
516
504
517
# Make sure that messages before execv() are logged
505
518
sys .stdout .flush ()
506
519
sys .stderr .flush ()
507
520
521
+ cmd_text = shlex .join (cmd )
508
522
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 "
514
532
f"Command: { cmd_text } " )
533
+ # continue executing main()
515
534
516
535
def _init (self ):
517
536
# Set sys.stdout encoder error handler to backslashreplace,
@@ -527,8 +546,8 @@ def _init(self):
527
546
self .tmp_dir = get_temp_dir (self .tmp_dir )
528
547
529
548
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 ()
532
551
533
552
self ._init ()
534
553
@@ -556,7 +575,7 @@ def main(self, tests: TestList | None = None):
556
575
sys .exit (exitcode )
557
576
558
577
559
- def main (tests = None , reexec = False , ** kwargs ):
578
+ def main (tests = None , _add_python_opts = False , ** kwargs ):
560
579
"""Run the Python suite."""
561
580
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