Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-93910: Fix enum performance regression #94614

Merged
merged 5 commits into from
Jul 7, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions Lib/enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,16 +309,17 @@ def __set_name__(self, enum_class, member_name):
if descriptor and not need_override:
# previous enum.property found, no further action needed
pass
else:
elif descriptor and need_override:
redirect = property()
redirect.__set_name__(enum_class, member_name)
if descriptor and need_override:
# previous enum.property found, but some other inherited attribute
# is in the way; copy fget, fset, fdel to this one
redirect.fget = descriptor.fget
redirect.fset = descriptor.fset
redirect.fdel = descriptor.fdel
# previous enum.property found, but some other inherited attribute
# is in the way; copy fget, fset, fdel to this one
mdboom marked this conversation as resolved.
Show resolved Hide resolved
redirect.fget = descriptor.fget
redirect.fset = descriptor.fset
redirect.fdel = descriptor.fdel
setattr(enum_class, member_name, redirect)
else:
setattr(enum_class, member_name, enum_member)
# now add to _member_map_ (even aliases)
enum_class._member_map_[member_name] = enum_member
try:
Expand Down Expand Up @@ -647,7 +648,7 @@ def __new__(metacls, cls, bases, classdict, *, boundary=None, _simple=False, **k
'member order does not match _order_:\n %r\n %r'
% (enum_class._member_names_, _order_)
)
#

return enum_class

def __bool__(cls):
Expand Down
1 change: 1 addition & 0 deletions Lib/test/test_enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -2646,6 +2646,7 @@ class Private(Enum):
self.assertEqual(Private._Private__corporal, 'Radar')
self.assertEqual(Private._Private__major_, 'Hoolihan')

@unittest.skip("Accessing all values retained for performance reasons, see GH-93910")
def test_exception_for_member_from_member_access(self):
with self.assertRaisesRegex(AttributeError, "<enum .Di.> member has no attribute .NO."):
class Di(Enum):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
The ability to access the other values of an enum on an enum (e.g.
``Color.RED.BLUE``) has been restored in order to fix a performance
regression.