Connecting Tech Pros Worldwide Help | Site Map

functools's partial and update_wrapper does not work together

 
LinkBack Thread Tools Search this Thread
  #1  
Old August 21st, 2008, 12:15 PM
piotr.findeisen@gmail.com
Guest
 
Posts: n/a
Default functools's partial and update_wrapper does not work together

Hello!

I wanted to use a decorator to wrap partially applied function like
this:
from functools import *

def never_throw(f):
@wraps(f)
def wrapper(*args, **kwargs):
try: return f(*args, **kwargs)
except: pass
return wrapper

def foo(i):
raise ValueError(str(i) + " is too little")

never_throw(partial(foo, 3))

However this fails with an exception saying
AttributeError: 'functools.partial' object has no attribute
'__module__'.

How can I use generic wrapper (never_throw) around partials?

Thank you,
Piotr

  #2  
Old August 21st, 2008, 03:45 PM
Bruno Desthuilliers
Guest
 
Posts: n/a
Default Re: functools's partial and update_wrapper does not work together

piotr.findeisen@gmail.com a écrit :
Quote:
Hello!
>
I wanted to use a decorator to wrap partially applied function like
this:
from functools import *
>
def never_throw(f):
@wraps(f)
def wrapper(*args, **kwargs):
try: return f(*args, **kwargs)
except: pass
Not an answer to your question, but: by all means, *never* use bare
except clauses. Please remember that SysExit and KeyboardInterrupt are
exceptions too.
Quote:
return wrapper
>
def foo(i):
raise ValueError(str(i) + " is too little")
>
never_throw(partial(foo, 3))
>
However this fails with an exception saying
AttributeError: 'functools.partial' object has no attribute
'__module__'.
>
How can I use generic wrapper (never_throw) around partials?
If not already done by someone else, it might be worth filling a ticket
- generic decorators should work with any callable, not only with
functions (well, IMHO at least).

A possible workaround is:

_dont_pass = (
SysExit,
KeyboardInterrupt,
OtherStuffYouDontWantToPassSilentyHere,
)

def never_throw(f):
"""well... almost never..."
try:
f.__module__
except AttributeError:
f.__module__ = "whatever"
@wraps(f)
def wrapper(*args, **kwargs):
try:
return f(*args, **kwargs)
except Exception, e:
if isinstance(e, _dont_pass):
raise



HTH
  #3  
Old August 21st, 2008, 08:55 PM
piotr.findeisen@gmail.com
Guest
 
Posts: n/a
Default Re: functools's partial and update_wrapper does not work together

Quote:
If not already done by someone else, it might be worth filling a ticket
- generic decorators should work with any callable, not only with
functions (well, IMHO at least).
you're right. it's already there http://bugs.python.org/issue3445

regards!
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.