https://github.com/python/cpython/commit/659631f7697079e31e2a8c31fd686b24c85c9b2d
commit: 659631f7697079e31e2a8c31fd686b24c85c9b2d
branch: 3.13
author: Victor Stinner <vstin...@python.org>
committer: vstinner <vstin...@python.org>
date: 2025-06-04T17:10:35+02:00
summary:

[3.13] gh-133256: Add _Py_NONSTRING macro (#133257) (#135135)

gh-133256: Add _Py_NONSTRING macro (#133257)

Fix GCC 15 compiler warnings such as:

    In file included from Python/pylifecycle.c:26:
    Include/internal/pycore_runtime.h:47:26: warning:
    initializer-string for array of 'char' truncates NUL terminator
    but destination lacks 'nonstring' attribute (9 chars into 8
    available) [-Wunterminated-string-initialization]
       47 | #define _Py_Debug_Cookie "xdebugpy"
          |                          ^~~~~~~~~~

(cherry picked from commit e26bafd107aa86a4bdd6051848640f36a56d0efb)

files:
M Include/internal/pycore_runtime.h
M Include/pyport.h

diff --git a/Include/internal/pycore_runtime.h 
b/Include/internal/pycore_runtime.h
index d4291b87261ae0..ed028944d18e04 100644
--- a/Include/internal/pycore_runtime.h
+++ b/Include/internal/pycore_runtime.h
@@ -60,7 +60,7 @@ typedef struct _Py_AuditHookEntry {
 } _Py_AuditHookEntry;
 
 typedef struct _Py_DebugOffsets {
-    char cookie[8];
+    char cookie[8] _Py_NONSTRING;
     uint64_t version;
     uint64_t free_threaded;
     // Runtime state offset;
diff --git a/Include/pyport.h b/Include/pyport.h
index 2ba81a4be42822..72a157e679d92f 100644
--- a/Include/pyport.h
+++ b/Include/pyport.h
@@ -541,6 +541,14 @@ extern "C" {
 #  define _Py__has_builtin(x) 0
 #endif
 
+// Preprocessor check for a compiler __attribute__. Always return 0
+// if __has_attribute() macro is not defined.
+#ifdef __has_attribute
+#  define _Py__has_attribute(x) __has_attribute(x)
+#else
+#  define _Py__has_attribute(x) 0
+#endif
+
 // _Py_TYPEOF(expr) gets the type of an expression.
 //
 // Example: _Py_TYPEOF(x) x_copy = (x);
@@ -606,4 +614,20 @@ extern "C" {
 #  define _SGI_MP_SOURCE
 #endif
 
+
+// _Py_NONSTRING: The nonstring variable attribute specifies that an object or
+// member declaration with type array of char, signed char, or unsigned char,
+// or pointer to such a type is intended to store character arrays that do not
+// necessarily contain a terminating NUL.
+//
+// Usage:
+//
+//   char name [8] _Py_NONSTRING;
+#if _Py__has_attribute(nonstring)
+#  define _Py_NONSTRING __attribute__((nonstring))
+#else
+#  define _Py_NONSTRING
+#endif
+
+
 #endif /* Py_PYPORT_H */

_______________________________________________
Python-checkins mailing list -- python-checkins@python.org
To unsubscribe send an email to python-checkins-le...@python.org
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: arch...@mail-archive.com

Reply via email to