Skip to content

Commit 4c8dad0

Browse files
pythongh-101696: Make sure immutable types have a valid version tag
1 parent de3669e commit 4c8dad0

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

Objects/typeobject.c

+13-2
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,13 @@ _PyType_CheckConsistency(PyTypeObject *type)
229229
CHECK(type->tp_traverse != NULL);
230230
}
231231

232+
if (_PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG)) {
233+
CHECK(type->tp_version_tag != 0);
234+
}
235+
else {
236+
CHECK(type->tp_version_tag == 0);
237+
}
238+
232239
if (type->tp_flags & Py_TPFLAGS_DISALLOW_INSTANTIATION) {
233240
CHECK(type->tp_new == NULL);
234241
CHECK(PyDict_Contains(type->tp_dict, &_Py_ID(__new__)) == 0);
@@ -4469,8 +4476,6 @@ _PyStaticType_Dealloc(PyTypeObject *type)
44694476
}
44704477

44714478
type->tp_flags &= ~Py_TPFLAGS_READY;
4472-
type->tp_flags &= ~Py_TPFLAGS_VALID_VERSION_TAG;
4473-
type->tp_version_tag = 0;
44744479

44754480
if (type->tp_flags & _Py_TPFLAGS_STATIC_BUILTIN) {
44764481
_PyStaticType_ClearWeakRefs(type);
@@ -6971,6 +6976,12 @@ PyType_Ready(PyTypeObject *type)
69716976
type->tp_flags |= Py_TPFLAGS_IMMUTABLETYPE;
69726977
}
69736978

6979+
/* All immutable types must have a static valid version tag */
6980+
if (type->tp_flags & Py_TPFLAGS_IMMUTABLETYPE) {
6981+
type->tp_version_tag = next_version_tag++;
6982+
type->tp_flags |= Py_TPFLAGS_VALID_VERSION_TAG;
6983+
}
6984+
69746985
if (type_ready(type) < 0) {
69756986
type->tp_flags &= ~Py_TPFLAGS_READYING;
69766987
return -1;

0 commit comments

Comments
 (0)