diff options
Diffstat (limited to 'contrib/tools/python3/Modules/zlibmodule.c')
| -rw-r--r-- | contrib/tools/python3/Modules/zlibmodule.c | 141 |
1 files changed, 86 insertions, 55 deletions
diff --git a/contrib/tools/python3/Modules/zlibmodule.c b/contrib/tools/python3/Modules/zlibmodule.c index f94c57e4c89..f59f9a76abd 100644 --- a/contrib/tools/python3/Modules/zlibmodule.c +++ b/contrib/tools/python3/Modules/zlibmodule.c @@ -3,12 +3,15 @@ /* Windows users: read Python's PCbuild\readme.txt */ -#define PY_SSIZE_T_CLEAN +#ifndef Py_BUILD_CORE_BUILTIN +# define Py_BUILD_CORE_MODULE 1 +#endif #include "Python.h" -#include "structmember.h" // PyMemberDef + #include "zlib.h" #include "stdbool.h" +#include <stddef.h> // offsetof() #if defined(ZLIB_VERNUM) && ZLIB_VERNUM < 0x1221 #error "At least zlib version 1.2.2.1 is required" @@ -218,6 +221,8 @@ typedef struct PyThread_type_lock lock; } compobject; +#define _compobject_CAST(op) ((compobject *)op) + static void zlib_error(zlibstate *state, z_stream zst, int err, const char *msg) { @@ -258,7 +263,9 @@ static compobject * newcompobject(PyTypeObject *type) { compobject *self; - self = PyObject_New(compobject, type); + assert(type != NULL); + assert(type->tp_alloc != NULL); + self = _compobject_CAST(type->tp_alloc(type, 0)); if (self == NULL) return NULL; self->eof = 0; @@ -701,31 +708,41 @@ zlib_decompressobj_impl(PyObject *module, int wbits, PyObject *zdict) } static void -Dealloc(compobject *self) +compobject_dealloc_impl(PyObject *op, int (*dealloc)(z_streamp)) { - PyObject *type = (PyObject *)Py_TYPE(self); + PyTypeObject *type = Py_TYPE(op); + PyObject_GC_UnTrack(op); + compobject *self = _compobject_CAST(op); + if (self->is_initialised) { + (void)dealloc(&self->zst); + } PyThread_free_lock(self->lock); Py_XDECREF(self->unused_data); Py_XDECREF(self->unconsumed_tail); Py_XDECREF(self->zdict); - PyObject_Free(self); + type->tp_free(self); Py_DECREF(type); } +static int +compobject_traverse(PyObject *op, visitproc visit, void *arg) +{ + compobject *self = _compobject_CAST(op); + Py_VISIT(Py_TYPE(op)); + Py_VISIT(self->zdict); + return 0; +} + static void -Comp_dealloc(compobject *self) +Comp_dealloc(PyObject *op) { - if (self->is_initialised) - deflateEnd(&self->zst); - Dealloc(self); + compobject_dealloc_impl(op, &deflateEnd); } static void -Decomp_dealloc(compobject *self) +Decomp_dealloc(PyObject *op) { - if (self->is_initialised) - inflateEnd(&self->zst); - Dealloc(self); + compobject_dealloc_impl(op, &inflateEnd); } /*[clinic input] @@ -1346,19 +1363,23 @@ typedef struct { decompress_buf() */ Py_ssize_t avail_in_real; bool is_initialised; - char eof; /* T_BOOL expects a char */ + char eof; /* Py_T_BOOL expects a char */ char needs_input; } ZlibDecompressor; +#define ZlibDecompressor_CAST(op) ((ZlibDecompressor *)(op)) + /*[clinic input] class zlib.ZlibDecompressor "ZlibDecompressor *" "&ZlibDecompressorType" [clinic start generated code]*/ /*[clinic end generated code: output=da39a3ee5e6b4b0d input=0658178ab94645df]*/ static void -ZlibDecompressor_dealloc(ZlibDecompressor *self) +ZlibDecompressor_dealloc(PyObject *op) { - PyObject *type = (PyObject *)Py_TYPE(self); + PyTypeObject *type = Py_TYPE(op); + PyObject_GC_UnTrack(op); + ZlibDecompressor *self = ZlibDecompressor_CAST(op); PyThread_free_lock(self->lock); if (self->is_initialised) { inflateEnd(&self->zst); @@ -1366,11 +1387,20 @@ ZlibDecompressor_dealloc(ZlibDecompressor *self) PyMem_Free(self->input_buffer); Py_CLEAR(self->unused_data); Py_CLEAR(self->zdict); - PyObject_Free(self); + type->tp_free(self); Py_DECREF(type); } static int +ZlibDecompressor_traverse(PyObject *op, visitproc visit, void *arg) +{ + ZlibDecompressor *self = ZlibDecompressor_CAST(op); + Py_VISIT(Py_TYPE(op)); + Py_VISIT(self->zdict); + return 0; +} + +static int set_inflate_zdict_ZlibDecompressor(zlibstate *state, ZlibDecompressor *self) { Py_buffer zdict_buf; @@ -1721,7 +1751,10 @@ ZlibDecompressor__new__(PyTypeObject *cls, args, kwargs, format, keywords, &wbits, &zdict)) { return NULL; } - ZlibDecompressor *self = PyObject_New(ZlibDecompressor, cls); + ZlibDecompressor *self = ZlibDecompressor_CAST(cls->tp_alloc(cls, 0)); + if (self == NULL) { + return NULL; + } self->eof = 0; self->needs_input = 1; self->avail_in_real = 0; @@ -1800,9 +1833,9 @@ static PyMethodDef ZlibDecompressor_methods[] = { #define COMP_OFF(x) offsetof(compobject, x) static PyMemberDef Decomp_members[] = { - {"unused_data", T_OBJECT, COMP_OFF(unused_data), READONLY}, - {"unconsumed_tail", T_OBJECT, COMP_OFF(unconsumed_tail), READONLY}, - {"eof", T_BOOL, COMP_OFF(eof), READONLY}, + {"unused_data", _Py_T_OBJECT, COMP_OFF(unused_data), Py_READONLY}, + {"unconsumed_tail", _Py_T_OBJECT, COMP_OFF(unconsumed_tail), Py_READONLY}, + {"eof", Py_T_BOOL, COMP_OFF(eof), Py_READONLY}, {NULL}, }; @@ -1816,11 +1849,11 @@ PyDoc_STRVAR(ZlibDecompressor_needs_input_doc, "True if more input is needed before more decompressed data can be produced."); static PyMemberDef ZlibDecompressor_members[] = { - {"eof", T_BOOL, offsetof(ZlibDecompressor, eof), - READONLY, ZlibDecompressor_eof__doc__}, - {"unused_data", T_OBJECT_EX, offsetof(ZlibDecompressor, unused_data), - READONLY, ZlibDecompressor_unused_data__doc__}, - {"needs_input", T_BOOL, offsetof(ZlibDecompressor, needs_input), READONLY, + {"eof", Py_T_BOOL, offsetof(ZlibDecompressor, eof), + Py_READONLY, ZlibDecompressor_eof__doc__}, + {"unused_data", Py_T_OBJECT_EX, offsetof(ZlibDecompressor, unused_data), + Py_READONLY, ZlibDecompressor_unused_data__doc__}, + {"needs_input", Py_T_BOOL, offsetof(ZlibDecompressor, needs_input), Py_READONLY, ZlibDecompressor_needs_input_doc}, {NULL}, }; @@ -1926,6 +1959,7 @@ static PyMethodDef zlib_methods[] = static PyType_Slot Comptype_slots[] = { {Py_tp_dealloc, Comp_dealloc}, + {Py_tp_traverse, compobject_traverse}, {Py_tp_methods, comp_methods}, {0, 0}, }; @@ -1933,12 +1967,17 @@ static PyType_Slot Comptype_slots[] = { static PyType_Spec Comptype_spec = { .name = "zlib.Compress", .basicsize = sizeof(compobject), - .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION, + .flags = ( + Py_TPFLAGS_DEFAULT + | Py_TPFLAGS_DISALLOW_INSTANTIATION + | Py_TPFLAGS_HAVE_GC + ), .slots= Comptype_slots, }; static PyType_Slot Decomptype_slots[] = { {Py_tp_dealloc, Decomp_dealloc}, + {Py_tp_traverse, compobject_traverse}, {Py_tp_methods, Decomp_methods}, {Py_tp_members, Decomp_members}, {0, 0}, @@ -1947,12 +1986,17 @@ static PyType_Slot Decomptype_slots[] = { static PyType_Spec Decomptype_spec = { .name = "zlib.Decompress", .basicsize = sizeof(compobject), - .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION, + .flags = ( + Py_TPFLAGS_DEFAULT + | Py_TPFLAGS_DISALLOW_INSTANTIATION + | Py_TPFLAGS_HAVE_GC + ), .slots = Decomptype_slots, }; static PyType_Slot ZlibDecompressor_type_slots[] = { {Py_tp_dealloc, ZlibDecompressor_dealloc}, + {Py_tp_traverse, ZlibDecompressor_traverse}, {Py_tp_members, ZlibDecompressor_members}, {Py_tp_new, ZlibDecompressor__new__}, {Py_tp_doc, (char *)ZlibDecompressor__new____doc__}, @@ -1967,7 +2011,11 @@ static PyType_Spec ZlibDecompressor_type_spec = { // ZlibDecompressor_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 = ZlibDecompressor_type_slots, }; PyDoc_STRVAR(zlib_module_documentation, @@ -2037,17 +2085,11 @@ zlib_exec(PyObject *mod) } state->ZlibError = PyErr_NewException("zlib.error", NULL, NULL); - if (state->ZlibError == NULL) { + if (PyModule_AddObjectRef(mod, "error", state->ZlibError) < 0) { return -1; } - - if (PyModule_AddObject(mod, "error", Py_NewRef(state->ZlibError)) < 0) { - Py_DECREF(state->ZlibError); - return -1; - } - if (PyModule_AddObject(mod, "_ZlibDecompressor", - Py_NewRef(state->ZlibDecompressorType)) < 0) { - Py_DECREF(state->ZlibDecompressorType); + if (PyModule_AddObjectRef(mod, "_ZlibDecompressor", + (PyObject *)state->ZlibDecompressorType) < 0) { return -1; } @@ -2089,26 +2131,14 @@ zlib_exec(PyObject *mod) #ifdef Z_TREES // 1.2.3.4, only for inflate ZLIB_ADD_INT_MACRO(Z_TREES); #endif - PyObject *ver = PyUnicode_FromString(ZLIB_VERSION); - if (ver == NULL) { + if (PyModule_Add(mod, "ZLIB_VERSION", + PyUnicode_FromString(ZLIB_VERSION)) < 0) { return -1; } - - if (PyModule_AddObject(mod, "ZLIB_VERSION", ver) < 0) { - Py_DECREF(ver); + if (PyModule_Add(mod, "ZLIB_RUNTIME_VERSION", + PyUnicode_FromString(zlibVersion())) < 0) { return -1; } - - ver = PyUnicode_FromString(zlibVersion()); - if (ver == NULL) { - return -1; - } - - if (PyModule_AddObject(mod, "ZLIB_RUNTIME_VERSION", ver) < 0) { - Py_DECREF(ver); - return -1; - } - if (PyModule_AddStringConstant(mod, "__version__", "1.0") < 0) { return -1; } @@ -2118,6 +2148,7 @@ zlib_exec(PyObject *mod) static PyModuleDef_Slot zlib_slots[] = { {Py_mod_exec, zlib_exec}, {Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED}, + {Py_mod_gil, Py_MOD_GIL_NOT_USED}, {0, NULL} }; |
