Skip to content

bpo-42972: Fully implement GC protocol for ssl heap types (GH-26370) #26370

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

Merged
merged 1 commit into from
May 27, 2021
Merged
Changes from all 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
73 changes: 45 additions & 28 deletions Modules/_ssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -434,11 +434,11 @@ static PyType_Slot sslerror_type_slots[] = {
};

static PyType_Spec sslerror_type_spec = {
"ssl.SSLError",
sizeof(PyOSErrorObject),
0,
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_IMMUTABLETYPE,
sslerror_type_slots
.name = "ssl.SSLError",
.basicsize = sizeof(PyOSErrorObject),
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
Py_TPFLAGS_IMMUTABLETYPE | Py_TPFLAGS_HAVE_GC),
.slots = sslerror_type_slots
};

static void
Expand Down Expand Up @@ -789,7 +789,8 @@ newPySSLSocket(PySSLContext *sslctx, PySocketSockObject *sock,
SSL_CTX *ctx = sslctx->ctx;
_PySSLError err = { 0 };

self = PyObject_New(PySSLSocket, get_state_ctx(sslctx)->PySSLSocket_Type);
self = PyObject_GC_New(PySSLSocket,
get_state_ctx(sslctx)->PySSLSocket_Type);
if (self == NULL)
return NULL;

Expand Down Expand Up @@ -896,6 +897,8 @@ newPySSLSocket(PySSLContext *sslctx, PySocketSockObject *sock,
return NULL;
}
}

PyObject_GC_Track(self);
return self;
}

Expand Down Expand Up @@ -2169,6 +2172,7 @@ PySSL_traverse(PySSLSocket *self, visitproc visit, void *arg)
Py_VISIT(self->exc_type);
Py_VISIT(self->exc_value);
Py_VISIT(self->exc_tb);
Py_VISIT(Py_TYPE(self));
return 0;
}

Expand All @@ -2185,13 +2189,15 @@ static void
PySSL_dealloc(PySSLSocket *self)
{
PyTypeObject *tp = Py_TYPE(self);
if (self->ssl)
PyObject_GC_UnTrack(self);
if (self->ssl) {
SSL_free(self->ssl);
}
Py_XDECREF(self->Socket);
Py_XDECREF(self->ctx);
Py_XDECREF(self->server_hostname);
Py_XDECREF(self->owner);
PyObject_Free(self);
PyObject_GC_Del(self);
Py_DECREF(tp);
}

Expand Down Expand Up @@ -2903,11 +2909,11 @@ static PyType_Slot PySSLSocket_slots[] = {
};

static PyType_Spec PySSLSocket_spec = {
"_ssl._SSLSocket",
sizeof(PySSLSocket),
0,
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE,
PySSLSocket_slots,
.name = "_ssl._SSLSocket",
.basicsize = sizeof(PySSLSocket),
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE |
Py_TPFLAGS_HAVE_GC),
.slots = PySSLSocket_slots,
};

/*
Expand Down Expand Up @@ -3159,6 +3165,7 @@ context_traverse(PySSLContext *self, visitproc visit, void *arg)
{
Py_VISIT(self->set_sni_cb);
Py_VISIT(self->msg_cb);
Py_VISIT(Py_TYPE(self));
return 0;
}

Expand Down Expand Up @@ -4641,11 +4648,11 @@ static PyType_Slot PySSLContext_slots[] = {
};

static PyType_Spec PySSLContext_spec = {
"_ssl._SSLContext",
sizeof(PySSLContext),
0,
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_IMMUTABLETYPE,
PySSLContext_slots,
.name = "_ssl._SSLContext",
.basicsize = sizeof(PySSLContext),
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC |
Py_TPFLAGS_IMMUTABLETYPE),
.slots = PySSLContext_slots,
};


Expand Down Expand Up @@ -4689,10 +4696,18 @@ _ssl_MemoryBIO_impl(PyTypeObject *type)
return (PyObject *) self;
}

static int
memory_bio_traverse(PySSLMemoryBIO *self, visitproc visit, void *arg)
{
Py_VISIT(Py_TYPE(self));
return 0;
}

static void
memory_bio_dealloc(PySSLMemoryBIO *self)
{
PyTypeObject *tp = Py_TYPE(self);
PyObject_GC_UnTrack(self);
BIO_free(self->bio);
Py_TYPE(self)->tp_free(self);
Py_DECREF(tp);
Expand Down Expand Up @@ -4843,15 +4858,16 @@ static PyType_Slot PySSLMemoryBIO_slots[] = {
{Py_tp_getset, memory_bio_getsetlist},
{Py_tp_new, _ssl_MemoryBIO},
{Py_tp_dealloc, memory_bio_dealloc},
{Py_tp_traverse, memory_bio_traverse},
{0, 0},
};

static PyType_Spec PySSLMemoryBIO_spec = {
"_ssl.MemoryBIO",
sizeof(PySSLMemoryBIO),
0,
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE,
PySSLMemoryBIO_slots,
.name = "_ssl.MemoryBIO",
.basicsize = sizeof(PySSLMemoryBIO),
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE |
Py_TPFLAGS_HAVE_GC),
.slots = PySSLMemoryBIO_slots,
};

/*
Expand Down Expand Up @@ -4934,6 +4950,7 @@ static int
PySSLSession_traverse(PySSLSession *self, visitproc visit, void *arg)
{
Py_VISIT(self->ctx);
Py_VISIT(Py_TYPE(self));
return 0;
}

Expand Down Expand Up @@ -5022,11 +5039,11 @@ static PyType_Slot PySSLSession_slots[] = {
};

static PyType_Spec PySSLSession_spec = {
"_ssl.SSLSession",
sizeof(PySSLSession),
0,
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_IMMUTABLETYPE,
PySSLSession_slots,
.name = "_ssl.SSLSession",
.basicsize = sizeof(PySSLSession),
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Py_TPFLAGS_IMMUTABLETYPE),
.slots = PySSLSession_slots,
};


Expand Down