Le mer. 5 sept. 2018 à 01:15, Hugh Fisher hugo.fisher@gmail.com a écrit :
On Wed, Sep 5, 2018 at 8:18 AM Victor Stinner vstinner@redhat.com wrote:
I'm trying to design an API which allows to remove the ob_type field from PyObject. I would to do that for many reasons. I wrote a whole website to explain why: https://pythoncapi.readthedocs.io/
I don't see how removing Py_TYPE() will make it easier to remove the ob_type field. I'd expect that it would make it even harder.
I have multiple goals for the new C API:
- hide implementation details to allow to experiment new optimizations like tagged pointers
- hide implementation details to allow to modify deeply C structures used internally in CPython
- don't access C structures to generate machine code which would be compatible with multiple Python versions: "stable ABI"
I listed some ideas of optimizations: https://pythoncapi.readthedocs.io/optimization_ideas.html
My plan doesn't include implementing these optimizations, but prepare the code to allow to experiment multiple kinds of optimizations.
As someone who does use the C API from time to time, I have never thought of Py_TYPE (or other similar macros/functions) as returning a borrowed reference. To me, it's a C programming style that asks me to please use these macros/functions to access properties of "objects" because the API designer wants to be able to later change the struct underneath without rewriting code.
Again, you rely on the exact behaviour of the current ("old") C API where PyObject* is really a pointer to a concrete PyObject which exist somewhere in memory. In my plan, a PyObject* can be something else (ex: tagged pointer where a number is directly stored inside the "pointer").
So if you want to remove ob_type, you can - as long as you keep Py_TYPE(). Phase 1, announce that ob_type will be removed in next version, or that it might be a tagged pointer and no longer usable as a C pointer. Phase 2, in the next version, rename ob_type and change Py_TYPE() to match. Existing code sitll runs, but compilation errors for any native extensions not using Py_TYPE(). Finally in phase 3 you can turn the type into a tagged pointer, handle, or whatever.
I already modifiy Py_TYPE() macro to emit a function call, _Py_TYPE_impl(). Currently, it's called the "Py_NEWCAPI_NO_STRUCT" API. => this API is incompatible with tagged pointer because it returns a borrowed reference
I also implemented an API without Py_TYPE() called the "Py_NEWCAPI" API... => no more borrored reference, ready for tagged pointer for example
... but removing Py_TYPE() simply broke all C extensions of CPython. So I reverted temporary this change and started this discussion.
It seems like I have to add a new Py_GetType() function which returns a strong reference (Py_INCREF) and start to modify C extension for use it instead of Py_TYPE().
Victor