msys2 / MSYS2-packages

Package scripts for MSYS2.
https://packages.msys2.org
BSD 3-Clause "New" or "Revised" License
1.28k stars 483 forks source link

python find_library doesn't behave as expected #2357

Open ktbarrett opened 3 years ago

ktbarrett commented 3 years ago

Describe the issue

Attempting to get the path to the libpython, I try the following:

>>> sysconfig.get_config_var("DLLLIBRARY")
'msys-pythonX.X.dll'
>>> ctypes.util.find_library("msys-pythonX.X.dll")
>>> ctypes.util.find_library("pythonX.X.dll")`
'msys-pythonX.X.dll'
>>> ctypes.CDLL("msys-pythonX.X.dll")
<CDLL 'msys-python3.8.dll', handle 5e6ab0000 at 0x6ffffff16790>

This seems a bit off... find_library doesn't seem to be doing anything useful here.

On Linux, Mac, Windows, and MinGW environments it returns the full path to the library, if found.

Steps to Reproduce the Problem

  1. Run
    import sysconfig, ctypes.util
    ctypes.util.find_library(sysconfig.get_config_var("DLLLIBRARY"))
  2. Have path to Python object
ktbarrett commented 3 years ago

Cannot work around this either since I can't seem to access kernel32?

ktbarrett commented 3 years ago

Patch for find_library is here. It doesn't seem to take LD_LIBRARY_PATH into account? And it only considers investigating import libraries, returning their linked dynamic library instead of a file path. It doesn't seem like it was implemented correctly when compared against the documentation.

lazka commented 3 years ago

Here is the upstream patch: https://cygwin.com/git-cygwin-packages/?p=git/cygwin-packages/python38.git;a=blob;f=3.7-ctypes-cygwin.patch;h=62e360cb4bced797afe8ea80d1524b4d38892e49;hb=HEAD

I guess the thing is that we have sys.platform == "msys", so we should adjust that patch. Can you try changing that file on your machine to check for "msys" there and see if it helps?

ktbarrett commented 3 years ago

As installed I see the check is elif sys.platform in ["cygwin", "msys"]: Not sure where that is coming from, it's not upstream. And the observed behavior follows the patch (same as what you posted as well), which is what doesn't seem correct.

lazka commented 3 years ago

ah, then there is probably another patch on top of that somewhere.

ktbarrett commented 3 years ago

Should I take this issue upstream? Or should I replace the cygwin patch with something more correct? In the meantime is there an alternative way to get the full path to a library? If I had access to kernel32, I could use GetModuleFileName to get the file path.

ktbarrett commented 3 years ago

I tried using dladdr to get the filename, with no luck. I don't think there is a way to solve this until the original patch is updated to also search for DLLs. I'll work on that soon.