Skip to content

Commit 90d26c5

Browse files
authored
Use Python 3.11 _Py_NULL (#36)
1 parent bed155e commit 90d26c5

File tree

2 files changed

+25
-23
lines changed

2 files changed

+25
-23
lines changed

pythoncapi_compat.h

+19-17
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ extern "C" {
3232
#endif
3333

3434

35-
// C++ compatibility
35+
// C++ compatibility: _Py_CAST() and _Py_NULL
3636
#ifndef _Py_CAST
3737
# ifdef __cplusplus
3838
# define _Py_CAST(type, expr) \
@@ -41,10 +41,12 @@ extern "C" {
4141
# define _Py_CAST(type, expr) ((type)(expr))
4242
# endif
4343
#endif
44-
#ifdef __cplusplus
45-
# define PYCAPI_COMPAT_NULL nullptr
46-
#else
47-
# define PYCAPI_COMPAT_NULL NULL
44+
#ifndef _Py_NULL
45+
# ifdef __cplusplus
46+
# define _Py_NULL nullptr
47+
# else
48+
# define _Py_NULL NULL
49+
# endif
4850
#endif
4951

5052
// Cast argument to PyObject* type.
@@ -150,8 +152,8 @@ _Py_SET_SIZE(PyVarObject *ob, Py_ssize_t size)
150152
PYCAPI_COMPAT_STATIC_INLINE(PyCodeObject*)
151153
PyFrame_GetCode(PyFrameObject *frame)
152154
{
153-
assert(frame != PYCAPI_COMPAT_NULL);
154-
assert(frame->f_code != PYCAPI_COMPAT_NULL);
155+
assert(frame != _Py_NULL);
156+
assert(frame->f_code != _Py_NULL);
155157
return _Py_CAST(PyCodeObject*, Py_NewRef(frame->f_code));
156158
}
157159
#endif
@@ -170,7 +172,7 @@ _PyFrame_GetCodeBorrow(PyFrameObject *frame)
170172
PYCAPI_COMPAT_STATIC_INLINE(PyFrameObject*)
171173
PyFrame_GetBack(PyFrameObject *frame)
172174
{
173-
assert(frame != PYCAPI_COMPAT_NULL);
175+
assert(frame != _Py_NULL);
174176
return _Py_CAST(PyFrameObject*, Py_XNewRef(frame->f_back));
175177
}
176178
#endif
@@ -248,7 +250,7 @@ PyFrame_GetLasti(PyFrameObject *frame)
248250
PYCAPI_COMPAT_STATIC_INLINE(PyInterpreterState *)
249251
PyThreadState_GetInterpreter(PyThreadState *tstate)
250252
{
251-
assert(tstate != PYCAPI_COMPAT_NULL);
253+
assert(tstate != _Py_NULL);
252254
return tstate->interp;
253255
}
254256
#endif
@@ -259,7 +261,7 @@ PyThreadState_GetInterpreter(PyThreadState *tstate)
259261
PYCAPI_COMPAT_STATIC_INLINE(PyFrameObject*)
260262
PyThreadState_GetFrame(PyThreadState *tstate)
261263
{
262-
assert(tstate != PYCAPI_COMPAT_NULL);
264+
assert(tstate != _Py_NULL);
263265
return _Py_CAST(PyFrameObject *, Py_XNewRef(tstate->frame));
264266
}
265267
#endif
@@ -284,11 +286,11 @@ PyInterpreterState_Get(void)
284286
PyInterpreterState *interp;
285287

286288
tstate = PyThreadState_GET();
287-
if (tstate == PYCAPI_COMPAT_NULL) {
289+
if (tstate == _Py_NULL) {
288290
Py_FatalError("GIL released (tstate is NULL)");
289291
}
290292
interp = tstate->interp;
291-
if (interp == PYCAPI_COMPAT_NULL) {
293+
if (interp == _Py_NULL) {
292294
Py_FatalError("no current interpreter");
293295
}
294296
return interp;
@@ -301,7 +303,7 @@ PyInterpreterState_Get(void)
301303
PYCAPI_COMPAT_STATIC_INLINE(uint64_t)
302304
PyThreadState_GetID(PyThreadState *tstate)
303305
{
304-
assert(tstate != PYCAPI_COMPAT_NULL);
306+
assert(tstate != _Py_NULL);
305307
return tstate->id;
306308
}
307309
#endif
@@ -325,8 +327,8 @@ PyThreadState_EnterTracing(PyThreadState *tstate)
325327
PYCAPI_COMPAT_STATIC_INLINE(void)
326328
PyThreadState_LeaveTracing(PyThreadState *tstate)
327329
{
328-
int use_tracing = (tstate->c_tracefunc != PYCAPI_COMPAT_NULL
329-
|| tstate->c_profilefunc != PYCAPI_COMPAT_NULL);
330+
int use_tracing = (tstate->c_tracefunc != _Py_NULL
331+
|| tstate->c_profilefunc != _Py_NULL);
330332
tstate->tracing--;
331333
#if PY_VERSION_HEX >= 0x030A00A1
332334
tstate->cframe->use_tracing = use_tracing;
@@ -387,9 +389,9 @@ PyModule_AddType(PyObject *module, PyTypeObject *type)
387389

388390
// inline _PyType_Name()
389391
name = type->tp_name;
390-
assert(name != PYCAPI_COMPAT_NULL);
392+
assert(name != _Py_NULL);
391393
dot = strrchr(name, '.');
392-
if (dot != PYCAPI_COMPAT_NULL) {
394+
if (dot != _Py_NULL) {
393395
name = dot + 1;
394396
}
395397

tests/test_pythoncapi_compat_cext.c

+6-6
Original file line numberDiff line numberDiff line change
@@ -509,17 +509,17 @@ static struct PyMethodDef methods[] = {
509509
static struct PyModuleDef module = {
510510
PyModuleDef_HEAD_INIT,
511511
MODULE_NAME_STR, // m_name
512-
PYCAPI_COMPAT_NULL, // m_doc
512+
_Py_NULL, // m_doc
513513
0, // m_doc
514514
methods, // m_methods
515515
#if PY_VERSION_HEX >= 0x03050000
516-
PYCAPI_COMPAT_NULL, // m_slots
516+
_Py_NULL, // m_slots
517517
#else
518-
PYCAPI_COMPAT_NULL, // m_reload
518+
_Py_NULL, // m_reload
519519
#endif
520-
PYCAPI_COMPAT_NULL, // m_traverse
521-
PYCAPI_COMPAT_NULL, // m_clear
522-
PYCAPI_COMPAT_NULL, // m_free
520+
_Py_NULL, // m_traverse
521+
_Py_NULL, // m_clear
522+
_Py_NULL, // m_free
523523
};
524524

525525

0 commit comments

Comments
 (0)