在Python 2和Python 3中捕获管道中断 [英] Catch Broken Pipe in Python 2 AND Python 3

查看:150
本文介绍了在Python 2和Python 3中捕获管道中断的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试编写一些代码来捕获断管错误。该代码应在Python 2.x和Python 3.x中运行。



在Python 2.x中,折断的管道由 socket.error表示。

  socket.error:[Errno 32]损坏的管道

这在Python 3.x中已更改-断开的管道现在是 BrokenPipeError

  BrokenPipeError:[Errno 32]损坏的管道

此外,异常处理的语法也有所变化(请参见 https://stackoverflow.com/ a / 34463112/263589 ),所以我需要做的是:

  try:
do_something()
除了BrokenPipeError为e:#表示Python 3.x
resolve_for_python2()
除了socket.error如e:
如果sys.version_info [0] == 2:#这是必需的,因为在Python> = 3.3
#socket.error是OSError
的别名#https://docs.python.org/3/library/socket.html#socket.error
resolve_for_python3()
其他:
筹集

(至少)还有一个问题:在Python 2.x中,没有 BrokenPipeError ,因此,只要 do_something()中有异常,Python 2.x都会引发另一个异常并抱怨它不知道 BrokenPipeError 。由于 socket.error 在Python 3.x中已过时,在不久的将来在Python 3.x中可能会出现类似的问题。



如何使此代码在Python 2.x和Python 3.x中运行?

解决方案

您关心的是管道破裂的错误,那么您可能想捕获 socket.error 并简单地检查它是否确实是管道破裂的错误。



您可以使用异常的 errno 属性执行此操作,该属性在Python 2和Python 3中都存在意味着,您不需要使用不同的Python 2 vs. 3逻辑(我认为这样的意图会更清楚):

  import socket 
import errno


try:
do_something()
除了socket.error如e:
如果e。 errno !!!



如果您关心的不只是破损的管道,thefourtheye的回答是适当而惯用的。


I try to write some code to catch a Broken Pipe Error. The code should run in Python 2.x and Python 3.x.

In Python 2.x a broken pipe is represented by a socket.error

socket.error: [Errno 32] Broken pipe

This was changed in Python 3.x - a broken pipe now is a BrokenPipeError

BrokenPipeError: [Errno 32] Broken pipe

Also the syntax of exception handling has changed a bit (see https://stackoverflow.com/a/34463112/263589) so what I need to do would be something like:

try:
    do_something()
except BrokenPipeError as e: # implies Python 3.x
    resolve_for_python2()
except socket.error as e:
    if sys.version_info[0] == 2: # this is necessary, as in Python >=3.3
                                 # socket.error is an alias of OSError
                                 # https://docs.python.org/3/library/socket.html#socket.error
        resolve_for_python3()
    else:
        raise

There's (at least) one remaining problem: In Python 2.x there is no BrokenPipeError, so whenever there is an exception in do_something() Python 2.x would throw another exception and complain that it doesn't know BrokenPipeError. As socket.error is deprecated in Python 3.x a similar problem could arise in Python 3.x in the near future.

What can I do to make this code run in Python 2.x and Python 3.x?

解决方案

If all you care about are broken pipe errors, then you might want to catch socket.error and simply check whether it's indeed a broken pipe error.

You can do so using the exception's errno attribute, which is present in both Python 2 and Python 3, which means, you don't need different Python 2 vs. 3 logic (I'd argue the intent is a little clearer this way):

import socket
import errno


try:
    do_something()
except socket.error as e:
    if e.errno != errno.EPIPE:
        # Not a broken pipe
        raise
    do_something_about_the_broken_pipe()


If you do care about more than broken pipes, thefourtheye's answer is appropriate and idiomatic.

这篇关于在Python 2和Python 3中捕获管道中断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆