bpo-32388: Remove cross-version binary compatibility requirement in tp_flags by pitrou · Pull Request #4944 · python/cpython (original) (raw)
Starting with 3.8, it is now allowed to add new fields at the end of the PyTypeObject struct (and even in the middle, if we are so inclined)
Not "in the middle" as that would break the typical code of the form
PyTypeObject PyCFunction_Type = { PyVarObject_HEAD_INIT(&PyType_Type, 0) "builtin_function_or_method", sizeof(PyCFunctionObject), 0, (destructor)meth_dealloc, /* tp_dealloc / 0, / tp_print / 0, / tp_getattr / 0, / tp_setattr / 0, / tp_reserved / (reprfunc)meth_repr, / tp_repr / 0, / tp_as_number / 0, / tp_as_sequence / 0, / tp_as_mapping / (hashfunc)meth_hash, / tp_hash / PyCFunction_Call, / tp_call / 0, / tp_str / PyObject_GenericGetAttr, / tp_getattro / 0, / tp_setattro / 0, / tp_as_buffer / Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/ tp_flags / 0, / tp_doc / (traverseproc)meth_traverse, / tp_traverse / 0, / tp_clear / meth_richcompare, / tp_richcompare / offsetof(PyCFunctionObject, m_weakreflist), / tp_weaklistoffset / 0, / tp_iter / 0, / tp_iternext / meth_methods, / tp_methods / meth_members, / tp_members / meth_getsets, / tp_getset / 0, / tp_base / 0, / tp_dict */ };
With C99, there are better ways to write the above, but we should still support that.