Skip to content
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

enum.Enum error on sys._getframe(2) #79898

Closed
vstinner opened this issue Jan 11, 2019 · 11 comments
Closed

enum.Enum error on sys._getframe(2) #79898

vstinner opened this issue Jan 11, 2019 · 11 comments
Assignees
Labels
3.8 only security fixes easy stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@vstinner
Copy link
Member

BPO 35717
Nosy @warsaw, @vstinner, @ethanfurman, @ilevkivskyi, @miss-islington, @remilapeyre
PRs
  • bpo-35717: Fix KeyError exception raised when using enums and compile #11523
  • [3.7] bpo-35717: Fix KeyError exception raised when using enums and compile (GH-11523) #11669
  • [3.7] bpo-35717: Fix KeyError exception raised when using enums and compile (GH-11523) #11669
  • [3.7] bpo-35717: Fix KeyError exception raised when using enums and compile (GH-11523) #11669
  • 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 = 'https://github.com/ethanfurman'
    closed_at = <Date 2019-01-30.17:38:33.376>
    created_at = <Date 2019-01-11.15:08:13.753>
    labels = ['easy', '3.8', 'type-bug', 'library']
    title = 'enum.Enum error on sys._getframe(2)'
    updated_at = <Date 2019-01-30.17:38:33.375>
    user = 'https://github.com/vstinner'

    bugs.python.org fields:

    activity = <Date 2019-01-30.17:38:33.375>
    actor = 'vstinner'
    assignee = 'ethan.furman'
    closed = True
    closed_date = <Date 2019-01-30.17:38:33.376>
    closer = 'vstinner'
    components = ['Library (Lib)']
    creation = <Date 2019-01-11.15:08:13.753>
    creator = 'vstinner'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 35717
    keywords = ['patch', 'easy']
    message_count = 11.0
    messages = ['333474', '333475', '333477', '333485', '333486', '333489', '333490', '333492', '334318', '334580', '334581']
    nosy_count = 7.0
    nosy_names = ['barry', 'vstinner', 'eli.bendersky', 'ethan.furman', 'levkivskyi', 'miss-islington', 'remi.lapeyre']
    pr_nums = ['11523', '11669', '11669', '11669']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue35717'
    versions = ['Python 3.8']

    @vstinner
    Copy link
    Member Author

    sys._getframe(2) fails in the following example:

    code = "from enum import Enum; Enum('Animal', 'ANT BEE CAT DOG')"
    code = compile(code, "<string>", "exec")
    global_ns = {}
    local_ls = {}
    exec(code, global_ns, local_ls)

    Error with Python 3.7.2 (Fedora 29):

    Traceback (most recent call last):
      File "x.py", line 5, in <module>
        exec(code, global_ns, local_ls)
      File "<string>", line 1, in <module>
      File "/usr/lib64/python3.7/enum.py", line 311, in __call__
        return cls._create_(value, names, module=module, qualname=qualname, type=type, start=start)
      File "/usr/lib64/python3.7/enum.py", line 429, in _create_
        module = sys._getframe(2).f_globals['__name__']
    KeyError: '__name__'

    @vstinner vstinner added 3.8 only security fixes stdlib Python modules in the Lib dir labels Jan 11, 2019
    @vstinner
    Copy link
    Member Author

    The bug has been reported in my perf project:
    psf/pyperf#49

    @ethanfurman
    Copy link
    Member

    Looks like the following code:

            if module is None:
                try:
                    module = sys._getframe(2).f_globals['__name__']
                except (AttributeError, ValueError) as exc:
                    pass

    needs to have KeyError added to the except line.

    @ethanfurman ethanfurman self-assigned this Jan 11, 2019
    @ethanfurman ethanfurman added the type-bug An unexpected behavior, bug, or error label Jan 11, 2019
    @ethanfurman
    Copy link
    Member

    Marking this as "easy". It needs a test showing the failing behavior, then the fix.

    @remilapeyre
    Copy link
    Mannequin

    remilapeyre mannequin commented Jan 11, 2019

    Hi, PR #11521 should fix the issue.

    @vstinner
    Copy link
    Member Author

    Hi, PR #11521 should fix the issue.

    It's PR 11523.

    @ethanfurman
    Copy link
    Member

    That PR does not go with this issue. ;)

    @vstinner
    Copy link
    Member Author

    That PR does not go with this issue. ;)

    It does. You removed the link to PR 11523 which is a fix for this issue.

    @miss-islington
    Copy link
    Contributor

    New changeset 1fd06f1 by Miss Islington (bot) (Rémi Lapeyre) in branch 'master':
    bpo-35717: Fix KeyError exception raised when using enums and compile (GH-11523)
    1fd06f1

    @vstinner
    Copy link
    Member Author

    New changeset 1c79891 by Victor Stinner (Miss Islington (bot)) in branch '3.7':
    bpo-35717: Fix KeyError exception raised when using enums and compile (GH-11523) (GH-11669)
    1c79891

    @vstinner
    Copy link
    Member Author

    I was confused with PR, so here you have:

    Both are merged, so I close the issue. Thanks Rémi Lapeyre for the fix!

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.8 only security fixes easy stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants