From 3dd0b0e17f3cc81143be2f58d4fe12fb02851752 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Mon, 1 Sep 2025 10:22:43 +0200 Subject: [PATCH] gh-116946: fully implement GC protocol for `lzma` objects (GH-138288) (cherry picked from commit 3ea16f990f81e1e3b2892f1dfd213937b1df2a68) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com> --- Modules/_lzmamodule.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/Modules/_lzmamodule.c b/Modules/_lzmamodule.c index 462c2181fa6036..fff4454e1ccde3 100644 --- a/Modules/_lzmamodule.c +++ b/Modules/_lzmamodule.c @@ -866,12 +866,13 @@ Compressor_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) static void Compressor_dealloc(PyObject *op) { + PyTypeObject *tp = Py_TYPE(op); + PyObject_GC_UnTrack(op); Compressor *self = Compressor_CAST(op); lzma_end(&self->lzs); if (self->lock != NULL) { PyThread_free_lock(self->lock); } - PyTypeObject *tp = Py_TYPE(self); tp->tp_free(self); Py_DECREF(tp); } @@ -933,7 +934,7 @@ static PyType_Spec lzma_compressor_type_spec = { // lzma_compressor_type_spec does not have Py_TPFLAGS_BASETYPE flag // which prevents to create a subclass. // So calling PyType_GetModuleState() in this file is always safe. - .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE), + .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE | Py_TPFLAGS_HAVE_GC), .slots = lzma_compressor_type_slots, }; @@ -1313,6 +1314,8 @@ _lzma_LZMADecompressor_impl(PyTypeObject *type, int format, static void Decompressor_dealloc(PyObject *op) { + PyTypeObject *tp = Py_TYPE(op); + PyObject_GC_UnTrack(op); Decompressor *self = Decompressor_CAST(op); if(self->input_buffer != NULL) PyMem_Free(self->input_buffer); @@ -1322,7 +1325,6 @@ Decompressor_dealloc(PyObject *op) if (self->lock != NULL) { PyThread_free_lock(self->lock); } - PyTypeObject *tp = Py_TYPE(self); tp->tp_free(self); Py_DECREF(tp); } @@ -1380,7 +1382,7 @@ static PyType_Spec lzma_decompressor_type_spec = { // lzma_decompressor_type_spec does not have Py_TPFLAGS_BASETYPE flag // which prevents to create a subclass. // So calling PyType_GetModuleState() in this file is always safe. - .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE), + .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE | Py_TPFLAGS_HAVE_GC), .slots = lzma_decompressor_type_slots, };