Repository: pyinstaller/pyinstaller

Issue:
Catch exception that ctypes.util.find_library raises
## Description of the issue

As detailed in https://github.com/python/cpython/issues/93094  ctypes.util.find_library can raise an exception (see minimal [example](https://github.com/python/cpython/issues/93094#issue-1244795134)) (occurs in Ubuntu 22.04), which prevents building [Specter Desktop](https://github.com/cryptoadvance/specter-desktop/issues/1710). 

## Proposed solution

Since it is apprarent that     https://github.com/pyinstaller/pyinstaller/blob/bcb824c1ad2989855a5fc01d95e5fc693b2f0966/PyInstaller/depend/utils.py#L186 **can** raise an exception, I suggest:

```
                    try:
                        libname = ctypes.util.find_library(libname)
                    except:
                        libname = None
                        logger.warn('ctypes.util.find_library raised an exception. Supressing.')
```

What do you think?



### Context information (for bug reports)

* Output of `pyinstaller --version`: Tested with 4.9 and 5.1
* Version of Python: 3.8 and 3.10
* Platform: Kubuntu 22.04 LTS x86_64, linux 5.15.0-27-generic
* How you installed Python: anaconda
* Did you also try this on another platform? Ubuntu 20.04 does not raise an exception and works fine.






Discussion:
Sounds good to me although put a comment in saying that `ctypes.util.find_library()` raising an exception is due to a bug in CPython rather than expected behaviour. Otherwise, future me will think that someone put the `try: except:` guard there out of misunderstanding and remove it.
> Sounds good to me although put a comment in saying that `ctypes.util.find_library()` raising an exception is due to a bug in CPython rather than expected behaviour. Otherwise, future me will think that someone put the `try: except:` guard there out of misunderstanding and remove it.

Great.   The https://github.com/python/cpython/issues/93094 bug is very weird, and I will try to dig a little bit deeper.  If I get no result, I will make a PR here soon.   

EDIT:  Ok, https://github.com/python/cpython/issues/93094#issuecomment-1143173452  shows pretty well how the error arises, and it would still be good to make try: except: guards around `ctypes.util.find_library(libname) `

Resolving patch:
diff --git a/PyInstaller/depend/utils.py b/PyInstaller/depend/utils.py
--- a/PyInstaller/depend/utils.py
+++ b/PyInstaller/depend/utils.py
@@ -160,7 +160,14 @@ def __recursively_scan_code_objects_for_ctypes(code: CodeType):
                 # 'libgs.so.9').
                 libname = args[0]
                 if libname:
-                    libname = ctypes.util.find_library(libname)
+                    try:  # this try was inserted due to the ctypes bug https://github.com/python/cpython/issues/93094
+                        libname = ctypes.util.find_library(libname)
+                    except FileNotFoundError:
+                        libname = None
+                        logger.warning(
+                            'ctypes.util.find_library raised a FileNotFoundError. '
+                            'Supressing and assuming no lib with the name "%s" was found.', args[0]
+                        )
                     if libname:
                         # On Windows, `find_library` may return a full pathname. See issue #1934.
                         libname = os.path.basename(libname)


Test patch: