Hi Antoine,

I'm in favor of making the C API more strict. The limited C API of Python
3.11 no longer cast arguments to PyObject* ! The caller must now cast to
PyObject*: see PEP 670 "convert macros to functions".

Converting macros to static inline functions (PEP 670) is already an
important change. I prefer to not change too many things at the same time.

Maybe in Python 3.12 (or later), we can consider requiring to not pass
"const PyObject*" in C++: remove the cast dropping constness of the
argument.

My long term goal is to only use opaque "PyObject*" "handles" in the C API.
But for practical reasons, changing the C API must be done slowly, step by
step. To reduce the maintenance burden in third party C and C+ extensions.

--

IMO it's a bad idea to use "const PyObject*" in the C API. Maybe today, a
function doesn't modify the memory. But maybe tomorrow, the implementation
will deeply change and will need to modify the memory. In C, using "const"
(for something other than const char*) is just not convenient.

Victor

On Tue, May 3, 2022 at 9:22 AM Antoine Pitrou <anto...@python.org> wrote:

> On Mon, 2 May 2022 15:21:24 +0200
> Victor Stinner <vstin...@python.org> wrote:
> >
> > Slowly, more and more tests can be added. For example, this change fixing
> > compiler warnings when passing "const PyObject*" to Python C API
> functions
> > adds tests to _testcppext.cpp:
> > https://github.com/python/cpython/pull/92138
>
> Doesn't passing "const PyObject*" leak implementation details, for
> example that the reference count does not change? It seems to go
> counter the objective of making the C API more abstract and more stable.
>
> (C++ has the "mutable" keyword for such situat but I don't think C has
> it yet)
>
> Regards
>
> Antoine.
>
>
> _______________________________________________
> Python-Dev mailing list -- python-dev@python.org
> To unsubscribe send an email to python-dev-le...@python.org
> https://mail.python.org/mailman3/lists/python-dev.python.org/
> Message archived at
> https://mail.python.org/archives/list/python-dev@python.org/message/YFD3WOOR6PRORTTKLTSSOSDNHRYAA2N7/
> Code of Conduct: http://python.org/psf/codeofconduct/
>


-- 
Night gathers, and now my watch begins. It shall not end until my death.
_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/python-dev@python.org/message/S3B2KMUL4OM5CCLTYCFCR5HFZBBFWVKV/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to