def test_cache(): configdir = configure.configdir with open(configdir.join('test_ctypes_platform2.h'), "w") as test_h: test_h.write('#define XYZZY 42\n' "#define large 2147483648L\n") class CConfig: _compilation_info_ = ExternalCompilationInfo( pre_include_lines = ["/* a C comment */", "#include <stdio.h>", "#include <test_ctypes_platform2.h>"], include_dirs = [str(configdir)] ) FILE = configure.Struct('FILE', []) ushort = configure.SimpleType('unsigned short') XYZZY = configure.ConstantInteger('XYZZY') XUZ = configure.Has('XUZ') large = configure.DefinedConstantInteger('large') undef = configure.Defined('really_undefined') res = configure.configure(CConfig) cachefile = configdir.join('cache') dumpcache.dumpcache('', str(cachefile), res) d = {} execfile(str(cachefile), d) assert d['XYZZY'] == res['XYZZY'] assert d['ushort'] == res['ushort'] assert d['FILE']._fields_ == res['FILE']._fields_ assert d['FILE'].__mro__[1:] == res['FILE'].__mro__[1:] assert d['undef'] == res['undef'] assert d['large'] == res['large'] assert d['XUZ'] == res['XUZ']
def test_cache(): configdir = configure.configdir test_h = configdir.join('test_ctypes_platform2.h') test_h.write('#define XYZZY 42\n' "#define large 2147483648L\n") class CConfig: _compilation_info_ = ExternalCompilationInfo( pre_include_lines = ["/* a C comment */", "#include <stdio.h>", "#include <test_ctypes_platform2.h>"], include_dirs = [str(configdir)] ) FILE = configure.Struct('FILE', []) ushort = configure.SimpleType('unsigned short') XYZZY = configure.ConstantInteger('XYZZY') XUZ = configure.Has('XUZ') large = configure.DefinedConstantInteger('large') undef = configure.Defined('really_undefined') res = configure.configure(CConfig) cachefile = configdir.join('cache') dumpcache.dumpcache('', str(cachefile), res) d = {} execfile(str(cachefile), d) assert d['XYZZY'] == res['XYZZY'] assert d['ushort'] == res['ushort'] assert d['FILE']._fields_ == res['FILE']._fields_ assert d['FILE'].__mro__[1:] == res['FILE'].__mro__[1:] assert d['undef'] == res['undef'] assert d['large'] == res['large'] assert d['XUZ'] == res['XUZ']
def _make_ctypes_struct_union(self, name, attributes): class CConfigure(object): _compilation_info_ = configure.ExternalCompilationInfo( pre_include_lines = [], includes = [self.include_file] if self.include_file else [], include_dirs = self.context.options.include_path, post_include_lines = [], libraries = [], library_dirs = [], separate_module_sources = [], separate_module_files = [], ) fields = self._make_struct_attr_list(attributes) setattr(CConfigure, str(name), configure.Struct("struct " + str(name), [(field[0], field[1]) for field in fields])) try: info = configure.configure(CConfigure) except: if len(fields) != 0: raise CompileError(u'Unable to find fields of struct from the C compiler. struct: %s; fields: %s' % (str(name), fields)) else: return [] return [(newField[0], newField[1], field[2]) for field, newField in zip(fields, info[str(name)]._fields_)]
def test_ifdef(): class CConfig: _compilation_info_ = ExternalCompilationInfo( post_include_lines = ['/* a C comment */', '#define XYZZY 42', 'typedef int foo;', 'struct s {', 'int i;', 'double f;' '};']) s = configure.Struct('struct s', [('i', ctypes.c_int)], ifdef='XYZZY') z = configure.Struct('struct z', [('i', ctypes.c_int)], ifdef='FOOBAR') foo = configure.SimpleType('foo', ifdef='XYZZY') bar = configure.SimpleType('bar', ifdef='FOOBAR') res = configure.configure(CConfig) assert res['s'] is not None assert res['z'] is None assert res['foo'] is not None assert res['bar'] is None
def __setup__(): class CConfigure(object): _compilation_info_ = configure.ExternalCompilationInfo( includes=['sched.h', 'numa.h'], libraries=[]) for cname in ['NUMA_NUM_NODES', '__CPU_SETSIZE', '__NCPUBITS']: if cname.startswith('__'): pyname = cname[2:] else: pyname = cname setattr(CConfigure, pyname, configure.ConstantInteger(cname)) return configure.configure(CConfigure)
def __setup__(): class CConfigure(object): _compilation_info_ = configure.ExternalCompilationInfo( includes = ['sched.h', 'numa.h'], libraries = [] ) for cname in ['NUMA_NUM_NODES', '__CPU_SETSIZE', '__NCPUBITS']: if cname.startswith('__'): pyname = cname[2:] else: pyname = cname setattr(CConfigure, pyname, configure.ConstantInteger(cname)) return configure.configure(CConfigure)
def _find_union_size(self, name): class CConfigure(object): _compilation_info_ = configure.ExternalCompilationInfo( pre_include_lines = [], includes = [self.include_file] if self.include_file else [], include_dirs = self.context.options.include_path, post_include_lines = [], libraries = [], library_dirs = [], separate_module_sources = [], separate_module_files = [], ) union_t = configure.SizeOf(name) info = configure.configure(CConfigure) return info['union_t']
def _get_actual_ctype(self, base_name, initial_type): class CConfigure(object): _compilation_info_ = configure.ExternalCompilationInfo( pre_include_lines = [], includes = [self.include_file], include_dirs = self.context.options.include_path, post_include_lines = [], libraries = [], library_dirs = [], separate_module_sources = [], separate_module_files = [], ) c_type = configure.SimpleType(base_name, initial_type) info = configure.configure(CConfigure) return info['c_type']
def test_ifdef(): class CConfig: _compilation_info_ = ExternalCompilationInfo(post_include_lines=[ '/* a C comment */', '#define XYZZY 42', 'typedef int foo;', 'struct s {', 'int i;', 'double f;' '};' ]) s = configure.Struct('struct s', [('i', ctypes.c_int)], ifdef='XYZZY') z = configure.Struct('struct z', [('i', ctypes.c_int)], ifdef='FOOBAR') foo = configure.SimpleType('foo', ifdef='XYZZY') bar = configure.SimpleType('bar', ifdef='FOOBAR') res = configure.configure(CConfig) assert res['s'] is not None assert res['z'] is None assert res['foo'] is not None assert res['bar'] is None
def test_configure(): configdir = configure.configdir test_h = configdir.join('test_ctypes_platform.h') test_h.write('#define XYZZY 42\n') class CConfig: _compilation_info_ = ExternalCompilationInfo( pre_include_lines=[ "/* a C comment */", "#include <stdio.h>", "#include <test_ctypes_platform.h>" ], include_dirs=[str(configdir)]) FILE = configure.Struct('FILE', []) ushort = configure.SimpleType('unsigned short') XYZZY = configure.ConstantInteger('XYZZY') res = configure.configure(CConfig) assert issubclass(res['FILE'], ctypes.Structure) assert res == {'FILE': res['FILE'], 'ushort': ctypes.c_ushort, 'XYZZY': 42}
def test_configure(): configdir = configure.configdir test_h = configdir.join('test_ctypes_platform.h') test_h.write('#define XYZZY 42\n') class CConfig: _compilation_info_ = ExternalCompilationInfo( pre_include_lines = ["/* a C comment */", "#include <stdio.h>", "#include <test_ctypes_platform.h>"], include_dirs = [str(configdir)] ) FILE = configure.Struct('FILE', []) ushort = configure.SimpleType('unsigned short') XYZZY = configure.ConstantInteger('XYZZY') res = configure.configure(CConfig) assert issubclass(res['FILE'], ctypes.Structure) assert res == {'FILE': res['FILE'], 'ushort': ctypes.c_ushort, 'XYZZY': 42}
def test_nested_structs(): class CConfig: _compilation_info_ = ExternalCompilationInfo(post_include_lines=""" struct x { int foo; unsigned long bar; }; struct y { char c; struct x x; }; """.split("\n")) x = configure.Struct("struct x", [("bar", ctypes.c_short)]) y = configure.Struct("struct y", [("x", x)]) res = configure.configure(CConfig) c_x = res["x"] c_y = res["y"] c_y_fields = dict(c_y._fields_) assert issubclass(c_x, ctypes.Structure) assert issubclass(c_y, ctypes.Structure) assert c_y_fields["x"] is c_x
def test_nested_structs(): class CConfig: _compilation_info_ = ExternalCompilationInfo( post_include_lines=""" struct x { int foo; unsigned long bar; }; struct y { char c; struct x x; }; """.split("\n")) x = configure.Struct("struct x", [("bar", ctypes.c_short)]) y = configure.Struct("struct y", [("x", x)]) res = configure.configure(CConfig) c_x = res["x"] c_y = res["y"] c_y_fields = dict(c_y._fields_) assert issubclass(c_x , ctypes.Structure) assert issubclass(c_y, ctypes.Structure) assert c_y_fields["x"] is c_x
""" 'ctypes_configure' source for _hashlib.py. Run this to rebuild _hashlib_cache.py. """ import autopath from ctypes import * from ctypes_configure import configure, dumpcache class CConfig: _compilation_info_ = configure.ExternalCompilationInfo( includes=['openssl/evp.h'], ) EVP_MD = configure.Struct('EVP_MD', []) EVP_MD_CTX = configure.Struct('EVP_MD_CTX', [('digest', c_void_p)]) config = configure.configure(CConfig) dumpcache.dumpcache(__file__, '_hashlib_cache.py', config)
('LOG_CRON', 'LOG_DAEMON'), ('LOG_NEWS', 'LOG_MAIL'), ('LOG_UUCP', 'LOG_MAIL'), ) class SyslogConfigure: _compilation_info_ = ExternalCompilationInfo(includes=['sys/syslog.h']) for key in _CONSTANTS: setattr(SyslogConfigure, key, ConstantInteger(key)) for key in _OPTIONAL_CONSTANTS: setattr(SyslogConfigure, key, DefinedConstantInteger(key)) config = configure(SyslogConfigure) for key in _CONSTANTS: globals()[key] = config[key] optional_constants = [] for key in _OPTIONAL_CONSTANTS: if config[key] is not None: globals()[key] = config[key] optional_constants.append(key) for alias, key in _ALIAS: if alias in optional_constants: continue globals()[alias] = globals()[key] optional_constants.append(alias) del config # Real prototype is:
for cname in ['ZMQ_AFFINITY', 'ZMQ_DOWNSTREAM', 'EADDRINUSE', 'EADDRNOTAVAIL', 'EAGAIN', 'ECONNREFUSED', 'EFAULT', 'EFSM', 'EINPROGRESS', 'EINVAL', 'EMTHREAD', 'ENETDOWN', 'ENOBUFS', 'ENOCOMPATPROTO', 'ENODEV', 'ENOMEM', 'ENOTSUP', 'EPROTONOSUPPORT', 'ETERM', 'ZMQ_FORWARDER', 'ZMQ_HWM', 'ZMQ_IDENTITY', 'ZMQ_MCAST_LOOP', 'ZMQ_NOBLOCK', 'ZMQ_PAIR', 'ZMQ_POLLERR', 'ZMQ_POLLIN', 'ZMQ_POLLOUT', 'ZMQ_PUB', 'ZMQ_PULL', 'ZMQ_PUSH', 'ZMQ_QUEUE', 'ZMQ_RATE', 'ZMQ_RCVBUF', 'ZMQ_RCVMORE', 'ZMQ_RECOVERY_IVL', 'ZMQ_REP', 'ZMQ_REQ', 'ZMQ_SNDBUF', 'ZMQ_SNDMORE', 'ZMQ_STREAMER', 'ZMQ_SUB', 'ZMQ_SUBSCRIBE', 'ZMQ_SWAP', 'ZMQ_UNSUBSCRIBE', 'ZMQ_UPSTREAM', 'ZMQ_XREP', 'ZMQ_XREQ', 'ZMQ_MAX_VSM_SIZE', 'ZMQ_FD', 'ZMQ_EVENTS', 'ZMQ_TYPE', 'ZMQ_LINGER', 'ZMQ_RECONNECT_IVL', 'ZMQ_BACKLOG', 'ZMQ_DEALER', 'ZMQ_ROUTER']: pyname = cname.split('_', 1)[-1] setattr(CConfigure, pyname, configure.ConstantInteger(cname)) info = configure.configure(CConfigure) globals().update(info) # collections of sockopts, based on type: bytes_sockopts = [SUBSCRIBE, UNSUBSCRIBE, IDENTITY] int64_sockopts = [HWM, SWAP, AFFINITY, RATE, RECOVERY_IVL, MCAST_LOOP, SNDBUF, RCVBUF, RCVMORE] int_sockopts = [FD, EVENTS, TYPE, LINGER, RECONNECT_IVL, BACKLOG] class ZMQBaseError(Exception): pass class ZMQError(ZMQBaseError): def __init__(self, errno=None): if errno is None: errno = get_errno() self.strerror = zmq_strerror(errno)
# check for existance of C functions has_write = configure.Has('write') no_xxxwrite = configure.Has('xxxwrite') # check for size of type sizeof_size_t = configure.SizeOf('size_t') # structure, with given hints for interesting fields, # types does not need to be too specific. # all interesting fields would end up with right offset # size and order struct_timeval = configure.Struct('struct timeval', [('tv_sec', ctypes.c_int), ('tv_usec', ctypes.c_int)]) info = configure.configure(CConfigure) assert info['has_write'] assert not info['no_xxxwrite'] assert info['NULL'] == 0 size_t = info['size_t'] print "size_t in ctypes is ", size_t assert ctypes.sizeof(size_t) == info['sizeof_size_t'] assert info['EXISTANT'] assert not info['NOT_EXISTANT'] print print "fields of struct timeval are " for name, value in info['struct_timeval']._fields_: print " ", name, " ", value
def bufferstr(x): if isinstance(x, basestring): return str(x) else: return buffer(x)[:] class CConfig: _compilation_info_ = configure.ExternalCompilationInfo( includes=['openssl/evp.h'], ) EVP_MD = configure.Struct('EVP_MD', []) EVP_MD_CTX = configure.Struct('EVP_MD_CTX', [('digest', c_void_p)]) c = configure.configure(CConfig) EVP_MD_CTX = c['EVP_MD_CTX'] EVP_MD = c['EVP_MD'] def patch_fields(fields): res = [] for k, v in fields: if k == 'digest': res.append((k, POINTER(EVP_MD))) else: res.append((k, v)) return res class EVP_MD_CTX(Structure): _fields_ = patch_fields(EVP_MD_CTX._fields_) del c
'RLIMIT_NICE', 'RLIMIT_RTPRIO', 'RLIMIT_VMEM', 'RUSAGE_BOTH', 'RUSAGE_SELF', 'RUSAGE_CHILDREN', ) # Setup our configure class ResourceConfigure: _compilation_info_ = ExternalCompilationInfo(includes=['sys/resource.h']) rlim_t = SimpleType('rlim_t') for key in _CONSTANTS: setattr(ResourceConfigure, key, ConstantInteger(key)) for key in _OPTIONAL_CONSTANTS: setattr(ResourceConfigure, key, DefinedConstantInteger(key)) # Configure constants and types config = configure(ResourceConfigure) config['rlim_t_max'] = (1<<(sizeof(config['rlim_t']) * 8)) - 1 optional_constants = [] for key in _OPTIONAL_CONSTANTS: if config[key] is not None: optional_constants.append(key) else: del config[key] config['ALL_CONSTANTS'] = _CONSTANTS + tuple(optional_constants) dumpcache(__file__, '_resource_cache.py', config)
_getpagesize = None # Setup our configure class ResourceConfigure: _compilation_info_ = ExternalCompilationInfo(includes=['sys/resource.h']) rlim_t = SimpleType('rlim_t') for key in _CONSTANTS: setattr(ResourceConfigure, key, ConstantInteger(key)) for key in _OPTIONAL_CONSTANTS: setattr(ResourceConfigure, key, DefinedConstantInteger(key)) # Configure constants and types config = configure(ResourceConfigure) rlim_t = config['rlim_t'] for key in _CONSTANTS: globals()[key] = config[key] optional_constants = [] for key in _OPTIONAL_CONSTANTS: if config[key] is not None: globals()[key] = config[key] optional_constants.append(key) del config class ResourceError(OSError): def __init__(self, errno): OSError.__init__(self, errno)
"LOG_LOCAL5", "LOG_LOCAL6", "LOG_LOCAL7", ) _OPTIONAL_CONSTANTS = ("LOG_NOWAIT", "LOG_PERROR", "LOG_SYSLOG", "LOG_CRON", "LOG_UUCP", "LOG_NEWS") # Constant aliases if there are not defined _ALIAS = (("LOG_SYSLOG", "LOG_DAEMON"), ("LOG_CRON", "LOG_DAEMON"), ("LOG_NEWS", "LOG_MAIL"), ("LOG_UUCP", "LOG_MAIL")) class SyslogConfigure: _compilation_info_ = ExternalCompilationInfo(includes=["sys/syslog.h"]) for key in _CONSTANTS: setattr(SyslogConfigure, key, ConstantInteger(key)) for key in _OPTIONAL_CONSTANTS: setattr(SyslogConfigure, key, DefinedConstantInteger(key)) config = configure(SyslogConfigure) for key in _OPTIONAL_CONSTANTS: if config[key] is None: del config[key] for alias, key in _ALIAS: config.setdefault(alias, config[key]) all_constants = config.keys() all_constants.sort() config["ALL_CONSTANTS"] = tuple(all_constants) dumpcache(__file__, "_syslog_cache.py", config)
) XML_Char = configure.SimpleType('XML_Char', c_char) XML_COMBINED_VERSION = configure.ConstantInteger('XML_COMBINED_VERSION') for name in [ 'XML_PARAM_ENTITY_PARSING_NEVER', 'XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE', 'XML_PARAM_ENTITY_PARSING_ALWAYS' ]: locals()[name] = configure.ConstantInteger(name) XML_Encoding = configure.Struct('XML_Encoding', [('data', c_void_p), ('convert', c_void_p), ('release', c_void_p), ('map', c_int * 256)]) XML_Content = configure.Struct('XML_Content', [ ('numchildren', c_int), ('children', c_void_p), ('name', c_char_p), ('type', c_int), ('quant', c_int), ]) # this is insanely stupid XML_FALSE = configure.ConstantInteger('XML_FALSE') XML_TRUE = configure.ConstantInteger('XML_TRUE') config = configure.configure(CConfigure) dumpcache.dumpcache2('pyexpat', config)
'ZMQ_TCP_KEEPALIVE_CNT', 'ZMQ_TCP_KEEPALIVE_IDLE', 'ZMQ_TCP_KEEPALIVE_INTVL', 'ZMQ_TCP_ACCEPT_FILTER', 'ZMQ_EVENT_CONNECTED', 'ZMQ_EVENT_CONNECT_DELAYED', 'ZMQ_EVENT_CONNECT_RETRIED', 'ZMQ_EVENT_LISTENING', 'ZMQ_EVENT_BIND_FAILED', 'ZMQ_EVENT_ACCEPTED', 'ZMQ_EVENT_ACCEPT_FAILED', 'ZMQ_EVENT_CLOSED', 'ZMQ_EVENT_CLOSE_FAILED'] names = None pynames = [] if zmq_version_info()[0] == 2: names = errnos + socket_cons + zmq_base_cons + zmq2_cons else: names = errnos + socket_cons + zmq_base_cons + zmq3_cons for cname in names: pyname = cname.split('_', 1)[-1] pynames.append(pyname) setattr(CConfigure, pyname, configure.ConstantInteger(cname)) _constants = configure.configure(CConfigure) globals().update(_constants) if zmq_version_info()[0] == 2: DONTWAIT = NOBLOCK else: NOBLOCK = DONTWAIT __all__ = pynames
_compilation_info_ = configure.ExternalCompilationInfo( includes=['signal.h', 'sys/types.h', 'unistd.h'], ) pid_t = configure.SimpleType('pid_t', ctypes.c_int) SA_SIGINFO = configure.ConstantInteger('SA_SIGINFO') struct_sigaction = configure.Struct('struct sigaction', [('sa_flags', ctypes.c_int), ('sa_sigaction', sa_sigaction_t)]) siginfo_t = configure.Struct('siginfo_t', [('si_signo', ctypes.c_int), ('si_value', sigval_t)]) _info = configure.configure(_CConfigure) pid_t = _info['pid_t'] struct_sigaction = _info['struct_sigaction'] siginfo_t._fields_ = _info['siginfo_t']._fields_ SA_SIGINFO = _info['SA_SIGINFO'] sigaction = libc.sigaction sigaction.argtypes = [ ctypes.c_int, ctypes.POINTER(struct_sigaction), ctypes.POINTER(struct_sigaction) ] sigaction.restype = ctypes.c_int sigqueue = libc.sigqueue
'LC_MESSAGES', 'LC_ALL', 'LC_PAPER', 'LC_NAME', 'LC_ADDRESS', 'LC_TELEPHONE', 'LC_MEASUREMENT', 'LC_IDENTIFICATION', ) class LocaleConfigure: _compilation_info_ = ExternalCompilationInfo(includes=['locale.h']) for key in _CONSTANTS: setattr(LocaleConfigure, key, ConstantInteger(key)) config = configure(LocaleConfigure) for key in _CONSTANTS: globals()[key] = config[key] del LocaleConfigure del config try: class LanginfoConfigure: _compilation_info_ = ExternalCompilationInfo(includes=['langinfo.h']) nl_item = SimpleType('nl_item') config = configure(LanginfoConfigure) nl_item = config['nl_item'] del LanginfoConfigure del config HAS_LANGINFO = True except:
'LC_ALL', 'LC_PAPER', 'LC_NAME', 'LC_ADDRESS', 'LC_TELEPHONE', 'LC_MEASUREMENT', 'LC_IDENTIFICATION', ) class LocaleConfigure: _compilation_info_ = ExternalCompilationInfo(includes=['locale.h']) for key in _CONSTANTS: setattr(LocaleConfigure, key, ConstantInteger(key)) try: locale_config = configure(LocaleConfigure, noerr=True) except Exception, e: # should probably be moved into configure() # as an optional feature raise ImportError("%s: %s" % (e.__class__, e)) for key in _CONSTANTS: globals()[key] = locale_config[key] del LocaleConfigure del locale_config HAS_LANGINFO = True # Ubuntu Gusty i386 structure class lconv(Structure): _fields_ = (
'LC_MESSAGES', 'LC_NUMERIC', 'LC_ALL', 'CHAR_MAX', ] class LocaleConfigure: _compilation_info_ = ExternalCompilationInfo( includes=['limits.h', 'locale.h']) for key in _CONSTANTS: setattr(LocaleConfigure, key, DefinedConstantInteger(key)) config = configure(LocaleConfigure, noerr=True) for key, value in config.items(): if value is None: del config[key] _CONSTANTS.remove(key) # ____________________________________________________________ eci = ExternalCompilationInfo(includes=['locale.h', 'langinfo.h']) HAS_LANGINFO = check_eci(eci) if HAS_LANGINFO: # list of all possible names langinfo_names = [ "RADIXCHAR", "THOUSEP",