Commit 6b108b84 authored by Brett Cannon's avatar Brett Cannon

Issue #23731: Implement PEP 488.

The concept of .pyo files no longer exists. Now .pyc files have an
optional `opt-` tag which specifies if any extra optimizations beyond
the peepholer were applied.
parent 5c5812df
...@@ -314,10 +314,10 @@ class build_py (Command): ...@@ -314,10 +314,10 @@ class build_py (Command):
if include_bytecode: if include_bytecode:
if self.compile: if self.compile:
outputs.append(importlib.util.cache_from_source( outputs.append(importlib.util.cache_from_source(
filename, debug_override=True)) filename, optimization=''))
if self.optimize > 0: if self.optimize > 0:
outputs.append(importlib.util.cache_from_source( outputs.append(importlib.util.cache_from_source(
filename, debug_override=False)) filename, optimization=self.optimize))
outputs += [ outputs += [
os.path.join(build_dir, filename) os.path.join(build_dir, filename)
......
...@@ -22,15 +22,15 @@ class install_lib(Command): ...@@ -22,15 +22,15 @@ class install_lib(Command):
# possible scenarios: # possible scenarios:
# 1) no compilation at all (--no-compile --no-optimize) # 1) no compilation at all (--no-compile --no-optimize)
# 2) compile .pyc only (--compile --no-optimize; default) # 2) compile .pyc only (--compile --no-optimize; default)
# 3) compile .pyc and "level 1" .pyo (--compile --optimize) # 3) compile .pyc and "opt-1" .pyc (--compile --optimize)
# 4) compile "level 1" .pyo only (--no-compile --optimize) # 4) compile "opt-1" .pyc only (--no-compile --optimize)
# 5) compile .pyc and "level 2" .pyo (--compile --optimize-more) # 5) compile .pyc and "opt-2" .pyc (--compile --optimize-more)
# 6) compile "level 2" .pyo only (--no-compile --optimize-more) # 6) compile "opt-2" .pyc only (--no-compile --optimize-more)
# #
# The UI for this is two option, 'compile' and 'optimize'. # The UI for this is two options, 'compile' and 'optimize'.
# 'compile' is strictly boolean, and only decides whether to # 'compile' is strictly boolean, and only decides whether to
# generate .pyc files. 'optimize' is three-way (0, 1, or 2), and # generate .pyc files. 'optimize' is three-way (0, 1, or 2), and
# decides both whether to generate .pyo files and what level of # decides both whether to generate .pyc files and what level of
# optimization to use. # optimization to use.
user_options = [ user_options = [
...@@ -166,10 +166,10 @@ class install_lib(Command): ...@@ -166,10 +166,10 @@ class install_lib(Command):
continue continue
if self.compile: if self.compile:
bytecode_files.append(importlib.util.cache_from_source( bytecode_files.append(importlib.util.cache_from_source(
py_file, debug_override=True)) py_file, optimization=''))
if self.optimize > 0: if self.optimize > 0:
bytecode_files.append(importlib.util.cache_from_source( bytecode_files.append(importlib.util.cache_from_source(
py_file, debug_override=False)) py_file, optimization=self.optimize))
return bytecode_files return bytecode_files
......
...@@ -120,8 +120,8 @@ class BuildPyTestCase(support.TempdirManager, ...@@ -120,8 +120,8 @@ class BuildPyTestCase(support.TempdirManager,
found = os.listdir(cmd.build_lib) found = os.listdir(cmd.build_lib)
self.assertEqual(sorted(found), ['__pycache__', 'boiledeggs.py']) self.assertEqual(sorted(found), ['__pycache__', 'boiledeggs.py'])
found = os.listdir(os.path.join(cmd.build_lib, '__pycache__')) found = os.listdir(os.path.join(cmd.build_lib, '__pycache__'))
self.assertEqual(sorted(found), expect = 'boiledeggs.{}.opt-1.pyc'.format(sys.implementation.cache_tag)
['boiledeggs.%s.pyo' % sys.implementation.cache_tag]) self.assertEqual(sorted(found), [expect])
def test_dir_in_package_data(self): def test_dir_in_package_data(self):
""" """
......
...@@ -44,12 +44,11 @@ class InstallLibTestCase(support.TempdirManager, ...@@ -44,12 +44,11 @@ class InstallLibTestCase(support.TempdirManager,
f = os.path.join(project_dir, 'foo.py') f = os.path.join(project_dir, 'foo.py')
self.write_file(f, '# python file') self.write_file(f, '# python file')
cmd.byte_compile([f]) cmd.byte_compile([f])
pyc_file = importlib.util.cache_from_source('foo.py', pyc_file = importlib.util.cache_from_source('foo.py', optimization='')
debug_override=True) pyc_opt_file = importlib.util.cache_from_source('foo.py',
pyo_file = importlib.util.cache_from_source('foo.py', optimization=cmd.optimize)
debug_override=False)
self.assertTrue(os.path.exists(pyc_file)) self.assertTrue(os.path.exists(pyc_file))
self.assertTrue(os.path.exists(pyo_file)) self.assertTrue(os.path.exists(pyc_opt_file))
def test_get_outputs(self): def test_get_outputs(self):
project_dir, dist = self.create_dist() project_dir, dist = self.create_dist()
...@@ -66,8 +65,8 @@ class InstallLibTestCase(support.TempdirManager, ...@@ -66,8 +65,8 @@ class InstallLibTestCase(support.TempdirManager,
cmd.distribution.packages = ['spam'] cmd.distribution.packages = ['spam']
cmd.distribution.script_name = 'setup.py' cmd.distribution.script_name = 'setup.py'
# get_outputs should return 4 elements: spam/__init__.py, .pyc and # get_outputs should return 4 elements: spam/__init__.py and .pyc,
# .pyo, foo.import-tag-abiflags.so / foo.pyd # foo.import-tag-abiflags.so / foo.pyd
outputs = cmd.get_outputs() outputs = cmd.get_outputs()
self.assertEqual(len(outputs), 4, outputs) self.assertEqual(len(outputs), 4, outputs)
......
...@@ -322,11 +322,11 @@ def byte_compile (py_files, ...@@ -322,11 +322,11 @@ def byte_compile (py_files,
prefix=None, base_dir=None, prefix=None, base_dir=None,
verbose=1, dry_run=0, verbose=1, dry_run=0,
direct=None): direct=None):
"""Byte-compile a collection of Python source files to either .pyc """Byte-compile a collection of Python source files to .pyc
or .pyo files in a __pycache__ subdirectory. 'py_files' is a list files in a __pycache__ subdirectory. 'py_files' is a list
of files to compile; any files that don't end in ".py" are silently of files to compile; any files that don't end in ".py" are silently
skipped. 'optimize' must be one of the following: skipped. 'optimize' must be one of the following:
0 - don't optimize (generate .pyc) 0 - don't optimize
1 - normal optimization (like "python -O") 1 - normal optimization (like "python -O")
2 - extra optimization (like "python -OO") 2 - extra optimization (like "python -OO")
If 'force' is true, all files are recompiled regardless of If 'force' is true, all files are recompiled regardless of
...@@ -438,8 +438,9 @@ byte_compile(files, optimize=%r, force=%r, ...@@ -438,8 +438,9 @@ byte_compile(files, optimize=%r, force=%r,
# cfile - byte-compiled file # cfile - byte-compiled file
# dfile - purported source filename (same as 'file' by default) # dfile - purported source filename (same as 'file' by default)
if optimize >= 0: if optimize >= 0:
opt = '' if optimize == 0 else optimize
cfile = importlib.util.cache_from_source( cfile = importlib.util.cache_from_source(
file, debug_override=not optimize) file, optimization=opt)
else: else:
cfile = importlib.util.cache_from_source(file) cfile = importlib.util.cache_from_source(file)
dfile = file dfile = file
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment