I tried using the Python decorator to detect exceptions and register exceptions.
import os.path
import shutil
class log(object):
def __init__(self, f):
print "Inside __init__()"
self.f = f
def __call__(self, *args):
print "Inside __call__()"
try:
self.f(*args)
except Exception:
print "Sorry"
@log
def testit(a, b, c):
print a,b,c
raise RuntimeError()
if __name__ == "__main__":
testit(1,2,3)
It works great
Desktop> python deco.py
Inside __init__()
Inside __call__()
1 2 3
Sorry
The problem is that when I tried to use for testing with doctest
@log
def testit(a, b, c):
"""
>>> testit(1,2,3)
"""
print a,b,c
raise RuntimeError()
if __name__ == "__main__":
import doctest
doctest.testmod()
Nothing happens.
Desktop> python deco2.py
Inside __init__()
What happened to this?
source
share