Repository: pyinstaller/pyinstaller

Issue:
Marshal error of WindowsPath in find_binary_dependencies()
## Description of the issue

I am trying to package a python project that is based on PySide6 in a windows executable, but PyInstaller fails with an exception.

The value of the "output" variable (see stacktrace below) was:

    (['my_venv\\lib\\site-packages\\numpy\\.libs', WindowsPath('my_venv/Lib/site-packages/shiboken6')], ['my_venv\\Lib\\site-packages\\PySide6'])

### Context information (for bug reports)

* Output of `pyinstaller --version`: **6.1.0**
* Version of Python: **3.9.13 (64-bit)**
* Platform: **Windows 10**
* How you installed Python: **python.org/downloads**
* Did you also try this on another platform? **Not tried**

* try the latest development version: **done, same result**

* follow *all* the instructions in our "If Things Go Wrong" Guide
  (https://github.com/pyinstaller/pyinstaller/wiki/If-Things-Go-Wrong): **done**

### Make sure [everything is packaged correctly](https://github.com/pyinstaller/pyinstaller/wiki/How-to-Report-Bugs#make-sure-everything-is-packaged-correctly)

  * [x] start with clean installation
  * [x] use the latest development version
  * [ ] Run your frozen program **from a command window (shell)** — instead of double-clicking on it
  * [ ] Package your program in **--onedir mode**
  * [ ] Package **without UPX**, say: use the option `--noupx` or set `upx=False` in your .spec-file
  * [ ] Repackage you application in **verbose/debug mode**. For this, pass the option `--debug` to `pyi-makespec` or `pyinstaller` or use `EXE(..., debug=1, ...)` in your .spec file.


### A minimal example program which shows the error

Sorry, can't generate minimal example :-/

### Stacktrace / full error message

    Traceback (most recent call last):
      File "C:\Python39\lib\runpy.py", line 197, in _run_module_as_main
        return _run_code(code, main_globals, None,
      File "C:\Python39\lib\runpy.py", line 87, in _run_code
        exec(code, run_globals)
      File "my_venv\lib\site-packages\PyInstaller\__main__.py", line 209, in <module>
        run()
      File "my_venv\lib\site-packages\PyInstaller\__main__.py", line 189, in run
        run_build(pyi_config, spec_file, **vars(args))
      File "my_venv\lib\site-packages\PyInstaller\__main__.py", line 61, in run_build
        PyInstaller.building.build_main.main(pyi_config, spec_file, **kwargs)
      File "my_venv\lib\site-packages\PyInstaller\building\build_main.py", line 1042, in main
        build(specfile, distpath, workpath, clean_build)
      File "my_venv\lib\site-packages\PyInstaller\building\build_main.py", line 982, in build
        exec(code, spec_namespace)
      File "my_project\build.spec", line 14, in <module>
        a = Analysis(['main.py'],
      File "my_venv\lib\site-packages\PyInstaller\building\build_main.py", line 468, in __init__
        self.__postinit__()
      File "my_venv\lib\site-packages\PyInstaller\building\datastruct.py", line 184, in __postinit__
        self.assemble()
      File "my_venv\lib\site-packages\PyInstaller\building\build_main.py", line 800, in assemble
        self.binaries.extend(find_binary_dependencies(self.binaries, collected_packages))
      File "my_venv\lib\site-packages\PyInstaller\building\build_main.py", line 217, in find_binary_dependencies
        added_dll_directories, added_path_directories = child.call(process_search_paths)
      File "my_venv\lib\site-packages\PyInstaller\isolated\_parent.py", line 319, in call
        raise RuntimeError(f"Child process call to {function.__name__}() failed with:\n" + output)
    RuntimeError: Child process call to process_search_paths() failed with:
      File "my_venv\lib\site-packages\PyInstaller\isolated\_child.py", line 67, in run_next_command
        marshalled = dumps((True, output))
    ValueError: unmarshallable object



Discussion:
Maybe related to #7515
> Maybe related to #7515

Similar, but with different origin.

If you look at lines printed above the error, that offending `WindowsPath` is probably also printed in the line that starts with `"Extra DLL search directories (AddDllDirectory): ...`
Ah, nevermind, those lines would have been printed *after* the problematic part.
Can you try changing this line:
https://github.com/pyinstaller/pyinstaller/blob/04db9fdaeab2bb321a6ffd00eb5949ed7aea07ae/PyInstaller/building/build_main.py#L204
into 
```python
dll_directories = [str(path) for path in os._added_dll_directories]
```
That seems to fix it. The log output now contains:

    INFO: Extra DLL search directories (AddDllDirectory): ['my_venv\\lib\\site-packages\\numpy\\.libs', 'my_venv\\Lib\\site-packages\\shiboken6']
    INFO: Extra DLL search directories (PATH): ['my_venv\\Lib\\site-packages\\PySide6']
Thanks for confirming!

Resolving patch:
diff --git a/PyInstaller/building/build_main.py b/PyInstaller/building/build_main.py
--- a/PyInstaller/building/build_main.py
+++ b/PyInstaller/building/build_main.py
@@ -201,7 +201,9 @@ def process_search_paths():
             """
             import os
 
-            dll_directories = os._added_dll_directories
+            # `os.add_dll_directory` might be called with a `pathlib.Path`, which cannot be marhsalled out of the helper
+            # process. So explicitly convert all entries to strings.
+            dll_directories = [str(path) for path in os._added_dll_directories]
 
             orig_path = set(os._original_path_env.split(os.pathsep))
             modified_path = os.environ.get('PATH', '').split(os.pathsep)


Test patch: