Skip to content

Document that compile(code, 'exec') has different behavior in 3.7+ #77658

Closed

Description

@Carreau
mannequin
BPO 33477
Nosy @methane, @Carreau
PRs
  • bpo-33477: clarify compile "exec" mode and changes in 3.7 #6973
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2018-06-05.18:28:22.638>
    created_at = <Date 2018-05-13.00:57:30.375>
    labels = ['interpreter-core', '3.7', '3.8', 'docs']
    title = "Document that compile(code, 'exec') has different behavior in 3.7+"
    updated_at = <Date 2018-06-05.18:28:22.636>
    user = 'https://github.com/Carreau'

    bugs.python.org fields:

    activity = <Date 2018-06-05.18:28:22.636>
    actor = 'mbussonn'
    assignee = 'docs@python'
    closed = True
    closed_date = <Date 2018-06-05.18:28:22.638>
    closer = 'mbussonn'
    components = ['Documentation', 'Interpreter Core']
    creation = <Date 2018-05-13.00:57:30.375>
    creator = 'mbussonn'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 33477
    keywords = ['patch']
    message_count = 5.0
    messages = ['316441', '316490', '317036', '317037', '318779']
    nosy_count = 3.0
    nosy_names = ['methane', 'docs@python', 'mbussonn']
    pr_nums = ['6973']
    priority = 'normal'
    resolution = None
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = None
    url = 'https://bugs.python.org/issue33477'
    versions = ['Python 3.7', 'Python 3.8']

    Activity

    1. Carreau commented on May 13, 2018

      Carreaumannequin
      MannequinAuthor

      In recent Python the following

      >> from ast import PyCF_ONLY_AST
      >> compile("'a'", 'whatever', 'exec', PyCF_ONLY_AST).body

      In 3.6 it return

      [<_ast.Expr at 0x10b7441d0>] # that contail Str('a')

      While on master:

      []

      This is inconveninent for alternative repl like IPython, where basically if the user is entering a single string, the result is nothing in Python 3.7+, while it _does_ return something on earlier Python [1].

      The documentation of compile says:

      ... it can be 'exec' if source consists of a sequence of statements,

      Which is not technically true any more as the first statement, if a string, will be removed.

      What's happening here is that since Python 3.7 if the _first_ statement is actually an expression containing a lonely string it is assign to the module docstring. So that's basically assuming you are parsing a module, and that the docstring make sens in this context, while in a REPL you are parsing a sucesssion of statements, in which case there is no need for a docstring that make no sens in this context.

      This is _usually_ not an issue, unless this lonely statement is also the last, and what the user wants to execute in a REPL, in which case it has no side effect.

      I don't have any objection to the new behavior, though I was wondering if this kind of side effect was anticipated.

      If that affect IPython, it will likely effect other alternative REPLs.

      Thus, I believe it would be good to have this at least documented a tiny bit better and added in what's new, and potentially clarified in the exec docs.

      I could argue that now the exec name may be a tiny bit unsuitable for the new behavior, and would love if this could be optional.

      1: ipython/ipython#11133 (comment)

    2. added
      docsDocumentation in the Doc dir
      interpreter-core(Objects, Python, Grammar, and Parser dirs)
      on May 13, 2018
    3. methane commented on May 14, 2018

      @methane
      Member
    4. methane commented on May 18, 2018

      @methane
      Member

      Any comments?
      Would you close this issue?

    5. Carreau commented on May 18, 2018

      Carreaumannequin
      MannequinAuthor

      Fair enough that what's new include things about Module

      The first statement in their body is not considered as a docstring anymore.

      Note that this sentence read backward to me. I understand what is meant because I know the new behavior. It might be good to clarify. potentially:

      The first statement in the body attribute of should not be considered the docstring of the module anymore, the docstring attribute is reserved for that.

      Though the documentation of compile() does not say that compile(...,'exec') compile a module. It says:

      The mode argument specifies what kind of code must be compiled;
      it can be 'exec' if source consists of a sequence of statements

      Which now is incorrect. I was expecting compile(..., 'exec') to return a Module with None or empty string as the docstring attribute – which is also a perfectly reasonable request.

      I think that compile documentation should be changed to reflect what it does.

      Or (but I see why this is un-reasonable) split add the mode='module' that has new behavior, while exec does not.

    6. Carreau commented on Jun 5, 2018

      Carreaumannequin
      MannequinAuthor

      Closing as the ast changes have been reverted.

    7. transferred this issue fromon Apr 10, 2022
    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

      3.7 (EOL)end of life3.8 (EOL)end of lifedocsDocumentation in the Doc dirinterpreter-core(Objects, Python, Grammar, and Parser dirs)

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions