Skip to content

Commit

Permalink
pythongh-101696: Make sure immutable types have a valid version tag
Browse files Browse the repository at this point in the history
  • Loading branch information
erlend-aasland committed Feb 9, 2023
1 parent de3669e commit 4c8dad0
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions Objects/typeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,13 @@ _PyType_CheckConsistency(PyTypeObject *type)
CHECK(type->tp_traverse != NULL);
}

if (_PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG)) {
CHECK(type->tp_version_tag != 0);
}
else {
CHECK(type->tp_version_tag == 0);
}

if (type->tp_flags & Py_TPFLAGS_DISALLOW_INSTANTIATION) {
CHECK(type->tp_new == NULL);
CHECK(PyDict_Contains(type->tp_dict, &_Py_ID(__new__)) == 0);
Expand Down Expand Up @@ -4469,8 +4476,6 @@ _PyStaticType_Dealloc(PyTypeObject *type)
}

type->tp_flags &= ~Py_TPFLAGS_READY;
type->tp_flags &= ~Py_TPFLAGS_VALID_VERSION_TAG;
type->tp_version_tag = 0;

if (type->tp_flags & _Py_TPFLAGS_STATIC_BUILTIN) {
_PyStaticType_ClearWeakRefs(type);
Expand Down Expand Up @@ -6971,6 +6976,12 @@ PyType_Ready(PyTypeObject *type)
type->tp_flags |= Py_TPFLAGS_IMMUTABLETYPE;
}

/* All immutable types must have a static valid version tag */
if (type->tp_flags & Py_TPFLAGS_IMMUTABLETYPE) {
type->tp_version_tag = next_version_tag++;
type->tp_flags |= Py_TPFLAGS_VALID_VERSION_TAG;
}

if (type_ready(type) < 0) {
type->tp_flags &= ~Py_TPFLAGS_READYING;
return -1;
Expand Down

0 comments on commit 4c8dad0

Please sign in to comment.