step在使用pdb/ipdb调试器时,我可以在进入函数后退出一个函数吗?
如果没有这样的选择 - 什么是摆脱步入功能的最快方法?
是否有一个命令可以在ipdb上调试时逐步退出循环(例如,for或while)而不必使用断点?
我使用until命令跳出列表推导,但不知道如果可能的话,我怎么能做整个循环块的类似事情.
如果我ipython正常运行,iPython会记住命令历史记录,例如在repl中测试基本内容,但是我希望能够从上一个调试会话中获取调试命令,而我只是通过运行来调试我的程序正常,程序包含
import ipdb
def info(type, value, info):
import traceback
traceback.print_exception(type, value, info)
ipdb.pm()
import sys
sys.excepthook = info
trace = ipdb.set_trace
Run Code Online (Sandbox Code Playgroud)
这是设置它所以我可以trace()在我的程序中的任何地方编写,以便在我运行程序时开始调试,或者当它自己死时自动启动事后调试.
当涉及到快速代码/测试迭代时,使用iPython的Python已经超越了其他语言,而我现在已经非常接近于必杀技......
有没有办法告诉 pdb 或 ipdb 跳过所有未来的断点并完成执行,就好像它们不存在一样?
最近,当使用设置断点时ipdb.set_trace(context=20),我可以看到我第一次输入的命令,按回车键后,下次我在 ipdb 提示符中写入指令或命令时不会显示。当我按回车键时,它会执行它并在前面的行中显示它。
直到最近才发生这种情况。我使用的是 mac,带有 iterm、最新的 ipdb 和 pytest。
编辑 2022-3-29
编辑 2022-3-31
编辑 2022-3-31 (2.0)
我正在使用 freezegun 1.2.1 和 pytest 6.2.5。当我运行此代码时,如果我执行 print 几次,光标就会消失。这是我能想到的最基本的复制测试。
导入ipdb
从 freezegun 导入 freeze_time
@freeze_time(“2022年3月12日”)
def test_prompt_ipdb():
ipdb.set_trace()
test_prompt_ipdb()
我现在相信这是这两个之一的错误,很可能是 freezegun 做了一些奇特的事情。
$ ./runtests.py -v tests/managers/test_customer.py:CustomerManagerTest.test_register_without_subscription --ipdb
...
test_register_without_subscription (tests.managers.test_customer.CustomerManagerTest) ...
- TRACEBACK --------------------------------------------------------------------
Traceback (most recent call last):
File "/usr/lib/python2.7/unittest/case.py", line 331, in run
testMethod()
File "*****/tests/managers/test_customer.py", line 198, in test_register_without_subscription
1/0
ZeroDivisionError: integer division or modulo by zero
--------------------------------------------------------------------------------
> *****/tests/managers/test_customer.py(198)test_register_without_subscription()
197 def test_register_without_subscription(self):
--> 198 1/0
199 ...
ipdb> import sys
ipdb> sys.exc_info()
(<type 'exceptions.AttributeError'>, AttributeError("Pdb instance has no attribute 'do_sys'",), <traceback object at 0x47eb908>)
ipdb>
Run Code Online (Sandbox Code Playgroud)
我找不到任何ipdb help显示当前异常的命令.
做import sys; print sys.exc_info()不起作用.
目前我这样做:
try:
do_something_that_raises_an_exception() …Run Code Online (Sandbox Code Playgroud) 我正在使用python.el如果我从菜单中选择'debugger'并输入'python -m pdb myfile.py',gud启动,并在拆分框架中我看到(Pdb)提示符在一个,我的python另一个代码在第一行有一个插入符号,表示它已准备就绪.例如,'n'步骤到下一行,插入符相应地移动.
如果我输入'python -m ipdb myfile.py',框架拆分,一个拆分标记为gud,但没有明显的ipdb控制台.换句话说,这种启动ipdb的方式似乎不起作用.如果我使用ipdb.set_trace()手动将断点插入到我的python代码中,Ipdb工作得很好,除了它不使用gud接口.这是故意的,以便ipdb的堆栈跟踪可以很好地工作吗?
如果是这样,那很好,但有没有办法从emacs启动ipdb而无需手动添加set_trace()命令?
当使用python的unittest模块运行测试时,有没有方便的方法来获取异常的ipdb调试器?
使用调试python代码很方便ipython --pdb my_script.py.但是,当我使用unittest模块时,用
class MyTestCase(unittest.TestCase):
def runTest(self):
x = 0
y = 3/x
Run Code Online (Sandbox Code Playgroud)
unittest捕获异常并退出.
我在--pdbipython中使用该命令,因此当我调试代码并发生错误时,它会显示堆栈跟踪.很多这些错误来自于输入错误的numpy或pandas函数.堆栈跟踪从最新的帧开始,来自这些库的代码.up稍后重复5-10次命令我实际上可以看到我做错了什么,这将在90%的时间内立即显现(例如,使用列表而不是数组调用).
有没有办法指定调试器最初启动哪个堆栈帧?最旧的堆栈帧,或最初运行的python文件中的最新堆栈帧,或类似的.这对于调试来说会更有效率.
这是一个简单的例子
import pandas as pd
def test(df): # (A)
df[:,0] = 4 #Bad indexing on dataframe, will cause error
return df
df = test(pd.DataFrame(range(3))) # (B)
Run Code Online (Sandbox Code Playgroud)
为清楚起见,添加了回溯,(A),(B),(C)
In [6]: ---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-6-66730543fac0> in <module>()
----> 1 import codecs, os;__pyfile = codecs.open('''/tmp/py29142W1d''', encoding='''utf-8''');__code = __pyfile.read().encode('''utf-8''');__pyfile.close();os.remove('''/tmp/py29142W1d''');exec(compile(__code, '''/test/stack_frames.py''', 'exec'));
/test/stack_frames.py in <module>()
6
7 if __name__ == '__main__':
(A)----> 8 df = test(pd.DataFrame(range(3)))
/test/stack_frames.py in test(df)
2
3 def test(df):
(B)----> 4 …Run Code Online (Sandbox Code Playgroud) ipdb ×10
python ×10
pdb ×6
debugging ×4
ipython ×2
emacs ×1
exception ×1
gud ×1
python-2.7 ×1
unit-testing ×1