-
-
Notifications
You must be signed in to change notification settings - Fork 31.8k
compile(optimize=2) doesn't del docstrings/asserts, ignores -OO & PYTHONOPTIMIZE=2 #112909
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
The doc says "The argument optimize specifies the optimization level of the compiler;". With the If you compile to a code object and exec the code object, the resulting test = '''\
def test() :
"""docstring"""
assert False
'''
exec(compile(
source = test,
filename = "" ,
mode = "exec" ,
# flags = ast.PyCF_ONLY_AST ,
dont_inherit = 1 ,
optimize = 2 ,
))
print(test.__doc__, test()) # prints `None None` Perhaps the doc could be clearer to Python learners, but I am not sure how. |
thank you terryjreedy for the detailed explanation 🙂 |
This is a duplicate of #108113. In #108154 @iritkatriel added |
Bug report
Bug description:
as i understand the compile() documentation ,
the above example should remove the docstring ,
and the assert line , but it doesn't .
setting the env var
PYTHONOPTIMIZE=2
and thenrunning python with the cmdLine option
-OO
this time with
a)
compile( ... , dont_inherit=False , ... )
without optimizeb)
compile( ... , dont_inherit=0 , ... )
without optimizec)
compile( ... )
without : dont_inherit , optimizedidn't chage the outcome :
compile() never removes docstrings/asserts .
looks like
compile()
ignores its argoptimize=2
and
-OO
andPYTHONOPTIMIZE=2
.CPython versions tested on:
3.11.5
Operating systems tested on:
Windows 10 (22H2) 64bit
The text was updated successfully, but these errors were encountered: