@@ -36,8 +36,8 @@ static PyObject *
36
36
test_object (PyObject * Py_UNUSED (module ), PyObject * Py_UNUSED (ignored ))
37
37
{
38
38
PyObject * obj = PyList_New (0 );
39
- if (obj == NULL ) {
40
- return NULL ;
39
+ if (obj == _Py_NULL ) {
40
+ return _Py_NULL ;
41
41
}
42
42
Py_ssize_t refcnt = Py_REFCNT (obj );
43
43
@@ -53,7 +53,7 @@ test_object(PyObject *Py_UNUSED(module), PyObject* Py_UNUSED(ignored))
53
53
assert (Py_REFCNT (obj ) == (refcnt + 1 ));
54
54
Py_DECREF (xref );
55
55
56
- assert (Py_XNewRef (NULL ) == NULL );
56
+ assert (Py_XNewRef (_Py_NULL ) == _Py_NULL );
57
57
58
58
// Py_SETREF()
59
59
PyObject * setref = Py_NewRef (obj );
@@ -65,20 +65,20 @@ test_object(PyObject *Py_UNUSED(module), PyObject* Py_UNUSED(ignored))
65
65
assert (Py_REFCNT (obj ) == refcnt );
66
66
Py_INCREF (setref );
67
67
68
- Py_SETREF (setref , NULL );
69
- assert (setref == NULL );
68
+ Py_SETREF (setref , _Py_NULL );
69
+ assert (setref == _Py_NULL );
70
70
71
71
// Py_XSETREF()
72
- PyObject * xsetref = NULL ;
72
+ PyObject * xsetref = _Py_NULL ;
73
73
74
74
Py_INCREF (obj );
75
75
assert (Py_REFCNT (obj ) == (refcnt + 1 ));
76
76
Py_XSETREF (xsetref , obj );
77
77
assert (xsetref == obj );
78
78
79
- Py_XSETREF (xsetref , NULL );
79
+ Py_XSETREF (xsetref , _Py_NULL );
80
80
assert (Py_REFCNT (obj ) == refcnt );
81
- assert (xsetref == NULL );
81
+ assert (xsetref == _Py_NULL );
82
82
83
83
// Py_SET_REFCNT
84
84
Py_SET_REFCNT (obj , Py_REFCNT (obj ));
@@ -103,8 +103,8 @@ test_py_is(PyObject *Py_UNUSED(module), PyObject* Py_UNUSED(ignored))
103
103
PyObject * o_true = Py_True ;
104
104
PyObject * o_false = Py_False ;
105
105
PyObject * obj = PyList_New (0 );
106
- if (obj == NULL ) {
107
- return NULL ;
106
+ if (obj == _Py_NULL ) {
107
+ return _Py_NULL ;
108
108
}
109
109
110
110
/* test Py_Is() */
@@ -138,9 +138,9 @@ test_frame(PyObject *Py_UNUSED(module), PyObject* Py_UNUSED(ignored))
138
138
139
139
// test PyThreadState_GetFrame()
140
140
PyFrameObject * frame = PyThreadState_GetFrame (tstate );
141
- if (frame == NULL ) {
141
+ if (frame == _Py_NULL ) {
142
142
PyErr_SetString (PyExc_AssertionError , "PyThreadState_GetFrame failed" );
143
- return NULL ;
143
+ return _Py_NULL ;
144
144
}
145
145
146
146
// test _PyThreadState_GetFrameBorrow()
@@ -151,7 +151,7 @@ test_frame(PyObject *Py_UNUSED(module), PyObject* Py_UNUSED(ignored))
151
151
152
152
// test PyFrame_GetCode()
153
153
PyCodeObject * code = PyFrame_GetCode (frame );
154
- assert (code != NULL );
154
+ assert (code != _Py_NULL );
155
155
assert (PyCode_Check (code ));
156
156
157
157
// test _PyFrame_GetCodeBorrow()
@@ -163,12 +163,12 @@ test_frame(PyObject *Py_UNUSED(module), PyObject* Py_UNUSED(ignored))
163
163
164
164
// PyFrame_GetBack()
165
165
PyFrameObject * back = PyFrame_GetBack (frame );
166
- if (back != NULL ) {
166
+ if (back != _Py_NULL ) {
167
167
assert (PyFrame_Check (back ));
168
168
}
169
169
170
170
// test _PyFrame_GetBackBorrow()
171
- if (back != NULL ) {
171
+ if (back != _Py_NULL ) {
172
172
Py_ssize_t back_refcnt = Py_REFCNT (back );
173
173
PyFrameObject * back2 = _PyFrame_GetBackBorrow (frame );
174
174
assert (back2 == back );
@@ -182,17 +182,17 @@ test_frame(PyObject *Py_UNUSED(module), PyObject* Py_UNUSED(ignored))
182
182
183
183
// test PyFrame_GetLocals()
184
184
PyObject * locals = PyFrame_GetLocals (frame );
185
- assert (locals != NULL );
185
+ assert (locals != _Py_NULL );
186
186
assert (PyDict_Check (locals ));
187
187
188
188
// test PyFrame_GetGlobals()
189
189
PyObject * globals = PyFrame_GetGlobals (frame );
190
- assert (globals != NULL );
190
+ assert (globals != _Py_NULL );
191
191
assert (PyDict_Check (globals ));
192
192
193
193
// test PyFrame_GetBuiltins()
194
194
PyObject * builtins = PyFrame_GetBuiltins (frame );
195
- assert (builtins != NULL );
195
+ assert (builtins != _Py_NULL );
196
196
assert (PyDict_Check (builtins ));
197
197
198
198
assert (locals != globals );
@@ -220,12 +220,12 @@ test_thread_state(PyObject *Py_UNUSED(module), PyObject* Py_UNUSED(ignored))
220
220
221
221
// test PyThreadState_GetInterpreter()
222
222
PyInterpreterState * interp = PyThreadState_GetInterpreter (tstate );
223
- assert (interp != NULL );
223
+ assert (interp != _Py_NULL );
224
224
225
225
#if !defined(PYPY_VERSION )
226
226
// test PyThreadState_GetFrame()
227
227
PyFrameObject * frame = PyThreadState_GetFrame (tstate );
228
- if (frame != NULL ) {
228
+ if (frame != _Py_NULL ) {
229
229
assert (PyFrame_Check (frame ));
230
230
}
231
231
Py_XDECREF (frame );
@@ -251,7 +251,7 @@ test_interpreter(PyObject *Py_UNUSED(module), PyObject* Py_UNUSED(ignored))
251
251
{
252
252
// test PyInterpreterState_Get()
253
253
PyInterpreterState * interp = PyInterpreterState_Get ();
254
- assert (interp != NULL );
254
+ assert (interp != _Py_NULL );
255
255
PyThreadState * tstate = PyThreadState_Get ();
256
256
PyInterpreterState * interp2 = PyThreadState_GetInterpreter (tstate );
257
257
assert (interp == interp2 );
@@ -267,21 +267,21 @@ test_calls(PyObject *Py_UNUSED(module), PyObject* Py_UNUSED(ignored))
267
267
268
268
// test PyObject_CallNoArgs(): str() returns ''
269
269
PyObject * res = PyObject_CallNoArgs (func );
270
- if (res == NULL ) {
271
- return NULL ;
270
+ if (res == _Py_NULL ) {
271
+ return _Py_NULL ;
272
272
}
273
273
assert (PyUnicode_Check (res ));
274
274
Py_DECREF (res );
275
275
276
276
// test PyObject_CallOneArg(): str(1) returns '1'
277
277
PyObject * arg = PyLong_FromLong (1 );
278
- if (arg == NULL ) {
279
- return NULL ;
278
+ if (arg == _Py_NULL ) {
279
+ return _Py_NULL ;
280
280
}
281
281
res = PyObject_CallOneArg (func , arg );
282
282
Py_DECREF (arg );
283
- if (res == NULL ) {
284
- return NULL ;
283
+ if (res == _Py_NULL ) {
284
+ return _Py_NULL ;
285
285
}
286
286
assert (PyUnicode_Check (res ));
287
287
Py_DECREF (res );
@@ -334,7 +334,7 @@ test_module_add_type(PyObject *module)
334
334
ASSERT_REFCNT (Py_REFCNT (type ) == refcnt + 1 );
335
335
336
336
PyObject * attr = PyObject_GetAttrString (module , type_name );
337
- if (attr == NULL ) {
337
+ if (attr == _Py_NULL ) {
338
338
return -1 ;
339
339
}
340
340
assert (attr == (PyObject * )type );
@@ -370,7 +370,7 @@ test_module_addobjectref(PyObject *module)
370
370
ASSERT_REFCNT (Py_REFCNT (obj ) == refcnt );
371
371
372
372
// PyModule_AddObjectRef() with value=NULL must not crash
373
- int res = PyModule_AddObjectRef (module , name , NULL );
373
+ int res = PyModule_AddObjectRef (module , name , _Py_NULL );
374
374
assert (res < 0 );
375
375
PyErr_Clear ();
376
376
@@ -382,8 +382,8 @@ static PyObject *
382
382
test_module (PyObject * Py_UNUSED (module ), PyObject * Py_UNUSED (ignored ))
383
383
{
384
384
PyObject * module = PyImport_ImportModule ("sys" );
385
- if (module == NULL ) {
386
- return NULL ;
385
+ if (module == _Py_NULL ) {
386
+ return _Py_NULL ;
387
387
}
388
388
assert (PyModule_Check (module ));
389
389
@@ -400,7 +400,7 @@ test_module(PyObject *Py_UNUSED(module), PyObject* Py_UNUSED(ignored))
400
400
401
401
error :
402
402
Py_DECREF (module );
403
- return NULL ;
403
+ return _Py_NULL ;
404
404
}
405
405
406
406
@@ -466,14 +466,14 @@ test_code(PyObject *Py_UNUSED(module), PyObject* Py_UNUSED(ignored))
466
466
{
467
467
PyThreadState * tstate = PyThreadState_Get ();
468
468
PyFrameObject * frame = PyThreadState_GetFrame (tstate );
469
- if (frame == NULL ) {
469
+ if (frame == _Py_NULL ) {
470
470
PyErr_SetString (PyExc_AssertionError , "PyThreadState_GetFrame failed" );
471
- return NULL ;
471
+ return _Py_NULL ;
472
472
}
473
473
PyCodeObject * code = PyFrame_GetCode (frame );
474
474
475
475
PyObject * co_code = PyCode_GetCode (code );
476
- assert (co_code != NULL );
476
+ assert (co_code != _Py_NULL );
477
477
assert (PyBytes_Check (co_code ));
478
478
Py_DECREF (co_code );
479
479
@@ -484,24 +484,85 @@ test_code(PyObject *Py_UNUSED(module), PyObject* Py_UNUSED(ignored))
484
484
#endif
485
485
486
486
487
+ #ifdef __cplusplus
488
+ // Class to test operator casting an object to PyObject*
489
+ class StrongRef
490
+ {
491
+ public :
492
+ StrongRef (PyObject * obj ) : m_obj (obj ) {
493
+ Py_INCREF (this -> m_obj );
494
+ }
495
+
496
+ ~StrongRef () {
497
+ Py_DECREF (this -> m_obj );
498
+ }
499
+
500
+ // Cast to PyObject*: get a borrowed reference
501
+ inline operator PyObject * () const { return this -> m_obj ; }
502
+
503
+ private :
504
+ PyObject * m_obj ; // Strong reference
505
+ };
506
+ #endif
507
+
508
+
509
+ static PyObject *
510
+ test_api_casts (PyObject * Py_UNUSED (module ), PyObject * Py_UNUSED (args ))
511
+ {
512
+ PyObject * obj = Py_BuildValue ("(ii)" , 1 , 2 );
513
+ if (obj == _Py_NULL ) {
514
+ return _Py_NULL ;
515
+ }
516
+ Py_ssize_t refcnt = Py_REFCNT (obj );
517
+ assert (refcnt >= 1 );
518
+
519
+ // gh-92138: For backward compatibility, functions of Python C API accepts
520
+ // "const PyObject*". Check that using it does not emit C++ compiler
521
+ // warnings.
522
+ const PyObject * const_obj = obj ;
523
+ Py_INCREF (const_obj );
524
+ Py_DECREF (const_obj );
525
+ PyTypeObject * type = Py_TYPE (const_obj );
526
+ assert (Py_REFCNT (const_obj ) == refcnt );
527
+ assert (type == & PyTuple_Type );
528
+ assert (PyTuple_GET_SIZE (const_obj ) == 2 );
529
+ PyObject * one = PyTuple_GET_ITEM (const_obj , 0 );
530
+ assert (PyLong_AsLong (one ) == 1 );
531
+
532
+ #ifdef __cplusplus
533
+ // gh-92898: StrongRef doesn't inherit from PyObject but has an operator to
534
+ // cast to PyObject*.
535
+ StrongRef strong_ref (obj );
536
+ assert (Py_TYPE (strong_ref ) == & PyTuple_Type );
537
+ assert (Py_REFCNT (strong_ref ) == (refcnt + 1 ));
538
+ Py_INCREF (strong_ref );
539
+ Py_DECREF (strong_ref );
540
+ #endif
541
+
542
+ Py_DECREF (obj );
543
+ Py_RETURN_NONE ;
544
+ }
545
+
546
+
487
547
static struct PyMethodDef methods [] = {
488
- {"test_object" , test_object , METH_NOARGS , NULL },
489
- {"test_py_is" , test_py_is , METH_NOARGS , NULL },
548
+ {"test_object" , test_object , METH_NOARGS , _Py_NULL },
549
+ {"test_py_is" , test_py_is , METH_NOARGS , _Py_NULL },
490
550
#if !defined (PYPY_VERSION )
491
- {"test_frame" , test_frame , METH_NOARGS , NULL },
551
+ {"test_frame" , test_frame , METH_NOARGS , _Py_NULL },
492
552
#endif
493
- {"test_thread_state" , test_thread_state , METH_NOARGS , NULL },
494
- {"test_interpreter" , test_interpreter , METH_NOARGS , NULL },
495
- {"test_calls" , test_calls , METH_NOARGS , NULL },
496
- {"test_gc" , test_gc , METH_NOARGS , NULL },
497
- {"test_module" , test_module , METH_NOARGS , NULL },
553
+ {"test_thread_state" , test_thread_state , METH_NOARGS , _Py_NULL },
554
+ {"test_interpreter" , test_interpreter , METH_NOARGS , _Py_NULL },
555
+ {"test_calls" , test_calls , METH_NOARGS , _Py_NULL },
556
+ {"test_gc" , test_gc , METH_NOARGS , _Py_NULL },
557
+ {"test_module" , test_module , METH_NOARGS , _Py_NULL },
498
558
#if (PY_VERSION_HEX <= 0x030B00A1 || 0x030B00A7 <= PY_VERSION_HEX ) && !defined (PYPY_VERSION )
499
- {"test_float_pack" , test_float_pack , METH_NOARGS , NULL },
559
+ {"test_float_pack" , test_float_pack , METH_NOARGS , _Py_NULL },
500
560
#endif
501
561
#if !defined (PYPY_VERSION )
502
- {"test_code" , test_code , METH_NOARGS , NULL },
562
+ {"test_code" , test_code , METH_NOARGS , _Py_NULL },
503
563
#endif
504
- {NULL , NULL , 0 , NULL }
564
+ {"test_api_casts" , test_api_casts , METH_NOARGS , _Py_NULL },
565
+ {_Py_NULL , _Py_NULL , 0 , _Py_NULL }
505
566
};
506
567
507
568
@@ -552,8 +613,8 @@ INIT_FUNC(void)
552
613
{
553
614
Py_InitModule4 (MODULE_NAME_STR ,
554
615
methods ,
555
- NULL ,
556
- NULL ,
616
+ _Py_NULL ,
617
+ _Py_NULL ,
557
618
PYTHON_API_VERSION );
558
619
}
559
620
#endif
0 commit comments