Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 73220fb

Browse files
committed
Add _Py_Borrow() and _Py_XBorrow()
1 parent b30738c commit 73220fb

3 files changed

Lines changed: 28 additions & 0 deletions

File tree

README.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ Python 3.10
3838

3939
PyObject* Py_NewRef(PyObject *obj);
4040
PyObject* Py_XNewRef(PyObject *obj);
41+
PyObject* Py_Borrow(PyObject *obj);
42+
PyObject* Py_XBorrow(PyObject *obj);
4143

4244
Python 3.9
4345
----------

pythoncapi_compat.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,20 @@ static inline PyObject* Py_XNewRef(PyObject *obj)
3232
#endif
3333

3434

35+
// bpo-42522
36+
static inline PyObject* _Py_Borrow(PyObject *obj)
37+
{
38+
Py_DECREF(obj);
39+
return obj;
40+
}
41+
42+
static inline PyObject* _Py_XBorrow(PyObject *obj)
43+
{
44+
Py_XDECREF(obj);
45+
return obj;
46+
}
47+
48+
3549
// bpo-39573: Py_TYPE(), Py_REFCNT() and Py_SIZE() can no longer be used
3650
// as l-value in Python 3.10.
3751
#if PY_VERSION_HEX < 0x030900A4

tests/test_pythoncapi_compat.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,18 @@ test_object(PyObject *self, PyObject *ignored)
3939
int is_type = Py_IS_TYPE(obj, Py_TYPE(obj));
4040
assert(is_type);
4141

42+
// test _Py_Borrow()
43+
Py_INCREF(obj);
44+
PyObject *borrowed = _Py_Borrow(obj);
45+
assert(borrowed == obj);
46+
assert(Py_REFCNT(obj) == refcnt);
47+
48+
// test _Py_XBorrow()
49+
Py_INCREF(obj);
50+
PyObject *xborrowed = _Py_XBorrow(obj);
51+
assert(xborrowed == obj);
52+
assert(Py_REFCNT(obj) == refcnt);
53+
4254
Py_DECREF(obj);
4355
Py_RETURN_NONE;
4456
}

0 commit comments

Comments
 (0)