Skip to content

Commit c02bb2f

Browse files
committed
Export PyInit_pkg for pkg.__init__ instead of PyInit___init__
1 parent ff11eed commit c02bb2f

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

distutils/command/build_ext.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -729,7 +729,7 @@ def get_export_symbols(self, ext):
729729
provided, "PyInit_" + module_name. Only relevant on Windows, where
730730
the .pyd file (DLL) must export the module "PyInit_" function.
731731
"""
732-
name = ext.name.split('.')[-1]
732+
name = self._get_module_name_for_symbol(ext)
733733
try:
734734
# Unicode module name support as defined in PEP-489
735735
# https://peps.python.org/pep-0489/#export-hook-name
@@ -744,6 +744,15 @@ def get_export_symbols(self, ext):
744744
ext.export_symbols.append(initfunc_name)
745745
return ext.export_symbols
746746

747+
def _get_module_name_for_symbol(self, ext):
748+
# Package name should be used for `__init__` modules
749+
# https://github.com/python/cpython/issues/80074
750+
# https://github.com/pypa/setuptools/issues/4826
751+
parts = ext.name.split(".")
752+
if parts[-1] == "__init__" and len(parts) >= 2:
753+
return parts[-2]
754+
return parts[-1]
755+
747756
def get_libraries(self, ext): # noqa: C901
748757
"""Return the list of libraries to link against when building a
749758
shared extension. On most platforms, this is just 'ext.libraries';

distutils/tests/test_build_ext.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,19 @@ def test_unicode_module_names(self):
345345
assert cmd.get_export_symbols(modules[0]) == ['PyInit_foo']
346346
assert cmd.get_export_symbols(modules[1]) == ['PyInitU_f_1gaa']
347347

348+
def test_export_symbols__init__(self):
349+
# https://github.com/python/cpython/issues/80074
350+
# https://github.com/pypa/setuptools/issues/4826
351+
modules = [
352+
Extension('foo.__init__', ['aaa']),
353+
Extension('föö.__init__', ['uuu']),
354+
]
355+
dist = Distribution({'name': 'xx', 'ext_modules': modules})
356+
cmd = self.build_ext(dist)
357+
cmd.ensure_finalized()
358+
assert cmd.get_export_symbols(modules[0]) == ['PyInit_foo']
359+
assert cmd.get_export_symbols(modules[1]) == ['PyInitU_f_1gaa']
360+
348361
def test_compiler_option(self):
349362
# cmd.compiler is an option and
350363
# should not be overridden by a compiler instance

0 commit comments

Comments
 (0)