diff options
author | Erlend Egeberg Aasland <erlend.aasland@innova.no> | 2021-05-27 07:50:09 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-05-27 07:50:09 (GMT) |
commit | dcb8786a9848516e823e090bb36079678913d8d3 (patch) | |
tree | a8c9250e29ec458622a3f932ff81711bb334ad43 /Modules | |
parent | 6ef5ba391d700bde7ec3ffd5fb7132a30dd309c4 (diff) | |
download | cpython-dcb8786a9848516e823e090bb36079678913d8d3.zip cpython-dcb8786a9848516e823e090bb36079678913d8d3.tar.gz cpython-dcb8786a9848516e823e090bb36079678913d8d3.tar.bz2 |
bpo-42972: Fully implement GC protocol for ssl heap types (GH-26370)
Diffstat (limited to 'Modules')
-rw-r--r-- | Modules/_ssl.c | 73 |
1 files changed, 45 insertions, 28 deletions
diff --git a/Modules/_ssl.c b/Modules/_ssl.c index 5f03214..1508327 100644 --- a/Modules/_ssl.c +++ b/Modules/_ssl.c @@ -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 @@ -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; @@ -896,6 +897,8 @@ newPySSLSocket(PySSLContext *sslctx, PySocketSockObject *sock, return NULL; } } + + PyObject_GC_Track(self); return self; } @@ -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; } @@ -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); } @@ -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, }; /* @@ -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; } @@ -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, }; @@ -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); @@ -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, }; /* @@ -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; } @@ -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, }; |