From 9be36ca8bb06c684052cb2021e58df316fe335d7 Mon Sep 17 00:00:00 2001 From: Adam Turner <9087854+aa-turner@users.noreply.github.com> Date: Fri, 9 May 2025 20:17:25 +0100 Subject: [PATCH 1/2] Make zstd types immutable --- Modules/_zstd/_zstdmodule.c | 3 +++ Modules/_zstd/decompressor.c | 3 ++- Modules/_zstd/zstddict.c | 3 ++- 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/Modules/_zstd/_zstdmodule.c b/Modules/_zstd/_zstdmodule.c index 4004bbb3461393..0af2045fd01434 100644 --- a/Modules/_zstd/_zstdmodule.c +++ b/Modules/_zstd/_zstdmodule.c @@ -682,6 +682,9 @@ do { \ ADD_INT_CONST_TO_TYPE(mod_state->ZstdCompressor_type, "FLUSH_FRAME", ZSTD_e_end); + /* Make ZstdCompressor immutable */ + PyType_Freeze(mod_state->ZstdCompressor_type); + #undef ADD_TYPE #undef ADD_INT_MACRO #undef ADD_ZSTD_COMPRESSOR_INT_CONST diff --git a/Modules/_zstd/decompressor.c b/Modules/_zstd/decompressor.c index d141e68efded26..d172d54e3e86dd 100644 --- a/Modules/_zstd/decompressor.c +++ b/Modules/_zstd/decompressor.c @@ -886,6 +886,7 @@ static PyType_Slot ZstdDecompressor_slots[] = { PyType_Spec zstd_decompressor_type_spec = { .name = "_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 b411eadfc95625..61d198d5a37090 100644 --- a/Modules/_zstd/zstddict.c +++ b/Modules/_zstd/zstddict.c @@ -281,6 +281,7 @@ static PyType_Slot zstddict_slots[] = { PyType_Spec zstd_dict_type_spec = { .name = "_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, }; From a02c4274b70733eb3e43b607123d3bd336156c3c Mon Sep 17 00:00:00 2001 From: Adam Turner <9087854+aa-turner@users.noreply.github.com> Date: Sat, 10 May 2025 23:11:55 +0100 Subject: [PATCH 2/2] Add comment --- Modules/_zstd/_zstdmodule.c | 2 +- Modules/_zstd/compressor.c | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/Modules/_zstd/_zstdmodule.c b/Modules/_zstd/_zstdmodule.c index 36a891a04bd336..c3852fe89732bc 100644 --- a/Modules/_zstd/_zstdmodule.c +++ b/Modules/_zstd/_zstdmodule.c @@ -679,7 +679,7 @@ do { \ ADD_INT_CONST_TO_TYPE(mod_state->ZstdCompressor_type, "FLUSH_FRAME", ZSTD_e_end); - /* Make ZstdCompressor immutable */ + /* Make ZstdCompressor immutable (set Py_TPFLAGS_IMMUTABLETYPE) */ PyType_Freeze(mod_state->ZstdCompressor_type); #undef ADD_TYPE diff --git a/Modules/_zstd/compressor.c b/Modules/_zstd/compressor.c index e70eb637b29f3e..355a27d2734a1b 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, };