Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Python debugger step in generators?

I am currently using NetBeans IDE with Jython 2.5.1

When debugging my project step by step, as soon as an iteration over a generator is encountered, the debugger goes straight to the end of the code. The output works ok, but it is impossible to do step by step debugging once the first generator is met.

Is this a standard behavior for Python debugging in all Python IDE? Is it not possible to debug the code "yield after yield" the same way we can debug VBA for each element of a "for" loop (sorry for the mention of VBA :)?

Thanks.

EDIT

Without the generator

Code:

def example(n):
i = 1
while i <= n:
    yield i
    i += 1

print "hello"

print "goodbye"

Output:

hello
goodbye

Debugging:

[LOG]PythonDebugger : overall Starting
[LOG]PythonDebugger.taskStarted : I am Starting a new Debugging Session ...
[LOG]This window is an interactive debugging context aware Python Shell 
[LOG]where you can enter python console commands while debugging 

(...)

>>>[stdout:]hello
>>>[stdout:]goodbye
Debug session normal end

With the Generator

Code:

def example(n):
    i = 1
    while i <= n:
        yield i
        i += 1

print "hello"

for n in example(3):
    print n

print "goodbye"

Output:

hello
1
2
3
goodbye

Debugging:

[LOG]PythonDebugger : overall Starting
[LOG]PythonDebugger.taskStarted : I am Starting a new Debugging Session ...
[LOG]This window is an interactive debugging context aware Python Shell 
[LOG]where you can enter python console commands while debugging 

(...)

>>>[stdout:]hello
>>>None['GeneratorExit
deamon ended
']

Debug session normal end
like image 291
StackyAndHutch Avatar asked Jan 29 '12 05:01

StackyAndHutch


People also ask

What does Python debugger do?

The Python debugger is an interactive source code debugger for Python programs. It can set conditional breakpoints and single stepping at the source line level. It also supports inspection of stack frames, source code listing, and evaluation of arbitrary Python code in any stack frame's context.

How do Python generators work?

A Python generator is a function that produces a sequence of results. It works by maintaining its local state, so that the function can resume again exactly where it left off when called subsequent times. Thus, you can think of a generator as something like a powerful iterator.

Does Python have a built in debugger?

Python has a built-in debugger called pdb . It's a simple utility with a command line interface that does the main job. It has all the debugger features you'll need, but if you're looking to pimp it up a little, you can extend it using ipdb, which will provide the debugger with features from IPython.


1 Answers

I don't use NetBeans, but pdb at least does step through generators. For example:

$ cat test.py
def the_generator():
    for i in xrange(10):
        yield i

for x in the_generator():
    print x

$ python -mpdb test.py
> test.py(1)<module>()
-> def the_generator():
(Pdb) n
> test.py(5)<module>()
-> for x in the_generator():
(Pdb) s
--Call--
> test.py(1)the_generator()
-> def the_generator():
(Pdb) n
> test.py(2)the_generator()
-> for i in xrange(10):
(Pdb) n
> test.py(3)the_generator()
-> yield i
(Pdb) n
--Return--
> test.py(3)the_generator()->0
-> yield i
(Pdb) n
> test.py(6)<module>()
-> print x
(Pdb) n
0

If you post some code, we could try to figure out exactly what's going on in your case.

like image 53
Danica Avatar answered Sep 25 '22 01:09

Danica