pdb

Python原生调试工具pdb实践小结

混江龙づ霸主 提交于 2019-11-28 07:08:51
使用 python -m pdb xxx.py 进入单步调试模式,默认会在脚本的第一行可执行命令处停止。此时,通过 b function 设置之后的函数断点会提示出错,从出错异常栈中可以看出,pdb是将 function 当做行号来识别,而函数名是字符串,转换成整形会失败,因此会提示出错。在处理转换整形失败异常时,会尝试执行 function ,如果当前解析器还执行到该函数(由于是刚开始调试,相关函数尚未解析),那么在该函数上设置断点又会触发异常。那么,通过单步调试,如何开始呢? 经过多番尝试,总结这样的工程实践经验,在单步进入时,先通过 b lineno 设置为期望调试的入口行号后,按 c 运行至此处,然后,通过 b function 设置后续的函数断点,设置的 function 要求已经过解释器解释过的。这可能是pdb单步调试的缺陷。 还有一种侵入式的调试方式就是在源代码中导入pdb模块,在需要设置断点的地方手动加上 pdb.set_trace() ,通过 python xx.py 命令启动程序后,可直接执行到断点处,然后再单步调试下去。这样对源代码有侵入性,不怎么友好。 来源: https://www.cnblogs.com/cherishui/p/11397933.html

all variables are undefined in python debugger

[亡魂溺海] 提交于 2019-11-28 06:33:18
问题 I'm facing a very strange issue on Python 3.6. In the middle of my code, I call import pdb; pdb.set_trace() to debug some code. And then I'm not able to debug properly, for instance: (Pdb) abc = 3 (Pdb) [abc for _ in range(2)] *** NameError: name 'abc' is not defined (Pdb) [abc, abc] [3, 3] It seems like whenever I use list comprehensions, there is an issue of variable not defined. However, if I call the debugger right after I open Python, I do not observe this behavior, everything runs fine.

How do you watch a variable in pdb

落花浮王杯 提交于 2019-11-28 03:33:29
I'm debugging a python script, and I want to watch a variable for a change (much like you can watch a memory adress in gdb). Is there a way to do this? Here is a really hacky way to do this with pdb . These commands can be put in your ~/.pdbrc for automatic loading every time you use pdb . !global __currentframe, __stack; from inspect import currentframe as __currentframe, stack as __stack !global __copy; from copy import copy as __copy !global __Pdb; from pdb import Pdb as __Pdb !global __pdb; __pdb = [__framerec[0].f_locals.get("pdb") or __framerec[0].f_locals.get("self") for __framerec in _

Is it possible to go into ipython from code?

谁说胖子不能爱 提交于 2019-11-28 02:46:53
For my debugging needs, pdb is pretty good. However, it would be much cooler (and helpful) if I could go into ipython . Is this thing possible? Daniel Roseman There is an ipdb project which embeds iPython into the standard pdb, so you can just do: import ipdb; ipdb.set_trace() It's installable via the usual pip install ipdb . ipdb is pretty short, so instead of easy_installing you can also create a file ipdb.py somewhere on your Python path and paste the following into the file: import sys from IPython.Debugger import Pdb from IPython.Shell import IPShell from IPython import ipapi shell =

How to execute multi-line statements within Python's own debugger (PDB)

大城市里の小女人 提交于 2019-11-28 02:35:06
So I am running a Python script within which I am calling Python's debugger, PDB by writing: import ipdb; ipdb.set_trace() (iPython's version of PDB, though for the matter I don't think it makes a difference; I use it for the colored output only). Now, when I get to the debugger I want to execute a multi-line statement such as an if clause or a for loop but as soon as I type if condition: and hit the return key, I get the error message *** SyntaxError: invalid syntax (<stdin>, line 1) How can one execute multi-line statements within PDB? If not possible is there a way around this to still

Watch for a variable change in python

孤人 提交于 2019-11-27 19:29:19
There is large python project where one attribute of one class just have wrong value in some place. It should be sqlalchemy.orm.attributes.InstrumentedAttribute, but when I run tests it is constant value, let's say string. There is some way to run python program in debug mode, and run some check (if variable changed type) after each step throught line of code automatically? P.S. I know how to log changes of attribute of class instance with help of inspect and property decorator. Possibly here I can use this method with metaclasses... But sometimes I need more general and powerfull solution...

Attaching a process with pdb

浪子不回头ぞ 提交于 2019-11-27 18:33:11
I have a python script that I suspect that there is a deadlock. I was trying to debug with pdb but if I go step by step it doesn't get the deadlock, and by the output returned I can see that it's not being hanged on the same iteration. I would like to attach my script to a debugger only when it gets locked, is it possible? I'm open to use other debuggers if necessary. At this time, pdb does not have the ability to halt and begin debugging on a running program. You have a few other options: GDB You can use GDB to debug at the C level. This is a bit more abstract because you're poking around

How to debug sublime plugins during development

回眸只為那壹抹淺笑 提交于 2019-11-27 17:52:28
问题 I want to debug my plugin with pdb but it doesn't work. I get these errors Traceback (most recent call last): File "./sublime_plugin.py", line 362, in run_ File "./useIt.py", line 14, in run for region in self.view.sel(): File "./useIt.py", line 14, in run for region in self.view.sel(): File ".\bdb.py", line 46, in trace_dispatch File ".\bdb.py", line 65, in dispatch_line bdb.BdbQuit Has anyone an idea? Or some other way to debug a sublime plugin? 回答1: The problem is that sys.stdin is not

Possible bug in pdb module in Python 3 when using list generators

ε祈祈猫儿з 提交于 2019-11-27 13:52:42
问题 After running this code in Python 3: import pdb def foo(): nums = [1, 2, 3] a = 5 pdb.set_trace() foo() The following expressions work: (Pdb) print(nums) [1, 2, 3] (Pdb) print(a) 5 (Pdb) [x for x in nums] [1, 2, 3] but the following expression fails: (Pdb) [x*a for x in nums] *** NameError: global name 'a' is not defined The above works fine in Python 2.7. Is this a bug or I am missing something? Update : See the new accepted answer. This was indeed a bug (or a problematic design) which has

Bdbquit raised when debugging python

醉酒当歌 提交于 2019-11-27 13:41:55
问题 Recently when adding the debugger to my python 2.7.10 code, I get this message: Traceback (most recent call last): File "/Users/isaachess/Programming/vivint/Platform/MessageProcessing/vivint_cloud/queues/connectors/amqplib_connector.py", line 191, in acking_callback callback(message.body) File "/Users/isaachess/Programming/vivint/Platform/MessageProcessing/vivint_cloud/queues/consumable_message_queue.py", line 32, in deserialized_callback self._callback_method(msg) File "/Users/isaachess