Skip to content

Commit ece74b0

Browse files
markshannonebonnal
authored andcommitted
pythonGH-126547: Pre-assign version numbers for a few common classes (pythonGH-126551)
1 parent 2ecfee6 commit ece74b0

12 files changed

+29
-2
lines changed

Include/internal/pycore_runtime_init.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ extern PyTypeObject _PyExc_MemoryError;
8787
.double_format = _py_float_format_unknown, \
8888
}, \
8989
.types = { \
90-
.next_version_tag = 1, \
90+
.next_version_tag = _Py_TYPE_VERSION_NEXT, \
9191
}, \
9292
.static_objects = { \
9393
.singletons = { \

Include/internal/pycore_typeobject.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,21 @@ extern "C" {
1414

1515
/* state */
1616

17+
#define _Py_TYPE_VERSION_INT 1
18+
#define _Py_TYPE_VERSION_FLOAT 2
19+
#define _Py_TYPE_VERSION_LIST 3
20+
#define _Py_TYPE_VERSION_TUPLE 4
21+
#define _Py_TYPE_VERSION_STR 5
22+
#define _Py_TYPE_VERSION_SET 6
23+
#define _Py_TYPE_VERSION_FROZEN_SET 7
24+
#define _Py_TYPE_VERSION_DICT 8
25+
#define _Py_TYPE_VERSION_BYTEARRAY 9
26+
#define _Py_TYPE_VERSION_BYTES 10
27+
#define _Py_TYPE_VERSION_COMPLEX 11
28+
29+
#define _Py_TYPE_VERSION_NEXT 16
30+
31+
1732
#define _Py_TYPE_BASE_VERSION_TAG (2<<16)
1833
#define _Py_MAX_GLOBAL_TYPE_VERSION_TAG (_Py_TYPE_BASE_VERSION_TAG - 1)
1934

Objects/bytearrayobject.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2452,6 +2452,7 @@ PyTypeObject PyByteArray_Type = {
24522452
PyType_GenericAlloc, /* tp_alloc */
24532453
PyType_GenericNew, /* tp_new */
24542454
PyObject_Free, /* tp_free */
2455+
.tp_version_tag = _Py_TYPE_VERSION_BYTEARRAY,
24552456
};
24562457

24572458
/*********************** Bytearray Iterator ****************************/

Objects/bytesobject.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3080,6 +3080,7 @@ PyTypeObject PyBytes_Type = {
30803080
bytes_alloc, /* tp_alloc */
30813081
bytes_new, /* tp_new */
30823082
PyObject_Free, /* tp_free */
3083+
.tp_version_tag = _Py_TYPE_VERSION_BYTES,
30833084
};
30843085

30853086
void

Objects/complexobject.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1250,4 +1250,5 @@ PyTypeObject PyComplex_Type = {
12501250
PyType_GenericAlloc, /* tp_alloc */
12511251
actual_complex_new, /* tp_new */
12521252
PyObject_Free, /* tp_free */
1253+
.tp_version_tag = _Py_TYPE_VERSION_COMPLEX,
12531254
};

Objects/dictobject.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4912,6 +4912,7 @@ PyTypeObject PyDict_Type = {
49124912
dict_new, /* tp_new */
49134913
PyObject_GC_Del, /* tp_free */
49144914
.tp_vectorcall = dict_vectorcall,
4915+
.tp_version_tag = _Py_TYPE_VERSION_DICT,
49154916
};
49164917

49174918
/* For backward compatibility with old dictionary interface */

Objects/floatobject.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1916,6 +1916,7 @@ PyTypeObject PyFloat_Type = {
19161916
0, /* tp_alloc */
19171917
float_new, /* tp_new */
19181918
.tp_vectorcall = (vectorcallfunc)float_vectorcall,
1919+
.tp_version_tag = _Py_TYPE_VERSION_FLOAT,
19191920
};
19201921

19211922
static void

Objects/listobject.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3774,6 +3774,7 @@ PyTypeObject PyList_Type = {
37743774
PyType_GenericNew, /* tp_new */
37753775
PyObject_GC_Del, /* tp_free */
37763776
.tp_vectorcall = list_vectorcall,
3777+
.tp_version_tag = _Py_TYPE_VERSION_LIST,
37773778
};
37783779

37793780
/*********************** List Iterator **************************/

Objects/longobject.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6584,6 +6584,7 @@ PyTypeObject PyLong_Type = {
65846584
long_new, /* tp_new */
65856585
PyObject_Free, /* tp_free */
65866586
.tp_vectorcall = long_vectorcall,
6587+
.tp_version_tag = _Py_TYPE_VERSION_INT,
65876588
};
65886589

65896590
static PyTypeObject Int_InfoType;

Objects/setobject.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2520,6 +2520,7 @@ PyTypeObject PySet_Type = {
25202520
set_new, /* tp_new */
25212521
PyObject_GC_Del, /* tp_free */
25222522
.tp_vectorcall = set_vectorcall,
2523+
.tp_version_tag = _Py_TYPE_VERSION_SET,
25232524
};
25242525

25252526
/* frozenset object ********************************************************/
@@ -2610,6 +2611,7 @@ PyTypeObject PyFrozenSet_Type = {
26102611
frozenset_new, /* tp_new */
26112612
PyObject_GC_Del, /* tp_free */
26122613
.tp_vectorcall = frozenset_vectorcall,
2614+
.tp_version_tag = _Py_TYPE_VERSION_FROZEN_SET,
26132615
};
26142616

26152617

Objects/tupleobject.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -909,6 +909,7 @@ PyTypeObject PyTuple_Type = {
909909
tuple_new, /* tp_new */
910910
PyObject_GC_Del, /* tp_free */
911911
.tp_vectorcall = tuple_vectorcall,
912+
.tp_version_tag = _Py_TYPE_VERSION_TUPLE,
912913
};
913914

914915
/* The following function breaks the notion that tuples are immutable:

Objects/typeobject.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8613,7 +8613,9 @@ init_static_type(PyInterpreterState *interp, PyTypeObject *self,
86138613
self->tp_flags |= Py_TPFLAGS_IMMUTABLETYPE;
86148614

86158615
assert(NEXT_GLOBAL_VERSION_TAG <= _Py_MAX_GLOBAL_TYPE_VERSION_TAG);
8616-
_PyType_SetVersion(self, NEXT_GLOBAL_VERSION_TAG++);
8616+
if (self->tp_version_tag == 0) {
8617+
_PyType_SetVersion(self, NEXT_GLOBAL_VERSION_TAG++);
8618+
}
86178619
}
86188620
else {
86198621
assert(!initial);

0 commit comments

Comments
 (0)