Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f39b674

Browse files
authored
bpo-32094: Update subprocess for -X dev (#4480)
Modify subprocess._args_from_interpreter_flags() to handle -X dev option. Add also unit tests for test.support.args_from_interpreter_flags() and test.support.optim_args_from_interpreter_flags().
1 parent 423fd36 commit f39b674

2 files changed

Lines changed: 79 additions & 2 deletions

File tree

Lib/subprocess.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,8 +260,29 @@ def _args_from_interpreter_flags():
260260
v = getattr(sys.flags, flag)
261261
if v > 0:
262262
args.append('-' + opt * v)
263-
for opt in sys.warnoptions:
263+
264+
# -W options
265+
warnoptions = sys.warnoptions
266+
xoptions = getattr(sys, '_xoptions', {})
267+
if 'dev' in xoptions and warnoptions and warnoptions[-1] == 'default':
268+
# special case: -X dev adds 'default' to sys.warnoptions
269+
warnoptions = warnoptions[:-1]
270+
for opt in warnoptions:
264271
args.append('-W' + opt)
272+
273+
# -X options
274+
if 'dev' in xoptions:
275+
args.extend(('-X', 'dev'))
276+
for opt in ('faulthandler', 'tracemalloc', 'importtime',
277+
'showalloccount', 'showrefcount'):
278+
if opt in xoptions:
279+
value = xoptions[opt]
280+
if value is True:
281+
arg = opt
282+
else:
283+
arg = '%s=%s' % (opt, value)
284+
args.extend(('-X', arg))
285+
265286
return args
266287

267288

Lib/test/test_support.py

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import shutil
77
import socket
88
import stat
9+
import subprocess
910
import sys
1011
import tempfile
1112
import time
@@ -426,6 +427,62 @@ def test_reap_children(self):
426427
# pending child process
427428
support.reap_children()
428429

430+
def check_options(self, args, func):
431+
code = f'from test.support import {func}; print(repr({func}()))'
432+
cmd = [sys.executable, *args, '-c', code]
433+
env = {key: value for key, value in os.environ.items()
434+
if not key.startswith('PYTHON')}
435+
proc = subprocess.run(cmd,
436+
stdout=subprocess.PIPE,
437+
stderr=subprocess.DEVNULL,
438+
universal_newlines=True,
439+
env=env)
440+
self.assertEqual(proc.stdout.rstrip(), repr(args))
441+
self.assertEqual(proc.returncode, 0)
442+
443+
def test_args_from_interpreter_flags(self):
444+
# Test test.support.args_from_interpreter_flags()
445+
for opts in (
446+
# no option
447+
[],
448+
# single option
449+
['-B'],
450+
['-s'],
451+
['-S'],
452+
['-E'],
453+
['-v'],
454+
['-b'],
455+
['-q'],
456+
# same option multiple times
457+
['-bb'],
458+
['-vvv'],
459+
# -W options
460+
['-Wignore'],
461+
# -X options
462+
['-X', 'dev'],
463+
['-Wignore', '-X', 'dev'],
464+
['-X', 'faulthandler'],
465+
['-X', 'importtime'],
466+
['-X', 'showalloccount'],
467+
['-X', 'showrefcount'],
468+
['-X', 'tracemalloc'],
469+
['-X', 'tracemalloc=3'],
470+
):
471+
with self.subTest(opts=opts):
472+
self.check_options(opts, 'args_from_interpreter_flags')
473+
474+
def test_optim_args_from_interpreter_flags(self):
475+
# Test test.support.optim_args_from_interpreter_flags()
476+
for opts in (
477+
# no option
478+
[],
479+
['-O'],
480+
['-OO'],
481+
['-OOOO'],
482+
):
483+
with self.subTest(opts=opts):
484+
self.check_options(opts, 'optim_args_from_interpreter_flags')
485+
429486
# XXX -follows a list of untested API
430487
# make_legacy_pyc
431488
# is_resource_enabled
@@ -447,7 +504,6 @@ def test_reap_children(self):
447504
# threading_cleanup
448505
# reap_threads
449506
# strip_python_stderr
450-
# args_from_interpreter_flags
451507
# can_symlink
452508
# skip_unless_symlink
453509
# SuppressCrashReport

0 commit comments

Comments
 (0)