Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Lib/enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ def _create_(cls, class_name, names, *, module=None, qualname=None, type=None, s
if module is None:
try:
module = sys._getframe(2).f_globals['__name__']
except (AttributeError, ValueError) as exc:
except (AttributeError, ValueError, KeyError) as exc:
pass
if module is None:
_make_class_unpicklable(enum_class)
Expand Down
9 changes: 9 additions & 0 deletions Lib/test/test_enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -1866,6 +1866,15 @@ class Decision2(MyEnum):
REVERT_ALL = "REVERT_ALL"
RETRY = "RETRY"

def test_empty_globals(self):
# bpo-35717: sys._getframe(2).f_globals['__name__'] fails with KeyError
# when using compile and exec because f_globals is empty
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)


class TestOrder(unittest.TestCase):

Expand Down
1 change: 1 addition & 0 deletions Misc/ACKS
Original file line number Diff line number Diff line change
Expand Up @@ -895,6 +895,7 @@ Glenn Langford
Andrew Langmead
Wolfgang Langner
Detlef Lannert
Rémi Lapeyre
Soren Larsen
Amos Latteier
Piers Lauder
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix KeyError exception raised when using enums and compile. Patch
contributed by Rémi Lapeyre.