Repository: mesonbuild/meson

Issue:
pymod.find_installation with modules is broken on conda Windows since 0.63.0
**Describe the bug**

Today I observed while updating the [xraylib conda-forge feedstock](https://github.com/conda-forge/xraylib-feedstock/pull/40) that the build fails while trying to execute `pymod.find_installation(get_option('python'), modules: ['numpy',])` on Windows, when using meson 0.63.x

I did a bit of debugging and realised that the command works fine, as long as I do not add the modules kwarg (which is not a realistic option for me though): the python interpreter is found but it fails at loading the requested modules.

Since this build works fine for 0.62.x, I had a look at the changes that could have led to this, and realised quickly that https://github.com/mesonbuild/meson/pull/10309 is the culprit: I confirmed this with a custom build of meson 0.63.1 with the patch from that PR reverted.

**To Reproduce**
You will need to create a Windows conda environment with meson, ninja and numpy installed from the conda-forge channel, and I expect that any `meson.build` file containing:

```meson
    pymod = import('python')
    python = pymod.find_installation(get_option('python'), modules: ['numpy',])
```

will error out.

**Expected behavior**
I expected the same behaviour from older releases

**system parameters**
* Is this a [cross build](https://mesonbuild.com/Cross-compilation.html) or just a plain native build (for the same computer)? No
* what operating system (e.g. MacOS Catalina, Windows 10, CentOS 8.0, Ubuntu 18.04, etc.) Windows 10
* what Python version are you using: 3.7, 3.8, 3.9 and 3.10
* what `meson --version`: 0.63.1
* what `ninja --version` if it's a Ninja build: 1.11.0



Discussion:
I have no knowledge of how or why `os.add_dll_directory()` is relevant, and I pointed that out in the PR which added it when I abstained from approving the PR.

Do you happen to know why it's good in some cases and bad in others? Maybe it's always bad?

Resolving patch:
diff --git a/mesonbuild/modules/python.py b/mesonbuild/modules/python.py
--- a/mesonbuild/modules/python.py
+++ b/mesonbuild/modules/python.py
@@ -765,22 +765,10 @@ def find_installation(self, state: 'ModuleState', args: T.Tuple[T.Optional[str]]
         found_modules: T.List[str] = []
         missing_modules: T.List[str] = []
         if python.found() and want_modules:
-            # Python 3.8.x or later require add_dll_directory() to be called on Windows if
-            # the needed modules require external DLLs that are not bundled with the modules.
-            # Simplify things by calling add_dll_directory() on the paths in %PATH%
-            add_paths_cmd = ''
-            if hasattr(os, 'add_dll_directory'):
-                add_paths_cmds = []
-                paths = os.environ['PATH'].split(os.pathsep)
-                for path in paths:
-                    if path != '' and os.path.isdir(path):
-                        add_paths_cmds.append(f'os.add_dll_directory({path!r})')
-                add_paths_cmd = 'import os;' + ';'.join(reversed(add_paths_cmds)) + ';'
-
             for mod in want_modules:
                 p, *_ = mesonlib.Popen_safe(
                     python.command +
-                    ['-c', f'{add_paths_cmd}import {mod}'])
+                    ['-c', f'import {mod}'])
                 if p.returncode != 0:
                     missing_modules.append(mod)
                 else:


Test patch: