summaryrefslogtreecommitdiffstats
path: root/Lib/enum.py
diff options
context:
space:
mode:
authorEthan Furman <ethan@stoneleaf.us>2014-03-03 20:42:52 (GMT)
committerEthan Furman <ethan@stoneleaf.us>2014-03-03 20:42:52 (GMT)
commit9c4544472734246195f28c2404190c7b33009db8 (patch)
tree699572ebbb1e0a0c0adb4b5f9b36d78108067984 /Lib/enum.py
parentcd9028ca692fdc3e1cb3a428bfa558bd8bcfbc1b (diff)
downloadcpython-9c4544472734246195f28c2404190c7b33009db8.zip
cpython-9c4544472734246195f28c2404190c7b33009db8.tar.gz
cpython-9c4544472734246195f28c2404190c7b33009db8.tar.bz2
Close issue20653: improve functional API docs; minor code changes
Diffstat (limited to 'Lib/enum.py')
-rw-r--r--Lib/enum.py39
1 files changed, 27 insertions, 12 deletions
diff --git a/Lib/enum.py b/Lib/enum.py
index c9bd7c0..844a956 100644
--- a/Lib/enum.py
+++ b/Lib/enum.py
@@ -115,14 +115,21 @@ class EnumMeta(type):
# Reverse value->name map for hashable values.
enum_class._value2member_map_ = {}
- # check for a supported pickle protocols, and if not present sabotage
- # pickling, since it won't work anyway.
- # if new class implements its own __reduce_ex__, do not sabotage
- if classdict.get('__reduce_ex__') is None:
+ # If a custom type is mixed into the Enum, and it does not know how
+ # to pickle itself, pickle.dumps will succeed but pickle.loads will
+ # fail. Rather than have the error show up later and possibly far
+ # from the source, sabotage the pickle protocol for this class so
+ # that pickle.dumps also fails.
+ #
+ # However, if the new class implements its own __reduce_ex__, do not
+ # sabotage -- it's on them to make sure it works correctly. We use
+ # __reduce_ex__ instead of any of the others as it is preferred by
+ # pickle over __reduce__, and it handles all pickle protocols.
+ if '__reduce_ex__' not in classdict:
if member_type is not object:
methods = ('__getnewargs_ex__', '__getnewargs__',
'__reduce_ex__', '__reduce__')
- if not any(map(member_type.__dict__.get, methods)):
+ if not any(m in member_type.__dict__ for m in methods):
_make_class_unpicklable(enum_class)
# instantiate them, checking for duplicates as we go
@@ -193,14 +200,22 @@ class EnumMeta(type):
to an enumeration member (i.e. Color(3)) and for the functional API
(i.e. Color = Enum('Color', names='red green blue')).
- When used for the functional API: `module`, if set, will be stored in
- the new class' __module__ attribute; `qualname`, if set, will be stored
- in the new class' __qualname__ attribute; `type`, if set, will be mixed
- in as the first base class.
+ When used for the functional API:
- Note: if `module` is not set this routine will attempt to discover the
- calling module by walking the frame stack; if this is unsuccessful
- the resulting class will not be pickleable.
+ `value` will be the name of the new class.
+
+ `names` should be either a string of white-space/comma delimited names
+ (values will start at 1), or an iterator/mapping of name, value pairs.
+
+ `module` should be set to the module this class is being created in;
+ if it is not set, an attempt to find that module will be made, but if
+ it fails the class will not be picklable.
+
+ `qualname` should be set to the actual location this class can be found
+ at in its module; by default it is set to the global scope. If this is
+ not correct, unpickling will fail in some circumstances.
+
+ `type`, if set, will be mixed in as the first base class.
"""
if names is None: # simple value lookup