From: Adam Turner <9087854+AA-Turner@users.noreply.github.com> Date: Sat, 10 May 2025 22:37:17 +0000 (+0100) Subject: gh-132983: Make zstd types immutable (#133784) X-Git-Tag: v3.15.0a1~1766 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1a87b6e9ae6da255f30465ed59a78913ebf2e898;p=thirdparty%2FPython%2Fcpython.git gh-132983: Make zstd types immutable (#133784) --- diff --git a/Modules/_zstd/_zstdmodule.c b/Modules/_zstd/_zstdmodule.c index bddef0d0b864..c3852fe89732 100644 --- a/Modules/_zstd/_zstdmodule.c +++ b/Modules/_zstd/_zstdmodule.c @@ -679,6 +679,9 @@ do { \ ADD_INT_CONST_TO_TYPE(mod_state->ZstdCompressor_type, "FLUSH_FRAME", ZSTD_e_end); + /* Make ZstdCompressor immutable (set Py_TPFLAGS_IMMUTABLETYPE) */ + PyType_Freeze(mod_state->ZstdCompressor_type); + #undef ADD_TYPE #undef ADD_INT_MACRO #undef ADD_ZSTD_COMPRESSOR_INT_CONST diff --git a/Modules/_zstd/compressor.c b/Modules/_zstd/compressor.c index e70eb637b29f..355a27d2734a 100644 --- a/Modules/_zstd/compressor.c +++ b/Modules/_zstd/compressor.c @@ -729,6 +729,9 @@ static PyType_Slot zstdcompressor_slots[] = { PyType_Spec zstd_compressor_type_spec = { .name = "compression.zstd.ZstdCompressor", .basicsize = sizeof(ZstdCompressor), + // Py_TPFLAGS_IMMUTABLETYPE is not used here as several + // associated constants need to be added to the type. + // PyType_Freeze is called later to set the flag. .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, .slots = zstdcompressor_slots, }; diff --git a/Modules/_zstd/decompressor.c b/Modules/_zstd/decompressor.c index 2ed88cd3f231..a654a9540e1f 100644 --- a/Modules/_zstd/decompressor.c +++ b/Modules/_zstd/decompressor.c @@ -902,6 +902,7 @@ static PyType_Slot ZstdDecompressor_slots[] = { PyType_Spec zstd_decompressor_type_spec = { .name = "compression.zstd.ZstdDecompressor", .basicsize = sizeof(ZstdDecompressor), - .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, + .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE + | Py_TPFLAGS_HAVE_GC, .slots = ZstdDecompressor_slots, }; diff --git a/Modules/_zstd/zstddict.c b/Modules/_zstd/zstddict.c index 99976cef85a2..47bc8a84ca24 100644 --- a/Modules/_zstd/zstddict.c +++ b/Modules/_zstd/zstddict.c @@ -278,6 +278,7 @@ static PyType_Slot zstddict_slots[] = { PyType_Spec zstd_dict_type_spec = { .name = "compression.zstd.ZstdDict", .basicsize = sizeof(ZstdDict), - .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, + .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE + | Py_TPFLAGS_HAVE_GC, .slots = zstddict_slots, };