Skip to content Skip to sidebar Skip to footer

Which Paths Does Python Ctypes Module Search For Libraries On Mac Os?

Python documentation (I checked both 2.7 and 3.4) states that: On OS X, find_library() tries several predefined naming schemes and paths to locate the library, and returns a full

Solution 1:

The function definition from the source lists the possible paths:

if os.name == "posix"and sys.platform == "darwin":
    from ctypes.macholib.dyld import dyld_find as _dyld_find
    deffind_library(name):
        possible = ['lib%s.dylib' % name,
                    '%s.dylib' % name,
                    '%s.framework/%s' % (name, name)]
        for name in possible:
            try:
                return _dyld_find(name)
            except ValueError:
                continuereturnNone

The relevant functions from macholib.dyld:

defdyld_env(env, var):
    if env isNone:
        env = os.environ
    rval = env.get(var)
    if rval isNone:
        return []
    return rval.split(':')
defdyld_image_suffix(env=None):
    if env isNone:
        env = os.environ
    return env.get('DYLD_IMAGE_SUFFIX')

defdyld_framework_path(env=None):
    return dyld_env(env, 'DYLD_FRAMEWORK_PATH')

defdyld_library_path(env=None):
    return dyld_env(env, 'DYLD_LIBRARY_PATH')

defdyld_fallback_framework_path(env=None):
    return dyld_env(env, 'DYLD_FALLBACK_FRAMEWORK_PATH')

defdyld_fallback_library_path(env=None):
    return dyld_env(env, 'DYLD_FALLBACK_LIBRARY_PATH')


defdyld_image_suffix_search(iterator, env=None):
    """For a potential path iterator, add DYLD_IMAGE_SUFFIX semantics"""
    suffix = dyld_image_suffix(env)
    if suffix isNone:
        return iterator
    def_inject(iterator=iterator, suffix=suffix):
        for path in iterator:
            if path.endswith('.dylib'):
                yield path[:-len('.dylib')] + suffix + '.dylib'else:
                yield path + suffix
            yield path
    return _inject()

defdyld_override_search(name, env=None):
    # If DYLD_FRAMEWORK_PATH is set and this dylib_name is a# framework name, use the first file that exists in the framework# path if any.  If there is none go on to search the DYLD_LIBRARY_PATH# if any.

    framework = framework_info(name)

    if framework isnotNone:
        for path in dyld_framework_path(env):
            yield os.path.join(path, framework['name'])

    # If DYLD_LIBRARY_PATH is set then use the first file that exists# in the path.  If none use the original name.for path in dyld_library_path(env):
        yield os.path.join(path, os.path.basename(name))

defdyld_executable_path_search(name, executable_path=None):
    # If we haven't done any searching and found a library and the# dylib_name starts with "@executable_path/" then construct the# library name.if name.startswith('@executable_path/') and executable_path isnotNone:
        yield os.path.join(executable_path, name[len('@executable_path/'):])

defdyld_find(name, executable_path=None, env=None):
    """
    Find a library or framework using dyld semantics
    """for path in dyld_image_suffix_search(chain(
                dyld_override_search(name, env),
                dyld_executable_path_search(name, executable_path),
                dyld_default_search(name, env),
            ), env):
        if os.path.isfile(path):
            return path
    raise ValueError("dylib %s could not be found" % (name,))

defdyld_default_search(name, env=None):
    yield name

    framework = framework_info(name)

    if framework isnotNone:
        fallback_framework_path = dyld_fallback_framework_path(env)
        for path in fallback_framework_path:
            yield os.path.join(path, framework['name'])

    fallback_library_path = dyld_fallback_library_path(env)
    for path in fallback_library_path:
        yield os.path.join(path, os.path.basename(name))

    if framework isnotNoneandnot fallback_framework_path:
        for path in DEFAULT_FRAMEWORK_FALLBACK:
            yield os.path.join(path, framework['name'])

    ifnot fallback_library_path:
        for path in DEFAULT_LIBRARY_FALLBACK:
            yield os.path.join(path, os.path.basename(name))

The defaults as per man dyld(1)

DEFAULT_FRAMEWORK_FALLBACK = [
    os.path.expanduser("~/Library/Frameworks"),
    "/Library/Frameworks",
    "/Network/Library/Frameworks",
    "/System/Library/Frameworks",
]

DEFAULT_LIBRARY_FALLBACK = [
    os.path.expanduser("~/lib"),
    "/usr/local/lib",
    "/lib",
    "/usr/lib",
]

Post a Comment for "Which Paths Does Python Ctypes Module Search For Libraries On Mac Os?"