summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRémi Lapeyre <remi.lapeyre@henki.fr>2019-01-24 19:43:13 (GMT)
committerMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2019-01-24 19:43:13 (GMT)
commit1fd06f1eca80dcbf3a916133919482a8327f3da4 (patch)
tree01c702f00536f7dbec9c1a5ad2e71af482c145fe
parent5c8f537669d3379fc50bb0a96accac756e43e281 (diff)
downloadcpython-1fd06f1eca80dcbf3a916133919482a8327f3da4.zip
cpython-1fd06f1eca80dcbf3a916133919482a8327f3da4.tar.gz
cpython-1fd06f1eca80dcbf3a916133919482a8327f3da4.tar.bz2
bpo-35717: Fix KeyError exception raised when using enums and compile (GH-11523)
https://bugs.python.org/issue17467
-rw-r--r--Lib/enum.py2
-rw-r--r--Lib/test/test_enum.py9
-rw-r--r--Misc/ACKS1
-rw-r--r--Misc/NEWS.d/next/Library/2019-01-11-17-56-15.bpo-35717.6TDTB_.rst2
4 files changed, 13 insertions, 1 deletions
diff --git a/Lib/enum.py b/Lib/enum.py
index f7452f0..a958ed8 100644
--- a/Lib/enum.py
+++ b/Lib/enum.py
@@ -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)
diff --git a/Lib/test/test_enum.py b/Lib/test/test_enum.py
index 572e873..99fc850 100644
--- a/Lib/test/test_enum.py
+++ b/Lib/test/test_enum.py
@@ -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):
diff --git a/Misc/ACKS b/Misc/ACKS
index 81b51f7..c9fa08b 100644
--- 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
index 0000000..7cae1d1
--- /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.