Skip to content

Commit f3a689f

Browse files
[3.12] gh-107249: Implement Py_UNUSED() for MSVC (GH-107250) (#127907)
gh-107249: Implement Py_UNUSED() for MSVC (GH-107250) 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. (cherry picked from commit 6a43cce) Co-authored-by: Victor Stinner <[email protected]>
1 parent b184f48 commit f3a689f

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

Include/pymacro.h

+9
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,15 @@
118118
*/
119119
#if defined(__GNUC__) || defined(__clang__)
120120
# define Py_UNUSED(name) _unused_ ## name __attribute__((unused))
121+
#elif defined(_MSC_VER)
122+
// Disable warning C4100: unreferenced formal parameter,
123+
// declare the parameter,
124+
// restore old compiler warnings.
125+
# define Py_UNUSED(name) \
126+
__pragma(warning(push)) \
127+
__pragma(warning(suppress: 4100)) \
128+
_unused_ ## name \
129+
__pragma(warning(pop))
121130
#else
122131
# define Py_UNUSED(name) _unused_ ## name
123132
#endif
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Implement the :c:macro:`Py_UNUSED` macro for Windows MSVC compiler. Patch by
2+
Victor Stinner.

0 commit comments

Comments
 (0)