Back to index

python3.2  3.2.2
Functions
ctypes.util Namespace Reference

Functions

def _get_build_version
def find_msvcrt
def find_library
def _findLib_gcc
def _get_soname
def _num_version
def _findLib_ldconfig
def _findSoname_ldconfig
def test
 test code

Function Documentation

def ctypes.util._findLib_gcc (   name) [private]

Definition at line 89 of file util.py.

00089 
00090     def _findLib_gcc(name):
00091         expr = r'[^\(\)\s]*lib%s\.[^\(\)\s]*' % re.escape(name)
00092         fdout, ccout = tempfile.mkstemp()
00093         os.close(fdout)
00094         cmd = 'if type gcc >/dev/null 2>&1; then CC=gcc; elif type cc >/dev/null 2>&1; then CC=cc;else exit 10; fi;' \
00095               '$CC -Wl,-t -o ' + ccout + ' 2>&1 -l' + name
00096         try:
00097             f = os.popen(cmd)
00098             try:
00099                 trace = f.read()
00100             finally:
00101                 rv = f.close()
00102         finally:
00103             try:
00104                 os.unlink(ccout)
00105             except OSError as e:
00106                 if e.errno != errno.ENOENT:
00107                     raise
00108         if rv == 10:
00109             raise OSError('gcc or cc command not found')
00110         res = re.search(expr, trace)
00111         if not res:
00112             return None
00113         return res.group(0)
00114 

Here is the call graph for this function:

Here is the caller graph for this function:

def ctypes.util._findLib_ldconfig (   name) [private]

Definition at line 174 of file util.py.

00174 
00175         def _findLib_ldconfig(name):
00176             # XXX assuming GLIBC's ldconfig (with option -p)
00177             expr = r'/[^\(\)\s]*lib%s\.[^\(\)\s]*' % re.escape(name)
00178             with contextlib.closing(os.popen('/sbin/ldconfig -p 2>/dev/null')) as f:
00179                 data = f.read()
00180             res = re.search(expr, data)
00181             if not res:
00182                 # Hm, this works only for libs needed by the python executable.
00183                 cmd = 'ldd %s 2>/dev/null' % sys.executable
00184                 with contextlib.closing(os.popen(cmd)) as f:
00185                     data = f.read()
00186                 res = re.search(expr, data)
00187                 if not res:
00188                     return None
00189             return res.group(0)

Here is the call graph for this function:

def ctypes.util._findSoname_ldconfig (   name) [private]

Definition at line 190 of file util.py.

00190 
00191         def _findSoname_ldconfig(name):
00192             import struct
00193             if struct.calcsize('l') == 4:
00194                 machine = os.uname()[4] + '-32'
00195             else:
00196                 machine = os.uname()[4] + '-64'
00197             mach_map = {
00198                 'x86_64-64': 'libc6,x86-64',
00199                 'ppc64-64': 'libc6,64bit',
00200                 'sparc64-64': 'libc6,64bit',
00201                 's390x-64': 'libc6,64bit',
00202                 'ia64-64': 'libc6,IA-64',
00203                 }
00204             abi_type = mach_map.get(machine, 'libc6')
00205 
00206             # XXX assuming GLIBC's ldconfig (with option -p)
00207             expr = r'(\S+)\s+\((%s(?:, OS ABI:[^\)]*)?)\)[^/]*(/[^\(\)\s]*lib%s\.[^\(\)\s]*)' \
00208                    % (abi_type, re.escape(name))
00209             with contextlib.closing(os.popen('LC_ALL=C LANG=C /sbin/ldconfig -p 2>/dev/null')) as f:
00210                 data = f.read()
00211             res = re.search(expr, data)
00212             if not res:
00213                 return None
00214             return res.group(1)

Here is the call graph for this function:

def ctypes.util._get_build_version ( ) [private]
Return the version of MSVC that was used to build Python.

For Python 2.3 and up, the version number is included in
sys.version.  For earlier versions, assume the compiler is MSVC 6.

Definition at line 7 of file util.py.

00007 
00008     def _get_build_version():
00009         """Return the version of MSVC that was used to build Python.
00010 
00011         For Python 2.3 and up, the version number is included in
00012         sys.version.  For earlier versions, assume the compiler is MSVC 6.
00013         """
00014         # This function was copied from Lib/distutils/msvccompiler.py
00015         prefix = "MSC v."
00016         i = sys.version.find(prefix)
00017         if i == -1:
00018             return 6
00019         i = i + len(prefix)
00020         s, rest = sys.version[i:].split(" ", 1)
00021         majorVersion = int(s[:-2]) - 6
00022         minorVersion = int(s[2:3]) / 10.0
00023         # I don't think paths are affected by minor version in version 6
00024         if majorVersion == 6:
00025             minorVersion = 0
00026         if majorVersion >= 6:
00027             return majorVersion + minorVersion
00028         # else we don't know what version of the compiler this is
00029         return None

Here is the call graph for this function:

Here is the caller graph for this function:

def ctypes.util._get_soname (   f) [private]

Definition at line 117 of file util.py.

00117 
00118         def _get_soname(f):
00119             if not f:
00120                 return None
00121             cmd = "/usr/ccs/bin/dump -Lpv 2>/dev/null " + f
00122             with contextlib.closing(os.popen(cmd)) as f:
00123                 data = f.read()
00124             res = re.search(r'\[.*\]\sSONAME\s+([^\s]+)', data)
00125             if not res:
00126                 return None
            return res.group(1)

Here is the call graph for this function:

Here is the caller graph for this function:

def ctypes.util._num_version (   libname) [private]

Definition at line 150 of file util.py.

00150 
00151         def _num_version(libname):
00152             # "libxyz.so.MAJOR.MINOR" => [ MAJOR, MINOR ]
00153             parts = libname.split(".")
00154             nums = []
00155             try:
00156                 while parts:
00157                     nums.insert(0, int(parts.pop()))
00158             except ValueError:
00159                 pass
00160             return nums or [ sys.maxsize ]

Here is the call graph for this function:

def ctypes.util.find_library (   name)

Definition at line 47 of file util.py.

00047 
00048     def find_library(name):
00049         if name in ('c', 'm'):
00050             return find_msvcrt()
00051         # See MSDN for the REAL search order.
00052         for directory in os.environ['PATH'].split(os.pathsep):
00053             fname = os.path.join(directory, name)
00054             if os.path.isfile(fname):
00055                 return fname
00056             if fname.lower().endswith(".dll"):
00057                 continue
00058             fname = fname + ".dll"
00059             if os.path.isfile(fname):
00060                 return fname
00061         return None

Here is the call graph for this function:

Here is the caller graph for this function:

Return the name of the VC runtime dll

Definition at line 30 of file util.py.

00030 
00031     def find_msvcrt():
00032         """Return the name of the VC runtime dll"""
00033         version = _get_build_version()
00034         if version is None:
00035             # better be safe than sorry
00036             return None
00037         if version <= 6:
00038             clibname = 'msvcrt'
00039         else:
00040             clibname = 'msvcr%d' % (version * 10)
00041 
00042         # If python was built with in debug mode
00043         import imp
00044         if imp.get_suffixes()[0][0] == '_d.pyd':
00045             clibname += 'd'
00046         return clibname+'.dll'

Here is the call graph for this function:

Here is the caller graph for this function:

test code

Definition at line 221 of file util.py.

00221 
00222 def test():
00223     from ctypes import cdll
00224     if os.name == "nt":
00225         print(cdll.msvcrt)
00226         print(cdll.load("msvcrt"))
00227         print(find_library("msvcrt"))
00228 
00229     if os.name == "posix":
00230         # find and load_version
00231         print(find_library("m"))
00232         print(find_library("c"))
00233         print(find_library("bz2"))
00234 
        # getattr

Here is the call graph for this function: