我正在使用pdb调试简单的python脚本:

#!/usr/bin/python

f = open("./hello.scala")
f.close()


调试流程如下:

[root@localhost ~]# python -m pdb test.py
> /root/test.py(3)<module>()
-> f = open("./hello.scala")
(Pdb) n
> /root/test.py(4)<module>()
-> f.close()
(Pdb) p f
<open file './hello.scala', mode 'r' at 0x7f4d69aae8a0>
(Pdb) pp f
<open file './hello.scala', mode 'r' at 0x7f4d69aae8a0>


我想检查文件对象f的详细信息,但是我只能获取它的地址。使用pdb调试python脚本时,是否有任何获取详细对象信息的方法?

最佳答案

您可以在pdb中使用大多数任意的Python表达式。

我不知道您在寻找什么“详细对象信息”,但总的来说,任何有用的东西都是一个属性。

因为您知道自己拥有的类型(一个file),并且它是Python随附的内置或stdlib类型,所以您可以look it up in the docs并查看它肯定具有的属性。例如,file对象具有closed属性。看到:

(Pdb) p f.closed
True


如果您不知道想要什么属性,可以使用inspect模块来提供帮助。例如:

(Pdb) import inspect
(Pdb) p inspect.getmembers(f, lambda x: not callable(x))
[('__doc__', "file(name[, mode[, buffering]]) -> file object\n\nOpen a file.  The mode can be 'r', 'w' or 'a' for reading (default),\nwriting or appending.  The file will be created if it doesn't exist\nwhen opened for writing or appending; it will be truncated when\nopened for writing.  Add a 'b' to the mode for binary files.\nAdd a '+' to the mode to allow simultaneous reading and writing.\nIf the buffering argument is given, 0 means unbuffered, 1 means line\nbuffered, and larger numbers specify the buffer size.  The preferred way\nto open a file is with the builtin open() function.\nAdd a 'U' to mode to open the file for input with universal newline\nsupport.  Any line ending in the input file will be seen as a '\\n'\nin Python.  Also, a file so opened gains the attribute 'newlines';\nthe value for this attribute is one of None (no newline read yet),\n'\\r', '\\n', '\\r\\n' or a tuple containing all the newline types seen.\n\n'U' cannot be combined with 'w' or '+' mode.\n"), ('closed', True), ('encoding', None), ('errors', None), ('mode', 'r'), ('name', 'temp.xml'), ('newlines', None), ('softspace', 0)]


如果希望打印漂亮,只需使用pp命令而不是p

(Pdb) pp inspect.getmembers(f, lambda x: not callable(x))
[('__doc__',
  "file(name[, mode[, buffering]]) -> file object\n\nOpen a file.  The mode can be 'r', 'w' or 'a' for reading (default),\nwriting or appending.  The file will be created if it doesn't exist\nwhen opened for writing or appending; it will be truncated when\nopened for writing.  Add a 'b' to the mode for binary files.\nAdd a '+' to the mode to allow simultaneous reading and writing.\nIf the buffering argument is given, 0 means unbuffered, 1 means line\nbuffered, and larger numbers specify the buffer size.  The preferred way\nto open a file is with the builtin open() function.\nAdd a 'U' to mode to open the file for input with universal newline\nsupport.  Any line ending in the input file will be seen as a '\\n'\nin Python.  Also, a file so opened gains the attribute 'newlines';\nthe value for this attribute is one of None (no newline read yet),\n'\\r', '\\n', '\\r\\n' or a tuple containing all the newline types seen.\n\n'U' cannot be combined with 'w' or '+' mode.\n"),
 ('closed', True),
 ('encoding', None),
 ('errors', None),
 ('mode', 'r'),
 ('name', 'temp.xml'),
 ('newlines', None),
 ('softspace', 0)]




正如文档在顶部附近解释的那样:


  getmembers()函数检索对象的成员,例如类或模块。名称以“ is”开头的十六个函数主要是为getmembers()的第二个参数提供方便的选择。


但是您不必使用这16个函数,您可以传递任何您想要的东西。正如getmembers文档所说:


  返回按名称排序的(名称,值)对列表中的对象的所有成员。如果提供了可选的谓词参数,则仅包含谓词为其返回真值的成员。


因此,作为示例,我编写了一个自定义函数lambda x: not callable(x),仅对不可调用的成员适用。这意味着我不会得到close方法之类的东西,而不会得到closed属性之类的东西。

如果您不理解lambda,则是在表达式中间定义简单函数的一种方法。就像我写的一样:

def my_function(x):
    return not callable(x)


…然后称为inspect.getmembers(f, my_function)



当我们讨论它时,您可能想看看IPython,它是默认Python交互模式的替代品。除其他事项外,它还使您可以在Pdb(以及正常的交互式会话)中完成制表符:

ipdb> n
--Return--
None
> /Users/abarnert/src/pt.py(2)<module>()
1     1 f = open('temp.txt')
----> 2 f.close()

ipdb> p f
<_io.TextIOWrapper name='temp.xml' mode='r' encoding='UTF-8'>
ipdb> p f.
f.buffer          f.fileno          f.newlines        f.seekable
f.close           f.flush           f.read            f.tell
f.closed          f.isatty          f.readable        f.truncate
f.detach          f.line_buffering  f.readline        f.writable
f.encoding        f.mode            f.readlines       f.write
f.errors          f.name            f.seek            f.writelines
ipdb> p f.


(很抱歉在该示例成绩单中使用Python 3;我的Python 2.7正好在升级IPython的中间...)

07-24 09:45
查看更多