-
-
Notifications
You must be signed in to change notification settings - Fork 32.7k
Refs #29253 -- Fixed method_decorator() crash if decorator sets a new attribute. #10091
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
Conversation
django/utils/decorators.py
Outdated
# a function because one can't set new attributes on bound method | ||
# objects -- only functions. | ||
def bound_method(*args2, **kwargs2): | ||
return method.__get__(self, type(self))(*args2, **kwargs2) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would functools.partial
work here?
from functools import partial
bound_method = partial(method.__get__(self, type(self)))
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems to work (the decorator tests passed locally), which surprised me. Thanks! The use of def bound_method(*args2, **kwargs2)
I restored from Django's code base prior to my change, but it wasn't clear it was needed since the tests passed without it (but with this PR it's now covered by the tests).
tests/decorators/tests.py
Outdated
""" | ||
Test transaction.non_atomic_requests(). | ||
""" | ||
import django.db.transaction as transaction |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Move to the top.
tests/decorators/tests.py
Outdated
obj = MyClass() | ||
obj.method() # This should not error out. | ||
|
||
def test_non_atomic_requests(self): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think both tests are required.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll remove the second one then (the test of transaction.non_atomic_requests()
), because the first one is more focused and tests the exact code path that went wrong.
The second one was useful in showing reviewers that the reported regression was actually fixed. I will remove the test after addressing your other review comment so that CI can at least run this test once with your first suggested change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm addressing the comments soon.
tests/decorators/tests.py
Outdated
obj = MyClass() | ||
obj.method() # This should not error out. | ||
|
||
def test_non_atomic_requests(self): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll remove the second one then (the test of transaction.non_atomic_requests()
), because the first one is more focused and tests the exact code path that went wrong.
The second one was useful in showing reviewers that the reported regression was actually fixed. I will remove the test after addressing your other review comment so that CI can at least run this test once with your first suggested change.
django/utils/decorators.py
Outdated
# a function because one can't set new attributes on bound method | ||
# objects -- only functions. | ||
def bound_method(*args2, **kwargs2): | ||
return method.__get__(self, type(self))(*args2, **kwargs2) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems to work (the decorator tests passed locally), which surprised me. Thanks! The use of def bound_method(*args2, **kwargs2)
I restored from Django's code base prior to my change, but it wasn't clear it was needed since the tests passed without it (but with this PR it's now covered by the tests).
I addressed @charettes's comments. |
… attribute. Regression in fdc936c.
258cc8b
to
b53fc83
Compare
This fixes a regression stated in PR #9819 in this comment:
#9819 (comment)