What is an equivalent of "except socket.error as (code, msg)" in Python3?

Viewed 2498

I have this try/except block in Python2 that does not run in Python3 due to the line except socket.error as (code, msg):

try:
    (conn, (ip,port)) = tcpServer.accept()
except socket.error as (code, msg):
    if code != errno.EINTR:
        raise
    else:
        break

What is the equivalent in Python3? Is there a way that works in both Python versions?

1 Answers

According to the PEP that removed the old exceptions, PEP 3151, this way should work:

try:
    (conn, (ip,port)) = tcpServer.accept()
except socket.error as e:
    if e.errno != errno.EINTR:
        raise
    else:
        break

Note that in Python > 3.3 socket.error is a deprecated alias for OSError.

Related