aegger
aegger

Reputation: 460

ipdb / set_trace() bug on Windows?

I am getting a really strange output when running a python script and setting a break point with ipdb as in this program:

import sys
import ipdb
parents, babies = (1, 1)
while babies < 100:
    ipdb.set_trace()
    print 'This generation has {0} babies'.format(babies)
    ipdb.set_trace()
    parents, babies = (babies, parents + babies)

It all works fine when running the script at first, stopping at the first break point and printing all the variables. But as soon as I approach the second break point where it doesn't matter if I step through it or just continue, I get these kind of weird characters as output in the console:

C:\pythontest>python ipdb_test2.py
> c:\pythontest\ipdb_test2.py(6)<module>()
      5         ipdb.set_trace()
----> 6         print 'This generation has {0} babies'.format(babies)
      7         ipdb.set_trace()

ipdb> n
This generation has 1 babies
> c:\pythontest\ipdb_test2.py(7)<module>()
      6         print 'This generation has {0} babies'.format(babies)
----> 7         ipdb.set_trace()
      8         parents, babies = (babies, parents + babies)

ipdb> n
> ←[1;32mc:\pythontest\ipdb_test2.py←[0m(8)←[0;36m<module>←[1;34m()←[0m
←[1;32m      6 ←[1;33m        ←[1;32mprint←[0m ←[1;34m'This generation has {0} b
abies'←[0m←[1;33m.←[0m←[0mformat←[0m←[1;33m(←[0m←[0mbabies←[0m←[1;33m)←[0m←[1;33
m←[0m←[0m
←[0m←[1;32m      7 ←[1;33m        ←[0mipdb←[0m←[1;33m.←[0m←[0mset_trace←[0m←[1;3
3m(←[0m←[1;33m)←[0m←[1;33m←[0m←[0m
←[0m←[1;32m----> 8 ←[1;33m        ←[0mparents←[0m←[1;33m,←[0m ←[0mbabies←[0m ←[1
;33m=←[0m ←[1;33m(←[0m←[0mbabies←[0m←[1;33m,←[0m ←[0mparents←[0m ←[1;33m+←[0m ←[
0mbabies←[0m←[1;33m)←[0m←[1;33m←[0m←[0m
←[0m
ipdb> n
> ←[1;32mc:\pythontest\ipdb_test2.py←[0m(4)←[0;36m<module>←[1;34m()←[0m
←[1;32m      3 ←[1;33m←[0mparents←[0m←[1;33m,←[0m ←[0mbabies←[0m ←[1;33m=←[0m ←[
1;33m(←[0m←[1;36m1←[0m←[1;33m,←[0m ←[1;36m1←[0m←[1;33m)←[0m←[1;33m←[0m←[0m
←[0m←[1;32m----> 4 ←[1;33m←[1;32mwhile←[0m ←[0mbabies←[0m ←[1;33m<←[0m ←[1;36m10
0←[0m←[1;33m:←[0m←[1;33m←[0m←[0m
←[0m←[1;32m      5 ←[1;33m        ←[0mipdb←[0m←[1;33m.←[0m←[0mset_trace←[0m←[1;3
3m(←[0m←[1;33m)←[0m←[1;33m←[0m←[0m
←[0m
ipdb>

As soon as I hit the ipdb.set_trace() command the second time it will output these kind of characters and from that point on the debugger becomes unusable. I tried it on different consoles but the error seems to persist.

I am using Python 2.7.8 with Anaconda 2.1.0 (64 bit) on Windows, any ideas how to solve this problem are warmly welcomed.

Upvotes: 3

Views: 1065

Answers (2)

Seth Sims
Seth Sims

Reputation: 66

The strange output are ANSI escape codes. That's how ipdb does syntax highlighting. However CMD windows don't support the escape codes by default, it's been that way since DOS days. You have to enable a special driver named ANSI.SYS for the control codes to work. ipdb must be pulling some kind of magic that breaks the second time you call set_trace().

Upvotes: 5

metakermit
metakermit

Reputation: 22351

The normal approach for using ipdb (and pdb as far as I know) is to only set one import ipdb; ipdb.set_trace() command in your code where you want to break into the debugger. From there you can set other breakpoints, using the break or b command and then pressing continue or c to get to that breakpoint. Consider this simple pdb session for example:

➜  python  python hello.py
hello
> /Users/kermit/Dropbox/dev/skripte/python/hello.py(4)<module>()
-> print('hello 2')
(Pdb) l
  1     print('hello')
  2     import pdb; pdb.set_trace()
  3
  4  -> print('hello 2')
  5     print('hello 3')
[EOF]
(Pdb) b 5
Breakpoint 1 at /Users/kermit/Dropbox/dev/skripte/python/hello.py:5
(Pdb) c
hello 2
> /Users/kermit/Dropbox/dev/skripte/python/hello.py(5)<module>()
-> print('hello 3')
(Pdb)

Upvotes: -1

Related Questions