def test_configure(): test_h = udir.join('test_ctypes_platform.h') test_h.write('#define XYZZY 42\n') class CConfig: _header_ = """ /* a C comment */ #include <stdio.h> #include <test_ctypes_platform.h> """ _include_dirs_ = [str(udir)] FILE = ctypes_platform.Struct('FILE', []) ushort = ctypes_platform.SimpleType('unsigned short') XYZZY = ctypes_platform.ConstantInteger('XYZZY') res = ctypes_platform.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: _header_ = """ struct x { int foo; unsigned long bar; }; struct y { char c; struct x x; }; """ x = ctypes_platform.Struct("struct x", [("bar", ctypes.c_short)]) y = ctypes_platform.Struct("struct y", [("x", x)]) res = ctypes_platform.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_ifdef(): class CConfig: _header_ = """ /* a C comment */ #define XYZZY 42 typedef int foo; struct s { int i; double f; }; """ s = ctypes_platform.Struct('struct s', [('i', ctypes.c_int)], ifdef='XYZZY') z = ctypes_platform.Struct('struct z', [('i', ctypes.c_int)], ifdef='FOOBAR') foo = ctypes_platform.SimpleType('foo', ifdef='XYZZY') bar = ctypes_platform.SimpleType('bar', ifdef='FOOBAR') res = ctypes_platform.configure(CConfig) assert res['s'] is not None assert res['z'] is None assert res['foo'] is not None assert res['bar'] is None
size_t = ctypes_platform.SimpleType("size_t", c_ulong) BUFSIZ = ctypes_platform.ConstantInteger("BUFSIZ") SEEK_SET = ctypes_platform.ConstantInteger("SEEK_SET") constants = {} constant_names = ['BZ_RUN', 'BZ_FLUSH', 'BZ_FINISH', 'BZ_OK', 'BZ_RUN_OK', 'BZ_FLUSH_OK', 'BZ_FINISH_OK', 'BZ_STREAM_END', 'BZ_SEQUENCE_ERROR', 'BZ_PARAM_ERROR', 'BZ_MEM_ERROR', 'BZ_DATA_ERROR', 'BZ_DATA_ERROR_MAGIC', 'BZ_IO_ERROR', 'BZ_UNEXPECTED_EOF', 'BZ_OUTBUFF_FULL', 'BZ_CONFIG_ERROR'] for name in constant_names: setattr(CConfig, name, ctypes_platform.DefinedConstantInteger(name)) class cConfig: pass cConfig.__dict__.update(ctypes_platform.configure(CConfig)) for name in constant_names: value = getattr(cConfig, name) if value is not None: constants[name] = value locals().update(constants) libbz2 = cConfig.libbz2 off_t = cConfig.off_t BUFSIZ = cConfig.BUFSIZ SEEK_SET = cConfig.SEEK_SET BZ_OK = cConfig.BZ_OK BZ_STREAM_END = cConfig.BZ_STREAM_END BZ_CONFIG_ERROR = cConfig.BZ_CONFIG_ERROR BZ_PARAM_ERROR = cConfig.BZ_PARAM_ERROR
import pypy.rpython.rctypes.implementation from pypy.rpython.rctypes.tool import util from pypy.rpython.rctypes.tool.ctypes_platform import Library, configure import distutils.errors prefix = '/usr/local' try: class CConfig: _header_ = '' _includes_ = ['SDL.h'] _include_dirs_ = [prefix+'/include/SDL'] SDL = Library('SDL') cconfig = configure(CConfig) libSDL = cconfig['SDL'] except distutils.errors.CCompilerError: libSDL = CDLL(prefix+'/lib/libSDL.dylib') STRING = c_char_p WSTRING = c_wchar_p SDLK_w = 119 SDLK_KP3 = 259 SDL_NUMEVENTS = 32 SDL_KEYDOWNMASK = 4 DUMMY_ENUM_VALUE = 0 SDLK_2 = 50 SDL_EVENT_RESERVED5 = 21 CD_STOPPED = 1
from pypy.interpreter.error import OperationError from pypy.interpreter.baseobjspace import ObjSpace, W_Root from pypy.rpython.rctypes.tool import ctypes_platform from pypy.rpython.rctypes.tool.util import find_library, load_library import sys from ctypes import * class CConfig: _includes_ = ('unistd.h',) if sys.platform != 'darwin': cryptlib = ctypes_platform.Library('crypt') globals().update(ctypes_platform.configure(CConfig)) if sys.platform == 'darwin': dllname = find_library('c') assert dllname is not None cryptlib = cdll.LoadLibrary(dllname) c_crypt = cryptlib.crypt c_crypt.argtypes = [c_char_p, c_char_p] c_crypt.restype = c_char_p def crypt(space, word, salt): """word will usually be a user's password. salt is a 2-character string which will be used to select one of 4096 variations of DES. The characters in salt must be either ".", "/", or an alphanumeric character. Returns the hashed password as a string, which will be composed of characters from the same alphabet as the salt.""" res = c_crypt(word, salt)
FD_SETSIZE = ctypes_platform.ConstantInteger("FD_SETSIZE") SSL_CTRL_OPTIONS = ctypes_platform.ConstantInteger("SSL_CTRL_OPTIONS") BIO_C_SET_NBIO = ctypes_platform.ConstantInteger("BIO_C_SET_NBIO") pollfd = ctypes_platform.Struct("struct pollfd", [("fd", c_int), ("events", c_short), ("revents", c_short)]) nfds_t = ctypes_platform.SimpleType("nfds_t", c_uint) POLLOUT = ctypes_platform.ConstantInteger("POLLOUT") POLLIN = ctypes_platform.ConstantInteger("POLLIN") class cConfig: pass cConfig.__dict__.update(ctypes_platform.configure(CConfig)) OPENSSL_VERSION_NUMBER = cConfig.OPENSSL_VERSION_NUMBER HAVE_OPENSSL_RAND = OPENSSL_VERSION_NUMBER >= 0x0090500fL SSL_FILETYPE_PEM = cConfig.SSL_FILETYPE_PEM SSL_OP_ALL = cConfig.SSL_OP_ALL SSL_VERIFY_NONE = cConfig.SSL_VERIFY_NONE SSL_ERROR_WANT_READ = cConfig.SSL_ERROR_WANT_READ SSL_ERROR_WANT_WRITE = cConfig.SSL_ERROR_WANT_WRITE SSL_ERROR_ZERO_RETURN = cConfig.SSL_ERROR_ZERO_RETURN SSL_ERROR_WANT_X509_LOOKUP = cConfig.SSL_ERROR_WANT_X509_LOOKUP SSL_ERROR_WANT_CONNECT = cConfig.SSL_ERROR_WANT_CONNECT SSL_ERROR_SYSCALL = cConfig.SSL_ERROR_SYSCALL SSL_ERROR_SSL = cConfig.SSL_ERROR_SSL FD_SETSIZE = cConfig.FD_SETSIZE SSL_CTRL_OPTIONS = cConfig.SSL_CTRL_OPTIONS
has_mremap = hasattr(libc, "mremap") if has_mremap: CConfig.MREMAP_MAYMOVE = ( ctypes_platform.DefinedConstantInteger("MREMAP_MAYMOVE")) elif _MS_WINDOWS: CConfig._includes_ += ("windows.h",) constant_names = ['PAGE_READONLY', 'PAGE_READWRITE', 'PAGE_WRITECOPY', 'FILE_MAP_READ', 'FILE_MAP_WRITE', 'FILE_MAP_COPY', 'DUPLICATE_SAME_ACCESS'] for name in constant_names: setattr(CConfig, name, ctypes_platform.ConstantInteger(name)) # export the constants inside and outside. see __init__.py constants.update(ctypes_platform.configure(CConfig)) if _POSIX: # MAP_ANONYMOUS is not always present but it's always available at CPython level if constants["MAP_ANONYMOUS"] is None: constants["MAP_ANONYMOUS"] = constants["MAP_ANON"] assert constants["MAP_ANONYMOUS"] is not None constants["MAP_ANON"] = constants["MAP_ANONYMOUS"] locals().update(constants) _ACCESS_DEFAULT, ACCESS_READ, ACCESS_WRITE, ACCESS_COPY = range(4) PTR = POINTER(c_char) # cannot use c_void_p as return value of functions :-(