bpo-46613: Add PyType_GetModuleByDef to the public & limited API by encukou · Pull Request #31081 · python/cpython (original) (raw)
Should I remove the stable ABI addition for now?
For Py_NewRef/Py_XNewRef, I chose to add it to the stable ABI immediately. For PyType_GetModuleByDef(), I'm more scared because I introduced a bug and noticed it before your recent review. Sadly, only a minority of 3rd party C extensions are using heap types. Moreover, we discovered issues with heap types defined in the stdlib in Python 3.10:
- Some heap types didn't implement fully the GC protocol: no Py_TPFLAGS_HAVE_GC flag, or no traverse function, or objects not tracked by the GC
- Types became mutable: add Py_TPFLAGS_HAVE_GC flag
- tp_new=NULL didn't work anymore: add Py_TPFLAGS_DISALLOW_INSTANTIATION flag
These bugs made me even more careful than I was previously.
Maybe PyType_GetModuleByDef() is fine, but it's just that heap types in general became "a minefield" if you don't pay attention.
I love heap types, I want to use them everywhere. Maybe we need to enhance the documentation, add more warnings in PEP 630, and even implement new runtime checks (in development/debug mode?).
For example, creating a type with Py_TPFLAGS_HAVE_GC but with no traverse function now raises an exception.