Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9538bc9

Browse files
authored
bpo-39432: Implement PEP-489 algorithm for non-ascii "PyInit_*" symbol names in distutils (GH-18150)
Make it export the correct init symbol also on Windows. https://bugs.python.org/issue39432
1 parent 850a4bd commit 9538bc9

3 files changed

Lines changed: 23 additions & 1 deletion

File tree

Lib/distutils/command/build_ext.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -689,7 +689,15 @@ def get_export_symbols(self, ext):
689689
provided, "PyInit_" + module_name. Only relevant on Windows, where
690690
the .pyd file (DLL) must export the module "PyInit_" function.
691691
"""
692-
initfunc_name = "PyInit_" + ext.name.split('.')[-1]
692+
suffix = '_' + ext.name.split('.')[-1]
693+
try:
694+
# Unicode module name support as defined in PEP-489
695+
# https://www.python.org/dev/peps/pep-0489/#export-hook-name
696+
suffix.encode('ascii')
697+
except UnicodeEncodeError:
698+
suffix = 'U' + suffix.encode('punycode').replace(b'-', b'_').decode('ascii')
699+
700+
initfunc_name = "PyInit" + suffix
693701
if initfunc_name not in ext.export_symbols:
694702
ext.export_symbols.append(initfunc_name)
695703
return ext.export_symbols

Lib/distutils/tests/test_build_ext.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,19 @@ def test_get_source_files(self):
304304
cmd.ensure_finalized()
305305
self.assertEqual(cmd.get_source_files(), ['xxx'])
306306

307+
def test_unicode_module_names(self):
308+
modules = [
309+
Extension('foo', ['aaa'], optional=False),
310+
Extension('föö', ['uuu'], optional=False),
311+
]
312+
dist = Distribution({'name': 'xx', 'ext_modules': modules})
313+
cmd = self.build_ext(dist)
314+
cmd.ensure_finalized()
315+
self.assertRegex(cmd.get_ext_filename(modules[0].name), r'foo\..*')
316+
self.assertRegex(cmd.get_ext_filename(modules[1].name), r'föö\..*')
317+
self.assertEqual(cmd.get_export_symbols(modules[0]), ['PyInit_foo'])
318+
self.assertEqual(cmd.get_export_symbols(modules[1]), ['PyInitU_f_gkaa'])
319+
307320
def test_compiler_option(self):
308321
# cmd.compiler is an option and
309322
# should not be overridden by a compiler instance
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Implement PEP-489 algorithm for non-ascii "PyInit\_..." symbol names in distutils to make it export the correct init symbol also on Windows.

0 commit comments

Comments
 (0)