Skip to content

Use Python 3.11 _Py_CAST() macro #35

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

Merged
merged 1 commit into from
May 3, 2022
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
59 changes: 22 additions & 37 deletions pythoncapi_compat.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,23 @@ extern "C" {


// C++ compatibility
#ifndef _Py_CAST
# ifdef __cplusplus
# define _Py_CAST(type, expr) \
const_cast<type>(reinterpret_cast<const type>(expr))
# else
# define _Py_CAST(type, expr) ((type)(expr))
# endif
#endif
#ifdef __cplusplus
# define PYCAPI_COMPAT_CAST(TYPE, EXPR) reinterpret_cast<TYPE>(EXPR)
# define PYCAPI_COMPAT_NULL nullptr
#else
# define PYCAPI_COMPAT_CAST(TYPE, EXPR) ((TYPE)(EXPR))
# define PYCAPI_COMPAT_NULL NULL
#endif

// Cast argument to PyObject* type.
#ifndef _PyObject_CAST
# define _PyObject_CAST(op) PYCAPI_COMPAT_CAST(PyObject*, op)
# define _PyObject_CAST(op) _Py_CAST(PyObject*, op)
#endif


Expand Down Expand Up @@ -71,30 +77,6 @@ _Py_XNewRef(PyObject *obj)
#endif


// See https://bugs.python.org/issue42522
#if !defined(_Py_StealRef)
PYCAPI_COMPAT_STATIC_INLINE(PyObject*)
__Py_StealRef(PyObject *obj)
{
Py_DECREF(obj);
return obj;
}
#define _Py_StealRef(obj) __Py_StealRef(_PyObject_CAST(obj))
#endif


// See https://bugs.python.org/issue42522
#if !defined(_Py_XStealRef)
PYCAPI_COMPAT_STATIC_INLINE(PyObject*)
__Py_XStealRef(PyObject *obj)
{
Py_XDECREF(obj);
return obj;
}
#define _Py_XStealRef(obj) __Py_XStealRef(_PyObject_CAST(obj))
#endif


// bpo-39573 added Py_SET_REFCNT() to Python 3.9.0a4
#if PY_VERSION_HEX < 0x030900A4 && !defined(Py_SET_REFCNT)
PYCAPI_COMPAT_STATIC_INLINE(void)
Expand Down Expand Up @@ -170,15 +152,16 @@ PyFrame_GetCode(PyFrameObject *frame)
{
assert(frame != PYCAPI_COMPAT_NULL);
assert(frame->f_code != PYCAPI_COMPAT_NULL);
return PYCAPI_COMPAT_CAST(PyCodeObject*, Py_NewRef(frame->f_code));
return _Py_CAST(PyCodeObject*, Py_NewRef(frame->f_code));
}
#endif

PYCAPI_COMPAT_STATIC_INLINE(PyCodeObject*)
_PyFrame_GetCodeBorrow(PyFrameObject *frame)
{
return PYCAPI_COMPAT_CAST(PyCodeObject *,
_Py_StealRef(PyFrame_GetCode(frame)));
PyCodeObject *code = PyFrame_GetCode(frame);
Py_DECREF(code);
return code;
}


Expand All @@ -188,16 +171,17 @@ PYCAPI_COMPAT_STATIC_INLINE(PyFrameObject*)
PyFrame_GetBack(PyFrameObject *frame)
{
assert(frame != PYCAPI_COMPAT_NULL);
return PYCAPI_COMPAT_CAST(PyFrameObject*, Py_XNewRef(frame->f_back));
return _Py_CAST(PyFrameObject*, Py_XNewRef(frame->f_back));
}
#endif

#if !defined(PYPY_VERSION)
PYCAPI_COMPAT_STATIC_INLINE(PyFrameObject*)
_PyFrame_GetBackBorrow(PyFrameObject *frame)
{
return PYCAPI_COMPAT_CAST(PyFrameObject *,
_Py_XStealRef(PyFrame_GetBack(frame)));
PyFrameObject *back = PyFrame_GetBack(frame);
Py_XDECREF(back);
return back;
}
#endif

Expand Down Expand Up @@ -276,16 +260,17 @@ PYCAPI_COMPAT_STATIC_INLINE(PyFrameObject*)
PyThreadState_GetFrame(PyThreadState *tstate)
{
assert(tstate != PYCAPI_COMPAT_NULL);
return PYCAPI_COMPAT_CAST(PyFrameObject *, Py_XNewRef(tstate->frame));
return _Py_CAST(PyFrameObject *, Py_XNewRef(tstate->frame));
}
#endif

#if !defined(PYPY_VERSION)
PYCAPI_COMPAT_STATIC_INLINE(PyFrameObject*)
_PyThreadState_GetFrameBorrow(PyThreadState *tstate)
{
return PYCAPI_COMPAT_CAST(PyFrameObject*,
_Py_XStealRef(PyThreadState_GetFrame(tstate)));
PyFrameObject *frame = PyThreadState_GetFrame(tstate);
Py_XDECREF(frame);
return frame;
}
#endif

Expand Down Expand Up @@ -429,7 +414,7 @@ PyObject_GC_IsTracked(PyObject* obj)
PYCAPI_COMPAT_STATIC_INLINE(int)
PyObject_GC_IsFinalized(PyObject *obj)
{
PyGC_Head *gc = PYCAPI_COMPAT_CAST(PyGC_Head *, obj) - 1;
PyGC_Head *gc = _Py_CAST(PyGC_Head*, obj) - 1;
return (PyObject_IS_GC(obj) && _PyGCHead_FINALIZED(gc));
}
#endif
Expand Down
30 changes: 0 additions & 30 deletions tests/test_pythoncapi_compat_cext.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,35 +130,6 @@ test_py_is(PyObject *Py_UNUSED(module), PyObject* Py_UNUSED(ignored))
}


static PyObject *
test_steal_ref(PyObject *Py_UNUSED(module), PyObject* Py_UNUSED(ignored))
{
PyObject *obj = PyList_New(0);
if (obj == NULL) {
return NULL;
}
Py_ssize_t refcnt = Py_REFCNT(obj);

// _Py_StealRef()
Py_INCREF(obj);
PyObject *ref = _Py_StealRef(obj);
assert(ref == obj);
assert(Py_REFCNT(obj) == refcnt);

// _Py_XStealRef()
Py_INCREF(obj);
PyObject *xref = _Py_XStealRef(obj);
assert(xref == obj);
assert(Py_REFCNT(obj) == refcnt);

assert(_Py_XStealRef(NULL) == NULL);

assert(Py_REFCNT(obj) == refcnt);
Py_DECREF(obj);
Py_RETURN_NONE;
}


#if !defined(PYPY_VERSION)
static PyObject *
test_frame(PyObject *Py_UNUSED(module), PyObject* Py_UNUSED(ignored))
Expand Down Expand Up @@ -516,7 +487,6 @@ test_code(PyObject *Py_UNUSED(module), PyObject* Py_UNUSED(ignored))
static struct PyMethodDef methods[] = {
{"test_object", test_object, METH_NOARGS, NULL},
{"test_py_is", test_py_is, METH_NOARGS, NULL},
{"test_steal_ref", test_steal_ref, METH_NOARGS, NULL},
#if !defined(PYPY_VERSION)
{"test_frame", test_frame, METH_NOARGS, NULL},
#endif
Expand Down