3

I try to patch a library to catch [Errno 32] Broken pipe.

The library is coded to run in Python 2 and Python 3. In Python 2 the exception is a

socket.error: [Errno 32] Broken pipe

in Python >= 3.3 it is a

BrokenPipeError: [Errno 32] Broken pipe

In Python 3.2 there is no BrokenPipeError implemented. There is a socket.error but it has quite a different description than in Python 2.

I have Debian Stretch installed on my system so it seems difficult to install Python 3.2 to check, which exception I would have to catch in this version. Still, I don't want to break the library in Python 3.2.

Therefore it would be very helpful if you could tell me which is the most specific exception to catch a broken pipe error in Python 3.2.

speendo
  • 13,045
  • 22
  • 71
  • 107
  • 1
    From the documentation you linked, it seems that you should catch `socket.error`. Then check if the `.errno` attrib is `errno.EPIPE`, if so then it's what you want, if not then re-raise the exception. – Tom Dalton Jan 18 '16 at 23:55
  • @TomDalton Thanks for your hint! I tried to catch socket.error, but get a strange Error message: `TypeError: catching classes that do not inherit from BaseException is not allowed` (https://travis-ci.org/Mic92/python-mpd2/jobs/105030049). What does that mean in this case? – speendo Jan 26 '16 at 23:08
  • 1
    I'm afraid I'm at a loss to explain that. You might want to log what socket.error actually is just before that error occurs to check it's what we'd expect it to be. There's an example of catching socket.error here: https://docs.python.org/3.2/library/socket.html#example so I am suspicious that there's something else at play. Are you using any monkey-patching packages that might have replaced the python socket package with something else? – Tom Dalton Jan 26 '16 at 23:25
  • 1
    It looks like your test file might be mocking out the entire socket package - https://github.com/Mic92/python-mpd2/blob/master/test.py#L42 If so that's probably the issue. – Tom Dalton Jan 26 '16 at 23:34

1 Answers1

1

From the documentation you linked, it seems that you should catch socket.error. Then check if the .errno attrib is errno.EPIPE, if so then it's what you want, if not then re-raise the exception.

The subsequent error you linked to https://travis-ci.org/Mic92/python-mpd2/jobs/105030049 looks like it's caused by your test file https://github.com/Mic92/python-mpd2/blob/master/test.py#L42 mocking out the whole socket package (including socket.error). From a very quick look at the code, you might be OK to only mock out socket.socket, and leave the rest as-is.

Tom Dalton
  • 6,122
  • 24
  • 35
  • Thanks! It's not my library nor my test file - I'm just trying to fix an issue there :) I'll look at this in the next days. Thank you *very* much! – speendo Jan 27 '16 at 00:23