I want to wrap the default public method with a wrapper, which should also catch exceptions. Here is an example of testing working :
truemethod = open
def fn(*args, **kwargs):
try:
return truemethod(*args, **kwargs)
except (IOError, OSError):
sys.exit('Can\'t open \'{0}\'. Error #{1[0]}: {1[1]}'.format(args[0], sys.exc_info()[1].args))
open = fn
I want to create a generic method:
def wrap(method, exceptions = (OSError, IOError)):
truemethod = method
def fn(*args, **kwargs):
try:
return truemethod(*args, **kwargs)
except exceptions:
sys.exit('Can\'t open \'{0}\'. Error #{1[0]}: {1[1]}'.format(args[0], sys.exc_info()[1].args))
method = fn
But this does not work:
>>> wrap(open)
>>> open
<built-in function open>
Apparently, it methodis a copy of the parameter, not a link, as I expected. Any pythonic workaround?
source
share