5

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?

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
roschach
  • 8,390
  • 14
  • 74
  • 124

1 Answers1

6

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.

Daniel F
  • 13,684
  • 11
  • 87
  • 116
SimonF
  • 1,855
  • 10
  • 23