]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
GH-126547: Pre-assign version numbers for a few common classes (GH-126551)
authorMark Shannon <mark@hotpy.org>
Fri, 8 Nov 2024 16:44:44 +0000 (16:44 +0000)
committerGitHub <noreply@github.com>
Fri, 8 Nov 2024 16:44:44 +0000 (16:44 +0000)
12 files changed:
Include/internal/pycore_runtime_init.h
Include/internal/pycore_typeobject.h
Objects/bytearrayobject.c
Objects/bytesobject.c
Objects/complexobject.c
Objects/dictobject.c
Objects/floatobject.c
Objects/listobject.c
Objects/longobject.c
Objects/setobject.c
Objects/tupleobject.c
Objects/typeobject.c

index bd3d704cb77730d09dfe6e2ce442f841d20b9cf9..8a8f47695fb8b0e46d73b5abf5ce12631f980d68 100644 (file)
@@ -87,7 +87,7 @@ extern PyTypeObject _PyExc_MemoryError;
             .double_format = _py_float_format_unknown, \
         }, \
         .types = { \
-            .next_version_tag = 1, \
+            .next_version_tag = _Py_TYPE_VERSION_NEXT, \
         }, \
         .static_objects = { \
             .singletons = { \
index e72592b8e98ef84738a272cabbb34bc5eec3b83c..5debdd68fe94ca26783b0d2e7fc6961128f0e38c 100644 (file)
@@ -14,6 +14,21 @@ extern "C" {
 
 /* state */
 
+#define _Py_TYPE_VERSION_INT 1
+#define _Py_TYPE_VERSION_FLOAT 2
+#define _Py_TYPE_VERSION_LIST 3
+#define _Py_TYPE_VERSION_TUPLE 4
+#define _Py_TYPE_VERSION_STR 5
+#define _Py_TYPE_VERSION_SET 6
+#define _Py_TYPE_VERSION_FROZEN_SET 7
+#define _Py_TYPE_VERSION_DICT 8
+#define _Py_TYPE_VERSION_BYTEARRAY 9
+#define _Py_TYPE_VERSION_BYTES 10
+#define _Py_TYPE_VERSION_COMPLEX 11
+
+#define _Py_TYPE_VERSION_NEXT 16
+
+
 #define _Py_TYPE_BASE_VERSION_TAG (2<<16)
 #define _Py_MAX_GLOBAL_TYPE_VERSION_TAG (_Py_TYPE_BASE_VERSION_TAG - 1)
 
index fd2a85a3fe0a615b4d3546e4fe7f0745ea39046a..5a52b2f702ad0bfb50014c70bd02abfcdbbd5dcb 100644 (file)
@@ -2452,6 +2452,7 @@ PyTypeObject PyByteArray_Type = {
     PyType_GenericAlloc,                /* tp_alloc */
     PyType_GenericNew,                  /* tp_new */
     PyObject_Free,                      /* tp_free */
+    .tp_version_tag = _Py_TYPE_VERSION_BYTEARRAY,
 };
 
 /*********************** Bytearray Iterator ****************************/
index dcc1aba76abbed4ce110678630537e00895b0056..ac02cfe7cf01c5f1478ccf74cae7901eb74fa108 100644 (file)
@@ -3080,6 +3080,7 @@ PyTypeObject PyBytes_Type = {
     bytes_alloc,                                /* tp_alloc */
     bytes_new,                                  /* tp_new */
     PyObject_Free,                              /* tp_free */
+    .tp_version_tag = _Py_TYPE_VERSION_BYTES,
 };
 
 void
index 787235c63a6be128adaa1cd2ab2a409f04f85e3b..7b4948fc8ebe3da58f53214cf66a72a7153e1961 100644 (file)
@@ -1250,4 +1250,5 @@ PyTypeObject PyComplex_Type = {
     PyType_GenericAlloc,                        /* tp_alloc */
     actual_complex_new,                         /* tp_new */
     PyObject_Free,                              /* tp_free */
+    .tp_version_tag = _Py_TYPE_VERSION_COMPLEX,
 };
index f28a92657834be612abcd73c51d5edd478549a05..2090008055b7c0e2dd528fa4c9857d466b570d48 100644 (file)
@@ -4912,6 +4912,7 @@ PyTypeObject PyDict_Type = {
     dict_new,                                   /* tp_new */
     PyObject_GC_Del,                            /* tp_free */
     .tp_vectorcall = dict_vectorcall,
+    .tp_version_tag = _Py_TYPE_VERSION_DICT,
 };
 
 /* For backward compatibility with old dictionary interface */
index 7e14a8ad959590ce130733dd85b56b957899270c..f00b6a6b4b2bdcd1748af405bf741f6df6b8c4c7 100644 (file)
@@ -1916,6 +1916,7 @@ PyTypeObject PyFloat_Type = {
     0,                                          /* tp_alloc */
     float_new,                                  /* tp_new */
     .tp_vectorcall = (vectorcallfunc)float_vectorcall,
+    .tp_version_tag = _Py_TYPE_VERSION_FLOAT,
 };
 
 static void
index 930aefde325a7c57a9478d39da2d7beb7f5ca791..bb0040cbe9f2722c15c28a0445c22d1515c4f645 100644 (file)
@@ -3774,6 +3774,7 @@ PyTypeObject PyList_Type = {
     PyType_GenericNew,                          /* tp_new */
     PyObject_GC_Del,                            /* tp_free */
     .tp_vectorcall = list_vectorcall,
+    .tp_version_tag = _Py_TYPE_VERSION_LIST,
 };
 
 /*********************** List Iterator **************************/
index 4e94894048573097c53ead5d1411c3aa0f578b28..b4c0f63a9843ce587931b417a7aed3d37cc5bb51 100644 (file)
@@ -6584,6 +6584,7 @@ PyTypeObject PyLong_Type = {
     long_new,                                   /* tp_new */
     PyObject_Free,                              /* tp_free */
     .tp_vectorcall = long_vectorcall,
+    .tp_version_tag = _Py_TYPE_VERSION_INT,
 };
 
 static PyTypeObject Int_InfoType;
index 2671792190d7a764de6317bbfbc6adac72f56bd9..955ccbebf74b5457c146645092ff83793c90b63c 100644 (file)
@@ -2520,6 +2520,7 @@ PyTypeObject PySet_Type = {
     set_new,                            /* tp_new */
     PyObject_GC_Del,                    /* tp_free */
     .tp_vectorcall = set_vectorcall,
+    .tp_version_tag = _Py_TYPE_VERSION_SET,
 };
 
 /* frozenset object ********************************************************/
@@ -2610,6 +2611,7 @@ PyTypeObject PyFrozenSet_Type = {
     frozenset_new,                      /* tp_new */
     PyObject_GC_Del,                    /* tp_free */
     .tp_vectorcall = frozenset_vectorcall,
+    .tp_version_tag = _Py_TYPE_VERSION_FROZEN_SET,
 };
 
 
index f3132e0933ac30f5038c5e13665e91433c2c114a..193914d54bd90ebc10f826d55fbfc2c2ea34e902 100644 (file)
@@ -909,6 +909,7 @@ PyTypeObject PyTuple_Type = {
     tuple_new,                                  /* tp_new */
     PyObject_GC_Del,                            /* tp_free */
     .tp_vectorcall = tuple_vectorcall,
+    .tp_version_tag = _Py_TYPE_VERSION_TUPLE,
 };
 
 /* The following function breaks the notion that tuples are immutable:
index 88db29e14b0d441de965b65b6d099dd00476ac77..4af7f0273aae91a948483bfc03e59b8be6d6051d 100644 (file)
@@ -8613,7 +8613,9 @@ init_static_type(PyInterpreterState *interp, PyTypeObject *self,
         self->tp_flags |= Py_TPFLAGS_IMMUTABLETYPE;
 
         assert(NEXT_GLOBAL_VERSION_TAG <= _Py_MAX_GLOBAL_TYPE_VERSION_TAG);
-        _PyType_SetVersion(self, NEXT_GLOBAL_VERSION_TAG++);
+        if (self->tp_version_tag == 0) {
+            _PyType_SetVersion(self, NEXT_GLOBAL_VERSION_TAG++);
+        }
     }
     else {
         assert(!initial);