Skip to content

PyMODINIT_FUNC does not apply __declspec(dllexport) on Windows #141671

@encukou

Description

@encukou

The macro is documented as:

Declare an extension module initialization function. This macro:

  • specifies the PyObject* return type,
  • adds any special linkage declarations required by the platform, and
  • for C++, declares the function as extern "C".

Specifically, the special linkage declarations should be __attribute__ ((visibility ("default"))) on GCC/clang, and __declspec(dllexport) on Windows.
However, the definition assumes we're building CPython itself, and only adds a linkage declaration with Py_ENABLE_SHARED -- that is, when Python itself is built with a shared library.

According to MSVC docs:

There are four methods for exporting a definition, listed in recommended order of use:

  1. __declspec(dllexport) in the source code
  2. An EXPORTS statement in a .def file
  3. An /EXPORT specification in a LINK command
  4. A comment directive in the source code, of the form #pragma comment(linker, "/export: definition ").

Setuptools, and historically distutils, does (2) -- and so does, presumably, any other build system for native extensions on Windows.
However, this breaks for modules that define the PEP-793 PyModExport hook.

It would be great to switch to the preferred (1), and then tell Setuptools to avoid /EXPORT for 3.15+.

Linked PRs

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions