question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ctypes.util.find_library doesn't work with python 3

See original GitHub issue

We talked about this in chat before, I just wanted to make an actual issue for this: ctypes.util.find_library doesn’t work when using python3crystax out of the box, because it seems to try something with a shell that doesn’t work due to a missing binary.

This code makes it work when put before any ctypes.util import (it includes a PySDL2-specific hack without which PySDL2 will fail to load - this one might actually need addressing in the recipe for PySDL2):

def apply_hack():
    import ctypes.util
    orig_func = ctypes.util.find_library
    def android_find_library_hack(*args):
        import os
        name = args[0]

        # Truncate ending for easier comparison:
        if name.find(".so.") >= 0:
            name = name.partition(".so.")[0]
        if name.endswith(".so"):
            name = name[:-len(".so")]
        if not name.endswith("."):
            name += "."

        # Helper function to check lib name:
        def check_name(lib_name, search_name):
            if filename.endswith('.so') and (
                    filename.startswith("lib" + search_name) or
                    filename.startswith(search_name)):
                return True
            if search_name.endswith("-2.0."):  # PySDL2 hack
                search_name = search_name[:-len("-2.0.")] + "."
                return check_name(lib_name, search_name)
            return False

        # Check the user app lib dir and system dir:
        app_root = os.path.normpath(os.path.abspath(os.path.join(
            os.path.dirname(__file__), '..', '..', 'lib')))
        sys_dir = "/system/lib"
        for lib_search in [app_root, sys_dir]:
            if not os.path.exists(lib_search):
                continue
            for filename in os.listdir(lib_search):
                if check_name(filename, name):
                    return os.path.join(lib_search, filename)
        return orig_func(*args)
    import ctypes.util
    ctypes.util.find_library = android_find_library_hack
apply_hack()

I don’t know enough to put this into python-for-android, but maybe someone with the knowledge can put this into the python launch wrapper somehow to make it magically work for everyone? Feel free to take it, consider it CC0/public domain (it’s vaguely based on python-for-android’s hack for Python 2 anyway)

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
ghostcommented, Jan 31, 2019

Oh it had the ticket number #1337 😍 haha

1reaction
inclementcommented, Aug 25, 2018

I think there wouldn’t be an issue with applying this monkey patch in the start.c, it would be nicer than having to patch python itself.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Issue 42580: ctypes.util.find_library("libc") fails - Python tracker
As I said before, ctypes.util.find_library("c") works in both 3.8 and 3.9. no, it doesn't work (and it shouldn't) neither in python 3.8 nor ......
Read more >
Why can't this library load using ctypes? - Stack Overflow
On linux, I am unable to get find_library to return the found library name. The full file name is libmystuff.so and it sits...
Read more >
Don't use ctypes.util.find_library in ipaclient - freeipa - Pagure.io
I just found out that it is causing AVCs. I'm not sure I like hardcoding the soname, how about a pure-python dumbed-down solution...
Read more >
16.16. ctypes — A foreign function library for Python
ctypes is a foreign function library for Python. It provides C compatible data types, and allows calling functions in DLLs or shared libraries....
Read more >
775839 – dev-lang/python: find_library broken on macOS
https://docs.python.org/3/library/ctypes.html#finding-shared-libraries ... I think we need to add Gentoo-specific magic here to load at ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found