Skip to content

Commit 22dcb69

Browse files
eli-schwartzjpakkane
authored andcommitted
python module: implicitly add python dep to extensions
If there isn't a preexisting dependency on python, append one. It's almost assuredly needed, so just do the right thing out of the box.
1 parent 43c6031 commit 22dcb69

File tree

6 files changed

+35
-15
lines changed

6 files changed

+35
-15
lines changed

docs/markdown/Python-module.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,9 @@ Additionally, the following diverge from [[shared_module]]'s default behavior:
9999
of Python that support this (the python headers define `PyMODINIT_FUNC` has
100100
default visibility).
101101

102-
`extension_module` does not add any dependencies to the library so
103-
user may need to add `dependencies : py_installation.dependency()`,
104-
see [[dependency]].
102+
*since 0.63.0* `extension_module` automatically adds a dependency to the library
103+
if one is not explicitly provided. To support older versions, the user may need to
104+
add `dependencies : py_installation.dependency()`, see [[dependency]].
105105

106106
**Returns**: a [[@build_tgt]] object
107107

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
## Python extension modules now depend on the python library by default
2+
3+
Python extension modules are usually expected to link to the python library
4+
and/or its headers in order to build correctly (via the default `embed: false`,
5+
which may not actually link to the library itself). This means that every
6+
single use of `.extension_module()` needed to include the `dependencies:
7+
py_installation.dependency()` kwarg explicitly.
8+
9+
In the interest of doing the right thing out of the box, this is now the
10+
default for extension modules that don't already include a dependency on
11+
python. This is not expected to break anything, because it should always be
12+
needed. Nevertheless, `py_installation.dependency().partial_dependency()` will
13+
be detected as already included while providing no compile/link args.

mesonbuild/modules/python.py

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -516,16 +516,26 @@ def extension_module_method(self, args: T.List['TYPE_var'], kwargs: 'TYPE_kwargs
516516

517517
kwargs['install_dir'] = os.path.join(self.platlib_install_path, subdir)
518518

519-
# On macOS and some Linux distros (Debian) distutils doesn't link
520-
# extensions against libpython. We call into distutils and mirror its
521-
# behavior. See https://github.com/mesonbuild/meson/issues/4117
522-
if not self.link_libpython:
523-
new_deps = []
524-
for dep in mesonlib.extract_as_list(kwargs, 'dependencies'):
525-
if isinstance(dep, _PythonDependencyBase):
519+
new_deps = []
520+
has_pydep = False
521+
for dep in mesonlib.extract_as_list(kwargs, 'dependencies'):
522+
if isinstance(dep, _PythonDependencyBase):
523+
has_pydep = True
524+
# On macOS and some Linux distros (Debian) distutils doesn't link
525+
# extensions against libpython. We call into distutils and mirror its
526+
# behavior. See https://github.com/mesonbuild/meson/issues/4117
527+
if not self.link_libpython:
526528
dep = dep.get_partial_dependency(compile_args=True)
527-
new_deps.append(dep)
528-
kwargs['dependencies'] = new_deps
529+
new_deps.append(dep)
530+
if not has_pydep:
531+
pydep = self._dependency_method_impl({})
532+
if not pydep.found():
533+
raise mesonlib.MesonException('Python dependency not found')
534+
new_deps.append(pydep)
535+
FeatureNew.single_use('python_installation.extension_module with implicit dependency on python',
536+
'0.63.0', self.subproject, 'use python_installation.dependency()',
537+
self.current_node)
538+
kwargs['dependencies'] = new_deps
529539

530540
# msys2's python3 has "-cpython-36m.dll", we have to be clever
531541
# FIXME: explain what the specific cleverness is here

test cases/python/2 extmodule/ext/meson.build

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
pylib = py.extension_module('tachyon',
22
'tachyon_module.c',
3-
dependencies : py_dep,
43
c_args: '-DMESON_MODULENAME="tachyon"',
54
install: true,
65
)

test cases/python/2 extmodule/ext/nested/meson.build

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
py.extension_module('tachyon',
22
'../tachyon_module.c',
3-
dependencies : py_dep,
43
c_args: '-DMESON_MODULENAME="nested.tachyon"',
54
install: true,
65
subdir: 'nested'

test cases/python/2 extmodule/ext/wrongdir/meson.build

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
py.extension_module('tachyon',
22
'../tachyon_module.c',
3-
dependencies : py_dep,
43
c_args: '-DMESON_MODULENAME="tachyon"',
54
install: true,
65
install_dir: get_option('libdir')

0 commit comments

Comments
 (0)