]> granicus.if.org Git - python/commitdiff
bpo-35717: Fix KeyError exception raised when using enums and compile (GH-11523)
authorRémi Lapeyre <remi.lapeyre@henki.fr>
Thu, 24 Jan 2019 19:43:13 +0000 (20:43 +0100)
committerMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Thu, 24 Jan 2019 19:43:13 +0000 (11:43 -0800)
https://bugs.python.org/issue17467

Lib/enum.py
Lib/test/test_enum.py
Misc/ACKS
Misc/NEWS.d/next/Library/2019-01-11-17-56-15.bpo-35717.6TDTB_.rst [new file with mode: 0644]

index f7452f0cc0aaa54a73aa98be2d4d6135d0035d32..a958ed8748afea531f336f0b33c978bd88affd02 100644 (file)
@@ -419,7 +419,7 @@ class EnumMeta(type):
         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)
index 572e8733f45b86258a1cf76f7a0c9132ca2f3c20..99fc85074b703b66b0e854b1034f9ae9bb0a2490 100644 (file)
@@ -1858,6 +1858,15 @@ class TestEnum(unittest.TestCase):
             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):
 
index 81b51f75199159d5046428ac6030867e730f7111..c9fa08bd614138fd0b60b1638ea7d7269fb0b219 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -906,6 +906,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 (file)
index 0000000..7cae1d1
--- /dev/null
@@ -0,0 +1,2 @@
+Fix KeyError exception raised when using enums and compile. Patch
+contributed by Rémi Lapeyre.