diff options
author | Pablo Galindo Salgado <Pablogsal@gmail.com> | 2021-08-25 14:24:32 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-08-25 14:24:32 (GMT) |
commit | 24da544014f78e6f1440d5ce5c2d14794a020340 (patch) | |
tree | 9f2dd662a313c14ac9ae001b5839c0926b2242dd /Lib/enum.py | |
parent | a3c11cebf174e0c822eda8c545f7548269ce7a25 (diff) | |
download | cpython-24da544014f78e6f1440d5ce5c2d14794a020340.zip cpython-24da544014f78e6f1440d5ce5c2d14794a020340.tar.gz cpython-24da544014f78e6f1440d5ce5c2d14794a020340.tar.bz2 |
bpo-44929: [Enum] Fix global repr (GH-27789)
* Fix typo in __repr__ code
* Add more tests for global int flag reprs
* use last module if multi-module string
- when an enum's `__module__` contains several module names, only
use the last one
Co-authored-by: Ćukasz Langa <lukasz@langa.pl>
Co-authored-by: Ethan Furman <ethan@stoneleaf.us>
Diffstat (limited to 'Lib/enum.py')
-rw-r--r-- | Lib/enum.py | 19 |
1 files changed, 15 insertions, 4 deletions
diff --git a/Lib/enum.py b/Lib/enum.py index 84e3cc1..0776761 100644 --- a/Lib/enum.py +++ b/Lib/enum.py @@ -1390,17 +1390,28 @@ def _power_of_two(value): return value == 2 ** _high_bit(value) def global_enum_repr(self): - return '%s.%s' % (self.__class__.__module__, self._name_) + """ + use module.enum_name instead of class.enum_name + + the module is the last module in case of a multi-module name + """ + module = self.__class__.__module__.split('.')[-1] + return '%s.%s' % (module, self._name_) def global_flag_repr(self): - module = self.__class__.__module__ + """ + use module.flag_name instead of class.flag_name + + the module is the last module in case of a multi-module name + """ + module = self.__class__.__module__.split('.')[-1] cls_name = self.__class__.__name__ if self._name_ is None: - return "%x" % (module, cls_name, self._value_) + return "%s.%s(0x%x)" % (module, cls_name, self._value_) if _is_single_bit(self): return '%s.%s' % (module, self._name_) if self._boundary_ is not FlagBoundary.KEEP: - return module + module.join(self.name.split('|')) + return '|'.join(['%s.%s' % (module, name) for name in self.name.split('|')]) else: name = [] for n in self._name_.split('|'): |