Skip to content

Commit 8565ddd

Browse files
authored
gh-141671: PyMODINIT_FUNC: apply __declspec(dllexport) on Windows (GH-141672)
1 parent b866a1c commit 8565ddd

File tree

2 files changed

+18
-15
lines changed

2 files changed

+18
-15
lines changed

Include/exports.h

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@
3535
#define Py_EXPORTED_SYMBOL
3636
#define Py_LOCAL_SYMBOL
3737
#endif
38+
/* module init functions outside the core must be exported */
39+
#if defined(Py_BUILD_CORE)
40+
#define _PyINIT_EXPORTED_SYMBOL Py_EXPORTED_SYMBOL
41+
#else
42+
#define _PyINIT_EXPORTED_SYMBOL __declspec(dllexport)
43+
#endif
3844
#else
3945
/*
4046
* If we only ever used gcc >= 5, we could use __has_attribute(visibility)
@@ -52,19 +58,16 @@
5258
#define Py_EXPORTED_SYMBOL
5359
#define Py_LOCAL_SYMBOL
5460
#endif
61+
#define _PyINIT_EXPORTED_SYMBOL Py_EXPORTED_SYMBOL
5562
#endif
5663

5764
/* only get special linkage if built as shared or platform is Cygwin */
5865
#if defined(Py_ENABLE_SHARED) || defined(__CYGWIN__)
5966
# if defined(HAVE_DECLSPEC_DLL)
6067
# if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
61-
# define PyAPI_FUNC(RTYPE) Py_EXPORTED_SYMBOL RTYPE
62-
# define PyAPI_DATA(RTYPE) extern Py_EXPORTED_SYMBOL RTYPE
6368
/* module init functions inside the core need no external linkage */
6469
/* except for Cygwin to handle embedding */
65-
# if defined(__CYGWIN__)
66-
# define _PyINIT_FUNC_DECLSPEC Py_EXPORTED_SYMBOL
67-
# else /* __CYGWIN__ */
70+
# if !defined(__CYGWIN__)
6871
# define _PyINIT_FUNC_DECLSPEC
6972
# endif /* __CYGWIN__ */
7073
# else /* Py_BUILD_CORE */
@@ -77,12 +80,6 @@
7780
# define PyAPI_FUNC(RTYPE) Py_IMPORTED_SYMBOL RTYPE
7881
# endif /* !__CYGWIN__ */
7982
# define PyAPI_DATA(RTYPE) extern Py_IMPORTED_SYMBOL RTYPE
80-
/* module init functions outside the core must be exported */
81-
# if defined(__cplusplus)
82-
# define _PyINIT_FUNC_DECLSPEC extern "C" Py_EXPORTED_SYMBOL
83-
# else /* __cplusplus */
84-
# define _PyINIT_FUNC_DECLSPEC Py_EXPORTED_SYMBOL
85-
# endif /* __cplusplus */
8683
# endif /* Py_BUILD_CORE */
8784
# endif /* HAVE_DECLSPEC_DLL */
8885
#endif /* Py_ENABLE_SHARED */
@@ -96,13 +93,17 @@
9693
#endif
9794
#ifndef _PyINIT_FUNC_DECLSPEC
9895
# if defined(__cplusplus)
99-
# define _PyINIT_FUNC_DECLSPEC extern "C" Py_EXPORTED_SYMBOL
96+
# define _PyINIT_FUNC_DECLSPEC extern "C" _PyINIT_EXPORTED_SYMBOL
10097
# else /* __cplusplus */
101-
# define _PyINIT_FUNC_DECLSPEC Py_EXPORTED_SYMBOL
98+
# define _PyINIT_FUNC_DECLSPEC _PyINIT_EXPORTED_SYMBOL
10299
# endif /* __cplusplus */
103100
#endif
104101

105-
#define PyMODINIT_FUNC _PyINIT_FUNC_DECLSPEC PyObject*
106-
#define PyMODEXPORT_FUNC _PyINIT_FUNC_DECLSPEC PyModuleDef_Slot*
102+
#ifndef PyMODINIT_FUNC
103+
#define PyMODINIT_FUNC _PyINIT_FUNC_DECLSPEC PyObject*
104+
#endif
105+
#ifndef PyMODEXPORT_FUNC
106+
#define PyMODEXPORT_FUNC _PyINIT_FUNC_DECLSPEC PyModuleDef_Slot*
107+
#endif
107108

108109
#endif /* Py_EXPORTS_H */
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
:c:macro:`PyMODINIT_FUNC` (and the new :c:macro:`PyMODEXPORT_FUNC`) now adds
2+
a linkage declaration (``__declspec(dllexport)``) on Windows.

0 commit comments

Comments
 (0)