On Sun, 18 Apr 2021 02:13:57 +1000 Nick Coghlan <ncoghlan@gmail.com> wrote:
If they want automatic resource management, then we want them working out how to lift the affected code out of C and into Python (for example, the import system rewrite).
There's a significant amount of wishful thinking here. When CPython code gets written in C, it's often because it's presumed to be performance-critical. The import system was indeed rewritten in Python... but some parts were then written back in C (see Python/import.c) to fix performance regressions.
In a lot of ways, CPython's C API can be viewed as one of the many competing approaches to enabling "C-with-objects" programming, just like C++.
It's a clumsy API to use *from C*. Witness the inconsistent behaviour of those APIs wrt. borrowed references, for example, or the fact that forgetting an INCREF or DECREF can lead to occasional leaks or crashes, or the delicate task of teaching the cyclic GC about a particular type, or all the special cases where those APIs deviate slightly in semantics from their pure Python equivalents, or the fact that APIs are added in adhoc manner, leading to an inconsistent set of primitives... (extracting a C long from a Python object works a bit differently from extracting a C unsigned long or long long... which then requires the caller to learn about those subtle differences and work around them). CPython's C API is probably fine for consumption by intermediate layers such as Cython. It's a maze to navigate for direct use. Regards Antoine.