Awesome, that's what I was hoping. Accepted! Congrats and thank you very much for writing the PEP and guiding the discussion.On Fri, Mar 20, 2015 at 4:00 PM, Brett Cannon <bcannon@gmail.com> wrote:On Fri, Mar 20, 2015 at 4:41 PM Guido van Rossum <guido@python.org> wrote:Or would I see this?foo.cpython-35.opt-1.pycI am willing to be the BDFL for this PEP. I have tried to skim the recent discussion (only python-dev) and I don't see much remaining controversy. HOWEVER... The PEP is not clear (or at least too subtle) about the actual name for optimization level 0. If I have foo.py, and I compile it three times with three different optimization levels (no optimization; -O; -OO), and then I look in __pycache__, would I see this:foo.cpython-35.pyc
# (1)
foo.cpython-35.opt-2.pyc
# (2)
foo.cpython-35.opt-0.pyc
foo.cpython-35.opt-1.pyc
foo.cpython-35.opt-2.pyc#1Your lead-in ("I have decided to have the default case of no optimization levels mean that the .pyc file name will have no optimization level specified in the name and thus be just as it is today.") makes me think I should expect (1), but I can't actually pinpoint where the language of the PEP says this.It was meant to be explained by "When no optimization level is specified, the pre-PEP ``.pyc`` file name will be used (i.e., no change in file namesemantics)", but obviously it's a bit too subtle. I just updated the PEP with an explicit list of bytecode file name examples based on no -O, -O, and -OO.-BrettOn Fri, Mar 20, 2015 at 11:34 AM, Brett Cannon <bcannon@gmail.com> wrote:I have decided to have the default case of no optimization levels mean that the .pyc file name will have no optimization level specified in the name and thus be just as it is today. I made this decision due to potential backwards-compatibility issues -- although I expect them to be minutes -- and to not force other implementations like PyPy to have some bogus value set since they don't have .pyo files to begin with (PyPy actually uses bytecode for -O and don't bother with -OO since PyPy already uses a bunch of memory when running).Since this closes out the last open issue, I need either a BDFL decision or a BDFAP to be assigned to make a decision. Guido?======================================PEP: 488Title: Elimination of PYO filesVersion: $Revision$Last-Modified: $Date$Author: Brett Cannon <brett@python.org>Status: DraftType: Standards TrackContent-Type: text/x-rstCreated: 20-Feb-2015Post-History:2015-03-062015-03-132015-03-20Abstract========This PEP proposes eliminating the concept of PYO files from Python.To continue the support of the separation of bytecode files based ontheir optimization level, this PEP proposes extending the PYC filename to include the optimization level in the bytecode repositorydirectory when it's called for (i.e., the ``__pycache__`` directory).Rationale=========As of today, bytecode files come in two flavours: PYC and PYO. A PYCfile is the bytecode file generated and read from when nooptimization level is specified at interpreter startup (i.e., ``-O``is not specified). A PYO file represents the bytecode file that isread/written when **any** optimization level is specified (i.e., when``-O`` **or** ``-OO`` is specified). This means that while PYCfiles clearly delineate the optimization level used when they weregenerated -- namely no optimizations beyond the peepholer -- the sameis not true for PYO files. To put this in terms of optimizationlevels and the file extension:- 0: ``.pyc``- 1 (``-O``): ``.pyo``- 2 (``-OO``): ``.pyo``The reuse of the ``.pyo`` file extension for both level 1 and 2optimizations means that there is no clear way to tell whatoptimization level was used to generate the bytecode file. In termsof reading PYO files, this can lead to an interpreter using a mixtureof optimization levels with its code if the user was not careful tomake sure all PYO files were generated using the same optimizationlevel (typically done by blindly deleting all PYO files and thenusing the `compileall` module to compile all-new PYO files [1]_).This issue is only compounded when people optimize Python code beyondwhat the interpreter natively supports, e.g., using the astoptimizerproject [2]_.In terms of writing PYO files, the need to delete all PYO filesevery time one either changes the optimization level they want to useor are unsure of what optimization was used the last time PYO fileswere generated leads to unnecessary file churn. The change proposedby this PEP also allows for **all** optimization levels to bepre-compiled for bytecode files ahead of time, something that iscurrently impossible thanks to the reuse of the ``.pyo`` fileextension for multiple optimization levels.As for distributing bytecode-only modules, having to distribute both``.pyc`` and ``.pyo`` files is unnecessary for the common use-caseof code obfuscation and smaller file deployments. This means thatbytecode-only modules will only load from their non-optimized``.pyc`` file name.Proposal========To eliminate the ambiguity that PYO files present, this PEP proposeseliminating the concept of PYO files and their accompanying ``.pyo``file extension. To allow for the optimization level to be unambiguousas well as to avoid having to regenerate optimized bytecode filesneedlessly in the `__pycache__` directory, the optimization levelused to generate the bytecode file will be incorporated into thebytecode file name. When no optimization level is specified, thepre-PEP ``.pyc`` file name will be used (i.e., no change in file namesemantics). This increases backwards-compatibility while also beingmore understanding of Python implementations which have no use foroptimization levels (e.g., PyPy[10]_).Currently bytecode file names are created by``importlib.util.cache_from_source()``, approximately using thefollowing expression defined by PEP 3147 [3]_, [4]_, [5]_::'{name}.{cache_tag}.pyc'.format(name=module_name,cache_tag=sys.implementation.cache_tag)This PEP proposes to change the expression when an optimizationlevel is specified to::'{name}.{cache_tag}.opt-{optimization}.pyc'.format(name=module_name,cache_tag=sys.implementation.cache_tag,optimization=str(sys.flags.optimize))The "opt-" prefix was chosen so as to provide a visual separatorfrom the cache tag. The placement of the optimization level afterthe cache tag was chosen to preserve lexicographic sort order ofbytecode file names based on module name and cache tag which willnot vary for a single interpreter. The "opt-" prefix was chosen over"o" so as to be somewhat self-documenting. The "opt-" prefix waschosen over "O" so as to not have any confusion in case "0" was theleading prefix of the optimization level.A period was chosen over a hyphen as a separator so as to distinguishclearly that the optimization level is not part of the interpreterversion as specified by the cache tag. It also lends to the use ofthe period in the file name to delineate semantically differentconcepts.For example, if ``-OO`` had been passed to the interpreter then insteadof ``importlib.cpython-35.pyo`` the file name would be``importlib.cpython-35.opt-2.pyc``.It should be noted that this change in no way affects the performanceof import. Since the import system looks for a single bytecode filebased on the optimization level of the interpreter already andgenerates a new bytecode file if it doesn't exist, the introductionof potentially more bytecode files in the ``__pycache__`` directoryhas no effect in terms of stat calls. The interpreter will continueto look for only a single bytecode file based on the optimizationlevel and thus no increase in stat calls will occur.The only potentially negative result of this PEP is the probableincrease in the number of ``.pyc`` files and thus increase in storageuse. But for platforms where this is an issue,``sys.dont_write_bytecode`` exists to turn off bytecode generation sothat it can be controlled offline.Implementation==============importlib---------As ``importlib.util.cache_from_source()`` is the API that exposesbytecode file paths as well as being directly used by importlib, itrequires the most critical change. As of Python 3.4, the function'ssignature is::importlib.util.cache_from_source(path, debug_override=None)This PEP proposes changing the signature in Python 3.5 to::importlib.util.cache_from_source(path, debug_override=None, *, optimization=None)The introduced ``optimization`` keyword-only parameter will controlwhat optimization level is specified in the file name. If theargument is ``None`` then the current optimization level of theinterpreter will be assumed (including no optimization). Any argumentgiven for ``optimization`` will be passed to ``str()`` and must have``str.isalnum()`` be true, else ``ValueError`` will be raised (thisprevents invalid characters being used in the file name). If theempty string is passed in for ``optimization`` then the addition ofthe optimization will be suppressed, reverting to the file nameformat which predates this PEP.It is expected that beyond Python's own two optimization levels,third-party code will use a hash of optimization names to specify theoptimization level, e.g.``hashlib.sha256(','.join(['no dead code', 'const folding'])).hexdigest()``.While this might lead to long file names, it is assumed that mostusers never look at the contents of the __pycache__ directory and sothis won't be an issue.The ``debug_override`` parameter will be deprecated. As the parameterexpects a boolean, the integer value of the boolean will be used asif it had been provided as the argument to ``optimization`` (a``None`` argument will mean the same as for ``optimization``). Adeprecation warning will be raised when ``debug_override`` is given avalue other than ``None``, but there are no plans for the completeremoval of the parameter at this time (but removal will be no laterthan Python 4).The various module attributes for importlib.machinery which relate tobytecode file suffixes will be updated [7]_. The``DEBUG_BYTECODE_SUFFIXES`` and ``OPTIMIZED_BYTECODE_SUFFIXES`` willboth be documented as deprecated and set to the same value as``BYTECODE_SUFFIXES`` (removal of ``DEBUG_BYTECODE_SUFFIXES`` and``OPTIMIZED_BYTECODE_SUFFIXES`` is not currently planned, but will benot later than Python 4).All various finders and loaders will also be updated as necessary,but updating the previous mentioned parts of importlib should be allthat is required.Rest of the standard library----------------------------The various functions exposed by the ``py_compile`` and``compileall`` functions will be updated as necessary to make surethey follow the new bytecode file name semantics [6]_, [1]_. The CLIfor the ``compileall`` module will not be directly affected (the``-b`` flag will be implicit as it will no longer generate ``.pyo``files when ``-O`` is specified).Compatibility Considerations============================Any code directly manipulating bytecode files from Python 3.2 onwill need to consider the impact of this change on their code (priorto Python 3.2 -- including all of Python 2 -- there was no__pycache__ which already necessitates bifurcating bytecode filehandling support). If code was setting the ``debug_override``argument to ``importlib.util.cache_from_source()`` then care will beneeded if they want the path to a bytecode file with an optimizationlevel of 2. Otherwise only code **not** using``importlib.util.cache_from_source()`` will need updating.As for people who distribute bytecode-only modules (i.e., use abytecode file instead of a source file), they will have to choosewhich optimization level they want their bytecode files to be sincedistributing a ``.pyo`` file with a ``.pyc`` file will no longer beof any use. Since people typically only distribute bytecode files forcode obfuscation purposes or smaller distribution size then onlyhaving to distribute a single ``.pyc`` should actually be beneficialto these use-cases. And since the magic number for bytecode fileschanged in Python 3.5 to support PEP 465 there is no need to supportpre-existing ``.pyo`` files [8]_.Rejected Ideas==============Completely dropping optimization levels from CPython----------------------------------------------------Some have suggested that instead of accommodating the variousoptimization levels in CPython, we should instead drop thementirely. The argument is that significant performance gains wouldoccur from runtime optimizations through something like a JIT and notthrough pre-execution bytecode optimizations.This idea is rejected for this PEP as that ignores the fact thatthere are people who do find the pre-existing optimization levels forCPython useful. It also assumes that no other Python interpreterwould find what this PEP proposes useful.Alternative formatting of the optimization level in the file name-----------------------------------------------------------------Using the "opt-" prefix and placing the optimization level betweenthe cache tag and file extension is not critical. All options whichhave been considered are:* ``importlib.cpython-35.opt-1.pyc``* ``importlib.cpython-35.opt1.pyc``* ``importlib.cpython-35.o1.pyc``* ``importlib.cpython-35.O1.pyc``* ``importlib.cpython-35.1.pyc``* ``importlib.cpython-35-O1.pyc``* ``importlib.O1.cpython-35.pyc``* ``importlib.o1.cpython-35.pyc``* ``importlib.1.cpython-35.pyc``These were initially rejected either because they would change thesort order of bytecode files, possible ambiguity with the cache tag,or were not self-documenting enough. An informal poll was taken andpeople clearly preferred the formatting proposed by the PEP [9]_.Since this topic is non-technical and of personal choice, the issueis considered solved.Embedding the optimization level in the bytecode metadata---------------------------------------------------------Some have suggested that rather than embedding the optimization levelof bytecode in the file name that it be included in the file'smetadata instead. This would mean every interpreter had a single copyof bytecode at any time. Changing the optimization level would thusrequire rewriting the bytecode, but there would also only be a singlefile to care about.This has been rejected due to the fact that Python is often installedas a root-level application and thus modifying the bytecode file formodules in the standard library are always possible. In thissituation integrators would need to guess at what a reasonableoptimization level was for users for any/all situations. Byallowing multiple optimization levels to co-exist simultaneously itfrees integrators from having to guess what users want and allowsusers to utilize the optimization level they want.References==========.. [1] The compileall module.. [2] The astoptimizer project.. [3] ``importlib.util.cache_from_source()``.. [4] Implementation of ``importlib.util.cache_from_source()`` from CPython 3.4.3rc1.. [5] PEP 3147, PYC Repository Directories, Warsaw.. [6] The py_compile module.. [7] The importlib.machinery module.. [8] ``importlib.util.MAGIC_NUMBER``.. [9] Informal poll of file name format options on Google+.. [10] The PyPy ProjectCopyright=========This document has been placed in the public domain...Local Variables:mode: indented-textindent-tabs-mode: nilsentence-end-double-space: tfill-column: 70coding: utf-8End:_______________________________________________
Python-Dev mailing list
Python-Dev@python.org
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: https://mail.python.org/mailman/options/python-dev/guido%40python.org
----Guido van Rossum (python.org/~guido)