Skip to content

Commit

Permalink
GH-126547: Pre-assign version numbers for a few common classes (GH-12…
Browse files Browse the repository at this point in the history
  • Loading branch information
markshannon authored Nov 8, 2024
1 parent fd5580c commit fa40922
Show file tree
Hide file tree
Showing 12 changed files with 29 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Include/internal/pycore_runtime_init.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 = { \
Expand Down
15 changes: 15 additions & 0 deletions Include/internal/pycore_typeobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
1 change: 1 addition & 0 deletions Objects/bytearrayobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 ****************************/
Expand Down
1 change: 1 addition & 0 deletions Objects/bytesobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions Objects/complexobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
1 change: 1 addition & 0 deletions Objects/dictobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
1 change: 1 addition & 0 deletions Objects/floatobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions Objects/listobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 **************************/
Expand Down
1 change: 1 addition & 0 deletions Objects/longobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions Objects/setobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 ********************************************************/
Expand Down Expand Up @@ -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,
};


Expand Down
1 change: 1 addition & 0 deletions Objects/tupleobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 3 additions & 1 deletion Objects/typeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit fa40922

Please sign in to comment.