| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -199,19 +199,23 @@ | |||
| 199 | 199 | } while(0) | |
| 200 | 200 | #endif | |
| 201 | 201 | ||
| 202 | - /* Get the number of elements in a visible array | ||
| 203 | - | ||
| 204 | - This does not work on pointers, or arrays declared as [], or function | ||
| 205 | - parameters. With correct compiler support, such usage will cause a build | ||
| 206 | - error (see Py_BUILD_ASSERT_EXPR). | ||
| 207 | - | ||
| 208 | - Written by Rusty Russell, public domain, http://ccodearchive.net/ | ||
| 209 | - | ||
| 210 | - Requires at GCC 3.1+ */ | ||
| 211 | - #if (defined(__GNUC__) && !defined(__STRICT_ANSI__) && \ | ||
| 212 | - (((__GNUC__ == 3) && (__GNUC_MINOR__ >= 1)) || (__GNUC__ >= 4))) | ||
| 213 | - /* Two gcc extensions. | ||
| 214 | - &a[0] degrades to a pointer: a different type from an array */ | ||
| 202 | + // Get the number of elements in a visible array. | ||
| 203 | + // | ||
| 204 | + // This does not work on pointers, or arrays declared as [], or function | ||
| 205 | + // parameters. With correct compiler support, such usage will cause a build | ||
| 206 | + // error (see Py_BUILD_ASSERT_EXPR). | ||
| 207 | + // | ||
| 208 | + // Written by Rusty Russell, public domain, http://ccodearchive.net/ | ||
| 209 | + // | ||
| 210 | + // Require GCC 4 (it works on GCC 3.1). | ||
| 211 | + // | ||
| 212 | + // Two GCC extensions: &a[0] degrades to a pointer, a different type from an | ||
| 213 | + // array. | ||
| 214 | + // | ||
| 215 | + // gh-158810: Do not use __builtin_types_compatible_p() in strict C ANSI mode | ||
| 216 | + // and on C++. | ||
| 217 | + #if (defined(__GNUC__) && __GNUC__ >= 4 \ | ||
| 218 | + && !defined(__STRICT_ANSI__) && !defined(__cplusplus)) | ||
| 215 | 219 | #define Py_ARRAY_LENGTH(array) \ | |
| 216 | 220 | (sizeof(array) / sizeof((array)[0]) \ | |
| 217 | 221 | + Py_BUILD_ASSERT_EXPR(!__builtin_types_compatible_p(typeof(array), \ | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -83,6 +83,7 @@ test_macros(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args)) | |||
| 83 | 83 | { | |
| 84 | 84 | PyObject *obj, *dict; | |
| 85 | 85 | PyObject *slots[1]; | |
| 86 | + int small_array[] = {2, 5, 7}; | ||
| 86 | 87 | ||
| 87 | 88 | // test Py_BUILD_ASSERT() and Py_BUILD_ASSERT_EXPR() | |
| 88 | 89 | Py_BUILD_ASSERT(sizeof(int) == sizeof(unsigned int)); | |
@@ -134,6 +135,9 @@ test_macros(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args)) | |||
| 134 | 135 | Py_END_CRITICAL_SECTION(); | |
| 135 | 136 | Py_DECREF(dict); | |
| 136 | 137 | ||
| 138 | + // Test Py_ARRAY_LENGTH() | ||
| 139 | + assert(Py_ARRAY_LENGTH(small_array) == 3); | ||
| 140 | + | ||
| 137 | 141 | Py_RETURN_NONE; | |
| 138 | 142 | } | |
| 139 | 143 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,2 @@ | |||
| 1 | + Fix :c:macro:`Py_ARRAY_LENGTH()` macro on C++: do not use | ||
| 2 | + ``__builtin_types_compatible_p()`` on C++. Patch by Victor Stinner. | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments