From db2d83393a0e42a35f9dc7eec0cb9c2d16b7cfe7 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Mon, 7 Sep 2026 16:47:56 +0200 Subject: [PATCH] gh-121617: Fix Py_CLEAR() in C++: replace NULL with _Py_NULL * Enhance Py_CLEAR() test in test_cext and test_cppext. Check that Py_CLEAR(obj) sets obj to NULL. * Add also tests on Py_SETREF() and Py_BEGIN_CRITICAL_SECTION(). --- Include/refcount.h | 2 +- Lib/test/test_cext/extension.c | 12 ++++++++++-- Lib/test/test_cppext/extension.cpp | 16 ++++++++++++++++ 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/Include/refcount.h b/Include/refcount.h index 80fe7ff70a11e87..8a3d440fd14b1e3 100644 --- a/Include/refcount.h +++ b/Include/refcount.h @@ -484,7 +484,7 @@ static inline Py_ALWAYS_INLINE void Py_DECREF(PyObject *op) do { \ _Py_TYPEOF(op)* _tmp_op_ptr = &(op); \ _Py_TYPEOF(op) _tmp_old_op = (*_tmp_op_ptr); \ - if (_tmp_old_op != NULL) { \ + if (_tmp_old_op != _Py_NULL) { \ *_tmp_op_ptr = _Py_NULL; \ Py_DECREF(_tmp_old_op); \ } \ diff --git a/Lib/test/test_cext/extension.c b/Lib/test/test_cext/extension.c index 895eca50f03b985..543a8096f16f8aa 100644 --- a/Lib/test/test_cext/extension.c +++ b/Lib/test/test_cext/extension.c @@ -95,9 +95,17 @@ _testcext_exec(PyObject *module) Py_BUILD_ASSERT(sizeof(int) == sizeof(unsigned int)); assert(Py_BUILD_ASSERT_EXPR(sizeof(int) == sizeof(unsigned int)) == 0); - // Test Py_CLEAR() - obj = NULL; + // Test Py_CLEAR(): use typeof()/__typeof__() if available, or memcpy() + obj = Py_None; Py_CLEAR(obj); + assert(obj == NULL); + +#ifndef Py_LIMITED_API + // Test Py_SETREF(): use typeof()/__typeof__() if available, or memcpy() + obj = Py_None; + Py_SETREF(obj, NULL); + assert(obj == NULL); +#endif // Test that Py_BEGIN_CRITICAL_SECTION is available Py_BEGIN_CRITICAL_SECTION(module); diff --git a/Lib/test/test_cppext/extension.cpp b/Lib/test/test_cppext/extension.cpp index 4db63df94f52334..62ce81e2b510c7a 100644 --- a/Lib/test/test_cppext/extension.cpp +++ b/Lib/test/test_cppext/extension.cpp @@ -294,6 +294,22 @@ _testcppext_exec(PyObject *module) Py_BUILD_ASSERT(sizeof(int) == sizeof(unsigned int)); assert(Py_BUILD_ASSERT_EXPR(sizeof(int) == sizeof(unsigned int)) == 0); + // Test Py_CLEAR(): use typeof()/__typeof__() if available, or memcpy() + PyObject *obj = Py_None; + Py_CLEAR(obj); + assert(obj == _Py_NULL); + +#ifndef Py_LIMITED_API + // Test Py_SETREF(): use typeof()/__typeof__() if available, or memcpy() + obj = Py_None; + Py_SETREF(obj, _Py_NULL); + assert(obj == _Py_NULL); +#endif + + // Test that Py_BEGIN_CRITICAL_SECTION is available + Py_BEGIN_CRITICAL_SECTION(module); + Py_END_CRITICAL_SECTION(); + return 0; }