Windows 上的 ipdb / set_trace() 错误?

ipdb / set_trace() bug on Windows?

当 运行 一个 python 脚本并使用 ipdb 设置一个断点时,我得到了一个非常奇怪的输出,就像在这个程序中一样:

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)

一开始 运行 脚本时一切正常,在第一个断点处停止并打印所有变量。但是一旦我接近第二个断点,无论我是单步执行还是继续,我都会在控制台输出这些奇怪的字符:

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>

一旦我第二次点击 ipdb.set_trace() 命令,它就会输出这些字符,从那时起,调试器就无法使用了。我在不同的控制台上尝试过,但错误似乎仍然存在。

我在 Windows 上使用 Python 2.7.8 和 Anaconda 2.1.0(64 位),热烈欢迎任何解决此问题的想法。

使用 ipdb(据我所知 pdb)的正常方法是在您想要进入调试器的代码中只设置一个 import ipdb; ipdb.set_trace() 命令.从那里您可以设置其他断点,使用 break or b command 然后按 continuec 到达该断点。以这个简单的 pdb 会话为例:

➜  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)

控制代码工作的奇怪输出是 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。 ipdb 必须在你第二次调用 set_trace().

时使用某种魔法来破坏