https://github.com/python/cpython/commit/b2bfbce9a2cb00cb75c08230698cde408c130c92
commit: b2bfbce9a2cb00cb75c08230698cde408c130c92
branch: main
author: Victor Stinner <[email protected]>
committer: vstinner <[email protected]>
date: 2026-10-05T13:30:16+02:00
summary:
gh-158810: Fix Py_ARRAY_LENGTH() macro on C++ (#158812)
files:
A Misc/NEWS.d/next/C_API/2026-10-05-01-09-32.gh-issue-158810.F1p-PK.rst
M Include/pymacro.h
M Lib/test/test_cext/extension.c
diff --git a/Include/pymacro.h b/Include/pymacro.h
index f31c9505a6a812..979e6563742b98 100644
--- a/Include/pymacro.h
+++ b/Include/pymacro.h
@@ -199,19 +199,23 @@
} while(0)
#endif
-/* Get the number of elements in a visible array
-
- This does not work on pointers, or arrays declared as [], or function
- parameters. With correct compiler support, such usage will cause a build
- error (see Py_BUILD_ASSERT_EXPR).
-
- Written by Rusty Russell, public domain, http://ccodearchive.net/
-
- Requires at GCC 3.1+ */
-#if (defined(__GNUC__) && !defined(__STRICT_ANSI__) && \
- (((__GNUC__ == 3) && (__GNUC_MINOR__ >= 1)) || (__GNUC__ >= 4)))
-/* Two gcc extensions.
- &a[0] degrades to a pointer: a different type from an array */
+// Get the number of elements in a visible array.
+//
+// This does not work on pointers, or arrays declared as [], or function
+// parameters. With correct compiler support, such usage will cause a build
+// error (see Py_BUILD_ASSERT_EXPR).
+//
+// Written by Rusty Russell, public domain, http://ccodearchive.net/
+//
+// Require GCC 4 (it works on GCC 3.1).
+//
+// Two GCC extensions: &a[0] degrades to a pointer, a different type from an
+// array.
+//
+// gh-158810: Do not use __builtin_types_compatible_p() in strict C ANSI mode
+// and on C++.
+#if (defined(__GNUC__) && __GNUC__ >= 4 \
+ && !defined(__STRICT_ANSI__) && !defined(__cplusplus))
#define Py_ARRAY_LENGTH(array) \
(sizeof(array) / sizeof((array)[0]) \
+ Py_BUILD_ASSERT_EXPR(!__builtin_types_compatible_p(typeof(array), \
diff --git a/Lib/test/test_cext/extension.c b/Lib/test/test_cext/extension.c
index ea7a1ca3c38a84..e392b66f19cc4a 100644
--- a/Lib/test/test_cext/extension.c
+++ b/Lib/test/test_cext/extension.c
@@ -83,6 +83,7 @@ test_macros(PyObject *Py_UNUSED(module), PyObject
*Py_UNUSED(args))
{
PyObject *obj, *dict;
PyObject *slots[1];
+ int small_array[] = {2, 5, 7};
// test Py_BUILD_ASSERT() and Py_BUILD_ASSERT_EXPR()
Py_BUILD_ASSERT(sizeof(int) == sizeof(unsigned int));
@@ -134,6 +135,9 @@ test_macros(PyObject *Py_UNUSED(module), PyObject
*Py_UNUSED(args))
Py_END_CRITICAL_SECTION();
Py_DECREF(dict);
+ // Test Py_ARRAY_LENGTH()
+ assert(Py_ARRAY_LENGTH(small_array) == 3);
+
Py_RETURN_NONE;
}
diff --git
a/Misc/NEWS.d/next/C_API/2026-10-05-01-09-32.gh-issue-158810.F1p-PK.rst
b/Misc/NEWS.d/next/C_API/2026-10-05-01-09-32.gh-issue-158810.F1p-PK.rst
new file mode 100644
index 00000000000000..8d0878fd057a04
--- /dev/null
+++ b/Misc/NEWS.d/next/C_API/2026-10-05-01-09-32.gh-issue-158810.F1p-PK.rst
@@ -0,0 +1,2 @@
+Fix :c:macro:`Py_ARRAY_LENGTH()` macro on C++: do not use
+``__builtin_types_compatible_p()`` on C++. Patch by Victor Stinner.
_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]