2.X functools.update_wrapper dislikes missing function attributes

S

samwyse

I noticed a behavior in Jython 2.5.2 that's arguably an implementation
bug, but I'm wondering if it's something to be fixed for all versions
of Python. I was wanting to decorate a Java instance method, and
discovered that it didn't have a __module__ attribute. This caused
the following message:

File "C:\jython2.5.2\Lib\functools.py", line 33, in update_wrapper
setattr(wrapper, attr, getattr(wrapped, attr))
AttributeError: 'instancemethod' object has no attribute '__module__'

The relevant code is:
for attr in assigned:
setattr(wrapper, attr, getattr(wrapped, attr))
for attr in updated:
getattr(wrapper, attr).update(getattr(wrapped, attr, {}))

Note that attributes to be updated get a default value. I'm proposing
that attributes to be assigned do the same, most likely an empty
string. A non-string value (such as None) could break anything
expecting a string value, so it seems like a bad idea. Python 3.2
catches AttributeError and passes. I don't like this solution. While
it prevents any attributes from being added to the wrapper, the
wrapper likely has its own values (at least for the default
attributes) and using those values could cause confusion.

Any opinions?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top