mirror of
https://github.com/python/cpython.git
synced 2025-10-09 16:34:44 +00:00
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.
This commit is contained in:
parent
c437d0cb4e
commit
9def284387
6 changed files with 35 additions and 18 deletions
|
@ -7,8 +7,8 @@ one of the other *util.py modules.
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
import importlib.util
|
import importlib.util
|
||||||
import sys
|
|
||||||
import string
|
import string
|
||||||
|
import sys
|
||||||
from distutils.errors import DistutilsPlatformError
|
from distutils.errors import DistutilsPlatformError
|
||||||
from distutils.dep_util import newer
|
from distutils.dep_util import newer
|
||||||
from distutils.spawn import spawn
|
from distutils.spawn import spawn
|
||||||
|
@ -350,6 +350,11 @@ def byte_compile (py_files,
|
||||||
generated in indirect mode; unless you know what you're doing, leave
|
generated in indirect mode; unless you know what you're doing, leave
|
||||||
it set to None.
|
it set to None.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
# Late import to fix a bootstrap issue: _posixsubprocess is built by
|
||||||
|
# setup.py, but setup.py uses distutils.
|
||||||
|
import subprocess
|
||||||
|
|
||||||
# nothing is done if sys.dont_write_bytecode is True
|
# nothing is done if sys.dont_write_bytecode is True
|
||||||
if sys.dont_write_bytecode:
|
if sys.dont_write_bytecode:
|
||||||
raise DistutilsByteCompileError('byte-compiling is disabled.')
|
raise DistutilsByteCompileError('byte-compiling is disabled.')
|
||||||
|
@ -412,11 +417,9 @@ byte_compile(files, optimize=%r, force=%r,
|
||||||
|
|
||||||
script.close()
|
script.close()
|
||||||
|
|
||||||
cmd = [sys.executable, script_name]
|
cmd = [sys.executable]
|
||||||
if optimize == 1:
|
cmd.extend(subprocess._optim_args_from_interpreter_flags())
|
||||||
cmd.insert(1, "-O")
|
cmd.append(script_name)
|
||||||
elif optimize == 2:
|
|
||||||
cmd.insert(1, "-OO")
|
|
||||||
spawn(cmd, dry_run=dry_run)
|
spawn(cmd, dry_run=dry_run)
|
||||||
execute(os.remove, (script_name,), "removing %s" % script_name,
|
execute(os.remove, (script_name,), "removing %s" % script_name,
|
||||||
dry_run=dry_run)
|
dry_run=dry_run)
|
||||||
|
|
|
@ -520,6 +520,16 @@ DEVNULL = -3
|
||||||
# but it's here so that it can be imported when Python is compiled without
|
# but it's here so that it can be imported when Python is compiled without
|
||||||
# threads.
|
# threads.
|
||||||
|
|
||||||
|
def _optim_args_from_interpreter_flags():
|
||||||
|
"""Return a list of command-line arguments reproducing the current
|
||||||
|
optimization settings in sys.flags."""
|
||||||
|
args = []
|
||||||
|
value = sys.flags.optimize
|
||||||
|
if value > 0:
|
||||||
|
args.append('-' + 'O' * value)
|
||||||
|
return args
|
||||||
|
|
||||||
|
|
||||||
def _args_from_interpreter_flags():
|
def _args_from_interpreter_flags():
|
||||||
"""Return a list of command-line arguments reproducing the current
|
"""Return a list of command-line arguments reproducing the current
|
||||||
settings in sys.flags and sys.warnoptions."""
|
settings in sys.flags and sys.warnoptions."""
|
||||||
|
@ -527,7 +537,6 @@ def _args_from_interpreter_flags():
|
||||||
'debug': 'd',
|
'debug': 'd',
|
||||||
# 'inspect': 'i',
|
# 'inspect': 'i',
|
||||||
# 'interactive': 'i',
|
# 'interactive': 'i',
|
||||||
'optimize': 'O',
|
|
||||||
'dont_write_bytecode': 'B',
|
'dont_write_bytecode': 'B',
|
||||||
'no_user_site': 's',
|
'no_user_site': 's',
|
||||||
'no_site': 'S',
|
'no_site': 'S',
|
||||||
|
@ -535,8 +544,9 @@ def _args_from_interpreter_flags():
|
||||||
'verbose': 'v',
|
'verbose': 'v',
|
||||||
'bytes_warning': 'b',
|
'bytes_warning': 'b',
|
||||||
'quiet': 'q',
|
'quiet': 'q',
|
||||||
|
# -O is handled in _optim_args_from_interpreter_flags()
|
||||||
}
|
}
|
||||||
args = []
|
args = _optim_args_from_interpreter_flags()
|
||||||
for flag, opt in flag_opt_map.items():
|
for flag, opt in flag_opt_map.items():
|
||||||
v = getattr(sys.flags, flag)
|
v = getattr(sys.flags, flag)
|
||||||
if v > 0:
|
if v > 0:
|
||||||
|
|
|
@ -2053,6 +2053,11 @@ def args_from_interpreter_flags():
|
||||||
settings in sys.flags and sys.warnoptions."""
|
settings in sys.flags and sys.warnoptions."""
|
||||||
return subprocess._args_from_interpreter_flags()
|
return subprocess._args_from_interpreter_flags()
|
||||||
|
|
||||||
|
def optim_args_from_interpreter_flags():
|
||||||
|
"""Return a list of command-line arguments reproducing the current
|
||||||
|
optimization settings in sys.flags."""
|
||||||
|
return subprocess._optim_args_from_interpreter_flags()
|
||||||
|
|
||||||
#============================================================
|
#============================================================
|
||||||
# Support for assertions about logging.
|
# Support for assertions about logging.
|
||||||
#============================================================
|
#============================================================
|
||||||
|
|
|
@ -138,9 +138,8 @@ class CmdLineTest(unittest.TestCase):
|
||||||
expected_argv0, expected_path0,
|
expected_argv0, expected_path0,
|
||||||
expected_package, expected_loader,
|
expected_package, expected_loader,
|
||||||
*cmd_line_switches):
|
*cmd_line_switches):
|
||||||
if not __debug__:
|
run_args = [*support.optim_args_from_interpreter_flags(),
|
||||||
cmd_line_switches += ('-' + 'O' * sys.flags.optimize,)
|
*cmd_line_switches, script_name, *example_args]
|
||||||
run_args = cmd_line_switches + (script_name,) + tuple(example_args)
|
|
||||||
rc, out, err = assert_python_ok(*run_args, __isolated=False)
|
rc, out, err = assert_python_ok(*run_args, __isolated=False)
|
||||||
self._check_output(script_name, rc, out + err, expected_file,
|
self._check_output(script_name, rc, out + err, expected_file,
|
||||||
expected_argv0, expected_path0,
|
expected_argv0, expected_path0,
|
||||||
|
|
|
@ -231,10 +231,9 @@ class CommandLineTests(unittest.TestCase):
|
||||||
raise unittest.SkipTest('not all entries on sys.path are writable')
|
raise unittest.SkipTest('not all entries on sys.path are writable')
|
||||||
|
|
||||||
def _get_run_args(self, args):
|
def _get_run_args(self, args):
|
||||||
interp_args = ['-S']
|
return [*support.optim_args_from_interpreter_flags(),
|
||||||
if sys.flags.optimize:
|
'-S', '-m', 'compileall',
|
||||||
interp_args.append({1 : '-O', 2 : '-OO'}[sys.flags.optimize])
|
*args]
|
||||||
return interp_args + ['-m', 'compileall'] + list(args)
|
|
||||||
|
|
||||||
def assertRunOK(self, *args, **env_vars):
|
def assertRunOK(self, *args, **env_vars):
|
||||||
rc, out, err = script_helper.assert_python_ok(
|
rc, out, err = script_helper.assert_python_ok(
|
||||||
|
|
|
@ -30,6 +30,7 @@ from test.support import MISSING_C_DOCSTRINGS, cpython_only
|
||||||
from test.support.script_helper import assert_python_ok, assert_python_failure
|
from test.support.script_helper import assert_python_ok, assert_python_failure
|
||||||
from test import inspect_fodder as mod
|
from test import inspect_fodder as mod
|
||||||
from test import inspect_fodder2 as mod2
|
from test import inspect_fodder2 as mod2
|
||||||
|
from test import support
|
||||||
|
|
||||||
from test.test_import import _ready_to_import
|
from test.test_import import _ready_to_import
|
||||||
|
|
||||||
|
@ -3536,14 +3537,14 @@ class TestMain(unittest.TestCase):
|
||||||
|
|
||||||
def test_details(self):
|
def test_details(self):
|
||||||
module = importlib.import_module('unittest')
|
module = importlib.import_module('unittest')
|
||||||
rc, out, err = assert_python_ok('-m', 'inspect',
|
args = support.optim_args_from_interpreter_flags()
|
||||||
|
rc, out, err = assert_python_ok(*args, '-m', 'inspect',
|
||||||
'unittest', '--details')
|
'unittest', '--details')
|
||||||
output = out.decode()
|
output = out.decode()
|
||||||
# Just a quick sanity check on the output
|
# Just a quick sanity check on the output
|
||||||
self.assertIn(module.__name__, output)
|
self.assertIn(module.__name__, output)
|
||||||
self.assertIn(module.__file__, output)
|
self.assertIn(module.__file__, output)
|
||||||
if not sys.flags.optimize:
|
self.assertIn(module.__cached__, output)
|
||||||
self.assertIn(module.__cached__, output)
|
|
||||||
self.assertEqual(err, b'')
|
self.assertEqual(err, b'')
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue