GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — compile(optimize=2) doesn't del docstrings/asserts, ignores -OO & PYTHONOPTIMIZE=2 · Issue #112909 · python/cpython

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

New issue
Copy link
New issue
Copy link
Closed as not planned
Closed as not planned
compile(optimize=2) doesn't del docstrings/asserts, ignores -OO & PYTHONOPTIMIZE=2#112909
Copy link
Labels
pendingThe issue will be closed if no feedback is providedThe issue will be closed if no feedback is provided

Description

@dreamflow
dreamflow
opened on Dec 9, 2023
Issue body actions

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

Activity

  1. dreamflow
    added
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    on Dec 9, 2023
  2. 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[/+] on Dec 9, 2023
  3. 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[/+] on Dec 9, 2023
  4. terryjreedy
    added
    pendingThe issue will be closed if no feedback is providedThe issue will be closed if no feedback is provided
    and removed
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    on Dec 10, 2023
  5. terryjreedy commented on Dec 10, 2023

    @terryjreedy
    terryjreedy
    on Dec 10, 2023
    Member
    More actions

    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.

  6. dreamflow commented on Dec 10, 2023

    @dreamflow
    dreamflow
    on Dec 10, 2023
    ContributorAuthor
    More actions

    thank you terryjreedy for the detailed explanation 🙂

  7. 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[/+] on Dec 10, 2023
  8. 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[/+] on Dec 10, 2023
  9. carljm commented on Dec 17, 2023

    @carljm
    carljm
    on Dec 17, 2023
    Member
    More actions

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

    Reacted by dreamflow
  10. carljm
    closed this as completedon Dec 17, 2023
  11. carljm
    closed this as not plannedon Dec 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    pendingThe issue will be closed if no feedback is providedThe 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

      You are visitor 01402520 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.