Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

bpo-1635741: Convert socket.SocketType to heap type, and establish global state #24175

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Changes from 1 commit
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
Next Next commit
Convert socket to heap type
  • Loading branch information
Erlend E. Aasland committed Jan 8, 2021
commit 5c1533b1047c51f984e7be1d017904038d8df7a7
103 changes: 43 additions & 60 deletions 103 Modules/socketmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ static PyObject *socket_gaierror;
The sock_type variable contains pointers to various functions,
some of which call new_sockobject(), which uses sock_type, so
there has to be a circular reference. */
static PyTypeObject sock_type;
static PyTypeObject *sock_type;

#if defined(HAVE_POLL_H)
#include <poll.h>
Expand Down Expand Up @@ -1009,7 +1009,7 @@ new_sockobject(SOCKET_T fd, int family, int type, int proto)
{
PySocketSockObject *s;
s = (PySocketSockObject *)
PyType_GenericNew(&sock_type, NULL, NULL);
PyType_GenericNew(sock_type, NULL, NULL);
if (s == NULL)
return NULL;
if (init_sockobject(s, fd, family, type, proto) == -1) {
Expand Down Expand Up @@ -5026,10 +5026,13 @@ sock_finalize(PySocketSockObject *s)
static void
sock_dealloc(PySocketSockObject *s)
{
PyTypeObject *tp = Py_TYPE(s);

if (PyObject_CallFinalizerFromDealloc((PyObject *)s) < 0)
return;

Py_TYPE(s)->tp_free((PyObject *)s);
tp->tp_free(s);
Py_DECREF(tp);
}


Expand Down Expand Up @@ -5314,58 +5317,31 @@ sock_initobj(PyObject *self, PyObject *args, PyObject *kwds)


/* Type object for socket objects. */
static PyType_Slot sock_slots[] = {
{Py_tp_dealloc, sock_dealloc},
{Py_tp_repr, sock_repr},
{Py_tp_getattro, PyObject_GenericGetAttr},
{Py_tp_doc, (void *)sock_doc},
{Py_tp_methods, sock_methods},
{Py_tp_members, sock_memberlist},
{Py_tp_getset, sock_getsetlist},
{Py_tp_init, sock_initobj},
{Py_tp_alloc, PyType_GenericAlloc},
{Py_tp_new, sock_new},
{Py_tp_free, PyObject_Del},
{Py_tp_finalize, sock_finalize},
{0, NULL},
};

static PyTypeObject sock_type = {
PyVarObject_HEAD_INIT(0, 0) /* Must fill in type value later */
"_socket.socket", /* tp_name */
sizeof(PySocketSockObject), /* tp_basicsize */
0, /* tp_itemsize */
(destructor)sock_dealloc, /* tp_dealloc */
0, /* tp_vectorcall_offset */
0, /* tp_getattr */
0, /* tp_setattr */
0, /* tp_as_async */
(reprfunc)sock_repr, /* tp_repr */
0, /* tp_as_number */
0, /* tp_as_sequence */
0, /* tp_as_mapping */
0, /* tp_hash */
0, /* tp_call */
0, /* tp_str */
PyObject_GenericGetAttr, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
sock_doc, /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
sock_methods, /* tp_methods */
sock_memberlist, /* tp_members */
sock_getsetlist, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
sock_initobj, /* tp_init */
PyType_GenericAlloc, /* tp_alloc */
sock_new, /* tp_new */
PyObject_Del, /* tp_free */
0, /* tp_is_gc */
0, /* tp_bases */
0, /* tp_mro */
0, /* tp_cache */
0, /* tp_subclasses */
0, /* tp_weaklist */
0, /* tp_del */
0, /* tp_version_tag */
(destructor)sock_finalize, /* tp_finalize */
static PyType_Spec sock_spec = {
.name = "_socket.socket",
.basicsize = sizeof(PySocketSockObject),
.flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
.slots = sock_slots,
};

static PyTypeObject *sock_type;


/* Python interface to gethostname(). */

Expand Down Expand Up @@ -7058,7 +7034,7 @@ sock_get_api(void)
return NULL;
}

capi->Sock_Type = (PyTypeObject *)Py_NewRef(&sock_type);
capi->Sock_Type = (PyTypeObject *)Py_NewRef(sock_type);
capi->error = Py_NewRef(PyExc_OSError);
capi->timeout_error = Py_NewRef(PyExc_TimeoutError);
return capi;
Expand Down Expand Up @@ -7106,11 +7082,16 @@ PyInit__socket(void)
}
#endif

Py_SET_TYPE(&sock_type, &PyType_Type);
m = PyModule_Create(&socketmodule);
if (m == NULL)
return NULL;

sock_type = (PyTypeObject *)PyType_FromModuleAndSpec(m, &sock_spec, NULL);
if (!sock_type) {
Py_DECREF(m);
return NULL;
}

Py_INCREF(PyExc_OSError);
PyModule_AddObject(m, "error", PyExc_OSError);
socket_herror = PyErr_NewException("socket.herror",
Expand All @@ -7127,14 +7108,16 @@ PyInit__socket(void)
PyModule_AddObject(m, "gaierror", socket_gaierror);
PyModule_AddObjectRef(m, "timeout", PyExc_TimeoutError);

Py_INCREF((PyObject *)&sock_type);
if (PyModule_AddObject(m, "SocketType",
(PyObject *)&sock_type) != 0)
Py_INCREF(sock_type);
if (PyModule_AddObject(m, "SocketType", (PyObject *)sock_type) < 0) {
Py_CLEAR(sock_type);
return NULL;
Py_INCREF((PyObject *)&sock_type);
if (PyModule_AddObject(m, "socket",
(PyObject *)&sock_type) != 0)
}
Py_INCREF((PyObject *)sock_type);
if (PyModule_AddObject(m, "socket", (PyObject *)sock_type) < 0) {
Py_CLEAR(sock_type);
return NULL;
}

#ifdef ENABLE_IPV6
has_ipv6 = Py_True;
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.