Should getter function return a strong reference?
![](https://secure.gravatar.com/avatar/15b1cd41a4c23e7dc10893777afb4281.jpg?s=120&d=mm&r=g)
Hi,
I added PyThreadState_GetFrame() and PyFrame_GetCode() functions to Python 3.9 (as part of my work to make structures opaque). These functions are basically getter to access PyThreadState.frame and PyFrameObject.f_code members.
Example:
struct _frame* PyThreadState_GetFrame(PyThreadState *tstate) { assert(tstate != NULL); return tstate->frame; }
These functions currently return *borrowed* references.
I understood that borrowed references makes it harder to write an efficient implementation of Python: https://github.com/vstinner/misc/blob/master/cpython/pep-opaque-c-api.rst#bo...
For example, PyPy has to convert all items of a list to PyObject when a C extension module calls PyList_GetItem().
Question: should I modify PyThreadState_GetFrame() and PyFrame_GetCode() functions to return a *strong* reference instead?
Victor
Night gathers, and now my watch begins. It shall not end until my death.
![](https://secure.gravatar.com/avatar/e8600d16ba667cc8d7f00ddc9f254340.jpg?s=120&d=mm&r=g)
On Tue, Apr 28, 2020 at 10:06 AM Victor Stinner <vstinner@python.org> wrote:
Hi,
I added PyThreadState_GetFrame() and PyFrame_GetCode() functions to Python 3.9 (as part of my work to make structures opaque). These functions are basically getter to access PyThreadState.frame and PyFrameObject.f_code members.
Example:
struct _frame* PyThreadState_GetFrame(PyThreadState *tstate) { assert(tstate != NULL); return tstate->frame; }
These functions currently return *borrowed* references.
I understood that borrowed references makes it harder to write an efficient implementation of Python:
https://github.com/vstinner/misc/blob/master/cpython/pep-opaque-c-api.rst#bo...
For example, PyPy has to convert all items of a list to PyObject when a C extension module calls PyList_GetItem().
Question: should I modify PyThreadState_GetFrame() and PyFrame_GetCode() functions to return a *strong* reference instead?
Yes, borrowed references are bad.
-Brett
Victor
Night gathers, and now my watch begins. It shall not end until my death.
capi-sig mailing list -- capi-sig@python.org To unsubscribe send an email to capi-sig-leave@python.org https://mail.python.org/mailman3/lists/capi-sig.python.org/
![](https://secure.gravatar.com/avatar/15b1cd41a4c23e7dc10893777afb4281.jpg?s=120&d=mm&r=g)
Le mar. 28 avr. 2020 à 20:44, Brett Cannon <brett@python.org> a écrit :
Yes, borrowed references are bad.
Ok, thanks for the confirmation. I created https://bugs.python.org/issue40429 to fix the newly added functions.
Victor
Night gathers, and now my watch begins. It shall not end until my death.
participants (2)
-
Brett Cannon
-
Victor Stinner