@@ -2836,7 +2836,7 @@ sock_close(PySocketSockObject *s)
28362836 Py_RETURN_NONE ;
28372837}
28382838
2839- PyDoc_STRVAR (close_doc ,
2839+ PyDoc_STRVAR (sock_close_doc ,
28402840"close()\n\
28412841\n\
28422842Close the socket. It cannot be used after this call." );
@@ -4558,7 +4558,7 @@ static PyMethodDef sock_methods[] = {
45584558 {"bind" , (PyCFunction )sock_bind , METH_O ,
45594559 bind_doc },
45604560 {"close" , (PyCFunction )sock_close , METH_NOARGS ,
4561- close_doc },
4561+ sock_close_doc },
45624562 {"connect" , (PyCFunction )sock_connect , METH_O ,
45634563 connect_doc },
45644564 {"connect_ex" , (PyCFunction )sock_connect_ex , METH_O ,
@@ -5456,6 +5456,31 @@ PyDoc_STRVAR(getprotobyname_doc,
54565456\n\
54575457Return the protocol number for the named protocol. (Rarely used.)" );
54585458
5459+ static PyObject *
5460+ socket_close (PyObject * self , PyObject * fdobj )
5461+ {
5462+ SOCKET_T fd ;
5463+ int res ;
5464+
5465+ fd = PyLong_AsSocket_t (fdobj );
5466+ if (fd == (SOCKET_T )(-1 ) && PyErr_Occurred ())
5467+ return NULL ;
5468+ Py_BEGIN_ALLOW_THREADS
5469+ res = SOCKETCLOSE (fd );
5470+ Py_END_ALLOW_THREADS
5471+ /* bpo-30319: The peer can already have closed the connection.
5472+ Python ignores ECONNRESET on close(). */
5473+ if (res < 0 && !CHECK_ERRNO (ECONNRESET )) {
5474+ return set_error ();
5475+ }
5476+ Py_RETURN_NONE ;
5477+ }
5478+
5479+ PyDoc_STRVAR (close_doc ,
5480+ "close(integer) -> None\n\
5481+ \n\
5482+ Close an integer socket file descriptor. This is like os.close(), but for\n\
5483+ sockets; on some platforms os.close() won't work for socket file descriptors." );
54595484
54605485#ifndef NO_DUP
54615486/* dup() function for socket fds */
@@ -6397,6 +6422,8 @@ static PyMethodDef socket_methods[] = {
63976422 METH_VARARGS , getservbyport_doc },
63986423 {"getprotobyname" , socket_getprotobyname ,
63996424 METH_VARARGS , getprotobyname_doc },
6425+ {"close" , socket_close ,
6426+ METH_O , close_doc },
64006427#ifndef NO_DUP
64016428 {"dup" , socket_dup ,
64026429 METH_O , dup_doc },
0 commit comments