【问题标题】:What kind of exception does Python 3.2 throw in case of [Errno 32] Broken pipePython 3.2 在 [Errno 32] Broken pipe 的情况下会抛出什么样的异常
【发布时间】:2016-01-18 22:32:39
【问题描述】:

我尝试修补一个库以捕获 [Errno 32] Broken pipe

该库被编码为在 Python 2 和 Python 3 中运行。在 Python 2 中,例外是

socket.error: [Errno 32] Broken pipe

在 Python >= 3.3 中是一个

BrokenPipeError: [Errno 32] Broken pipe

在 Python 3.2 中没有实现 BrokenPipeError。有一个socket.error,但它有quite a different description 而不是in Python 2

我的系统上安装了 Debian Stretch,因此安装 Python 3.2 似乎很难检查,我必须在这个版本中捕获哪个异常。不过,我不想破坏 Python 3.2 中的库。

因此,如果您能告诉我在 Python 3.2 中捕获损坏管道错误的最具体异常是哪个,那将非常有帮助。

【问题讨论】:

  • 从您链接的文档中,您似乎应该抓住socket.error。然后检查.errno 属性是否为errno.EPIPE,如果是,那么它就是你想要的,如果不是,则重新引发异常。
  • @TomDalton 感谢您的提示!我试图捕捉 socket.error,但收到一条奇怪的错误消息:TypeError: catching classes that do not inherit from BaseException is not allowed (travis-ci.org/Mic92/python-mpd2/jobs/105030049)。在这种情况下,这意味着什么?
  • 恐怕我无法解释这一点。您可能想在错误发生之前记录 socket.error 实际上是什么,以检查它是否是我们期望的。这里有一个捕获 socket.error 的例子:docs.python.org/3.2/library/socket.html#example 所以我怀疑还有别的东西在起作用。您是否正在使用任何可能已将 python 套接字包替换为其他东西的猴子补丁包?
  • 看起来您的测试文件可能正在模拟整个套接字包 - github.com/Mic92/python-mpd2/blob/master/test.py#L42 如果是这样,那可能就是问题所在。

标签: python python-3.x exception python-3.2 broken-pipe


【解决方案1】:

从您链接的文档看来,您应该捕获 socket.error。然后检查 .errno 属性是否为 errno.EPIPE,如果是,那么这就是您想要的,如果不是,则重新引发异常。

您链接到https://travis-ci.org/Mic92/python-mpd2/jobs/105030049 的后续错误看起来像是由您的测试文件https://github.com/Mic92/python-mpd2/blob/master/test.py#L42 模拟了整个套接字包(包括socket.error)引起的。 很快看一下代码,你可能只模拟出socket.socket,其余的保持原样。

【讨论】:

  • 谢谢!这不是我的库,也不是我的测试文件——我只是想解决那里的一个问题:)我会在接下来的几天里看看这个。非常感谢非常
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-29
  • 2021-01-22
  • 2014-05-02
相关资源
最近更新 更多