GeezerVison is showing github.com — [3.13] gh-121617: Fix Py_CLEAR() in C++: replace NULL with _Py_NULL (… · python/cpython@a4ab2c1
File tree
Expand file treeCollapse file tree
Open diff view settings
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: Include/object.h +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1006,7 +1006,7 @@ static inline Py_ALWAYS_INLINE void Py_DECREF(PyObject *op) |
1006 | 1006 | do { \ |
1007 | 1007 | _Py_TYPEOF(op)* _tmp_op_ptr = &(op); \ |
1008 | 1008 | _Py_TYPEOF(op) _tmp_old_op = (*_tmp_op_ptr); \ |
1009 | | - if (_tmp_old_op != NULL) { \ |
| 1009 | + if (_tmp_old_op != _Py_NULL) { \ |
1010 | 1010 | *_tmp_op_ptr = _Py_NULL; \ |
1011 | 1011 | Py_DECREF(_tmp_old_op); \ |
1012 | 1012 | } \ |
@@ -1016,7 +1016,7 @@ static inline Py_ALWAYS_INLINE void Py_DECREF(PyObject *op) |
1016 | 1016 | do { \ |
1017 | 1017 | PyObject **_tmp_op_ptr = _Py_CAST(PyObject**, &(op)); \ |
1018 | 1018 | PyObject *_tmp_old_op = (*_tmp_op_ptr); \ |
1019 | | - if (_tmp_old_op != NULL) { \ |
| 1019 | + if (_tmp_old_op != _Py_NULL) { \ |
1020 | 1020 | PyObject *_null_ptr = _Py_NULL; \ |
1021 | 1021 | memcpy(_tmp_op_ptr, &_null_ptr, sizeof(PyObject*)); \ |
1022 | 1022 | Py_DECREF(_tmp_old_op); \ |
|
Collapse file
Copy file name to clipboardExpand all lines: Lib/test/test_cext/extension.c +18Lines changed: 18 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -45,6 +45,8 @@ _testcext_exec( |
45 | 45 | #endif |
46 | 46 | ) |
47 | 47 | { |
| 48 | + PyObject *obj; |
| 49 | + |
48 | 50 | #ifdef __STDC_VERSION__ |
49 | 51 | if (PyModule_AddIntMacro(module, __STDC_VERSION__) < 0) { |
50 | 52 | return -1; |
@@ -55,6 +57,22 @@ _testcext_exec( |
55 | 57 | Py_BUILD_ASSERT(sizeof(int) == sizeof(unsigned int)); |
56 | 58 | assert(Py_BUILD_ASSERT_EXPR(sizeof(int) == sizeof(unsigned int)) == 0); |
57 | 59 |
|
| 60 | + // Test Py_CLEAR(): use typeof()/__typeof__() if available, or memcpy() |
| 61 | + obj = Py_None; |
| 62 | + Py_CLEAR(obj); |
| 63 | + assert(obj == NULL); |
| 64 | + |
| 65 | +#ifndef Py_LIMITED_API |
| 66 | + // Test Py_SETREF(): use typeof()/__typeof__() if available, or memcpy() |
| 67 | + obj = Py_None; |
| 68 | + Py_SETREF(obj, NULL); |
| 69 | + assert(obj == NULL); |
| 70 | + |
| 71 | + // Test that Py_BEGIN_CRITICAL_SECTION is available |
| 72 | + Py_BEGIN_CRITICAL_SECTION(module); |
| 73 | + Py_END_CRITICAL_SECTION(); |
| 74 | +#endif |
| 75 | + |
58 | 76 | return 0; |
59 | 77 | } |
60 | 78 |
|
|
Collapse file
Copy file name to clipboardExpand all lines: Lib/test/test_cppext/extension.cpp +16Lines changed: 16 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -238,6 +238,22 @@ _testcppext_exec(PyObject *module) |
238 | 238 | Py_BUILD_ASSERT(sizeof(int) == sizeof(unsigned int)); |
239 | 239 | assert(Py_BUILD_ASSERT_EXPR(sizeof(int) == sizeof(unsigned int)) == 0); |
240 | 240 |
|
| 241 | + // Test Py_CLEAR(): use typeof()/__typeof__() if available, or memcpy() |
| 242 | + PyObject *obj = Py_None; |
| 243 | + Py_CLEAR(obj); |
| 244 | + assert(obj == _Py_NULL); |
| 245 | + |
| 246 | +#ifndef Py_LIMITED_API |
| 247 | + // Test Py_SETREF(): use typeof()/__typeof__() if available, or memcpy() |
| 248 | + obj = Py_None; |
| 249 | + Py_SETREF(obj, _Py_NULL); |
| 250 | + assert(obj == _Py_NULL); |
| 251 | + |
| 252 | + // Test that Py_BEGIN_CRITICAL_SECTION is available |
| 253 | + Py_BEGIN_CRITICAL_SECTION(module); |
| 254 | + Py_END_CRITICAL_SECTION(); |
| 255 | +#endif |
| 256 | + |
241 | 257 | return 0; |
242 | 258 | } |
243 | 259 |
|
|