Python:httplib 内令人费解的行为

发布于 2024-08-25 10:28:56 字数 1202 浏览 8 评论 0原文

我在 httplib 的 HTTPConnection.putheader 中添加了一行( import pdb; pdb.set_trace() ),这样我就可以看到里面发生了什么。

Python26\Lib\httplib.py,第 489 行:

def putheader(self, header, value):
    """Send a request header line to the server.

    For example: h.putheader('Accept', 'text/html')
    """
    import pdb; pdb.set_trace()
    if self.__state != _CS_REQ_STARTED:
        raise CannotSendHeader()

    str = '%s: %s' % (header, value)
    self._output(str)

然后从解释器运行这个

import urllib2
urllib2.urlopen('http://www.ioerror.us/ip/headers')

......并且正如预期的 PDB 启动:

> c:\python26\lib\httplib.py(858)putheader()
-> if self.__state != _CS_REQ_STARTED:
(Pdb)

在 PDB 中,我可以快速评估表达式,所以我有尝试输入 self.__state

(Pdb) self.__state
*** AttributeError: HTTPConnection instance has no attribute '__state'

唉,这个实例没有 __state。 越过该

if self.__state != _CS_REQ_STARTED:

但是,当我输入 step 时,调试器会毫无问题地 行。为什么会发生这种情况?如果 self.__state 不存在,python 将必须引发异常,就像我输入表达式时一样。

Python 版本:win32 上的 2.6.4

I have added one line ( import pdb; pdb.set_trace() ) to httplib's HTTPConnection.putheader, so I can see what's going on inside.

Python26\Lib\httplib.py, line 489:

def putheader(self, header, value):
    """Send a request header line to the server.

    For example: h.putheader('Accept', 'text/html')
    """
    import pdb; pdb.set_trace()
    if self.__state != _CS_REQ_STARTED:
        raise CannotSendHeader()

    str = '%s: %s' % (header, value)
    self._output(str)

then ran this from the interpreter

import urllib2
urllib2.urlopen('http://www.ioerror.us/ip/headers')

... and as expected PDB kicks in:

> c:\python26\lib\httplib.py(858)putheader()
-> if self.__state != _CS_REQ_STARTED:
(Pdb)

in PDB I have the luxury of evaluating expressions on the fly, so I have tried to enter self.__state:

(Pdb) self.__state
*** AttributeError: HTTPConnection instance has no attribute '__state'

Alas, there is no __state of this instance. However when I enter step, the debugger gets past the

if self.__state != _CS_REQ_STARTED:

line without a problem. Why is this happening? If the self.__state doesn't exist python would have to raise an exception as it did when I entered the expression.

Python version: 2.6.4 on win32

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

送舟行 2024-09-01 10:28:56

回答我自己的问题:

http://en.wikipedia.org/wiki/Name_mangling#Name_mangling_in_Python

__state 是对象内部的私有名称,它被破坏为 _HTTPConnection__state,所以当我想在 PDB 中访问它时,我必须将其命名为 self ._HTTPConnection__状态。只有对象才能将其引用为__state

Answering my own question:

http://en.wikipedia.org/wiki/Name_mangling#Name_mangling_in_Python

__state is a private name inside the object, it gets mangled as _HTTPConnection__state, so when I want to access it in PDB I have to name it as self._HTTPConnection__state. Only the object can refer to it as __state.

叹沉浮 2024-09-01 10:28:56

如果 self.__state 不存在,python 将不得不引发异常,就像我输入表达式时一样。

在 Python 中,您不必显式声明变量。
当您分配给它们时,它们就“诞生”了。

一些代码验证器(例如 pylint)会警告这些情况。
在您的情况下,您可以在 HTTPConnection.__init__() 中有类似 self.__state = None 的内容,

但这并不是很重要。

If the self.__state doesn't exist python would have to raise an exception as it did when I entered the expression.

In Python, you don't have to declare variables explicitly.
They are "born" when you assign to them.

Some code validators like pylint warn about these situations.
In your case you could have something like self.__state = None in HTTPConnection.__init__()

but this is not very important.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文