Make socket exceptions more specific to match Python semantics

This commit is contained in:
Joseph Henry
2021-05-19 22:14:15 -07:00
parent 4e0813d167
commit 81d03c3692
3 changed files with 25 additions and 1 deletions

View File

@@ -200,7 +200,7 @@ typedef enum {
extern int zts_errno;
typedef enum {
/** Operation not permitted (`zts_errno` value) */
/** Operation not permitted */
ZTS_EPERM = 1,
/** No such file or directory */
ZTS_ENOENT = 2,
@@ -278,6 +278,8 @@ typedef enum {
ZTS_ENOTCONN = 107,
/** Connection timed out */
ZTS_ETIMEDOUT = 110,
/* Connection refused */
ZTS_ECONNREFUSED = 111,
/** No route to host */
ZTS_EHOSTUNREACH = 113,
/** Operation already in progress */

View File

@@ -158,6 +158,7 @@ ZTS_ENOBUFS = _libzt.ZTS_ENOBUFS
ZTS_EISCONN = _libzt.ZTS_EISCONN
ZTS_ENOTCONN = _libzt.ZTS_ENOTCONN
ZTS_ETIMEDOUT = _libzt.ZTS_ETIMEDOUT
ZTS_ECONNREFUSED = _libzt.ZTS_ECONNREFUSED
ZTS_EHOSTUNREACH = _libzt.ZTS_EHOSTUNREACH
ZTS_EALREADY = _libzt.ZTS_EALREADY
ZTS_EINPROGRESS = _libzt.ZTS_EINPROGRESS

View File

@@ -6,6 +6,27 @@ import libzt
def handle_error(err):
"""Convert libzt error code to exception"""
if err == libzt.ZTS_ERR_SOCKET:
if errno() == libzt.ZTS_EAGAIN:
raise BlockingIOError()
return
if errno() == libzt.ZTS_EINPROGRESS:
raise BlockingIOError()
return
if errno() == libzt.ZTS_EALREADY:
raise BlockingIOError()
return
if errno() == libzt.ZTS_ECONNABORTED:
raise ConnectionAbortedError()
return
if errno() == libzt.ZTS_ECONNREFUSED:
raise ConnectionRefusedError()
return
if errno() == libzt.ZTS_ECONNRESET:
raise ConnectionResetError()
return
if errno() == libzt.ZTS_ETIMEDOUT:
raise TimeoutError()
return
raise Exception("ZTS_ERR_SOCKET (" + str(err) + ")")
if err == libzt.ZTS_ERR_SERVICE:
raise Exception("ZTS_ERR_SERVICE (" + str(err) + ")")