Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bpo-42262: Py_NewRef() casts its argument to PyObject* #23626

Merged
merged 1 commit into from
Dec 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions Include/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,6 @@ static inline void _Py_INCREF(PyObject *op)
#endif
op->ob_refcnt++;
}

#define Py_INCREF(op) _Py_INCREF(_PyObject_CAST(op))

static inline void _Py_DECREF(
Expand All @@ -449,7 +448,6 @@ static inline void _Py_DECREF(
_Py_Dealloc(op);
}
}

#ifdef Py_REF_DEBUG
# define Py_DECREF(op) _Py_DECREF(__FILE__, __LINE__, _PyObject_CAST(op))
#else
Expand Down Expand Up @@ -548,8 +546,8 @@ static inline PyObject* _Py_XNewRef(PyObject *obj)
// Py_NewRef() and Py_XNewRef() are exported as functions for the stable ABI.
// Names overriden with macros by static inline functions for best
// performances.
#define Py_NewRef(obj) _Py_NewRef(obj)
#define Py_XNewRef(obj) _Py_XNewRef(obj)
#define Py_NewRef(obj) _Py_NewRef(_PyObject_CAST(obj))
#define Py_XNewRef(obj) _Py_XNewRef(_PyObject_CAST(obj))


/*
Expand Down
32 changes: 31 additions & 1 deletion Modules/_testcapimodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -5614,7 +5614,7 @@ static PyObject *test_buildvalue_issue38913(PyObject *, PyObject *);


static PyObject*
test_set_type_size(PyObject* self, PyObject* ignored)
test_set_type_size(PyObject *self, PyObject *Py_UNUSED(ignored))
{
PyObject *obj = PyList_New(0);
if (obj == NULL) {
Expand All @@ -5636,6 +5636,35 @@ test_set_type_size(PyObject* self, PyObject* ignored)
}


// Test Py_NewRef() and Py_XNewRef() functions
static PyObject*
test_refcount(PyObject *self, PyObject *Py_UNUSED(ignored))
{
PyObject *obj = PyList_New(0);
if (obj == NULL) {
return NULL;
}
assert(Py_REFCNT(obj) == 1);

// Test Py_NewRef()
PyObject *ref = Py_NewRef(obj);
assert(ref == obj);
assert(Py_REFCNT(obj) == 2);
Py_DECREF(ref);

// Test Py_XNewRef()
PyObject *xref = Py_XNewRef(obj);
assert(xref == obj);
assert(Py_REFCNT(obj) == 2);
Py_DECREF(xref);

assert(Py_XNewRef(NULL) == NULL);

Py_DECREF(obj);
Py_RETURN_NONE;
}


static PyMethodDef TestMethods[] = {
{"raise_exception", raise_exception, METH_VARARGS},
{"raise_memoryerror", raise_memoryerror, METH_NOARGS},
Expand Down Expand Up @@ -5908,6 +5937,7 @@ static PyMethodDef TestMethods[] = {
{"pynumber_tobase", pynumber_tobase, METH_VARARGS},
{"without_gc", without_gc, METH_O},
{"test_set_type_size", test_set_type_size, METH_NOARGS},
{"test_refcount", test_refcount, METH_NOARGS},
{NULL, NULL} /* sentinel */
};

Expand Down