]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-107249: Implement Py_UNUSED() for MSVC (#107250)
authorVictor Stinner <vstinner@python.org>
Tue, 25 Jul 2023 17:28:16 +0000 (19:28 +0200)
committerGitHub <noreply@github.com>
Tue, 25 Jul 2023 17:28:16 +0000 (19:28 +0200)
Fix warnings C4100 in Py_UNUSED() when Python is built with "cl /W4".

Example with this function included by Python.h:

    static inline unsigned int
    PyUnicode_IS_READY(PyObject* Py_UNUSED(op))
    { return 1; }

Without this change, building a C program with "cl /W4" which just
includes Python.h emits the warning:

    Include\cpython/unicodeobject.h(199):
    warning C4100: '_unused_op': unreferenced formal parameter

This change fix this warning.

Include/pymacro.h
Misc/NEWS.d/next/C API/2023-07-25-17-23-08.gh-issue-107249.xqk2ke.rst [new file with mode: 0644]

index 342d2a7b844adfca189cc2a936bb46680029c557..9d264fe6eea1d44236f9580b7656180ff0a9bb69 100644 (file)
  */
 #if defined(__GNUC__) || defined(__clang__)
 #  define Py_UNUSED(name) _unused_ ## name __attribute__((unused))
+#elif defined(_MSC_VER)
+   // Disable warning C4100: unreferenced formal parameter,
+   // declare the parameter,
+   // restore old compiler warnings.
+#  define Py_UNUSED(name) \
+        __pragma(warning(push)) \
+        __pragma(warning(suppress: 4100)) \
+        _unused_ ## name \
+        __pragma(warning(pop))
 #else
 #  define Py_UNUSED(name) _unused_ ## name
 #endif
diff --git a/Misc/NEWS.d/next/C API/2023-07-25-17-23-08.gh-issue-107249.xqk2ke.rst b/Misc/NEWS.d/next/C API/2023-07-25-17-23-08.gh-issue-107249.xqk2ke.rst
new file mode 100644 (file)
index 0000000..a713902
--- /dev/null
@@ -0,0 +1,2 @@
+Implement the :c:macro:`Py_UNUSED` macro for Windows MSVC compiler. Patch by
+Victor Stinner.