Skip to content

Conversation

@vstinner
Copy link
Member

@vstinner vstinner commented Dec 1, 2020

Added _Py_Borrow() and _Py_XBorrow() functions to borrow a reference
to an object: decrement the object reference count and return the
object.
:c:func:`Py_NewRef` function can be used to create a new :term:`strong
reference`.
In other terms, the :c:func:`_Py_Borrow` function should be avoided whenever

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering if the documentation should explain very briefly why borrowing references is bad? The documentation shouldn't go on a long rant, but at the moment the reader might be left wondering why _Py_Borrow should be avoided.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The "term" syntax creates a link to: https://docs.python.org/dev/glossary.html#term-borrowed-reference which is where I explained in which cases borrowed references are unsafe.

I also wrote https://docs.python.org/dev/glossary.html#term-strong-reference which describes reference counting.

@vstinner
Copy link
Member Author

vstinner commented Dec 1, 2020

@hodgestar: Would you mind to review the updated PR?



static PyObject*
test_refcount(PyObject* self, PyObject* ignored)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: PyObject *self, PyObject *ignored

Copy link
Member

@shihai1991 shihai1991 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR LGTM. I just to say pls take careful when using this function :)

Py_INCREF(obj);
PyObject *xborrowed = _Py_XBorrow(obj);
assert(xborrowed == obj);
assert(Py_REFCNT(obj) == 1);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we perhaps also test that Py_XNewRef(NULL) and _Py_XBorrow(NULL) behave as expected?

Copy link

@hodgestar hodgestar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left a small suggestion for strengthening the test, but +1 on these changes.

Looking forward to a borrowed-reference-free C API in 3.10! :D

@vstinner
Copy link
Member Author

vstinner commented Dec 1, 2020

https://bugs.python.org/issue42522#msg382270:

I find it confusing that the function is called Py_Borrow() but steals the reference.

Should I rename _Py_Borrow() to _Py_StealRef()?

@hodgestar
Copy link

hodgestar commented Dec 1, 2020

Should I rename _Py_Borrow() to _Py_StealRef()?

I see why the commenter suggested Py_StealRef, but I'm in favour of keeping the name Py_Borrow.

The function itself doesn't so much steals a reference as throw it away (a typical stolen reference would only be DECREFed much later). The effect for the caller is that the reference is now borrowed and since the only intended use case for this function is to turn a reference into a borrowed reference, the current name seems good to me.

@vstinner
Copy link
Member Author

vstinner commented Dec 1, 2020

https://bugs.python.org/issue42522#msg382284

@vstinner vstinner closed this Dec 1, 2020
@vstinner vstinner deleted the py_borrow branch December 1, 2020 23:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants