This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Switching from -OO to -O still uses cached bytecode
Type: Stage:
Components: Versions: Python 2.7
process
Status: closed Resolution: duplicate
Dependencies: Superseder: pyo's are not overwritten by different optimization levels
View: 1538778
Assigned To: Nosy List: Matthew.Fernandez, barry, brett.cannon, pitrou
Priority: normal Keywords:

Created on 2014-05-12 23:56 by Matthew.Fernandez, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (4)
msg218385 - (view) Author: Matthew Fernandez (Matthew.Fernandez) Date: 2014-05-12 23:56
Perhaps others wouldn't consider this a bug, but it was definitely surprising to me. When switching between optimisation levels -OO (optimise and strip docstrings) and -O (just optimise), you will find the docstrings are still stripped. E.g.

$ ls
hello.py world.py
$ cat hello.py
import world
$ cat world.py
def foo():
    '''I'm a docstring'''
    pass

import sys
print 'optimization: %d' % sys.flags.optimize
if sys.flags.optimize < 2:
    print foo.__doc__
else:
    print 'optimisation is enabled; no docstrings'
$ python -OO hello.py
optimization: 2
optimisation is enabled; no docstrings
$ python -O hello.py
optimization: 1
None
$ rm world.pyo
$ python -O hello.py
optimization: 1
I'm a docstring

Is this behaviour intentional?
msg218387 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2014-05-13 00:04
Not really intentional, it's just a limitation of the current implementation (where the same .pyo files are used for both -O and -OO). Already reported as issue1538778 (which was closed as won't fix).
msg218390 - (view) Author: Matthew Fernandez (Matthew.Fernandez) Date: 2014-05-13 00:22
Ah thanks, Antoine. Sorry, failed to find that issue in my prior searching.
msg218465 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2014-05-13 15:32
It's something that could potentially be changed in the future, but no one has prioritized it enough to worry about it. Typically this opens a pandora's box of wanting to generalize it so it supports custom user optimizations as well and then people lose motivation to push it through to the end. =)
History
Date User Action Args
2022-04-11 14:58:03adminsetgithub: 65688
2014-05-13 15:32:17brett.cannonsetstatus: open -> closed

nosy: + brett.cannon
messages: + msg218465

resolution: duplicate
2014-05-13 00:22:18Matthew.Fernandezsetmessages: + msg218390
2014-05-13 00:04:51pitrousetnosy: + barry, pitrou
superseder: pyo's are not overwritten by different optimization levels
messages: + msg218387
2014-05-12 23:56:36Matthew.Fernandezcreate