From 39d18d11bfcfae8173bf21d7f999ab7c770d65c8 Mon Sep 17 00:00:00 2001 From: Petr Viktorin Date: Mon, 17 Nov 2025 17:42:47 +0100 Subject: [PATCH 1/9] gh-141671: Use __declspec(dllexport) when not building core --- Include/exports.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Include/exports.h b/Include/exports.h index 62feb09ed2b433..73f0a1fca8102e 100644 --- a/Include/exports.h +++ b/Include/exports.h @@ -26,7 +26,7 @@ #endif #if defined(_WIN32) || defined(__CYGWIN__) - #if defined(Py_ENABLE_SHARED) + #if !defined(Py_BUILD_CORE) || defined(Py_ENABLE_SHARED) #define Py_IMPORTED_SYMBOL __declspec(dllimport) #define Py_EXPORTED_SYMBOL __declspec(dllexport) #define Py_LOCAL_SYMBOL From ad9110ca1c5d1a87dd0cb2bbdb8e24e396f8698f Mon Sep 17 00:00:00 2001 From: Petr Viktorin Date: Mon, 17 Nov 2025 17:47:14 +0100 Subject: [PATCH 2/9] blurb --- .../next/C_API/2025-11-17-17-46-16.gh-issue-141671.cVXNW5.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/C_API/2025-11-17-17-46-16.gh-issue-141671.cVXNW5.rst diff --git a/Misc/NEWS.d/next/C_API/2025-11-17-17-46-16.gh-issue-141671.cVXNW5.rst b/Misc/NEWS.d/next/C_API/2025-11-17-17-46-16.gh-issue-141671.cVXNW5.rst new file mode 100644 index 00000000000000..5dd6913522ecc3 --- /dev/null +++ b/Misc/NEWS.d/next/C_API/2025-11-17-17-46-16.gh-issue-141671.cVXNW5.rst @@ -0,0 +1,2 @@ +:c:macro:`PyMODINIT_FUNC` (and the new :c:macro:`!PyMODEXPORT_FUNC`) now adds +a linkage declaration (``__declspec(dllexport)``) on Windows. From fd34aff83744a9343a7f327f353c788bd86dbf51 Mon Sep 17 00:00:00 2001 From: Petr Viktorin Date: Tue, 18 Nov 2025 11:07:00 +0100 Subject: [PATCH 3/9] Use _PyINIT_EXPORTED_SYMBOL --- Include/exports.h | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/Include/exports.h b/Include/exports.h index 73f0a1fca8102e..e0bbf3dc9c393c 100644 --- a/Include/exports.h +++ b/Include/exports.h @@ -25,8 +25,10 @@ # define HAVE_DECLSPEC_DLL #endif + #if defined(_WIN32) || defined(__CYGWIN__) - #if !defined(Py_BUILD_CORE) || defined(Py_ENABLE_SHARED) + #define _PyINIT_EXPORTED_SYMBOL __declspec(dllexport) + #if defined(Py_ENABLE_SHARED) #define Py_IMPORTED_SYMBOL __declspec(dllimport) #define Py_EXPORTED_SYMBOL __declspec(dllexport) #define Py_LOCAL_SYMBOL @@ -52,6 +54,7 @@ #define Py_EXPORTED_SYMBOL #define Py_LOCAL_SYMBOL #endif + #define _PyINIT_EXPORTED_SYMBOL Py_EXPORTED_SYMBOL #endif /* only get special linkage if built as shared or platform is Cygwin */ @@ -63,7 +66,7 @@ /* module init functions inside the core need no external linkage */ /* except for Cygwin to handle embedding */ # if defined(__CYGWIN__) -# define _PyINIT_FUNC_DECLSPEC Py_EXPORTED_SYMBOL +# define _PyINIT_FUNC_DECLSPEC _PyINIT_EXPORTED_SYMBOL # else /* __CYGWIN__ */ # define _PyINIT_FUNC_DECLSPEC # endif /* __CYGWIN__ */ @@ -79,9 +82,10 @@ # define PyAPI_DATA(RTYPE) extern Py_IMPORTED_SYMBOL RTYPE /* module init functions outside the core must be exported */ # if defined(__cplusplus) -# define _PyINIT_FUNC_DECLSPEC extern "C" Py_EXPORTED_SYMBOL +# define _PyINIT_FUNC_DECLSPEC extern "C" \ + _PyINIT_EXPORTED_SYMBOL # else /* __cplusplus */ -# define _PyINIT_FUNC_DECLSPEC Py_EXPORTED_SYMBOL +# define _PyINIT_FUNC_DECLSPEC _PyINIT_EXPORTED_SYMBOL # endif /* __cplusplus */ # endif /* Py_BUILD_CORE */ # endif /* HAVE_DECLSPEC_DLL */ @@ -96,9 +100,9 @@ #endif #ifndef _PyINIT_FUNC_DECLSPEC # if defined(__cplusplus) -# define _PyINIT_FUNC_DECLSPEC extern "C" Py_EXPORTED_SYMBOL +# define _PyINIT_FUNC_DECLSPEC extern "C" _PyINIT_EXPORTED_SYMBOL # else /* __cplusplus */ -# define _PyINIT_FUNC_DECLSPEC Py_EXPORTED_SYMBOL +# define _PyINIT_FUNC_DECLSPEC _PyINIT_EXPORTED_SYMBOL # endif /* __cplusplus */ #endif From c0e0a836c5279d46040203829f2398e97bed266c Mon Sep 17 00:00:00 2001 From: Petr Viktorin Date: Tue, 18 Nov 2025 11:22:54 +0100 Subject: [PATCH 4/9] Use `__declspec(dllexport)` outside core --- Include/exports.h | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/Include/exports.h b/Include/exports.h index e0bbf3dc9c393c..0e9e9c666d5892 100644 --- a/Include/exports.h +++ b/Include/exports.h @@ -27,7 +27,6 @@ #if defined(_WIN32) || defined(__CYGWIN__) - #define _PyINIT_EXPORTED_SYMBOL __declspec(dllexport) #if defined(Py_ENABLE_SHARED) #define Py_IMPORTED_SYMBOL __declspec(dllimport) #define Py_EXPORTED_SYMBOL __declspec(dllexport) @@ -37,6 +36,12 @@ #define Py_EXPORTED_SYMBOL #define Py_LOCAL_SYMBOL #endif + /* module init functions outside the core must be exported */ + #if defined(Py_BUILD_CORE) + #define _PyINIT_EXPORTED_SYMBOL Py_EXPORTED_SYMBOL + #else + #define _PyINIT_EXPORTED_SYMBOL __declspec(dllexport) + #endif #else /* * If we only ever used gcc >= 5, we could use __has_attribute(visibility) @@ -80,13 +85,6 @@ # define PyAPI_FUNC(RTYPE) Py_IMPORTED_SYMBOL RTYPE # endif /* !__CYGWIN__ */ # define PyAPI_DATA(RTYPE) extern Py_IMPORTED_SYMBOL RTYPE - /* module init functions outside the core must be exported */ -# if defined(__cplusplus) -# define _PyINIT_FUNC_DECLSPEC extern "C" \ - _PyINIT_EXPORTED_SYMBOL -# else /* __cplusplus */ -# define _PyINIT_FUNC_DECLSPEC _PyINIT_EXPORTED_SYMBOL -# endif /* __cplusplus */ # endif /* Py_BUILD_CORE */ # endif /* HAVE_DECLSPEC_DLL */ #endif /* Py_ENABLE_SHARED */ From 5abc323ab9178fb4e920073a506a4f43e42747bd Mon Sep 17 00:00:00 2001 From: Petr Viktorin Date: Tue, 18 Nov 2025 11:24:18 +0100 Subject: [PATCH 5/9] Allow embedders to customize --- Include/exports.h | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/Include/exports.h b/Include/exports.h index 0e9e9c666d5892..2afccadf66fc53 100644 --- a/Include/exports.h +++ b/Include/exports.h @@ -104,7 +104,11 @@ # endif /* __cplusplus */ #endif -#define PyMODINIT_FUNC _PyINIT_FUNC_DECLSPEC PyObject* -#define PyMODEXPORT_FUNC _PyINIT_FUNC_DECLSPEC PyModuleDef_Slot* +#ifndef PyMODINIT_FUNC + #define PyMODINIT_FUNC _PyINIT_FUNC_DECLSPEC PyObject* +#endif +#ifndef PyMODEXPORT_FUNC + #define PyMODEXPORT_FUNC _PyINIT_FUNC_DECLSPEC PyModuleDef_Slot* +#endif #endif /* Py_EXPORTS_H */ From 82ad329f5197034154dee40e842a15d52cd3b1c5 Mon Sep 17 00:00:00 2001 From: Petr Viktorin Date: Tue, 18 Nov 2025 11:29:10 +0100 Subject: [PATCH 6/9] Remove duplication with defaults --- Include/exports.h | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/Include/exports.h b/Include/exports.h index 2afccadf66fc53..fbc99d1012b235 100644 --- a/Include/exports.h +++ b/Include/exports.h @@ -66,13 +66,9 @@ #if defined(Py_ENABLE_SHARED) || defined(__CYGWIN__) # if defined(HAVE_DECLSPEC_DLL) # if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) -# define PyAPI_FUNC(RTYPE) Py_EXPORTED_SYMBOL RTYPE -# define PyAPI_DATA(RTYPE) extern Py_EXPORTED_SYMBOL RTYPE /* module init functions inside the core need no external linkage */ /* except for Cygwin to handle embedding */ -# if defined(__CYGWIN__) -# define _PyINIT_FUNC_DECLSPEC _PyINIT_EXPORTED_SYMBOL -# else /* __CYGWIN__ */ +# if !defined(__CYGWIN__) # define _PyINIT_FUNC_DECLSPEC # endif /* __CYGWIN__ */ # else /* Py_BUILD_CORE */ @@ -81,7 +77,9 @@ /* Under Cygwin, auto-import functions to prevent compilation */ /* failures similar to those described at the bottom of 4.1: */ /* http://docs.python.org/extending/windows.html#a-cookbook-approach */ -# if !defined(__CYGWIN__) +# if defined(__CYGWIN__) +# define _PyINIT_FUNC_DECLSPEC _PyINIT_EXPORTED_SYMBOL +# else /* __CYGWIN__ */ # define PyAPI_FUNC(RTYPE) Py_IMPORTED_SYMBOL RTYPE # endif /* !__CYGWIN__ */ # define PyAPI_DATA(RTYPE) extern Py_IMPORTED_SYMBOL RTYPE From 35a088a3768ba2c63cba15d2d557beef2e158d2a Mon Sep 17 00:00:00 2001 From: Petr Viktorin Date: Tue, 18 Nov 2025 11:56:43 +0100 Subject: [PATCH 7/9] Remove duplication with default --- Include/exports.h | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Include/exports.h b/Include/exports.h index fbc99d1012b235..9d98362932c70e 100644 --- a/Include/exports.h +++ b/Include/exports.h @@ -77,9 +77,7 @@ /* Under Cygwin, auto-import functions to prevent compilation */ /* failures similar to those described at the bottom of 4.1: */ /* http://docs.python.org/extending/windows.html#a-cookbook-approach */ -# if defined(__CYGWIN__) -# define _PyINIT_FUNC_DECLSPEC _PyINIT_EXPORTED_SYMBOL -# else /* __CYGWIN__ */ +# if !defined(__CYGWIN__) # define PyAPI_FUNC(RTYPE) Py_IMPORTED_SYMBOL RTYPE # endif /* !__CYGWIN__ */ # define PyAPI_DATA(RTYPE) extern Py_IMPORTED_SYMBOL RTYPE From b39a5a80563445e661f866b53424fc4d1531b58d Mon Sep 17 00:00:00 2001 From: Petr Viktorin Date: Tue, 18 Nov 2025 11:58:19 +0100 Subject: [PATCH 8/9] Reduce diff --- Include/exports.h | 1 - 1 file changed, 1 deletion(-) diff --git a/Include/exports.h b/Include/exports.h index 9d98362932c70e..97a674ec2403a4 100644 --- a/Include/exports.h +++ b/Include/exports.h @@ -25,7 +25,6 @@ # define HAVE_DECLSPEC_DLL #endif - #if defined(_WIN32) || defined(__CYGWIN__) #if defined(Py_ENABLE_SHARED) #define Py_IMPORTED_SYMBOL __declspec(dllimport) From 5091b7963c654b66482a0431e33ffff1e6da66ea Mon Sep 17 00:00:00 2001 From: Petr Viktorin Date: Tue, 16 Dec 2025 16:19:07 +0100 Subject: [PATCH 9/9] Link macro that's now documented --- .../next/C_API/2025-11-17-17-46-16.gh-issue-141671.cVXNW5.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/C_API/2025-11-17-17-46-16.gh-issue-141671.cVXNW5.rst b/Misc/NEWS.d/next/C_API/2025-11-17-17-46-16.gh-issue-141671.cVXNW5.rst index 5dd6913522ecc3..304a6a5f75ff3b 100644 --- a/Misc/NEWS.d/next/C_API/2025-11-17-17-46-16.gh-issue-141671.cVXNW5.rst +++ b/Misc/NEWS.d/next/C_API/2025-11-17-17-46-16.gh-issue-141671.cVXNW5.rst @@ -1,2 +1,2 @@ -:c:macro:`PyMODINIT_FUNC` (and the new :c:macro:`!PyMODEXPORT_FUNC`) now adds +:c:macro:`PyMODINIT_FUNC` (and the new :c:macro:`PyMODEXPORT_FUNC`) now adds a linkage declaration (``__declspec(dllexport)``) on Windows.