Skip to content

Commit 1c79891

Browse files
miss-islingtonRémi Lapeyre
authored andcommitted
bpo-35717: Fix KeyError exception raised when using enums and compile (GH-11523) (GH-11669)
https://bugs.python.org/issue17467 (cherry picked from commit 1fd06f1) Co-authored-by: Rémi Lapeyre <remi.lapeyre@henki.fr>
1 parent ebae1ce commit 1c79891

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
@@ -427,7 +427,7 @@ def _create_(cls, class_name, names, *, module=None, qualname=None, type=None, s
427427
if module is None:
428428
try:
429429
module = sys._getframe(2).f_globals['__name__']
430-
except (AttributeError, ValueError) as exc:
430+
except (AttributeError, ValueError, KeyError) as exc:
431431
pass
432432
if module is None:
433433
_make_class_unpicklable(enum_class)

Lib/test/test_enum.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1866,6 +1866,15 @@ class Decision2(MyEnum):
18661866
REVERT_ALL = "REVERT_ALL"
18671867
RETRY = "RETRY"
18681868

1869+
def test_empty_globals(self):
1870+
# bpo-35717: sys._getframe(2).f_globals['__name__'] fails with KeyError
1871+
# when using compile and exec because f_globals is empty
1872+
code = "from enum import Enum; Enum('Animal', 'ANT BEE CAT DOG')"
1873+
code = compile(code, "<string>", "exec")
1874+
global_ns = {}
1875+
local_ls = {}
1876+
exec(code, global_ns, local_ls)
1877+
18691878

18701879
class TestOrder(unittest.TestCase):
18711880

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -895,6 +895,7 @@ Glenn Langford
895895
Andrew Langmead
896896
Wolfgang Langner
897897
Detlef Lannert
898+
Rémi Lapeyre
898899
Soren Larsen
899900
Amos Latteier
900901
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)