Python ctypes.RTLD_GLOBAL Examples

The following are 30 code examples of ctypes.RTLD_GLOBAL(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module ctypes , or try the search function .
Example #1
Source File: simple_julia_py.py    From restrain-jit with MIT License 6 votes vote down vote up
def get_jl_lib(conf) -> JuliaPreLoad:
    jl = conf["julia"]
    lib_path = jl["lib"]
    sys_image = jl["image"]
    binary = jl["bin"]

    lib = ctypes.PyDLL(lib_path, ctypes.RTLD_GLOBAL)
    lib.jl_eval_string.argtypes = [ctypes.c_char_p]
    lib.jl_eval_string.restype = ctypes.c_void_p

    try:
        init = lib.jl_init_with_image
    except AttributeError:
        init = lib.jl_init_with_image__threading

    return JuliaPreLoad(
        lambda: init(binary.encode(), sys_image.encode()), lib) 
Example #2
Source File: ctypes_utils.py    From sharpy with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def import_ctypes_lib(route, libname):
    lib_path = route + libname
    if platform.system() == 'Darwin':
        ext = '.dylib'
    elif platform.system() == 'Linux':
        ext = '.so'
    else:
        raise NotImplementedError('The platform ' + platform.system() + 'is not supported')

    lib_path += ext
    lib_path = os.path.abspath(lib_path)
    try:
        library = ct.CDLL(lib_path, mode=ct.RTLD_GLOBAL)
    except:
        import traceback
        import sys
        traceback.print_exc(file=sys.stderr)
        sys.exit(1)
    return library 
Example #3
Source File: __init__.py    From pySINDy with MIT License 6 votes vote down vote up
def _load_libzmq():
    """load bundled libzmq if there is one"""
    import sys, ctypes, platform, os
    dlopen = hasattr(sys, 'getdlopenflags') # unix-only
    # RTLD flags are added to os in Python 3
    # get values from os because ctypes values are WRONG on pypy
    PYPY = platform.python_implementation().lower() == 'pypy'
    
    if dlopen:
        dlflags = sys.getdlopenflags()
        # set RTLD_GLOBAL, unset RTLD_LOCAL
        flags = ctypes.RTLD_GLOBAL | dlflags
        # ctypes.RTLD_LOCAL is 0 on pypy, which is *wrong*
        flags &= ~ getattr(os, 'RTLD_LOCAL', 4)
        # pypy on darwin needs RTLD_LAZY for some reason
        if PYPY and sys.platform == 'darwin':
            flags |= getattr(os, 'RTLD_LAZY', 1)
            flags &= ~ getattr(os, 'RTLD_NOW', 2)
        sys.setdlopenflags(flags)
    try:
        from . import libzmq
    except ImportError:
        pass
    else:
        # store libzmq as zmq._libzmq for backward-compat
        globals()['_libzmq'] = libzmq
        if PYPY:
            # some versions of pypy (5.3 < ? < 5.8) needs explicit CDLL load for some reason,
            # otherwise symbols won't be globally available
            # do this unconditionally because it should be harmless (?)
            ctypes.CDLL(libzmq.__file__, ctypes.RTLD_GLOBAL)
    finally:
        if dlopen:
            sys.setdlopenflags(dlflags) 
Example #4
Source File: __init__.py    From Carnets with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def _load_libzmq():
    """load bundled libzmq if there is one"""
    import sys, ctypes, platform, os
    dlopen = hasattr(sys, 'getdlopenflags') # unix-only
    # RTLD flags are added to os in Python 3
    # get values from os because ctypes values are WRONG on pypy
    PYPY = platform.python_implementation().lower() == 'pypy'
    
    if dlopen:
        dlflags = sys.getdlopenflags()
        # set RTLD_GLOBAL, unset RTLD_LOCAL
        flags = ctypes.RTLD_GLOBAL | dlflags
        # ctypes.RTLD_LOCAL is 0 on pypy, which is *wrong*
        flags &= ~ getattr(os, 'RTLD_LOCAL', 4)
        # pypy on darwin needs RTLD_LAZY for some reason
        if PYPY and sys.platform == 'darwin':
            flags |= getattr(os, 'RTLD_LAZY', 1)
            flags &= ~ getattr(os, 'RTLD_NOW', 2)
        sys.setdlopenflags(flags)
    try:
        from . import libzmq
    except ImportError:
        pass
    else:
        # store libzmq as zmq._libzmq for backward-compat
        globals()['_libzmq'] = libzmq
        if PYPY:
            # some versions of pypy (5.3 < ? < 5.8) needs explicit CDLL load for some reason,
            # otherwise symbols won't be globally available
            # do this unconditionally because it should be harmless (?)
            ctypes.CDLL(libzmq.__file__, ctypes.RTLD_GLOBAL)
    finally:
        if dlopen:
            sys.setdlopenflags(dlflags) 
Example #5
Source File: server.py    From training_results_v0.6 with Apache License 2.0 6 votes vote down vote up
def _server_env(load_library):
    """Server environment function return temp dir"""
    temp = util.tempdir()

    # pylint: disable=unused-variable
    @register_func("tvm.rpc.server.workpath")
    def get_workpath(path):
        return temp.relpath(path)

    @register_func("tvm.rpc.server.load_module", override=True)
    def load_module(file_name):
        """Load module from remote side."""
        path = temp.relpath(file_name)
        m = _load_module(path)
        logger.info("load_module %s", path)
        return m

    libs = []
    load_library = load_library.split(":") if load_library else []
    for file_name in load_library:
        file_name = find_lib_path(file_name)[0]
        libs.append(ctypes.CDLL(file_name, ctypes.RTLD_GLOBAL))
        logger.info("Load additional library %s", file_name)
    temp.libs = libs
    return temp 
Example #6
Source File: coin.py    From GridCal with GNU General Public License v3.0 6 votes vote down vote up
def COINMP_DLL_load_dll(path):
    """
    function that loads the DLL useful for debugging installation problems
    """
    import ctypes
    if os.name == 'nt':
        lib = ctypes.windll.LoadLibrary(str(path[-1]))
    else:
        # linux hack to get working
        mode = ctypes.RTLD_GLOBAL
        for libpath in path[:-1]:
            # RTLD_LAZY = 0x00001
            ctypes.CDLL(libpath, mode=mode)

        lib = ctypes.CDLL(path[-1], mode=mode)

    return lib 
Example #7
Source File: __init__.py    From vnpy_crypto with MIT License 6 votes vote down vote up
def _load_libzmq():
    """load bundled libzmq if there is one"""
    import sys, ctypes, platform, os
    dlopen = hasattr(sys, 'getdlopenflags') # unix-only
    # RTLD flags are added to os in Python 3
    # get values from os because ctypes values are WRONG on pypy
    PYPY = platform.python_implementation().lower() == 'pypy'
    
    if dlopen:
        dlflags = sys.getdlopenflags()
        # set RTLD_GLOBAL, unset RTLD_LOCAL
        flags = ctypes.RTLD_GLOBAL | dlflags
        # ctypes.RTLD_LOCAL is 0 on pypy, which is *wrong*
        flags &= ~ getattr(os, 'RTLD_LOCAL', 4)
        # pypy on darwin needs RTLD_LAZY for some reason
        if PYPY and sys.platform == 'darwin':
            flags |= getattr(os, 'RTLD_LAZY', 1)
            flags &= ~ getattr(os, 'RTLD_NOW', 2)
        sys.setdlopenflags(flags)
    try:
        from . import libzmq
    except ImportError:
        pass
    else:
        # store libzmq as zmq._libzmq for backward-compat
        globals()['_libzmq'] = libzmq
        if PYPY:
            # some versions of pypy (5.3 < ? < 5.8) needs explicit CDLL load for some reason,
            # otherwise symbols won't be globally available
            # do this unconditionally because it should be harmless (?)
            ctypes.CDLL(libzmq.__file__, ctypes.RTLD_GLOBAL)
    finally:
        if dlopen:
            sys.setdlopenflags(dlflags) 
Example #8
Source File: utils.py    From qutebrowser with GNU General Public License v3.0 5 votes vote down vote up
def libgl_workaround() -> None:
    """Work around QOpenGLShaderProgram issues, especially for Nvidia.

    See https://bugs.launchpad.net/ubuntu/+source/python-qt4/+bug/941826
    """
    if os.environ.get('QUTE_SKIP_LIBGL_WORKAROUND'):
        return

    libgl = ctypes.util.find_library("GL")
    if libgl is not None:  # pragma: no branch
        ctypes.CDLL(libgl, mode=ctypes.RTLD_GLOBAL) 
Example #9
Source File: wrapper.py    From python-ovrsdk with Do What The F*ck You Want To Public License 5 votes vote down vote up
def load(self,path):
        """Given a path to a library, load it."""
        try:
            # Darwin requires dlopen to be called with mode RTLD_GLOBAL instead
            # of the default RTLD_LOCAL.  Without this, you end up with
            # libraries not being loadable, resulting in "Symbol not found"
            # errors
            if sys.platform == 'darwin':
                return ctypes.CDLL(path, ctypes.RTLD_GLOBAL)
            else:
                return ctypes.cdll.LoadLibrary(path)
        except OSError,e:
            raise ImportError(e) 
Example #10
Source File: backend_ctypes.py    From quickstart-git2s3 with Apache License 2.0 5 votes vote down vote up
def __init__(self):
        self.RTLD_LAZY = 0   # not supported anyway by ctypes
        self.RTLD_NOW  = 0
        self.RTLD_GLOBAL = ctypes.RTLD_GLOBAL
        self.RTLD_LOCAL = ctypes.RTLD_LOCAL 
Example #11
Source File: backend_ctypes.py    From quickstart-git2s3 with Apache License 2.0 5 votes vote down vote up
def __init__(self):
        self.RTLD_LAZY = 0   # not supported anyway by ctypes
        self.RTLD_NOW  = 0
        self.RTLD_GLOBAL = ctypes.RTLD_GLOBAL
        self.RTLD_LOCAL = ctypes.RTLD_LOCAL 
Example #12
Source File: c_lib.py    From MAgent with MIT License 5 votes vote down vote up
def _load_lib():
    """ Load library in build/lib. """
    cur_path = os.path.dirname(os.path.abspath(os.path.expanduser(__file__)))
    lib_path = os.path.join(cur_path, "../../build/")
    if platform.system() == 'Darwin':
        path_to_so_file = os.path.join(lib_path, "libmagent.dylib")
    elif platform.system() == 'Linux':
        path_to_so_file = os.path.join(lib_path, "libmagent.so")
    else:
        raise BaseException("unsupported system: " + platform.system())
    lib = ctypes.CDLL(path_to_so_file, ctypes.RTLD_GLOBAL)
    return lib 
Example #13
Source File: backend_ctypes.py    From odoo13-x64 with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self):
        self.RTLD_LAZY = 0   # not supported anyway by ctypes
        self.RTLD_NOW  = 0
        self.RTLD_GLOBAL = ctypes.RTLD_GLOBAL
        self.RTLD_LOCAL = ctypes.RTLD_LOCAL 
Example #14
Source File: _base.py    From Aurora with Apache License 2.0 5 votes vote down vote up
def _load_lib():
    """Load libary in build/lib."""
    lib_root = Path(__file__).parents[2]
    lib_path = os.path.join(lib_root, 'cuda/build/lib/')
    path_to_so_file = os.path.join(lib_path, "libc_runtime_api.so")
    lib = ctypes.CDLL(path_to_so_file, ctypes.RTLD_GLOBAL)
    return lib


# global library instance 
Example #15
Source File: backend.py    From dragon with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def dlopen_guard(extra_flags=ctypes.RTLD_GLOBAL):
    old_flags = None
    if _getdlopenflags and _setdlopenflags:
        old_flags = _getdlopenflags()
        _setdlopenflags(old_flags | extra_flags)
    try:
        yield
    finally:
        if old_flags is not None:
            _setdlopenflags(old_flags) 
Example #16
Source File: backend_ctypes.py    From odoo12-x64 with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self):
        self.RTLD_LAZY = 0   # not supported anyway by ctypes
        self.RTLD_NOW  = 0
        self.RTLD_GLOBAL = ctypes.RTLD_GLOBAL
        self.RTLD_LOCAL = ctypes.RTLD_LOCAL 
Example #17
Source File: util.py    From dm_control with Apache License 2.0 5 votes vote down vote up
def _maybe_load_linux_dynamic_deps(library_dir):
  """Ensures that GL and GLEW symbols are available on Linux."""
  interpreter_symbols = ctypes.cdll.LoadLibrary("")
  if not hasattr(interpreter_symbols, "glewInit"):
    # This means our interpreter is not yet linked against GLEW.
    if _render.BACKEND == "osmesa":
      libglew_path = os.path.join(library_dir, "libglewosmesa.so")
    elif _render.BACKEND == "egl":
      libglew_path = os.path.join(library_dir, "libglewegl.so")
    else:
      libglew_path = ctypes.util.find_library("GLEW")
    ctypes.CDLL(libglew_path, ctypes.RTLD_GLOBAL)  # Also loads GL implicitly. 
Example #18
Source File: __init__.py    From incubator-tvm with Apache License 2.0 5 votes vote down vote up
def load_lib():
    """Load library, the functions will be registered into TVM"""
    curr_path = os.path.dirname(os.path.abspath(os.path.expanduser(__file__)))
    # load in as global so the global extern symbol is visible to other dll.
    lib = ctypes.CDLL(
        os.path.join(curr_path, "../../lib/libtvm_ext.so"), ctypes.RTLD_GLOBAL)
    return lib 
Example #19
Source File: backend_ctypes.py    From quickstart-redhat-openshift with Apache License 2.0 5 votes vote down vote up
def __init__(self):
        self.RTLD_LAZY = 0   # not supported anyway by ctypes
        self.RTLD_NOW  = 0
        self.RTLD_GLOBAL = ctypes.RTLD_GLOBAL
        self.RTLD_LOCAL = ctypes.RTLD_LOCAL 
Example #20
Source File: c_lib.py    From mfrl with MIT License 5 votes vote down vote up
def _load_lib():
    """ Load library in build/lib. """
    cur_path = os.path.dirname(os.path.abspath(os.path.expanduser(__file__)))
    lib_path = os.path.join(cur_path, "../../build/")
    if platform.system() == 'Darwin':
        path_to_so_file = os.path.join(lib_path, "libmagent.dylib")
    elif platform.system() == 'Linux':
        path_to_so_file = os.path.join(lib_path, "libmagent.so")
    else:
        raise BaseException("unsupported system: " + platform.system())
    lib = ctypes.CDLL(path_to_so_file, ctypes.RTLD_GLOBAL)
    return lib 
Example #21
Source File: xenomai.py    From OpenPLC_Editor with GNU General Public License v3.0 5 votes vote down vote up
def TryPreloadXenomai():
    """
    Xenomai 3 (at least for version <= 3.0.6) do not handle properly dlclose
    of shared objects whose dlopen did trigger xenomai_init.
    As a workaround, this pre-loads xenomai libraries that need to be
    initialized and call xenomai_init once for all.

    Xenomai auto init of libs MUST be disabled (see --auto-init-solib in xeno-config)
    """
    try:
        for name in ["cobalt", "modechk", "copperplate", "alchemy"]:
            globals()[name] = CDLL("lib"+name+".so", mode=RTLD_GLOBAL)
        cobalt.xenomai_init(pointer(c_int(0)), pointer((POINTER(c_char)*2)(create_string_buffer("prog_name"), None)))
    except Exception:
        pass 
Example #22
Source File: aot.py    From relay-aot with Apache License 2.0 5 votes vote down vote up
def load_lib(name):
    return ctypes.CDLL(name, ctypes.RTLD_GLOBAL) 
Example #23
Source File: server.py    From incubator-tvm with Apache License 2.0 5 votes vote down vote up
def _server_env(load_library, work_path=None):
    """Server environment function return temp dir"""
    if work_path:
        temp = work_path
    else:
        temp = util.tempdir()

    # pylint: disable=unused-variable
    @tvm._ffi.register_func("tvm.rpc.server.workpath", override=True)
    def get_workpath(path):
        return temp.relpath(path)

    @tvm._ffi.register_func("tvm.rpc.server.load_module", override=True)
    def load_module(file_name):
        """Load module from remote side."""
        path = temp.relpath(file_name)
        m = _load_module(path)
        logger.info("load_module %s", path)
        return m

    libs = []
    load_library = load_library.split(":") if load_library else []
    for file_name in load_library:
        file_name = find_lib_path(file_name)[0]
        libs.append(ctypes.CDLL(file_name, ctypes.RTLD_GLOBAL))
        logger.info("Load additional library %s", file_name)
    temp.libs = libs
    return temp 
Example #24
Source File: base.py    From incubator-tvm with Apache License 2.0 5 votes vote down vote up
def _load_lib():
    """Load libary by searching possible path."""
    lib_path = libinfo.find_lib_path()
    lib = ctypes.CDLL(lib_path[0], ctypes.RTLD_GLOBAL)
    lib.TVMGetLastError.restype = ctypes.c_char_p
    return lib, os.path.basename(lib_path[0]) 
Example #25
Source File: simulator.py    From incubator-tvm with Apache License 2.0 5 votes vote down vote up
def _load_sw():
    """Load hardware library for simulator."""

    env = get_env()
    lib_driver_name = "libvta_tsim" if env.TARGET == "tsim" else "libvta_fsim"

    # Load driver library
    lib_driver = find_libvta(lib_driver_name, optional=True)
    assert lib_driver
    try:
        libs = [ctypes.CDLL(lib_driver[0], ctypes.RTLD_GLOBAL)]
    except OSError:
        return []

    if env.TARGET == "tsim":
        lib_hw = find_libvta("libvta_hw", optional=True)
        assert lib_hw # make sure to make in ${VTA_HW_PATH}/hardware/chisel
        try:
            f = tvm.get_global_func("vta.tsim.init")
            m = tvm.runtime.load_module(lib_hw[0], "vta-tsim")
            f(m)
            return lib_hw
        except OSError:
            return []

    return libs 
Example #26
Source File: impl.py    From incubator-tvm with Apache License 2.0 5 votes vote down vote up
def _load_lib():
    """Load libary by searching possible path."""
    curr_path = os.path.dirname(os.path.realpath(os.path.expanduser(__file__)))
    lib_search = [curr_path, os.path.dirname(curr_path)]
    lib_path = libinfo.find_lib_path(_get_lib_names(), lib_search, optional=True)
    if lib_path is None:
        return None, None
    lib = ctypes.CDLL(lib_path[0], ctypes.RTLD_GLOBAL)
    return lib, os.path.basename(lib_path[0]) 
Example #27
Source File: simulator.py    From training_results_v0.6 with Apache License 2.0 5 votes vote down vote up
def _load_lib():
    """Load local library, assuming they are simulator."""
    lib_path = find_libvta(optional=True)
    if not lib_path:
        return []
    try:
        return [ctypes.CDLL(lib_path[0], ctypes.RTLD_GLOBAL)]
    except OSError:
        return [] 
Example #28
Source File: solvers.py    From MatchingMarkets.py with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def COINMP_DLL_load_dll(path):
    """
    function that loads the DLL useful for debugging installation problems
    """
    import ctypes
    if os.name == 'nt':
        lib = ctypes.windll.LoadLibrary(str(path[-1]))
    else:
        #linux hack to get working
        mode = ctypes.RTLD_GLOBAL
        for libpath in path[:-1]:
            #RTLD_LAZY = 0x00001
            ctypes.CDLL(libpath, mode = mode)
        lib = ctypes.CDLL(path[-1], mode = mode)
    return lib 
Example #29
Source File: base.py    From dgl with Apache License 2.0 5 votes vote down vote up
def _load_lib():
    """Load libary by searching possible path."""
    lib_path = libinfo.find_lib_path()
    lib = ctypes.CDLL(lib_path[0], ctypes.RTLD_GLOBAL)
    # DMatrix functions
    lib.DGLGetLastError.restype = ctypes.c_char_p
    return lib, os.path.basename(lib_path[0])

# version number 
Example #30
Source File: librace.py    From racepwn with MIT License 5 votes vote down vote up
def load(self,path):
        """Given a path to a library, load it."""
        try:
            # Darwin requires dlopen to be called with mode RTLD_GLOBAL instead
            # of the default RTLD_LOCAL.  Without this, you end up with
            # libraries not being loadable, resulting in "Symbol not found"
            # errors
            if sys.platform == 'darwin':
                return ctypes.CDLL(path, ctypes.RTLD_GLOBAL)
            else:
                return ctypes.cdll.LoadLibrary(path)
        except OSError as e:
            raise ImportError(e)