Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
socketmodule: Handle errors returned by internal_connect()
19 changes: 17 additions & 2 deletions Modules/socketmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -3405,6 +3405,18 @@ sock_connect_impl(PySocketSockObject *s, void* Py_UNUSED(data))
return 1;
}

/* Common functionality for socket.connect and socket.connect_ex.
*
* If *raise* is set:
* - On success, return 0.
* - On any failure, return -1 with an exception set.
* If *raise* is zero:
* - On success, return 0.
* - On connect() failure, return errno (without an exception set)
* - On other error, return -1 with an exception set.
*
* Note that -1 is a valid errno value on some systems.
*/
static int
internal_connect(PySocketSockObject *s, struct sockaddr *addr, int addrlen,
int raise)
Expand Down Expand Up @@ -3489,8 +3501,10 @@ sock_connect(PySocketSockObject *s, PyObject *addro)
}

res = internal_connect(s, SAS2SA(&addrbuf), addrlen, 1);
if (res < 0)
if (res < 0) {
assert(PyErr_Occurred());
return NULL;
}

Py_RETURN_NONE;
}
Expand Down Expand Up @@ -3520,8 +3534,9 @@ sock_connect_ex(PySocketSockObject *s, PyObject *addro)
}

res = internal_connect(s, SAS2SA(&addrbuf), addrlen, 0);
if (res < 0)
if (res == -1 && PyErr_Occurred()) {
return NULL;
}

return PyLong_FromLong((long) res);
}
Expand Down