]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-35717: Fix KeyError exception raised when using enums and compile (GH-11523)...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Wed, 30 Jan 2019 17:36:51 +0000 (09:36 -0800)
committerVictor Stinner <vstinner@redhat.com>
Wed, 30 Jan 2019 17:36:51 +0000 (18:36 +0100)
https://bugs.python.org/issue17467
(cherry picked from commit 1fd06f1eca80dcbf3a916133919482a8327f3da4)

Co-authored-by: Rémi Lapeyre <remi.lapeyre@henki.fr>
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 782d37433a6e8e8877fccbf474cf44aa8b3f3150..8984cac6095562a68e29b3f09b0b59655d8a7b7e 100644 (file)
@@ -427,7 +427,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 b221045328dbd3509906cd2ad24fc337afa2668a..fdc3dd9c38a0f8815a0c626e68801290f4d25565 100644 (file)
@@ -1866,6 +1866,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 027e013072258235d317b814aab48add2deb5d1d..193592290f0e6234ea9d46c899e17e4e1c34886d 100644 (file)
--- 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 (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.