[Python-checkins] bpo-35717: Fix KeyError exception raised when using enums and compile (GH-11523) (GH-11669)

Victor Stinner webhook-mailer at python.org
Wed Jan 30 12:36:55 EST 2019


https://github.com/python/cpython/commit/1c79891026c57046f5ac596080ea60c515e0560a
commit: 1c79891026c57046f5ac596080ea60c515e0560a
branch: 3.7
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: Victor Stinner <vstinner at redhat.com>
date: 2019-01-30T18:36:51+01:00
summary:

bpo-35717: Fix KeyError exception raised when using enums and compile (GH-11523) (GH-11669)

https://bugs.python.org/issue17467
(cherry picked from commit 1fd06f1eca80dcbf3a916133919482a8327f3da4)

Co-authored-by: Rémi Lapeyre <remi.lapeyre at henki.fr>

files:
A Misc/NEWS.d/next/Library/2019-01-11-17-56-15.bpo-35717.6TDTB_.rst
M Lib/enum.py
M Lib/test/test_enum.py
M Misc/ACKS

diff --git a/Lib/enum.py b/Lib/enum.py
index 782d37433a6e..8984cac60955 100644
--- a/Lib/enum.py
+++ b/Lib/enum.py
@@ -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)
diff --git a/Lib/test/test_enum.py b/Lib/test/test_enum.py
index b221045328db..fdc3dd9c38a0 100644
--- a/Lib/test/test_enum.py
+++ b/Lib/test/test_enum.py
@@ -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):
 
diff --git a/Misc/ACKS b/Misc/ACKS
index 027e01307225..193592290f0e 100644
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -895,6 +895,7 @@ Glenn Langford
 Andrew Langmead
 Wolfgang Langner
 Detlef Lannert
+Rémi Lapeyre
 Soren Larsen
 Amos Latteier
 Piers Lauder
diff --git a/Misc/NEWS.d/next/Library/2019-01-11-17-56-15.bpo-35717.6TDTB_.rst b/Misc/NEWS.d/next/Library/2019-01-11-17-56-15.bpo-35717.6TDTB_.rst
new file mode 100644
index 000000000000..7cae1d1c82c7
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2019-01-11-17-56-15.bpo-35717.6TDTB_.rst
@@ -0,0 +1,2 @@
+Fix KeyError exception raised when using enums and compile. Patch
+contributed by Rémi Lapeyre.



More information about the Python-checkins mailing list