]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.14] gh-132983: Make zstd types immutable (GH-133784) (#133857)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Sat, 10 May 2025 23:02:44 +0000 (01:02 +0200)
committerGitHub <noreply@github.com>
Sat, 10 May 2025 23:02:44 +0000 (23:02 +0000)
gh-132983: Make zstd types immutable (GH-133784)
(cherry picked from commit 1a87b6e9ae6da255f30465ed59a78913ebf2e898)

Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com>
Modules/_zstd/_zstdmodule.c
Modules/_zstd/compressor.c
Modules/_zstd/decompressor.c
Modules/_zstd/zstddict.c

index bddef0d0b864cd7b4d59e55834106cc32b213a8f..c3852fe89732bc71c9be75d91dfdfdde823eeb9e 100644 (file)
@@ -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
index e70eb637b29f3ec7f24966c33f945b7d7ad7c26b..355a27d2734a1bd3ed97f6a081a6c2c227d7bae3 100644 (file)
@@ -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,
 };
index 2ed88cd3f231f15f3481bb2a17500636cd170816..a654a9540e1f05bf24fb5e63f29e2cadb8dce617 100644 (file)
@@ -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,
 };
index 99976cef85a2101b7e1c7cbdc8bfc23d77a4bb36..47bc8a84ca24a7821c26187786f32126e1c0d3c2 100644 (file)
@@ -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,
 };