Skip to content

eval() in a list comprehension #81827

Closed
Closed

Description

@GrzegorzKraso
BPO 37646
Nosy @rhettinger, @stevendaprano
PRs
  • bpo-37646: Document that eval() cannot access nested scopes #15117
  • [3.8] bpo-37646: Document that eval() cannot access nested scopes (GH-15117) #15155
  • Files
  • demo.py
  • 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 2019-08-07.01:08:28.601>
    created_at = <Date 2019-07-21.23:24:25.417>
    labels = ['type-bug', '3.7', 'docs']
    title = 'eval() in a list comprehension'
    updated_at = <Date 2019-08-07.01:08:28.591>
    user = 'https://bugs.python.org/GrzegorzKraso'

    bugs.python.org fields:

    activity = <Date 2019-08-07.01:08:28.591>
    actor = 'rhettinger'
    assignee = 'docs@python'
    closed = True
    closed_date = <Date 2019-08-07.01:08:28.601>
    closer = 'rhettinger'
    components = ['Documentation']
    creation = <Date 2019-07-21.23:24:25.417>
    creator = 'Grzegorz Kraso\xc5\x84'
    dependencies = []
    files = ['48495']
    hgrepos = []
    issue_num = 37646
    keywords = ['patch']
    message_count = 9.0
    messages = ['348271', '348272', '348274', '348293', '348313', '348405', '349001', '349144', '349145']
    nosy_count = 4.0
    nosy_names = ['rhettinger', 'steven.daprano', 'docs@python', 'Grzegorz Kraso\xc5\x84']
    pr_nums = ['15117', '15155']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue37646'
    versions = ['Python 3.7']

    Activity

    1. GrzegorzKraso commented on Jul 21, 2019

      GrzegorzKrasomannequin
      MannequinAuthor

      eval() works in a global scope when invoked in a list comprehension.

    2. stevendaprano commented on Jul 22, 2019

      @stevendaprano
      Member

      What leads you to believe that eval *shouldn't* work in the global scope in a comprehension?

      If not the global scope, which scope should it be, local or nonlocal? Is the behaviour documented differently?

      For reference, the current docs for eval are here:
      https://docs.python.org/3.5/library/functions.html#eval

    3. rhettinger commented on Jul 22, 2019

      @rhettinger
      Contributor

      This used to work as you expected in Python 2.

      In Python 3, list comprehensions create their own inner scope just like generator expressions.

      Per the eval() docs, "if both dictionaries are omitted, the expression is executed in the environment where eval() is called."

      In your code example, the inner scope doesn't have a local variable "x", so the global variable "x" is retrieved.

      That said, I would have expected the inner "x" to be found as a non-local. So yes, this does seem odd an it isn't really true that "the expression is executed in the environment where eval() is called." Instead, it uses the globals() and locals() of the environment where it is called but not the nested scope. Perhaps this should be clarified in the docs if it is in fact the intended behavior.

    4. GrzegorzKraso commented on Jul 22, 2019

      GrzegorzKrasomannequin
      MannequinAuthor

      Steven, I believed that any <expression> replaced by eval('<expression>') will not change behaviour of the code. Now I understand that my assumption was incorrect.

      Raymond, thanks for helping me understand this.

    5. stevendaprano commented on Jul 23, 2019

      @stevendaprano
      Member

      I'm not sure we should be so quick to close this. At the very least, I
      think the documentation could be improved.

      It does seem desirable to have the invariant:

      `expression` == `eval("expression")`
      

      apply in any environment. Was the change in behaviour between 2 and 3
      intentional, or just a side-effect of the change in implementation?

    6. GrzegorzKraso commented on Jul 24, 2019

      GrzegorzKrasomannequin
      MannequinAuthor

      I re-opened the issue.

      Dear core developers, can we ask you to confirm if described behavior of eval() is expected?

    7. rhettinger commented on Aug 4, 2019

      @rhettinger
      Contributor

      After more thought, I think the existing behavior is probably what we want. There may not be a clean way to allow access and updates to non-locals. Even if a way was found, it may tie our hands and preclude other implementation changes down the road. Also, such a feature may be at odds with the current API which allows the execution environment to be retargeted. There is also a risk of introducing new security issues.

      I've attached a PR to update the eval() docs to reflect the actual behavior.

    8. rhettinger commented on Aug 7, 2019

      @rhettinger
      Contributor

      New changeset 610a482 by Raymond Hettinger in branch 'master':
      bpo-37646: Document that eval() cannot access nested scopes (GH-15117)
      610a482

    9. rhettinger commented on Aug 7, 2019

      @rhettinger
      Contributor

      New changeset 9341dcb by Raymond Hettinger (Miss Islington (bot)) in branch '3.8':
      bpo-37646: Document that eval() cannot access nested scopes (GH-15117) (GH-15155)
      9341dcb

    10. added
      docsDocumentation in the Doc dir
      and removed
      interpreter-core(Objects, Python, Grammar, and Parser dirs)
      on Aug 7, 2019
    11. 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 lifedocsDocumentation in the Doc dirtype-bugAn unexpected behavior, bug, or error

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions