diff options
author | Zac Medico <zmedico@gentoo.org> | 2012-10-25 01:34:37 -0700 |
---|---|---|
committer | Zac Medico <zmedico@gentoo.org> | 2012-10-25 01:34:37 -0700 |
commit | 978f3c6d514f0fcf9329d536cc43cf1230e23112 (patch) | |
tree | 9438c9df0111770cbd072d1bac26893d90a363c2 | |
parent | e440548da95775019dc89bb612f8049240eed4e9 (diff) | |
download | portage-978f3c6d514f0fcf9329d536cc43cf1230e23112.tar.gz portage-978f3c6d514f0fcf9329d536cc43cf1230e23112.tar.bz2 portage-978f3c6d514f0fcf9329d536cc43cf1230e23112.zip |
Add portage.util._ctypes module, for bug #439584.
-rw-r--r-- | pym/portage/util/_ctypes.py | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/pym/portage/util/_ctypes.py b/pym/portage/util/_ctypes.py new file mode 100644 index 000000000..4e5aa2a6b --- /dev/null +++ b/pym/portage/util/_ctypes.py @@ -0,0 +1,47 @@ +# Copyright 2012 Gentoo Foundation +# Distributed under the terms of the GNU General Public License v2 + +try: + import ctypes + import ctypes.util +except ImportError: + ctypes = None +else: + try: + ctypes.cdll + except AttributeError: + ctypes = None + +_library_names = {} + +def find_library(name): + """ + Calls ctype.util.find_library() if the ctypes module is available, + and otherwise returns None. Results are cached for future invocations. + """ + filename = _library_names.get(name) + if filename is None: + if ctypes is not None: + filename = ctypes.util.find_library(name) + if filename is None: + filename = False + _library_names[name] = filename + + if filename is False: + return None + return filename + +_library_handles = {} + +def LoadLibrary(name): + """ + Calls ctypes.cdll.LoadLibrary(name) if the ctypes module is available, + and otherwise returns None. Results are cached for future invocations. + """ + handle = _library_handles.get(name) + + if handle is None and ctypes is not None: + handle = ctypes.cdll.LoadLibrary(name) + _library_handles[name] = handle + + return handle |