2.X functools.update_wrapper dislikes missing function attributes

samwyse samwyse at gmail.com
Tue Apr 26 23:12:09 CEST 2011


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?



More information about the Python-list mailing list