Skip to content

Commit 1fd06f1

Browse files
Rémi Lapeyremiss-islington
authored andcommitted
bpo-35717: Fix KeyError exception raised when using enums and compile (GH-11523)
https://bugs.python.org/issue17467
1 parent 5c8f537 commit 1fd06f1

File tree

4 files changed

+13
-1
lines changed

4 files changed

+13
-1
lines changed

Lib/enum.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ def _create_(cls, class_name, names, *, module=None, qualname=None, type=None, s
419419
if module is None:
420420
try:
421421
module = sys._getframe(2).f_globals['__name__']
422-
except (AttributeError, ValueError) as exc:
422+
except (AttributeError, ValueError, KeyError) as exc:
423423
pass
424424
if module is None:
425425
_make_class_unpicklable(enum_class)

Lib/test/test_enum.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1858,6 +1858,15 @@ class Decision2(MyEnum):
18581858
REVERT_ALL = "REVERT_ALL"
18591859
RETRY = "RETRY"
18601860

1861+
def test_empty_globals(self):
1862+
# bpo-35717: sys._getframe(2).f_globals['__name__'] fails with KeyError
1863+
# when using compile and exec because f_globals is empty
1864+
code = "from enum import Enum; Enum('Animal', 'ANT BEE CAT DOG')"
1865+
code = compile(code, "<string>", "exec")
1866+
global_ns = {}
1867+
local_ls = {}
1868+
exec(code, global_ns, local_ls)
1869+
18611870

18621871
class TestOrder(unittest.TestCase):
18631872

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -906,6 +906,7 @@ Glenn Langford
906906
Andrew Langmead
907907
Wolfgang Langner
908908
Detlef Lannert
909+
Rémi Lapeyre
909910
Soren Larsen
910911
Amos Latteier
911912
Piers Lauder
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix KeyError exception raised when using enums and compile. Patch
2+
contributed by Rémi Lapeyre.

0 commit comments

Comments
 (0)