Skip to content

Commit 8b6c4a9

Browse files
authored
bpo-42262: Py_NewRef() casts its argument to PyObject* (GH-23626)
Write also unit tests on Py_NewRef() and Py_XNewRef().
1 parent 7e5e13d commit 8b6c4a9

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

Include/object.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,6 @@ static inline void _Py_INCREF(PyObject *op)
426426
#endif
427427
op->ob_refcnt++;
428428
}
429-
430429
#define Py_INCREF(op) _Py_INCREF(_PyObject_CAST(op))
431430

432431
static inline void _Py_DECREF(
@@ -449,7 +448,6 @@ static inline void _Py_DECREF(
449448
_Py_Dealloc(op);
450449
}
451450
}
452-
453451
#ifdef Py_REF_DEBUG
454452
# define Py_DECREF(op) _Py_DECREF(__FILE__, __LINE__, _PyObject_CAST(op))
455453
#else
@@ -548,8 +546,8 @@ static inline PyObject* _Py_XNewRef(PyObject *obj)
548546
// Py_NewRef() and Py_XNewRef() are exported as functions for the stable ABI.
549547
// Names overriden with macros by static inline functions for best
550548
// performances.
551-
#define Py_NewRef(obj) _Py_NewRef(obj)
552-
#define Py_XNewRef(obj) _Py_XNewRef(obj)
549+
#define Py_NewRef(obj) _Py_NewRef(_PyObject_CAST(obj))
550+
#define Py_XNewRef(obj) _Py_XNewRef(_PyObject_CAST(obj))
553551

554552

555553
/*

Modules/_testcapimodule.c

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5614,7 +5614,7 @@ static PyObject *test_buildvalue_issue38913(PyObject *, PyObject *);
56145614

56155615

56165616
static PyObject*
5617-
test_set_type_size(PyObject* self, PyObject* ignored)
5617+
test_set_type_size(PyObject *self, PyObject *Py_UNUSED(ignored))
56185618
{
56195619
PyObject *obj = PyList_New(0);
56205620
if (obj == NULL) {
@@ -5636,6 +5636,35 @@ test_set_type_size(PyObject* self, PyObject* ignored)
56365636
}
56375637

56385638

5639+
// Test Py_NewRef() and Py_XNewRef() functions
5640+
static PyObject*
5641+
test_refcount(PyObject *self, PyObject *Py_UNUSED(ignored))
5642+
{
5643+
PyObject *obj = PyList_New(0);
5644+
if (obj == NULL) {
5645+
return NULL;
5646+
}
5647+
assert(Py_REFCNT(obj) == 1);
5648+
5649+
// Test Py_NewRef()
5650+
PyObject *ref = Py_NewRef(obj);
5651+
assert(ref == obj);
5652+
assert(Py_REFCNT(obj) == 2);
5653+
Py_DECREF(ref);
5654+
5655+
// Test Py_XNewRef()
5656+
PyObject *xref = Py_XNewRef(obj);
5657+
assert(xref == obj);
5658+
assert(Py_REFCNT(obj) == 2);
5659+
Py_DECREF(xref);
5660+
5661+
assert(Py_XNewRef(NULL) == NULL);
5662+
5663+
Py_DECREF(obj);
5664+
Py_RETURN_NONE;
5665+
}
5666+
5667+
56395668
static PyMethodDef TestMethods[] = {
56405669
{"raise_exception", raise_exception, METH_VARARGS},
56415670
{"raise_memoryerror", raise_memoryerror, METH_NOARGS},
@@ -5908,6 +5937,7 @@ static PyMethodDef TestMethods[] = {
59085937
{"pynumber_tobase", pynumber_tobase, METH_VARARGS},
59095938
{"without_gc", without_gc, METH_O},
59105939
{"test_set_type_size", test_set_type_size, METH_NOARGS},
5940+
{"test_refcount", test_refcount, METH_NOARGS},
59115941
{NULL, NULL} /* sentinel */
59125942
};
59135943

0 commit comments

Comments
 (0)