Skip to content

Commit 9def284

Browse files
committed
subprocess._optim_args_from_interpreter_flags()
Issue #26100: * Add subprocess._optim_args_from_interpreter_flags() * Add test.support.optim_args_from_interpreter_flags() * Use new functions in distutils, test_cmd_line_script, test_compileall and test_inspect The change enables test_details() test of test_inspect when -O or -OO command line option is used.
1 parent c437d0c commit 9def284

File tree

6 files changed

+35
-18
lines changed

6 files changed

+35
-18
lines changed

Lib/distutils/util.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
import os
88
import re
99
import importlib.util
10-
import sys
1110
import string
11+
import sys
1212
from distutils.errors import DistutilsPlatformError
1313
from distutils.dep_util import newer
1414
from distutils.spawn import spawn
@@ -350,6 +350,11 @@ def byte_compile (py_files,
350350
generated in indirect mode; unless you know what you're doing, leave
351351
it set to None.
352352
"""
353+
354+
# Late import to fix a bootstrap issue: _posixsubprocess is built by
355+
# setup.py, but setup.py uses distutils.
356+
import subprocess
357+
353358
# nothing is done if sys.dont_write_bytecode is True
354359
if sys.dont_write_bytecode:
355360
raise DistutilsByteCompileError('byte-compiling is disabled.')
@@ -412,11 +417,9 @@ def byte_compile (py_files,
412417

413418
script.close()
414419

415-
cmd = [sys.executable, script_name]
416-
if optimize == 1:
417-
cmd.insert(1, "-O")
418-
elif optimize == 2:
419-
cmd.insert(1, "-OO")
420+
cmd = [sys.executable]
421+
cmd.extend(subprocess._optim_args_from_interpreter_flags())
422+
cmd.append(script_name)
420423
spawn(cmd, dry_run=dry_run)
421424
execute(os.remove, (script_name,), "removing %s" % script_name,
422425
dry_run=dry_run)

Lib/subprocess.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -520,23 +520,33 @@ def _cleanup():
520520
# but it's here so that it can be imported when Python is compiled without
521521
# threads.
522522

523+
def _optim_args_from_interpreter_flags():
524+
"""Return a list of command-line arguments reproducing the current
525+
optimization settings in sys.flags."""
526+
args = []
527+
value = sys.flags.optimize
528+
if value > 0:
529+
args.append('-' + 'O' * value)
530+
return args
531+
532+
523533
def _args_from_interpreter_flags():
524534
"""Return a list of command-line arguments reproducing the current
525535
settings in sys.flags and sys.warnoptions."""
526536
flag_opt_map = {
527537
'debug': 'd',
528538
# 'inspect': 'i',
529539
# 'interactive': 'i',
530-
'optimize': 'O',
531540
'dont_write_bytecode': 'B',
532541
'no_user_site': 's',
533542
'no_site': 'S',
534543
'ignore_environment': 'E',
535544
'verbose': 'v',
536545
'bytes_warning': 'b',
537546
'quiet': 'q',
547+
# -O is handled in _optim_args_from_interpreter_flags()
538548
}
539-
args = []
549+
args = _optim_args_from_interpreter_flags()
540550
for flag, opt in flag_opt_map.items():
541551
v = getattr(sys.flags, flag)
542552
if v > 0:

Lib/test/support/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2053,6 +2053,11 @@ def args_from_interpreter_flags():
20532053
settings in sys.flags and sys.warnoptions."""
20542054
return subprocess._args_from_interpreter_flags()
20552055

2056+
def optim_args_from_interpreter_flags():
2057+
"""Return a list of command-line arguments reproducing the current
2058+
optimization settings in sys.flags."""
2059+
return subprocess._optim_args_from_interpreter_flags()
2060+
20562061
#============================================================
20572062
# Support for assertions about logging.
20582063
#============================================================

Lib/test/test_cmd_line_script.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,8 @@ def _check_script(self, script_name, expected_file,
138138
expected_argv0, expected_path0,
139139
expected_package, expected_loader,
140140
*cmd_line_switches):
141-
if not __debug__:
142-
cmd_line_switches += ('-' + 'O' * sys.flags.optimize,)
143-
run_args = cmd_line_switches + (script_name,) + tuple(example_args)
141+
run_args = [*support.optim_args_from_interpreter_flags(),
142+
*cmd_line_switches, script_name, *example_args]
144143
rc, out, err = assert_python_ok(*run_args, __isolated=False)
145144
self._check_output(script_name, rc, out + err, expected_file,
146145
expected_argv0, expected_path0,

Lib/test/test_compileall.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -231,10 +231,9 @@ def _skip_if_sys_path_not_writable(self):
231231
raise unittest.SkipTest('not all entries on sys.path are writable')
232232

233233
def _get_run_args(self, args):
234-
interp_args = ['-S']
235-
if sys.flags.optimize:
236-
interp_args.append({1 : '-O', 2 : '-OO'}[sys.flags.optimize])
237-
return interp_args + ['-m', 'compileall'] + list(args)
234+
return [*support.optim_args_from_interpreter_flags(),
235+
'-S', '-m', 'compileall',
236+
*args]
238237

239238
def assertRunOK(self, *args, **env_vars):
240239
rc, out, err = script_helper.assert_python_ok(

Lib/test/test_inspect.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
from test.support.script_helper import assert_python_ok, assert_python_failure
3131
from test import inspect_fodder as mod
3232
from test import inspect_fodder2 as mod2
33+
from test import support
3334

3435
from test.test_import import _ready_to_import
3536

@@ -3536,14 +3537,14 @@ def test_builtins(self):
35363537

35373538
def test_details(self):
35383539
module = importlib.import_module('unittest')
3539-
rc, out, err = assert_python_ok('-m', 'inspect',
3540+
args = support.optim_args_from_interpreter_flags()
3541+
rc, out, err = assert_python_ok(*args, '-m', 'inspect',
35403542
'unittest', '--details')
35413543
output = out.decode()
35423544
# Just a quick sanity check on the output
35433545
self.assertIn(module.__name__, output)
35443546
self.assertIn(module.__file__, output)
3545-
if not sys.flags.optimize:
3546-
self.assertIn(module.__cached__, output)
3547+
self.assertIn(module.__cached__, output)
35473548
self.assertEqual(err, b'')
35483549

35493550

0 commit comments

Comments
 (0)