Skip to content

compile(optimize=2) doesn't del docstrings/asserts, ignores -OO & PYTHONOPTIMIZE=2 #112909

@dreamflow

Description

@dreamflow

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    pendingThe issue will be closed if no feedback is provided

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions