I am trying to write some code to catch a Broken Pipe error. The code should work in Python 2.x and Python 3.x.
In Python 2.x, a broken pipe is represented by socket.error
socket.error: [Errno 32] Broken pipe
This has been changed in Python 3.x - a broken pipe now represents a BrokenPipeError
BrokenPipeError: [Errno 32] Broken pipe
Also, the syntax of exception handling has changed a bit (see https://stackoverflow.com/a/126328/ ... ), so I needed 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 is (at least) one remaining problem: Python 2.x does not have BrokenPipeError , so whenever there is an exception in do_something() , Python 2.x throws another exception and complains that it does not know BrokenPipeError . Since socket.error deprecated in Python 3.x, a similar problem may 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?
source share