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

Created on 2019-01-11 15:08 by vstinner, last changed 2019-01-30 17:38 by vstinner. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 11523 merged serhiy.storchaka, 2019-01-11 17:36
PR 11669 merged miss-islington, 2019-01-24 19:43
PR 11669 merged miss-islington, 2019-01-24 19:43
PR 11669 merged miss-islington, 2019-01-24 19:43
Messages (11)
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.
msg334318 - (view) Author: miss-islington (miss-islington) Date: 2019-01-24 19:43
New changeset 1fd06f1eca80dcbf3a916133919482a8327f3da4 by Miss Islington (bot) (Rémi Lapeyre) in branch 'master':
bpo-35717: Fix KeyError exception raised when using enums and compile (GH-11523)
https://github.com/python/cpython/commit/1fd06f1eca80dcbf3a916133919482a8327f3da4
msg334580 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2019-01-30 17:36
New changeset 1c79891026c57046f5ac596080ea60c515e0560a 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)
https://github.com/python/cpython/commit/1c79891026c57046f5ac596080ea60c515e0560a
msg334581 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2019-01-30 17:38
I was confused with PR, so here you have:

* master: https://github.com/python/cpython/pull/11523
* 3.7: https://github.com/python/cpython/pull/11669

Both are merged, so I close the issue. Thanks Rémi Lapeyre for the fix!
History
Date User Action Args
2019-01-30 17:38:33vstinnersetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2019-01-30 17:38:01vstinnersetmessages: + msg334581
2019-01-30 17:36:54vstinnersetmessages: + msg334580
2019-01-24 19:43:52miss-islingtonsetpull_requests: + pull_request11480
2019-01-24 19:43:43miss-islingtonsetpull_requests: + pull_request11479
2019-01-24 19:43:33miss-islingtonsetpull_requests: + pull_request11478
2019-01-24 19:43:17miss-islingtonsetnosy: + miss-islington
messages: + msg334318
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