I see advice on how to debug a python module that hangs up the process
Bengt Richter
bokr at oz.net
Tue Dec 16 20:48:11 EST 2003
On Tue, 16 Dec 2003 15:37:22 -0800, JD <lists at webcrunchers.com> wrote:
>
>On Dec 16, 2003, at 2:45 PM, Robin Munn wrote:
>
>> The first step I'd suggest is to sprinkle print statements all around
>> your code:
>>
>> print "About to call some_func()..."
>> some_func()
>> print "Returned from calling some_func()..."
>>
>> Then you can watch the console output (or redirect it to a file for
>> later perusal) and see when the thing hangs.
>
>We already know the function it hangs up at. It's a Python function
>we defined.
>
>Obviously, we put a print statement right at the beginning of the
>errant
>function, but it doesn't appear to be getting there.
>
>IE:
>
>print "before function call"
>my_function(some_arguments)
>print "after function call"
>
>def my_function(some_arguments):
> print "start of function call" <--- this never gets called, or
>we don't see the output
> <rest of code>
>
>So when this piece of code is called, we get:
>
>before function call
> <system hangs up>
>
>Then, we have to do a "kill -9" the process.
>
It would be more interesting if the above was real code instead of prose ;-)
If you know the failing function, and the place where it's being called as above,
what about actually copy/pasting the few lines instead of ad-libbing?
Also, as paul says, what are the arguments? What happens if you change
--
print 'before function call'
my_function(some_arguments)
print 'after function call'
--
to
--
print 'before argument evaluation'
the_arg_tuple = (arg_expr1, arg_expr2, etc.blah) # i.e., whatever "some_arguments" is, in parens
print 'before function call'
my_function(*the_arg_tuple)
print 'after function call'
--
If that hangs during some_expression tuple evaluation, you can do the expressions one at a time, e.g.
the_arg_tuple = (arg_expr1,)
print 'first ok'
the_arg_tuple = the_arg_tuple + (arg_expr2,)
print 'second ok'
etc.
**** But show us a real session copy/pasted, not code-prose, please. ****
You could also run python with -u so you're sure to get all the output flushed immediately,
just in case.
And just after the first before print, you could also use sys.settrace to hook in a debugging
trace that can at least trace non-C python stuff. It might get you closer.
E.g., (quick hack)
>>> import sys
>>> def ptrace(frame, event, arg):
... sys.stdout.write('%7s %5s: %10s %r\n' % (
... event,
... frame.f_lineno,
... frame.f_code.co_name,
... frame.f_code.co_filename))
... sys.stdout.flush()
... if event != 'return': return ptrace
...
>>> sys.settrace(ptrace)
>>> def foo(x):
... return x*2
...
call 1: ? '<stdin>'
line 1: ? '<stdin>'
return 1: ? '<stdin>'
>>> foo(foo(3))
call 1: ? '<stdin>'
line 1: ? '<stdin>'
call 1: foo '<stdin>'
line 2: foo '<stdin>'
return 2: foo '<stdin>'
call 1: foo '<stdin>'
line 2: foo '<stdin>'
return 2: foo '<stdin>'
12
return 1: ? '<stdin>'
Maybe it will give more clues.
Regards,
Bengt Richter
More information about the Python-list
mailing list