Skip to content

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

Closed
dreamflow opened this issue Dec 9, 2023 · 3 comments
Labels
pending The issue will be closed if no feedback is provided

Comments

@dreamflow
Copy link
Contributor

dreamflow commented Dec 9, 2023

Bug report

Bug description:

import ast
py_test_source = '''
def test() :
    """docstring"""
    assert True
    ...
'''
ast_obj = compile( # https://docs.python.org/library/functions.html#compile
    source = py_test_source ,
    filename = "" ,
    mode = "exec" ,
    flags = ast.PyCF_ONLY_AST ,
    dont_inherit = 1 ,
    optimize = 2 ,
) 
print( ast.unparse( ast_obj ) )

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 then
running python with the cmdLine option -OO
this time with
a) compile( ... , dont_inherit=False , ... ) without optimize
b) compile( ... , dont_inherit=0 , ... ) without optimize
c) compile( ... ) without : dont_inherit , optimize
didn't chage the outcome :
compile() never removes docstrings/asserts .

looks like compile() ignores its arg optimize=2
and -OO and PYTHONOPTIMIZE=2 .

CPython versions tested on:

3.11.5

Operating systems tested on:

Windows 10 (22H2) 64bit

@dreamflow dreamflow added the type-bug An unexpected behavior, bug, or error label Dec 9, 2023
@dreamflow dreamflow changed the title compile( optimize=2 ) doesn't del docstrings , ignores -OO & PYTHONOPTIMIZE=2 compile( optimize=2 ) doesn't del asserts/docstrings , ignores -OO & PYTHONOPTIMIZE=2 Dec 9, 2023
@dreamflow dreamflow changed the title compile( optimize=2 ) doesn't del asserts/docstrings , ignores -OO & PYTHONOPTIMIZE=2 compile( optimize=2 ) doesn't del docstrings/asserts , ignores -OO & PYTHONOPTIMIZE=2 Dec 9, 2023
@terryjreedy terryjreedy added pending The issue will be closed if no feedback is provided and removed type-bug An unexpected behavior, bug, or error labels Dec 10, 2023
@terryjreedy
Copy link
Member

The doc says "The argument optimize specifies the optimization level of the compiler;". With the PyCF_ONLY_AST, you are only running the parser, not the compiler itself. If the parser stripped docstrings and assert statements, then unparse would not work right. Also, def f(): "docstring"` would not compile as the docstring is, here, the required non-empty suite.

If you compile to a code object and exec the code object, the resulting test function object will not have a docstring or assert.

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.

@dreamflow
Copy link
Contributor Author

dreamflow commented Dec 10, 2023

thank you terryjreedy for the detailed explanation 🙂

@hugovk hugovk changed the title compile( optimize=2 ) doesn't del docstrings/asserts , ignores -OO & PYTHONOPTIMIZE=2 compile(optimize=2 ) doesn't del docstrings/asserts , ignores -OO & PYTHONOPTIMIZE=2 Dec 10, 2023
@hugovk hugovk changed the title compile(optimize=2 ) doesn't del docstrings/asserts , ignores -OO & PYTHONOPTIMIZE=2 compile(optimize=2) doesn't del docstrings/asserts, ignores -OO & PYTHONOPTIMIZE=2 Dec 10, 2023
@carljm
Copy link
Member

carljm commented Dec 17, 2023

This is a duplicate of #108113. In #108154 @iritkatriel added PyCF_OPTIMIZED_AST to solve this (but for Python 3.13+ only.)

@carljm carljm closed this as completed Dec 17, 2023
@carljm carljm closed this as not planned Won't fix, can't repro, duplicate, stale Dec 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pending The issue will be closed if no feedback is provided
Projects
None yet
Development

No branches or pull requests

3 participants