473,587 Members | 2,505 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(par tial(foo, 3))

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

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

Thank you,
Piotr
Aug 21 '08 #1
2 2794
pi************* @gmail.com a écrit :
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 KeyboardInterru pt are
exceptions too.
return wrapper

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

never_throw(par tial(foo, 3))

However this fails with an exception saying
AttributeError: 'functools.part ial' 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,
KeyboardInterru pt,
OtherStuffYouDo ntWantToPassSil entyHere,
)

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
Aug 21 '08 #2
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!
Aug 21 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

43
2619
by: nospam | last post by:
I got three (3) files (1) Untitled.aspx (2) Untitled.aspx.1.cs (3) Untitled.aspx.2.cs These three files must be used together to make file #1, Untitled.aspx, page work via J.I.T. when the User first hits Internet Explorer 6.0 on your browser.
7
2649
by: nospam | last post by:
Ok, 3rd or is it the 4th time I have asked this question on Partial Types, so, since it seems to me that Partial Types is still in the design or development stages at Microsoft, I am going to ask it differently. FOUR QUESTIONS: The background: I got three (3) files
8
1693
by: Paul Rubin | last post by:
I just had to write some programs that crunched a lot of large files, both text and binary. As I use iterators more I find myself wishing for some maybe-obvious enhancements: 1. File iterator for blocks of chars: f = open('foo') for block in f.iterchars(n=1024): ... iterates through 1024-character blocks from the file. The default...
0
7920
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7849
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8215
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8347
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8220
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5718
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3844
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1189
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.