In bpo-42100, I added the private function _PyType_GetModuleByDef, which
allows module state access from slot methods (like tp_init or nb_add),
the main thing missing from PEP 573 (Module State Access from C
Extension Methods). The method is now used in 10 stdlib modules, without
Since it was has optimized (thanks, Victor!), I haven't seen complaints
about performance issues. Sadly, it has a loop so it's much slower than
using C statics, but it's OK for many use cases.
I haven't found a better way to get per-module state for isolated
modules (and the stable ABI).
So, I'd like to drop the leading underscore, and add
PyType_GetModuleByDef directly to the limited API.
The function itself can be implemented using only limited API, though
it's a bit tricky to do so correctly (and our implementation uses
private speedups), so it's better if extension authors can use it as a
pre-made building block.
The fact that it can be implemented with only limited API means that
adding it should not add big burden for long-term maintenance in
CPython, nor for alternate implementations of the C-API.
Is anyone against? Does anyone feel this should be discussed more widely
than on capi-sig?