我正在实现一个用于 Python 开发的“断点”系统,该系统允许我调用一个函数,该函数本质上调用 pdb.set_trace();
我想要实现的一些功能要求我在 set_trace 上下文中时通过代码控制 pdb。
例子:
disableList = []
def breakpoint(name=None):
def d():
disableList.append(name)
#****
#issue 'run' command to pdb so user
#does not have to type 'c'
#****
if name in disableList:
return
print "Use d() to disable breakpoint, 'c' to continue"
pdb.set_trace();
Run Code Online (Sandbox Code Playgroud)
在上面的例子中,如何实现 标记的注释#****?
在该系统的其他部分,我想发出一个“up”命令,或两个连续的“up”命令,而不离开 pdb 会话(因此用户最终会出现 pdb 提示符,但在调用堆栈上上升两级)。
如何检查 python 内置函数的代码,例如单步执行sum()?
https://docs.python.org/2/library/functions.html#sum。
我希望看到sum()使用下面的代码和spdb 中的命令会做什么:
import pdb
def adder(nums):
x = sum(nums)
return x
pdb.set_trace()
print adder([1, 2, 3,4])
Run Code Online (Sandbox Code Playgroud) 有没有人在调试模式(PDB)中使用spyder遇到此问题?它在交互模式下工作正常.
一个建议的解决方案是使用pause(1)而不是show()之后imshow(img).
有没有更好的方法在调试模式下查看我的数字?如果有,那将是一个真正的Matlab杀手!
考虑以下代码:
def inner(a):
if a == 75:
raise RuntimeError()
return a**2
def outer():
results = []
for a in range(100):
results.append(inner(a))
return results
outer()
Run Code Online (Sandbox Code Playgroud)
在 IPython 中,在引发异常后,%debug行魔术会在以下范围内打开 python 调试器inner():
In [4]: %debug
> <ipython-input-3-eff43b15b2ef>(3)inner()
2 if a == 75:
----> 3 raise RuntimeError()
4 return a**2
ipdb> a
a = 75
ipdb> results
*** NameError: name 'results' is not defined
Run Code Online (Sandbox Code Playgroud)
如何告诉 (i)pdb 进入范围outer()以保存到目前为止生成的结果?
我从python脚本调用一个方法,其中一个变量为args.一旦我进入方法,当我试图看到变量args的值时,"print args"并且只是执行'args'会显示两个不同的值.任何人都可以让我知道这两个命令之间的区别.
我希望这两个命令显示相同的值.
(Pdb) print args
<lib.framework.testmanager.RunArgs object at 0xb26acac>
(Pdb) args
args = <lib.framework.testmanager.RunArgs object at 0xb26acac>
u = <upgradelib.UpgradeManager object at 0x946cf8c>
spec = {'excludeHosts': None, 'evacuateAllData': True, 'WaitTime': None, 'IssueType': 'Host Disconnect', 'performObjectUpgrade': True, 'downgradeFormat': False}
result = True
Run Code Online (Sandbox Code Playgroud) 当我在 pdb 中并且有一个大字符串时,有时我想用换行符作为实际换行符将其打印出来,而不是在终端中将它们视为 \n 。
(Pdb) p large_string
"very large string with newline\nanother line in the large string\n\netc\n"
Run Code Online (Sandbox Code Playgroud)
我宁愿看到
(Pdb) printwithnewline large_string
"very large string with newline
another line in the large string
etc
"
Run Code Online (Sandbox Code Playgroud)
有小费吗?
您如何杀死PDB及其正在运行的程序,类似于LLDB proc kill; exit或exit (y)命令?
Ctrl+ D不起作用,我在这里看到的所有问题都是如何在保持程序运行的情况下退出。但是,我正坐在PDB会话中,我发现了一个导致无限循环的错误,尽管确实有其他方法可以杀死该程序,但我发现我想问一下PDB命令是做什么的。
我正在尝试调试这样的函数one_away:
import pdb
def is_one_away(first: str, other: str) -> bool:
skip_diff = {
-1: lambda i: (i, i + 1),
1: lambda i: (i + 1, i),
0: lambda i: (i + 1, i + 1)
}
try:
skip = skip_diff[len(first) - len(other)]
except KeyError:
return False
pdb.set_trace()
for i, (l1, l2) in enumerate(zip(first, other)):
if l1 != l2:
i -= 1
break
Run Code Online (Sandbox Code Playgroud)
并称之为我写道:
import one_away
one_away.is_one_away('pale', 'kale')
Run Code Online (Sandbox Code Playgroud)
运行到 时pdb.set_trace(),我想查看 的结果zip(first ,other)。所以我写:
(Pdb) >? …Run Code Online (Sandbox Code Playgroud) 我在python3.6中编写了一个解析器; 我仍在尽可能地简化它,同时仍然产生错误:
def tokenize(expr):
for i in expr:
try:
yield int(i)
except ValueError:
yield i
def push_on_stream(obj, stream):
yield obj
yield from stream
class OpenBracket:
"just a token value, could have used Ellipsis"
pass
def parse_toks(tokstream):
result = []
leading_brak = False
for tok in tokstream:
if tok == OpenBracket:
leading_brak = True
elif tok == '(':
result.append(parse_toks(
push_on_stream(OpenBracket, tokstream)))
elif tok == ')':
if not leading_brak:
raise SyntaxError("Very bad ')'.")
break
else:
result.append(tok)
return sum(result)
def test(expr="12(34)21"):
tokens = …Run Code Online (Sandbox Code Playgroud) 我最近想知道与 Python 相关的时间旅行调试。我找到了有关工具的信息,例如:
由于项目更新这么久,我想知道现在用于TTD的工具是否有变化?
我指望建设性的讨论以及建议和建议现在使用什么。这一切都是为了分享知识。
pdb ×10
python ×10
debugging ×6
python-2.7 ×2
built-in ×1
ipdb ×1
ipython ×1
matplotlib ×1
parsing ×1
python-3.x ×1
spyder ×1
zip ×1