Skip to content

Commit f299abd

Browse files
committed
Issue python#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.
1 parent a63cc21 commit f299abd

56 files changed

Lines changed: 4724 additions & 4614 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Doc/c-api/import.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,9 +183,9 @@ Importing Modules
183183
184184
.. c:function:: long PyImport_GetMagicNumber()
185185
186-
Return the magic number for Python bytecode files (a.k.a. :file:`.pyc` and
187-
:file:`.pyo` files). The magic number should be present in the first four bytes
188-
of the bytecode file, in little-endian byte order. Returns -1 on error.
186+
Return the magic number for Python bytecode files (a.k.a. :file:`.pyc` file).
187+
The magic number should be present in the first four bytes of the bytecode
188+
file, in little-endian byte order. Returns -1 on error.
189189
190190
.. versionchanged:: 3.3
191191
Return value of -1 upon failure.

Doc/distutils/apiref.rst

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1193,12 +1193,12 @@ other utility module.
11931193

11941194
.. function:: byte_compile(py_files[, optimize=0, force=0, prefix=None, base_dir=None, verbose=1, dry_run=0, direct=None])
11951195

1196-
Byte-compile a collection of Python source files to either :file:`.pyc` or
1197-
:file:`.pyo` files in a :file:`__pycache__` subdirectory (see :pep:`3147`).
1196+
Byte-compile a collection of Python source files to :file:`.pyc` files in a
1197+
:file:`__pycache__` subdirectory (see :pep:`3147` and :pep:`488`).
11981198
*py_files* is a list of files to compile; any files that don't end in
11991199
:file:`.py` are silently skipped. *optimize* must be one of the following:
12001200

1201-
* ``0`` - don't optimize (generate :file:`.pyc`)
1201+
* ``0`` - don't optimize
12021202
* ``1`` - normal optimization (like ``python -O``)
12031203
* ``2`` - extra optimization (like ``python -OO``)
12041204

@@ -1222,10 +1222,13 @@ other utility module.
12221222
doing, leave it set to ``None``.
12231223

12241224
.. versionchanged:: 3.2.3
1225-
Create ``.pyc`` or ``.pyo`` files with an :func:`import magic tag
1225+
Create ``.pyc`` files with an :func:`import magic tag
12261226
<imp.get_tag>` in their name, in a :file:`__pycache__` subdirectory
12271227
instead of files without tag in the current directory.
12281228

1229+
.. versionchanged: 3.5
1230+
Create ``.pyc`` files according to :pep:`488`.
1231+
12291232
12301233
.. function:: rfc822_escape(header)
12311234

Doc/distutils/introduction.rst

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,8 @@ module
156156

157157
pure Python module
158158
a module written in Python and contained in a single :file:`.py` file (and
159-
possibly associated :file:`.pyc` and/or :file:`.pyo` files). Sometimes referred
160-
to as a "pure module."
159+
possibly associated :file:`.pyc` files). Sometimes referred to as a
160+
"pure module."
161161

162162
extension module
163163
a module written in the low-level language of the Python implementation: C/C++
@@ -210,5 +210,3 @@ distribution root
210210
the top-level directory of your source tree (or source distribution); the
211211
directory where :file:`setup.py` exists. Generally :file:`setup.py` will be
212212
run from this directory.
213-
214-

Doc/library/compileall.rst

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ compile Python sources.
9393
.. versionchanged:: 3.5
9494
``-q`` option was changed to a multilevel value.
9595

96+
.. versionchanged:: 3.5
97+
``-b`` will always produce a byte-code file ending in ``.pyc``, never
98+
``.pyo``.
99+
96100

97101
There is no command-line option to control the optimization level used by the
98102
:func:`compile` function, because the Python interpreter itself already
@@ -150,6 +154,10 @@ Public functions
150154
.. versionchanged:: 3.5
151155
*quiet* parameter was changed to a multilevel value.
152156

157+
.. versionchanged:: 3.5
158+
The *legacy* parameter only writes out ``.pyc`` files, not ``.pyo`` files
159+
no matter what the value of *optimize* is.
160+
153161
.. function:: compile_file(fullname, ddir=None, force=False, rx=None, quiet=0, legacy=False, optimize=-1)
154162

155163
Compile the file with path *fullname*.
@@ -182,6 +190,10 @@ Public functions
182190
.. versionchanged:: 3.5
183191
*quiet* parameter was changed to a multilevel value.
184192

193+
.. versionchanged:: 3.5
194+
The *legacy* parameter only writes out ``.pyc`` files, not ``.pyo`` files
195+
no matter what the value of *optimize* is.
196+
185197
.. function:: compile_path(skip_curdir=True, maxlevels=0, force=False, quiet=0, legacy=False, optimize=-1)
186198

187199
Byte-compile all the :file:`.py` files found along ``sys.path``. If
@@ -196,6 +208,10 @@ Public functions
196208
.. versionchanged:: 3.5
197209
*quiet* parameter was changed to a multilevel value.
198210

211+
.. versionchanged:: 3.5
212+
The *legacy* parameter only writes out ``.pyc`` files, not ``.pyo`` files
213+
no matter what the value of *optimize* is.
214+
199215
To force a recompile of all the :file:`.py` files in the :file:`Lib/`
200216
subdirectory and all its subdirectories::
201217

Doc/library/imp.rst

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -203,11 +203,9 @@ file paths.
203203
value would be ``/foo/bar/__pycache__/baz.cpython-32.pyc`` for Python 3.2.
204204
The ``cpython-32`` string comes from the current magic tag (see
205205
:func:`get_tag`; if :attr:`sys.implementation.cache_tag` is not defined then
206-
:exc:`NotImplementedError` will be raised). The returned path will end in
207-
``.pyc`` when ``__debug__`` is ``True`` or ``.pyo`` for an optimized Python
208-
(i.e. ``__debug__`` is ``False``). By passing in ``True`` or ``False`` for
209-
*debug_override* you can override the system's value for ``__debug__`` for
210-
extension selection.
206+
:exc:`NotImplementedError` will be raised). By passing in ``True`` or
207+
``False`` for *debug_override* you can override the system's value for
208+
``__debug__``, leading to optimized bytecode.
211209

212210
*path* need not exist.
213211

@@ -218,6 +216,9 @@ file paths.
218216
.. deprecated:: 3.4
219217
Use :func:`importlib.util.cache_from_source` instead.
220218

219+
.. versionchanged:: 3.5
220+
The *debug_override* parameter no longer creates a ``.pyo`` file.
221+
221222

222223
.. function:: source_from_cache(path)
223224

Doc/library/importlib.rst

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -711,21 +711,29 @@ find and load modules.
711711

712712
.. versionadded:: 3.3
713713

714+
.. deprecated:: 3.5
715+
Use :attr:`BYTECODE_SUFFIXES` instead.
716+
714717
.. attribute:: OPTIMIZED_BYTECODE_SUFFIXES
715718

716719
A list of strings representing the file suffixes for optimized bytecode
717720
modules.
718721

719722
.. versionadded:: 3.3
720723

724+
.. deprecated:: 3.5
725+
Use :attr:`BYTECODE_SUFFIXES` instead.
726+
721727
.. attribute:: BYTECODE_SUFFIXES
722728

723729
A list of strings representing the recognized file suffixes for bytecode
724-
modules. Set to either :attr:`DEBUG_BYTECODE_SUFFIXES` or
725-
:attr:`OPTIMIZED_BYTECODE_SUFFIXES` based on whether ``__debug__`` is true.
730+
modules (including the leading dot).
726731

727732
.. versionadded:: 3.3
728733

734+
.. versionchanged:: 3.5
735+
The value is no longer dependent on ``__debug__``.
736+
729737
.. attribute:: EXTENSION_SUFFIXES
730738

731739
A list of strings representing the recognized file suffixes for
@@ -1074,31 +1082,45 @@ an :term:`importer`.
10741082

10751083
.. versionadded:: 3.4
10761084

1077-
.. function:: cache_from_source(path, debug_override=None)
1085+
.. function:: cache_from_source(path, debug_override=None, *, optimization=None)
10781086

1079-
Return the :pep:`3147` path to the byte-compiled file associated with the
1080-
source *path*. For example, if *path* is ``/foo/bar/baz.py`` the return
1087+
Return the :pep:`3147`/:pep:`488` path to the byte-compiled file associated
1088+
with the source *path*. For example, if *path* is ``/foo/bar/baz.py`` the return
10811089
value would be ``/foo/bar/__pycache__/baz.cpython-32.pyc`` for Python 3.2.
10821090
The ``cpython-32`` string comes from the current magic tag (see
10831091
:func:`get_tag`; if :attr:`sys.implementation.cache_tag` is not defined then
1084-
:exc:`NotImplementedError` will be raised). The returned path will end in
1085-
``.pyc`` when ``__debug__`` is ``True`` or ``.pyo`` for an optimized Python
1086-
(i.e. ``__debug__`` is ``False``). By passing in ``True`` or ``False`` for
1087-
*debug_override* you can override the system's value for ``__debug__`` for
1088-
extension selection.
1089-
1090-
*path* need not exist.
1092+
:exc:`NotImplementedError` will be raised).
1093+
1094+
The *optimization* parameter is used to specify the optimization level of the
1095+
bytecode file. An empty string represents no optimization, so
1096+
``/foo/bar/baz.py`` with an *optimization* of ``''`` will result in a
1097+
bytecode path of ``/foo/bar/__pycache__/baz.cpython-32.pyc``. ``None`` causes
1098+
the interpter's optimization level to be used. Any other value's string
1099+
representation being used, so ``/foo/bar/baz.py`` with an *optimization* of
1100+
``2`` will lead to the bytecode path of
1101+
``/foo/bar/__pycache__/baz.cpython-32.opt-2.pyc``. The string representation
1102+
of *optimization* can only be alphanumeric, else :exc:`ValueError` is raised.
1103+
1104+
The *debug_override* parameter is deprecated and can be used to override
1105+
the system's value for ``__debug__``. A ``True`` value is the equivalent of
1106+
setting *optimization* to the empty string. A ``False`` value is the same as
1107+
setting *optimization* to ``1``. If both *debug_override* an *optimization*
1108+
are not ``None`` then :exc:`TypeError` is raised.
10911109

10921110
.. versionadded:: 3.4
10931111

1112+
.. versionchanged ::3.5
1113+
The *optimization* parameter was added and the *debug_override* parameter
1114+
was deprecated.
1115+
10941116
10951117
.. function:: source_from_cache(path)
10961118

10971119
Given the *path* to a :pep:`3147` file name, return the associated source code
10981120
file path. For example, if *path* is
10991121
``/foo/bar/__pycache__/baz.cpython-32.pyc`` the returned path would be
11001122
``/foo/bar/baz.py``. *path* need not exist, however if it does not conform
1101-
to :pep:`3147` format, a ``ValueError`` is raised. If
1123+
to :pep:`3147` or :pep`488` format, a ``ValueError`` is raised. If
11021124
:attr:`sys.implementation.cache_tag` is not defined,
11031125
:exc:`NotImplementedError` is raised.
11041126

Doc/library/py_compile.rst

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ byte-code cache files in the directory containing the source code.
2929
.. function:: compile(file, cfile=None, dfile=None, doraise=False, optimize=-1)
3030

3131
Compile a source file to byte-code and write out the byte-code cache file.
32-
The source code is loaded from the file name *file*. The byte-code is
33-
written to *cfile*, which defaults to the :PEP:`3147` path, ending in
34-
``.pyc`` (``.pyo`` if optimization is enabled in the current interpreter).
32+
The source code is loaded from the file name *file*. The byte-code is
33+
written to *cfile*, which defaults to the :pep:`3147`/:pep`488` path, ending
34+
in ``.pyc``.
3535
For example, if *file* is ``/foo/bar/baz.py`` *cfile* will default to
3636
``/foo/bar/__pycache__/baz.cpython-32.pyc`` for Python 3.2. If *dfile* is
3737
specified, it is used as the name of the source file in error messages when
@@ -68,7 +68,7 @@ byte-code cache files in the directory containing the source code.
6868
.. function:: main(args=None)
6969

7070
Compile several source files. The files named in *args* (or on the command
71-
line, if *args* is ``None``) are compiled and the resulting bytecode is
71+
line, if *args* is ``None``) are compiled and the resulting byte-code is
7272
cached in the normal manner. This function does not search a directory
7373
structure to locate source files; it only compiles files named explicitly.
7474
If ``'-'`` is the only parameter in args, the list of files is taken from
@@ -86,4 +86,3 @@ could not be compiled.
8686

8787
Module :mod:`compileall`
8888
Utilities to compile all Python source files in a directory tree.
89-

Doc/library/sys.rst

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ always available.
167167

168168
.. data:: dont_write_bytecode
169169

170-
If this is true, Python won't try to write ``.pyc`` or ``.pyo`` files on the
170+
If this is true, Python won't try to write ``.pyc`` files on the
171171
import of source modules. This value is initially set to ``True`` or
172172
``False`` depending on the :option:`-B` command line option and the
173173
:envvar:`PYTHONDONTWRITEBYTECODE` environment variable, but you can set it
@@ -1231,4 +1231,3 @@ always available.
12311231
.. rubric:: Citations
12321232

12331233
.. [C99] ISO/IEC 9899:1999. "Programming languages -- C." A public draft of this standard is available at http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf\ .
1234-

Doc/library/tracemalloc.rst

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ Filter
363363
Filter on traces of memory blocks.
364364

365365
See the :func:`fnmatch.fnmatch` function for the syntax of
366-
*filename_pattern*. The ``'.pyc'`` and ``'.pyo'`` file extensions are
366+
*filename_pattern*. The ``'.pyc'`` file extension is
367367
replaced with ``'.py'``.
368368

369369
Examples:
@@ -374,6 +374,10 @@ Filter
374374
:mod:`tracemalloc` module
375375
* ``Filter(False, "<unknown>")`` excludes empty tracebacks
376376

377+
378+
.. versionchanged:: 3.5
379+
The ``'.pyo'`` file extension is no longer replaced with ``'.py'``.
380+
377381
.. attribute:: inclusive
378382

379383
If *inclusive* is ``True`` (include), only trace memory blocks allocated
@@ -631,4 +635,3 @@ Traceback
631635
obj = Object()
632636
File "test.py", line 12
633637
tb = tracemalloc.get_object_traceback(f())
634-

Doc/library/zipfile.rst

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -405,8 +405,7 @@ The :class:`PyZipFile` constructor takes the same parameters as the
405405
archive.
406406

407407
If the *optimize* parameter to :class:`PyZipFile` was not given or ``-1``,
408-
the corresponding file is a :file:`\*.pyo` file if available, else a
409-
:file:`\*.pyc` file, compiling if necessary.
408+
the corresponding file is a :file:`\*.pyc` file, compiling if necessary.
410409

411410
If the *optimize* parameter to :class:`PyZipFile` was ``0``, ``1`` or
412411
``2``, only files with that optimization level (see :func:`compile`) are
@@ -569,4 +568,3 @@ Instances have the following attributes:
569568
.. attribute:: ZipInfo.file_size
570569

571570
Size of the uncompressed file.
572-

0 commit comments

Comments
 (0)