Skip to content

Commit 46a733d

Browse files
committed
Patch python#100926 - Better error messages for socket exceptions on Windows. [Slight style differences from posted patch]
1 parent e28c296 commit 46a733d

1 file changed

Lines changed: 78 additions & 2 deletions

File tree

Modules/socketmodule.c

Lines changed: 78 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,9 +231,85 @@ static PyObject *
231231
PySocket_Err(void)
232232
{
233233
#ifdef MS_WINDOWS
234-
if (WSAGetLastError()) {
234+
int err_no = WSAGetLastError();
235+
if (err_no) {
236+
static struct { int no; const char *msg; } *msgp, msgs[] = {
237+
{ WSAEINTR, "Interrupted system call" },
238+
{ WSAEBADF, "Bad file descriptor" },
239+
{ WSAEACCES, "Permission denied" },
240+
{ WSAEFAULT, "Bad address" },
241+
{ WSAEINVAL, "Invalid argument" },
242+
{ WSAEMFILE, "Too many open files" },
243+
{ WSAEWOULDBLOCK,
244+
"The socket operation could not complete "
245+
"without blocking" },
246+
{ WSAEINPROGRESS, "Operation now in progress" },
247+
{ WSAEALREADY, "Operation already in progress" },
248+
{ WSAENOTSOCK, "Socket operation on non-socket" },
249+
{ WSAEDESTADDRREQ, "Destination address required" },
250+
{ WSAEMSGSIZE, "Message too long" },
251+
{ WSAEPROTOTYPE, "Protocol wrong type for socket" },
252+
{ WSAENOPROTOOPT, "Protocol not available" },
253+
{ WSAEPROTONOSUPPORT, "Protocol not supported" },
254+
{ WSAESOCKTNOSUPPORT, "Socket type not supported" },
255+
{ WSAEOPNOTSUPP, "Operation not supported" },
256+
{ WSAEPFNOSUPPORT, "Protocol family not supported" },
257+
{ WSAEAFNOSUPPORT, "Address family not supported" },
258+
{ WSAEADDRINUSE, "Address already in use" },
259+
{ WSAEADDRNOTAVAIL,
260+
"Can't assign requested address" },
261+
{ WSAENETDOWN, "Network is down" },
262+
{ WSAENETUNREACH, "Network is unreachable" },
263+
{ WSAENETRESET,
264+
"Network dropped connection on reset" },
265+
{ WSAECONNABORTED,
266+
"Software caused connection abort" },
267+
{ WSAECONNRESET, "Connection reset by peer" },
268+
{ WSAENOBUFS, "No buffer space available" },
269+
{ WSAEISCONN, "Socket is already connected" },
270+
{ WSAENOTCONN, "Socket is not connected" },
271+
{ WSAESHUTDOWN, "Can't send after socket shutdown" },
272+
{ WSAETOOMANYREFS,
273+
"Too many references: can't splice" },
274+
{ WSAETIMEDOUT, "Operation timed out" },
275+
{ WSAECONNREFUSED, "Connection refused" },
276+
{ WSAELOOP, "Too many levels of symbolic links" },
277+
{ WSAENAMETOOLONG, "File name too long" },
278+
{ WSAEHOSTDOWN, "Host is down" },
279+
{ WSAEHOSTUNREACH, "No route to host" },
280+
{ WSAENOTEMPTY, "Directory not empty" },
281+
{ WSAEPROCLIM, "Too many processes" },
282+
{ WSAEUSERS, "Too many users" },
283+
{ WSAEDQUOT, "Disc quota exceeded" },
284+
{ WSAESTALE, "Stale NFS file handle" },
285+
{ WSAEREMOTE, "Too many levels of remote in path" },
286+
{ WSASYSNOTREADY,
287+
"Network subsystem is unvailable" },
288+
{ WSAVERNOTSUPPORTED,
289+
"WinSock version is not supported" },
290+
{ WSANOTINITIALISED,
291+
"Successful WSAStartup() not yet performed" },
292+
{ WSAEDISCON, "Graceful shutdown in progress" },
293+
/* Resolver errors */
294+
{ WSAHOST_NOT_FOUND, "No such host is known" },
295+
{ WSATRY_AGAIN, "Host not found, or server failed" },
296+
{ WSANO_RECOVERY,
297+
"Unexpected server error encountered" },
298+
{ WSANO_DATA, "Valid name without requested data" },
299+
{ WSANO_ADDRESS, "No address, look for MX record" },
300+
{ 0, NULL }
301+
};
235302
PyObject *v;
236-
v = Py_BuildValue("(is)", WSAGetLastError(), "winsock error");
303+
const char *msg = "winsock error";
304+
305+
for (msgp = msgs; msgp->msg; msgp++) {
306+
if (err_no == msgp->no) {
307+
msg = msgp->msg;
308+
break;
309+
}
310+
}
311+
312+
v = Py_BuildValue("(is)", err_no, msg);
237313
if (v != NULL) {
238314
PyErr_SetObject(PySocket_Error, v);
239315
Py_DECREF(v);

0 commit comments

Comments
 (0)