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

Use Python 3.11 _Py_NULL #36

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
36 changes: 19 additions & 17 deletions pythoncapi_compat.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ extern "C" {
#endif


// C++ compatibility
// C++ compatibility: _Py_CAST() and _Py_NULL
#ifndef _Py_CAST
# ifdef __cplusplus
# define _Py_CAST(type, expr) \
Expand All @@ -41,10 +41,12 @@ extern "C" {
# define _Py_CAST(type, expr) ((type)(expr))
# endif
#endif
#ifdef __cplusplus
# define PYCAPI_COMPAT_NULL nullptr
#else
# define PYCAPI_COMPAT_NULL NULL
#ifndef _Py_NULL
# ifdef __cplusplus
# define _Py_NULL nullptr
# else
# define _Py_NULL NULL
# endif
#endif

// Cast argument to PyObject* type.
Expand Down Expand Up @@ -150,8 +152,8 @@ _Py_SET_SIZE(PyVarObject *ob, Py_ssize_t size)
PYCAPI_COMPAT_STATIC_INLINE(PyCodeObject*)
PyFrame_GetCode(PyFrameObject *frame)
{
assert(frame != PYCAPI_COMPAT_NULL);
assert(frame->f_code != PYCAPI_COMPAT_NULL);
assert(frame != _Py_NULL);
assert(frame->f_code != _Py_NULL);
return _Py_CAST(PyCodeObject*, Py_NewRef(frame->f_code));
}
#endif
Expand All @@ -170,7 +172,7 @@ _PyFrame_GetCodeBorrow(PyFrameObject *frame)
PYCAPI_COMPAT_STATIC_INLINE(PyFrameObject*)
PyFrame_GetBack(PyFrameObject *frame)
{
assert(frame != PYCAPI_COMPAT_NULL);
assert(frame != _Py_NULL);
return _Py_CAST(PyFrameObject*, Py_XNewRef(frame->f_back));
}
#endif
Expand Down Expand Up @@ -248,7 +250,7 @@ PyFrame_GetLasti(PyFrameObject *frame)
PYCAPI_COMPAT_STATIC_INLINE(PyInterpreterState *)
PyThreadState_GetInterpreter(PyThreadState *tstate)
{
assert(tstate != PYCAPI_COMPAT_NULL);
assert(tstate != _Py_NULL);
return tstate->interp;
}
#endif
Expand All @@ -259,7 +261,7 @@ PyThreadState_GetInterpreter(PyThreadState *tstate)
PYCAPI_COMPAT_STATIC_INLINE(PyFrameObject*)
PyThreadState_GetFrame(PyThreadState *tstate)
{
assert(tstate != PYCAPI_COMPAT_NULL);
assert(tstate != _Py_NULL);
return _Py_CAST(PyFrameObject *, Py_XNewRef(tstate->frame));
}
#endif
Expand All @@ -284,11 +286,11 @@ PyInterpreterState_Get(void)
PyInterpreterState *interp;

tstate = PyThreadState_GET();
if (tstate == PYCAPI_COMPAT_NULL) {
if (tstate == _Py_NULL) {
Py_FatalError("GIL released (tstate is NULL)");
}
interp = tstate->interp;
if (interp == PYCAPI_COMPAT_NULL) {
if (interp == _Py_NULL) {
Py_FatalError("no current interpreter");
}
return interp;
Expand All @@ -301,7 +303,7 @@ PyInterpreterState_Get(void)
PYCAPI_COMPAT_STATIC_INLINE(uint64_t)
PyThreadState_GetID(PyThreadState *tstate)
{
assert(tstate != PYCAPI_COMPAT_NULL);
assert(tstate != _Py_NULL);
return tstate->id;
}
#endif
Expand All @@ -325,8 +327,8 @@ PyThreadState_EnterTracing(PyThreadState *tstate)
PYCAPI_COMPAT_STATIC_INLINE(void)
PyThreadState_LeaveTracing(PyThreadState *tstate)
{
int use_tracing = (tstate->c_tracefunc != PYCAPI_COMPAT_NULL
|| tstate->c_profilefunc != PYCAPI_COMPAT_NULL);
int use_tracing = (tstate->c_tracefunc != _Py_NULL
|| tstate->c_profilefunc != _Py_NULL);
tstate->tracing--;
#if PY_VERSION_HEX >= 0x030A00A1
tstate->cframe->use_tracing = use_tracing;
Expand Down Expand Up @@ -387,9 +389,9 @@ PyModule_AddType(PyObject *module, PyTypeObject *type)

// inline _PyType_Name()
name = type->tp_name;
assert(name != PYCAPI_COMPAT_NULL);
assert(name != _Py_NULL);
dot = strrchr(name, '.');
if (dot != PYCAPI_COMPAT_NULL) {
if (dot != _Py_NULL) {
name = dot + 1;
}

Expand Down
12 changes: 6 additions & 6 deletions tests/test_pythoncapi_compat_cext.c
Original file line number Diff line number Diff line change
Expand Up @@ -509,17 +509,17 @@ static struct PyMethodDef methods[] = {
static struct PyModuleDef module = {
PyModuleDef_HEAD_INIT,
MODULE_NAME_STR, // m_name
PYCAPI_COMPAT_NULL, // m_doc
_Py_NULL, // m_doc
0, // m_doc
methods, // m_methods
#if PY_VERSION_HEX >= 0x03050000
PYCAPI_COMPAT_NULL, // m_slots
_Py_NULL, // m_slots
#else
PYCAPI_COMPAT_NULL, // m_reload
_Py_NULL, // m_reload
#endif
PYCAPI_COMPAT_NULL, // m_traverse
PYCAPI_COMPAT_NULL, // m_clear
PYCAPI_COMPAT_NULL, // m_free
_Py_NULL, // m_traverse
_Py_NULL, // m_clear
_Py_NULL, // m_free
};


Expand Down