Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
019f0a0c
Commit
019f0a0c
authored
Sep 12, 2018
by
Ethan Furman
Committed by
GitHub
Sep 12, 2018
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
bpo-34536: raise error for invalid _missing_ results (GH-9147)
* raise exception if _missing_ returns None or invalid type
parent
a5d1eb8d
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
54 additions
and
1 deletion
+54
-1
Lib/enum.py
Lib/enum.py
+19
-1
Lib/test/test_enum.py
Lib/test/test_enum.py
+33
-0
Misc/NEWS.d/next/Library/2018-09-11-15-49-09.bpo-34536.3IPIH5.rst
...S.d/next/Library/2018-09-11-15-49-09.bpo-34536.3IPIH5.rst
+2
-0
No files found.
Lib/enum.py
View file @
019f0a0c
...
...
@@ -585,7 +585,25 @@ class Enum(metaclass=EnumMeta):
if
member
.
_value_
==
value
:
return
member
# still not found -- try _missing_ hook
return
cls
.
_missing_
(
value
)
try
:
exc
=
None
result
=
cls
.
_missing_
(
value
)
except
Exception
as
e
:
exc
=
e
result
=
None
if
isinstance
(
result
,
cls
):
return
result
else
:
ve_exc
=
ValueError
(
"%r is not a valid %s"
%
(
value
,
cls
.
__name__
))
if
result
is
None
and
exc
is
None
:
raise
ve_exc
elif
exc
is
None
:
exc
=
TypeError
(
'error in %s._missing_: returned %r instead of None or a valid member'
%
(
cls
.
__name__
,
result
)
)
exc
.
__context__
=
ve_exc
raise
exc
def
_generate_next_value_
(
name
,
start
,
count
,
last_values
):
for
last_value
in
reversed
(
last_values
):
...
...
Lib/test/test_enum.py
View file @
019f0a0c
...
...
@@ -3,6 +3,7 @@ import inspect
import
pydoc
import
sys
import
unittest
import
sys
import
threading
from
collections
import
OrderedDict
from
enum
import
Enum
,
IntEnum
,
EnumMeta
,
Flag
,
IntFlag
,
unique
,
auto
...
...
@@ -1697,6 +1698,38 @@ class TestEnum(unittest.TestCase):
third
=
auto
()
self
.
assertEqual
([
Dupes
.
first
,
Dupes
.
second
,
Dupes
.
third
],
list
(
Dupes
))
def
test_missing
(
self
):
class
Color
(
Enum
):
red
=
1
green
=
2
blue
=
3
@
classmethod
def
_missing_
(
cls
,
item
):
if
item
==
'three'
:
return
cls
.
blue
elif
item
==
'bad return'
:
# trigger internal error
return
5
elif
item
==
'error out'
:
raise
ZeroDivisionError
else
:
# trigger not found
return
None
self
.
assertIs
(
Color
(
'three'
),
Color
.
blue
)
self
.
assertRaises
(
ValueError
,
Color
,
7
)
try
:
Color
(
'bad return'
)
except
TypeError
as
exc
:
self
.
assertTrue
(
isinstance
(
exc
.
__context__
,
ValueError
))
else
:
raise
Exception
(
'Exception not raised.'
)
try
:
Color
(
'error out'
)
except
ZeroDivisionError
as
exc
:
self
.
assertTrue
(
isinstance
(
exc
.
__context__
,
ValueError
))
else
:
raise
Exception
(
'Exception not raised.'
)
class
TestOrder
(
unittest
.
TestCase
):
...
...
Misc/NEWS.d/next/Library/2018-09-11-15-49-09.bpo-34536.3IPIH5.rst
0 → 100644
View file @
019f0a0c
`Enum._missing_`: raise `ValueError` if None returned and `TypeError` if
non-member is returned.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment