我试图修补库以捕获[Errno 32] Broken pipe
。
库的代码是在Python 2和Python 3中运行的。在Python 2中,异常是
socket.error: [Errno 32] Broken pipe
在Python>=3.3中,它是
BrokenPipeError: [Errno 32] Broken pipe
在Python3.2中没有实现
BrokenPipeError
。有一个socket.error
但它有quite a different description比in Python 2。我的系统上安装了Debian Stretch,所以很难安装Python3.2来检查,在这个版本中我必须捕获哪个异常。不过,我不想破坏Python 3.2中的库。
因此,如果您能告诉我,在Python3.2中捕获断管错误的最具体的异常是哪个,那将非常有帮助。
最佳答案
从链接的文档中,似乎应该捕获socket.error。然后检查.errno attrib是否为errno.eppe,如果是,则为所需,如果不是,则重新引发异常。
随后链接到https://travis-ci.org/Mic92/python-mpd2/jobs/105030049的错误看起来是由测试文件https://github.com/Mic92/python-mpd2/blob/master/test.py#L42模拟整个套接字包(包括socket.error)引起的。从一个非常快的代码来看,您可能只需要模拟socket.socket
,其余的就可以了。