FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Remove Borrow functions by vstinner · Pull Request #202 · python/pythoncapi-compat · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (1) .h  (1) .rst  (2) All 3 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
40 changes: 0 additions & 40 deletions docs/api.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -1129,43 +1129,3 @@ Python 3.0

There are too many changes compared to Python 2.7, but it's no longer useful
to document them.


Borrow variant
--------------

To ease migration of C extensions to the new C API, a variant is provided
to return borrowed references rather than strong references.

These functions are only available in ``pythoncapi_compat.h`` and are not
part of the Python C API.

.. c:function:: PyObject* _Py_StealRef(PyObject *ob)

Similar to ``Py_DECREF(ob); return ob;``.

.. c:function:: PyObject* _Py_XStealRef(PyObject *ob)

Similar to ``Py_XDECREF(ob); return ob;``.

.. c:function:: PyFrameObject* _PyThreadState_GetFrameBorrow(PyThreadState *tstate)

:c:func:`PyThreadState_GetFrame` variant.

Not available on PyPy nor with the limited C API 3.8 and older.

Not available on limited C API 3.8 and older.

.. c:function:: PyCodeObject* _PyFrame_GetCodeBorrow(PyFrameObject *frame)

:c:func:`PyFrame_GetCode` variant.

.. c:function:: PyFrameObject* _PyFrame_GetBackBorrow(PyFrameObject *frame)

:c:func:`PyFrame_GetBack` variant.

Not available on PyPy nor with the limited C API.

For example, ``tstate->frame`` can be replaced with
``_PyThreadState_GetFrameBorrow(tstate)`` to avoid accessing directly
``PyThreadState.frame`` member.
5 changes: 5 additions & 0 deletions docs/changelog.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ Changelog
* ``PyEval_GetFrameGlobals()``
* ``PyEval_GetFrameLocals()``

* 2026-10-12: Delete 5 ``Borrow`` functions which were specific to
pythoncapi-compat and only used by the removed script
``upgrade_pythoncapi.py``: ``_Py_StealRef()`` ``_Py_XStealRef()``
``_PyThreadState_GetFrameBorrow()`` ``_PyFrame_GetCodeBorrow()``
and ``_PyFrame_GetBackBorrow()``.
* 2026-10-11: Remove the ``upgrade_pythoncapi.py`` script.
* 2026-10-10: Add ``PyObject_GenericHash()`` and
``PyUnicodeWriter_WriteUCS4()`` functions. runtests.py now runs tests in
Expand Down
29 changes: 0 additions & 29 deletions pythoncapi_compat.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -202,15 +202,6 @@ static inline PyCodeObject* PyFrame_GetCode(PyFrameObject *frame)
}
#endif

#if !defined(Py_LIMITED_API) || PY_VERSION_HEX >= _Py_PACK_VERSION(3, 9)
static inline PyCodeObject* _PyFrame_GetCodeBorrow(PyFrameObject *frame)
{
PyCodeObject *code = PyFrame_GetCode(frame);
Py_DECREF(code);
return code;
}
#endif


// bpo-40421 added PyFrame_GetBack() to Python 3.9
#if (PY_VERSION_HEX < _Py_PACK_VERSION(3, 9) \
Expand All @@ -222,15 +213,6 @@ static inline PyFrameObject* PyFrame_GetBack(PyFrameObject *frame)
}
#endif

#if !defined(PYPY_VERSION) && !defined(Py_LIMITED_API)
static inline PyFrameObject* _PyFrame_GetBackBorrow(PyFrameObject *frame)
{
PyFrameObject *back = PyFrame_GetBack(frame);
Py_XDECREF(back);
return back;
}
#endif


// bpo-40421 added PyFrame_GetLocals() to Python 3.11
#if (PY_VERSION_HEX < _Py_PACK_VERSION(3, 11) \
Expand Down Expand Up @@ -353,17 +335,6 @@ static inline PyFrameObject* PyThreadState_GetFrame(PyThreadState *tstate)
}
#endif

#if ((!defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= _Py_PACK_VERSION(3, 9)) \
&& (!defined(PYPY_VERSION) || PY_VERSION_HEX >= _Py_PACK_VERSION(3, 11)))
static inline PyFrameObject*
_PyThreadState_GetFrameBorrow(PyThreadState *tstate)
{
PyFrameObject *frame = PyThreadState_GetFrame(tstate);
Py_XDECREF(_PyObject_CAST(frame));
return frame;
}
#endif


// Added to Python 3.9 and limited C API 3.9. On Windows, it's not available
// before stable ABI 3.10.
Expand Down
26 changes: 1 addition & 25 deletions tests/test_pythoncapi_compat_cext.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -208,12 +208,6 @@ test_frame(PyObject *Py_UNUSED(module), PyObject* Py_UNUSED(ignored))
return _Py_NULL;
}

// test _PyThreadState_GetFrameBorrow()
Py_ssize_t frame_refcnt = Py_REFCNT(_PyObject_CAST(frame));
PyFrameObject *frame2 = _PyThreadState_GetFrameBorrow(tstate);
assert(frame2 == frame);
assert(Py_REFCNT(_PyObject_CAST(frame)) == frame_refcnt);

// Test issue: the current thread has no code on PyPy
#ifndef PYPY_VERSION
// test PyFrame_GetCode()
Expand All @@ -223,34 +217,16 @@ test_frame(PyObject *Py_UNUSED(module), PyObject* Py_UNUSED(ignored))
assert(PyCode_Check(code));
#endif

// test _PyFrame_GetCodeBorrow()
Py_ssize_t code_refcnt = Py_REFCNT(_PyObject_CAST(code));
PyCodeObject *code2 = _PyFrame_GetCodeBorrow(frame);
assert(code2 == code);
assert(Py_REFCNT(_PyObject_CAST(code)) == code_refcnt);
Py_DECREF(code);
#endif

#if !defined(PYPY_VERSION) && !defined(Py_LIMITED_API)
// PyFrame_GetBack()
PyFrameObject* back = PyFrame_GetBack(frame);
if (back != _Py_NULL) {
assert(PyFrame_Check(back));
Py_DECREF(back);
}

// test _PyFrame_GetBackBorrow()
if (back != _Py_NULL) {
Py_ssize_t back_refcnt = Py_REFCNT(back);
PyFrameObject *back2 = _PyFrame_GetBackBorrow(frame);
assert(back2 == back);
assert(Py_REFCNT(back) == back_refcnt);
}
else {
PyFrameObject *back2 = _PyFrame_GetBackBorrow(frame);
assert(back2 == back);
}
Py_XDECREF(back);

// test PyFrame_GetLocals()
PyObject *locals = PyFrame_GetLocals(frame);
assert(locals != _Py_NULL);
Expand Down
Loading

Back | FazBrowse Home | New Git URL