gh-99948: Support ctypes.util.find_library in emscripten environment (#138519)

Adds support for ctypes loading .so files directly.

---------

Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
Co-authored-by: Russell Keith-Magee <russell@keith-magee.com>
This commit is contained in:
Gyeongjae Choi 2025-09-17 19:17:56 +09:00 committed by GitHub
parent 69a5ea5340
commit 218b8dbc78
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 88 additions and 0 deletions

View file

@ -173,6 +173,25 @@ def find_library(name):
fname = f"{directory}/lib{name}.so"
return fname if os.path.isfile(fname) else None
elif sys.platform == "emscripten":
def _is_wasm(filename):
# Return True if the given file is an WASM module
wasm_header = b"\x00asm"
with open(filename, 'br') as thefile:
return thefile.read(4) == wasm_header
def find_library(name):
candidates = [f"lib{name}.so", f"lib{name}.wasm"]
paths = os.environ.get("LD_LIBRARY_PATH", "")
for libdir in paths.split(":"):
for name in candidates:
libfile = os.path.join(libdir, name)
if os.path.isfile(libfile) and _is_wasm(libfile):
return libfile
return None
elif os.name == "posix":
# Andreas Degert's find functions, using gcc, /sbin/ldconfig, objdump
import re, tempfile