Source

pypy / ctypes_configure / doc / sample.py

from ctypes_configure import configure
import ctypes

class CConfigure:
    _compilation_info_ = configure.ExternalCompilationInfo(
        
        # all lines landing in C header before includes
        pre_include_lines = [],

        # list of .h files to include
        includes = ['time.h', 'sys/time.h', 'unistd.h'],

        # list of directories to search for include files
        include_dirs = [],

        # all lines landing in C header after includes
        post_include_lines = [],

        # libraries to link with
        libraries = [],

        # library directories
        library_dirs = [],

        # additional C sources to compile with (that go to
        # created .c files)
        separate_module_sources = [],

        # additional existing C source file names
        separate_module_files = [],
        )

    # get real int type out of hint and name
    size_t = configure.SimpleType('size_t', ctypes.c_int)

    # grab value of numerical #define
    NULL = configure.ConstantInteger('NULL')

    # grab #define, whether it's defined or not
    EXISTANT = configure.Defined('NULL')
    NOT_EXISTANT = configure.Defined('XXXNOTNULL')

    # 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
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.