python / pythoncapi-compat

The pythoncapi-compat project can be used to write a C extension supporting a wide range of Python versions with a single code base.
https://pythoncapi-compat.readthedocs.io/
BSD Zero Clause License
80 stars 23 forks source link

Question re PyObject_Del() being converted to PyObject_Free() #103

Closed lazka closed 3 months ago

lazka commented 3 months ago

The upgrade script replaces the following call to PyObject_Del() with PyObject_Free() here: https://github.com/pygobject/pycairo/blob/ce6118526606f93d3132f881bf548ce746b782fe/cairo/path.c#L242 and it seems deliberate (??)

PyObject_Del() is documented to be used for PyObject_New() or PyObject_NewVar(), while PyObject_Free() is documented to be used for PyObject_Malloc(), PyObject_Realloc() or PyObject_Calloc(), otherwise it is undefined behavior.

I'm a bit confused by this.

vstinner commented 3 months ago

PyObject_Del() is just a deprecated alias to PyObject_Free():

#define PyObject_Del PyObject_Free

src: https://github.com/python/cpython/blob/3b034d26eb8480f8d12ae11f42d038d24cf8498a/Include/objimpl.h#L107

I agree that it's confusing :-)

lazka commented 3 months ago

I see, so it's just outdated documentation.

Should I move this to CPython?

lazka commented 3 months ago

Hm, PyObject_Del is used all over the place in the docs, and in CPython source, with no indication that it is deprecated besides that one inline comment. So I'm wondering if it really is deprecated at all?

vstinner commented 3 months ago

It's just deprecated in a comment, nothing more. Maybe the upgrade script should leave them unchanged.

vstinner commented 3 months ago

I wrote https://github.com/python/cpython/pull/122453 for Python.

lazka commented 3 months ago

Thanks!