You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

530 lines
20 KiB

  1. import sys
  2. from collections import OrderedDict
  3. from types import MappingProxyType, DynamicClassAttribute
  4. __all__ = ['Enum', 'IntEnum', 'unique']
  5. def _is_descriptor(obj):
  6. """Returns True if obj is a descriptor, False otherwise."""
  7. return (
  8. hasattr(obj, '__get__') or
  9. hasattr(obj, '__set__') or
  10. hasattr(obj, '__delete__'))
  11. def _is_dunder(name):
  12. """Returns True if a __dunder__ name, False otherwise."""
  13. return (name[:2] == name[-2:] == '__' and
  14. name[2:3] != '_' and
  15. name[-3:-2] != '_' and
  16. len(name) > 4)
  17. def _is_sunder(name):
  18. """Returns True if a _sunder_ name, False otherwise."""
  19. return (name[0] == name[-1] == '_' and
  20. name[1:2] != '_' and
  21. name[-2:-1] != '_' and
  22. len(name) > 2)
  23. def _make_class_unpicklable(cls):
  24. """Make the given class un-picklable."""
  25. def _break_on_call_reduce(self, proto):
  26. raise TypeError('%r cannot be pickled' % self)
  27. cls.__reduce_ex__ = _break_on_call_reduce
  28. cls.__module__ = '<unknown>'
  29. class _EnumDict(dict):
  30. """Track enum member order and ensure member names are not reused.
  31. EnumMeta will use the names found in self._member_names as the
  32. enumeration member names.
  33. """
  34. def __init__(self):
  35. super().__init__()
  36. self._member_names = []
  37. def __setitem__(self, key, value):
  38. """Changes anything not dundered or not a descriptor.
  39. If an enum member name is used twice, an error is raised; duplicate
  40. values are not checked for.
  41. Single underscore (sunder) names are reserved.
  42. """
  43. if _is_sunder(key):
  44. raise ValueError('_names_ are reserved for future Enum use')
  45. elif _is_dunder(key):
  46. pass
  47. elif key in self._member_names:
  48. # descriptor overwriting an enum?
  49. raise TypeError('Attempted to reuse key: %r' % key)
  50. elif not _is_descriptor(value):
  51. if key in self:
  52. # enum overwriting a descriptor?
  53. raise TypeError('Key already defined as: %r' % self[key])
  54. self._member_names.append(key)
  55. super().__setitem__(key, value)
  56. # Dummy value for Enum as EnumMeta explicitly checks for it, but of course
  57. # until EnumMeta finishes running the first time the Enum class doesn't exist.
  58. # This is also why there are checks in EnumMeta like `if Enum is not None`
  59. Enum = None
  60. class EnumMeta(type):
  61. """Metaclass for Enum"""
  62. @classmethod
  63. def __prepare__(metacls, cls, bases):
  64. return _EnumDict()
  65. def __new__(metacls, cls, bases, classdict):
  66. # an Enum class is final once enumeration items have been defined; it
  67. # cannot be mixed with other types (int, float, etc.) if it has an
  68. # inherited __new__ unless a new __new__ is defined (or the resulting
  69. # class will fail).
  70. member_type, first_enum = metacls._get_mixins_(bases)
  71. __new__, save_new, use_args = metacls._find_new_(classdict, member_type,
  72. first_enum)
  73. # save enum items into separate mapping so they don't get baked into
  74. # the new class
  75. members = {k: classdict[k] for k in classdict._member_names}
  76. for name in classdict._member_names:
  77. del classdict[name]
  78. # check for illegal enum names (any others?)
  79. invalid_names = set(members) & {'mro', }
  80. if invalid_names:
  81. raise ValueError('Invalid enum member name: {0}'.format(
  82. ','.join(invalid_names)))
  83. # create our new Enum type
  84. enum_class = super().__new__(metacls, cls, bases, classdict)
  85. enum_class._member_names_ = [] # names in definition order
  86. enum_class._member_map_ = OrderedDict() # name->value map
  87. enum_class._member_type_ = member_type
  88. # Reverse value->name map for hashable values.
  89. enum_class._value2member_map_ = {}
  90. # If a custom type is mixed into the Enum, and it does not know how
  91. # to pickle itself, pickle.dumps will succeed but pickle.loads will
  92. # fail. Rather than have the error show up later and possibly far
  93. # from the source, sabotage the pickle protocol for this class so
  94. # that pickle.dumps also fails.
  95. #
  96. # However, if the new class implements its own __reduce_ex__, do not
  97. # sabotage -- it's on them to make sure it works correctly. We use
  98. # __reduce_ex__ instead of any of the others as it is preferred by
  99. # pickle over __reduce__, and it handles all pickle protocols.
  100. if '__reduce_ex__' not in classdict:
  101. if member_type is not object:
  102. methods = ('__getnewargs_ex__', '__getnewargs__',
  103. '__reduce_ex__', '__reduce__')
  104. if not any(m in member_type.__dict__ for m in methods):
  105. _make_class_unpicklable(enum_class)
  106. # instantiate them, checking for duplicates as we go
  107. # we instantiate first instead of checking for duplicates first in case
  108. # a custom __new__ is doing something funky with the values -- such as
  109. # auto-numbering ;)
  110. for member_name in classdict._member_names:
  111. value = members[member_name]
  112. if not isinstance(value, tuple):
  113. args = (value, )
  114. else:
  115. args = value
  116. if member_type is tuple: # special case for tuple enums
  117. args = (args, ) # wrap it one more time
  118. if not use_args:
  119. enum_member = __new__(enum_class)
  120. if not hasattr(enum_member, '_value_'):
  121. enum_member._value_ = value
  122. else:
  123. enum_member = __new__(enum_class, *args)
  124. if not hasattr(enum_member, '_value_'):
  125. enum_member._value_ = member_type(*args)
  126. value = enum_member._value_
  127. enum_member._name_ = member_name
  128. enum_member.__objclass__ = enum_class
  129. enum_member.__init__(*args)
  130. # If another member with the same value was already defined, the
  131. # new member becomes an alias to the existing one.
  132. for name, canonical_member in enum_class._member_map_.items():
  133. if canonical_member._value_ == enum_member._value_:
  134. enum_member = canonical_member
  135. break
  136. else:
  137. # Aliases don't appear in member names (only in __members__).
  138. enum_class._member_names_.append(member_name)
  139. enum_class._member_map_[member_name] = enum_member
  140. try:
  141. # This may fail if value is not hashable. We can't add the value
  142. # to the map, and by-value lookups for this value will be
  143. # linear.
  144. enum_class._value2member_map_[value] = enum_member
  145. except TypeError:
  146. pass
  147. # double check that repr and friends are not the mixin's or various
  148. # things break (such as pickle)
  149. for name in ('__repr__', '__str__', '__format__', '__reduce_ex__'):
  150. class_method = getattr(enum_class, name)
  151. obj_method = getattr(member_type, name, None)
  152. enum_method = getattr(first_enum, name, None)
  153. if obj_method is not None and obj_method is class_method:
  154. setattr(enum_class, name, enum_method)
  155. # replace any other __new__ with our own (as long as Enum is not None,
  156. # anyway) -- again, this is to support pickle
  157. if Enum is not None:
  158. # if the user defined their own __new__, save it before it gets
  159. # clobbered in case they subclass later
  160. if save_new:
  161. enum_class.__new_member__ = __new__
  162. enum_class.__new__ = Enum.__new__
  163. return enum_class
  164. def __call__(cls, value, names=None, *, module=None, qualname=None, type=None):
  165. """Either returns an existing member, or creates a new enum class.
  166. This method is used both when an enum class is given a value to match
  167. to an enumeration member (i.e. Color(3)) and for the functional API
  168. (i.e. Color = Enum('Color', names='red green blue')).
  169. When used for the functional API:
  170. `value` will be the name of the new class.
  171. `names` should be either a string of white-space/comma delimited names
  172. (values will start at 1), or an iterator/mapping of name, value pairs.
  173. `module` should be set to the module this class is being created in;
  174. if it is not set, an attempt to find that module will be made, but if
  175. it fails the class will not be picklable.
  176. `qualname` should be set to the actual location this class can be found
  177. at in its module; by default it is set to the global scope. If this is
  178. not correct, unpickling will fail in some circumstances.
  179. `type`, if set, will be mixed in as the first base class.
  180. """
  181. if names is None: # simple value lookup
  182. return cls.__new__(cls, value)
  183. # otherwise, functional API: we're creating a new Enum type
  184. return cls._create_(value, names, module=module, qualname=qualname, type=type)
  185. def __contains__(cls, member):
  186. return isinstance(member, cls) and member._name_ in cls._member_map_
  187. def __delattr__(cls, attr):
  188. # nicer error message when someone tries to delete an attribute
  189. # (see issue19025).
  190. if attr in cls._member_map_:
  191. raise AttributeError(
  192. "%s: cannot delete Enum member." % cls.__name__)
  193. super().__delattr__(attr)
  194. def __dir__(self):
  195. return (['__class__', '__doc__', '__members__', '__module__'] +
  196. self._member_names_)
  197. def __getattr__(cls, name):
  198. """Return the enum member matching `name`
  199. We use __getattr__ instead of descriptors or inserting into the enum
  200. class' __dict__ in order to support `name` and `value` being both
  201. properties for enum members (which live in the class' __dict__) and
  202. enum members themselves.
  203. """
  204. if _is_dunder(name):
  205. raise AttributeError(name)
  206. try:
  207. return cls._member_map_[name]
  208. except KeyError:
  209. raise AttributeError(name) from None
  210. def __getitem__(cls, name):
  211. return cls._member_map_[name]
  212. def __iter__(cls):
  213. return (cls._member_map_[name] for name in cls._member_names_)
  214. def __len__(cls):
  215. return len(cls._member_names_)
  216. @property
  217. def __members__(cls):
  218. """Returns a mapping of member name->value.
  219. This mapping lists all enum members, including aliases. Note that this
  220. is a read-only view of the internal mapping.
  221. """
  222. return MappingProxyType(cls._member_map_)
  223. def __repr__(cls):
  224. return "<enum %r>" % cls.__name__
  225. def __reversed__(cls):
  226. return (cls._member_map_[name] for name in reversed(cls._member_names_))
  227. def __setattr__(cls, name, value):
  228. """Block attempts to reassign Enum members.
  229. A simple assignment to the class namespace only changes one of the
  230. several possible ways to get an Enum member from the Enum class,
  231. resulting in an inconsistent Enumeration.
  232. """
  233. member_map = cls.__dict__.get('_member_map_', {})
  234. if name in member_map:
  235. raise AttributeError('Cannot reassign members.')
  236. super().__setattr__(name, value)
  237. def _create_(cls, class_name, names=None, *, module=None, qualname=None, type=None):
  238. """Convenience method to create a new Enum class.
  239. `names` can be:
  240. * A string containing member names, separated either with spaces or
  241. commas. Values are auto-numbered from 1.
  242. * An iterable of member names. Values are auto-numbered from 1.
  243. * An iterable of (member name, value) pairs.
  244. * A mapping of member name -> value.
  245. """
  246. metacls = cls.__class__
  247. bases = (cls, ) if type is None else (type, cls)
  248. classdict = metacls.__prepare__(class_name, bases)
  249. # special processing needed for names?
  250. if isinstance(names, str):
  251. names = names.replace(',', ' ').split()
  252. if isinstance(names, (tuple, list)) and isinstance(names[0], str):
  253. names = [(e, i) for (i, e) in enumerate(names, 1)]
  254. # Here, names is either an iterable of (name, value) or a mapping.
  255. for item in names:
  256. if isinstance(item, str):
  257. member_name, member_value = item, names[item]
  258. else:
  259. member_name, member_value = item
  260. classdict[member_name] = member_value
  261. enum_class = metacls.__new__(metacls, class_name, bases, classdict)
  262. # TODO: replace the frame hack if a blessed way to know the calling
  263. # module is ever developed
  264. if module is None:
  265. try:
  266. module = sys._getframe(2).f_globals['__name__']
  267. except (AttributeError, ValueError) as exc:
  268. pass
  269. if module is None:
  270. _make_class_unpicklable(enum_class)
  271. else:
  272. enum_class.__module__ = module
  273. if qualname is not None:
  274. enum_class.__qualname__ = qualname
  275. return enum_class
  276. @staticmethod
  277. def _get_mixins_(bases):
  278. """Returns the type for creating enum members, and the first inherited
  279. enum class.
  280. bases: the tuple of bases that was given to __new__
  281. """
  282. if not bases:
  283. return object, Enum
  284. # double check that we are not subclassing a class with existing
  285. # enumeration members; while we're at it, see if any other data
  286. # type has been mixed in so we can use the correct __new__
  287. member_type = first_enum = None
  288. for base in bases:
  289. if (base is not Enum and
  290. issubclass(base, Enum) and
  291. base._member_names_):
  292. raise TypeError("Cannot extend enumerations")
  293. # base is now the last base in bases
  294. if not issubclass(base, Enum):
  295. raise TypeError("new enumerations must be created as "
  296. "`ClassName([mixin_type,] enum_type)`")
  297. # get correct mix-in type (either mix-in type of Enum subclass, or
  298. # first base if last base is Enum)
  299. if not issubclass(bases[0], Enum):
  300. member_type = bases[0] # first data type
  301. first_enum = bases[-1] # enum type
  302. else:
  303. for base in bases[0].__mro__:
  304. # most common: (IntEnum, int, Enum, object)
  305. # possible: (<Enum 'AutoIntEnum'>, <Enum 'IntEnum'>,
  306. # <class 'int'>, <Enum 'Enum'>,
  307. # <class 'object'>)
  308. if issubclass(base, Enum):
  309. if first_enum is None:
  310. first_enum = base
  311. else:
  312. if member_type is None:
  313. member_type = base
  314. return member_type, first_enum
  315. @staticmethod
  316. def _find_new_(classdict, member_type, first_enum):
  317. """Returns the __new__ to be used for creating the enum members.
  318. classdict: the class dictionary given to __new__
  319. member_type: the data type whose __new__ will be used by default
  320. first_enum: enumeration to check for an overriding __new__
  321. """
  322. # now find the correct __new__, checking to see of one was defined
  323. # by the user; also check earlier enum classes in case a __new__ was
  324. # saved as __new_member__
  325. __new__ = classdict.get('__new__', None)
  326. # should __new__ be saved as __new_member__ later?
  327. save_new = __new__ is not None
  328. if __new__ is None:
  329. # check all possibles for __new_member__ before falling back to
  330. # __new__
  331. for method in ('__new_member__', '__new__'):
  332. for possible in (member_type, first_enum):
  333. target = getattr(possible, method, None)
  334. if target not in {
  335. None,
  336. None.__new__,
  337. object.__new__,
  338. Enum.__new__,
  339. }:
  340. __new__ = target
  341. break
  342. if __new__ is not None:
  343. break
  344. else:
  345. __new__ = object.__new__
  346. # if a non-object.__new__ is used then whatever value/tuple was
  347. # assigned to the enum member name will be passed to __new__ and to the
  348. # new enum member's __init__
  349. if __new__ is object.__new__:
  350. use_args = False
  351. else:
  352. use_args = True
  353. return __new__, save_new, use_args
  354. class Enum(metaclass=EnumMeta):
  355. """Generic enumeration.
  356. Derive from this class to define new enumerations.
  357. """
  358. def __new__(cls, value):
  359. # all enum instances are actually created during class construction
  360. # without calling this method; this method is called by the metaclass'
  361. # __call__ (i.e. Color(3) ), and by pickle
  362. if type(value) is cls:
  363. # For lookups like Color(Color.red)
  364. return value
  365. # by-value search for a matching enum member
  366. # see if it's in the reverse mapping (for hashable values)
  367. try:
  368. if value in cls._value2member_map_:
  369. return cls._value2member_map_[value]
  370. except TypeError:
  371. # not there, now do long search -- O(n) behavior
  372. for member in cls._member_map_.values():
  373. if member._value_ == value:
  374. return member
  375. raise ValueError("%r is not a valid %s" % (value, cls.__name__))
  376. def __repr__(self):
  377. return "<%s.%s: %r>" % (
  378. self.__class__.__name__, self._name_, self._value_)
  379. def __str__(self):
  380. return "%s.%s" % (self.__class__.__name__, self._name_)
  381. def __dir__(self):
  382. added_behavior = [
  383. m
  384. for cls in self.__class__.mro()
  385. for m in cls.__dict__
  386. if m[0] != '_'
  387. ]
  388. return (['__class__', '__doc__', '__module__', 'name', 'value'] +
  389. added_behavior)
  390. def __format__(self, format_spec):
  391. # mixed-in Enums should use the mixed-in type's __format__, otherwise
  392. # we can get strange results with the Enum name showing up instead of
  393. # the value
  394. # pure Enum branch
  395. if self._member_type_ is object:
  396. cls = str
  397. val = str(self)
  398. # mix-in branch
  399. else:
  400. cls = self._member_type_
  401. val = self._value_
  402. return cls.__format__(val, format_spec)
  403. def __hash__(self):
  404. return hash(self._name_)
  405. def __reduce_ex__(self, proto):
  406. return self.__class__, (self._value_, )
  407. # DynamicClassAttribute is used to provide access to the `name` and
  408. # `value` properties of enum members while keeping some measure of
  409. # protection from modification, while still allowing for an enumeration
  410. # to have members named `name` and `value`. This works because enumeration
  411. # members are not set directly on the enum class -- __getattr__ is
  412. # used to look them up.
  413. @DynamicClassAttribute
  414. def name(self):
  415. """The name of the Enum member."""
  416. return self._name_
  417. @DynamicClassAttribute
  418. def value(self):
  419. """The value of the Enum member."""
  420. return self._value_
  421. class IntEnum(int, Enum):
  422. """Enum where members are also (and must be) ints"""
  423. def unique(enumeration):
  424. """Class decorator for enumerations ensuring unique member values."""
  425. duplicates = []
  426. for name, member in enumeration.__members__.items():
  427. if name != member.name:
  428. duplicates.append((name, member.name))
  429. if duplicates:
  430. alias_details = ', '.join(
  431. ["%s -> %s" % (alias, name) for (alias, name) in duplicates])
  432. raise ValueError('duplicate values found in %r: %s' %
  433. (enumeration, alias_details))
  434. return enumeration