wrapping all class methods

Bengt Richter bokr at oz.net
Sat Nov 16 18:16:36 EST 2002


On Sat, 16 Nov 2002 21:47:53 +0000, Robin Becker <robin at jessikat.fsnet.co.uk> wrote:

>In article <ar61rk$gun$0 at 216.39.172.122>, Bengt Richter <bokr at oz.net>
>writes
>>On Sat, 16 Nov 2002 12:35:45 +0000, Robin Becker <robin at jessikat.fsnet.co.uk> 
>>wrote:
>>
>>>In article <pan.2002.11.16.12.39.49.262808.1346 at club-internet.fr>, Pedro 
>>Rodriguez <pedro_rodriguez at club-internet.fr> writes
>>>
>>>This might be true for a proper program, but in this context
>>>I don't know which exceptions are being silently ignored further
>>>up the chain. So this was an attempt at a quick and dirty diagnostic
>>> to discover which exceptions were being raised in the backend and
>>
>>Just wondering if you had used
>        http://www.python.org/doc/current/lib/debugger-
>hooks.html#debugger-hooks
>..... no I hadn't used that, but then I don't want a trace I want to
>discover which tracebacks are being detected and trapped.

I thought you might be able to do a silent trace and see the exceptions
before they're "trapped". (The hook doesn't do any output or force you to.
You choose what to do if/when you see something of interest).

E.g., this is what my tracewatch does being told to watch only calls
and returns from foo and bar, but incidentally reporting on exceptions:

 >>> from ut.tracewatch import TraceWatch as TW
 >>> def foo(n):
 ...     if n>4: raise StopIteration # or whatever
 ...     bar(n+1)
 ...
 >>> def bar(n):
 ...     if n>3: foo(n+1)
 ...     try:
 ...         foo(n+1)
 ...     except StopIteration:
 ...         print 'Caught stopiter with n = %s' % n
 ...
 >>> tw=TW()
 >>> tw.addwatch('foo','#f')
 >>> tw.addwatch('bar','#f')
 >>> tw.on()
 >>> foo(2)
 --------------------------------------------------------------------
 File: "<stdin>"
 Line [scope] C:all, R:eturn eX:cept S:tack N:ew M:od E:quiv U:nbound
 ---- ------- -------------------------------------------------------
    1 [foo]:             C: foo(n=2)
    1 [bar]:             C: bar(n=3)
    1 [foo]:             C: foo(n=4)
    1 [bar]:             C: bar(n=5)
    1 [foo]:             C: foo(n=6)
    2 [foo]:             X: StopIteration()
                         S: < bar:2 < foo:3 < bar:4 < foo:3 < ?:1
    2 [bar]:             X: StopIteration()
                         S: < foo:3 < bar:4 < foo:3 < ?:1
    3 [foo]:             X: StopIteration()
                         S: < bar:4 < foo:3 < ?:1
    4 [bar]:             X: StopIteration()
                         S: < foo:3 < ?:1
 Caught stopiter with n = 3
    6 [bar]:             R: bar(...) => None
    3 [foo]:             R: foo(...) => None

Whereas if we start off with foo at 3, bar won't set the trap, so we get
the full unwind and no returns (R:):

 >>> foo(3)
    1 [foo]:             C: foo(n=3)
    1 [bar]:             C: bar(n=4)
    1 [foo]:             C: foo(n=5)
    2 [foo]:             X: StopIteration()
                         S: < bar:2 < foo:3 < ?:1
    2 [bar]:             X: StopIteration()
                         S: < foo:3 < ?:1
    3 [foo]:             X: StopIteration()
                         S: < ?:1
 Traceback (most recent call last):
   File "<stdin>", line 1, in ?
   File "<stdin>", line 3, in foo
   File "<stdin>", line 2, in bar
   File "<stdin>", line 2, in foo
 StopIteration

Regards,
Bengt Richter



More information about the Python-list mailing list