classification
Title: enum.Enum error on sys._getframe(2)
Type: behavior Stage: patch review
Components: Library (Lib) Versions: Python 3.8
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: ethan.furman Nosy List: barry, eli.bendersky, ethan.furman, levkivskyi, remi.lapeyre, vstinner
Priority: normal Keywords: easy, patch

Created on 2019-01-11 15:08 by vstinner, last changed 2019-01-11 21:14 by levkivskyi.

Pull Requests
URL Status Linked Edit
PR 11523 serhiy.storchaka, 2019-01-11 17:36
Messages (8)
msg333474 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2019-01-11 15:08
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__'
msg333475 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2019-01-11 15:08
The bug has been reported in my perf project:
https://github.com/vstinner/perf/issues/49
msg333477 - (view) Author: Ethan Furman (ethan.furman) * (Python committer) Date: 2019-01-11 15:19
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.
msg333485 - (view) Author: Ethan Furman (ethan.furman) * (Python committer) Date: 2019-01-11 16:50
Marking this as "easy".  It needs a test showing the failing behavior, then the fix.
msg333486 - (view) Author: Rémi Lapeyre (remi.lapeyre) * Date: 2019-01-11 17:08
Hi, PR https://github.com/python/cpython/pull/11521 should fix the issue.
msg333489 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2019-01-11 17:16
> Hi, PR https://github.com/python/cpython/pull/11521 should fix the issue.

It's PR 11523.
msg333490 - (view) Author: Ethan Furman (ethan.furman) * (Python committer) Date: 2019-01-11 17:16
That PR does not go with this issue.  ;)
msg333492 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2019-01-11 17:20
> That PR does not go with this issue.  ;)

It does. You removed the link to PR 11523 which is a fix for this issue.
History
Date User Action Args
2019-01-11 21:14:10levkivskyisetnosy: + levkivskyi
2019-01-11 17:36:13serhiy.storchakasetpull_requests: + pull_request11108
2019-01-11 17:20:33vstinnersetmessages: + msg333492
2019-01-11 17:16:53ethan.furmansetpull_requests: - pull_request11107
2019-01-11 17:16:18ethan.furmansetmessages: + msg333490
2019-01-11 17:16:08vstinnersetmessages: + msg333489
2019-01-11 17:08:17remi.lapeyresetmessages: + msg333486
2019-01-11 17:07:22remi.lapeyresetkeywords: + patch
stage: test needed -> patch review
pull_requests: + pull_request11107
2019-01-11 16:50:22ethan.furmansetkeywords: + easy

messages: + msg333485
2019-01-11 16:46:15remi.lapeyresetnosy: + remi.lapeyre
2019-01-11 15:19:35ethan.furmansetassignee: ethan.furman
type: behavior
messages: + msg333477
stage: test needed
2019-01-11 15:08:42vstinnersetmessages: + msg333475
2019-01-11 15:08:13vstinnercreate