diff options
author | Armin Rigo <arigo@tunes.org> | 2015-05-12 11:07:25 +0200 |
---|---|---|
committer | Armin Rigo <arigo@tunes.org> | 2015-05-12 11:07:25 +0200 |
commit | fe4bb73d2191ea7b2ee5586848e1c5bbbcbaa72b (patch) | |
tree | 5c80d5c2ee50bc8e99039c7483522e2febc08cb7 /demo | |
parent | 34dbd9932de50a5de29f0fdaab9e9a06526d93a7 (diff) | |
download | cffi-fe4bb73d2191ea7b2ee5586848e1c5bbbcbaa72b.tar.gz |
the big Moving Files Around step
Diffstat (limited to 'demo')
-rw-r--r-- | demo/manual.c | 169 | ||||
-rw-r--r-- | demo/manual2.py | 34 | ||||
-rw-r--r-- | demo/setup_manual.py | 5 |
3 files changed, 208 insertions, 0 deletions
diff --git a/demo/manual.c b/demo/manual.c new file mode 100644 index 0000000..38539b8 --- /dev/null +++ b/demo/manual.c @@ -0,0 +1,169 @@ +#include "_cffi_include.h" + + +#define AA (42) +#define BB (&bb) +static int bb = 16261; + +int foo42(int a, int *b) +{ + return a - *b; +} + +int foo64(int a) +{ + return ~a; +} + +struct foo_s { + int a; +}; + +/************************************************************/ + +static void *_cffi_types[] = { + _CFFI_OP(_CFFI_OP_FUNCTION, 1), + _CFFI_OP(_CFFI_OP_PRIMITIVE, _CFFI_PRIM_INT), + _CFFI_OP(_CFFI_OP_POINTER, 1), + _CFFI_OP(_CFFI_OP_FUNCTION_END, 0), + _CFFI_OP(_CFFI_OP_FUNCTION, 1), + _CFFI_OP(_CFFI_OP_PRIMITIVE, _CFFI_PRIM_INT), + _CFFI_OP(_CFFI_OP_FUNCTION_END, 0), + _CFFI_OP(_CFFI_OP_STRUCT_UNION, 0), +}; + +#ifndef PYPY_VERSION +static PyObject * +_cffi_f_foo42(PyObject *self, PyObject *args) +{ + int x0; + int * x1; + Py_ssize_t datasize; + int result; + PyObject *arg0; + PyObject *arg1; + + if (!PyArg_ParseTuple(args, "OO:foo42", &arg0, &arg1)) + return NULL; + + x0 = _cffi_to_c_int(arg0, int); + if (x0 == (int)-1 && PyErr_Occurred()) + return NULL; + + datasize = _cffi_prepare_pointer_call_argument( + _cffi_types[1], arg1, (char **)&x1); + if (datasize != 0) { + if (datasize < 0) + return NULL; + x1 = alloca(datasize); + memset((void *)x1, 0, datasize); + if (_cffi_convert_array_from_object((char *)x1, _cffi_types[1], arg1) < 0) + return NULL; + } + + Py_BEGIN_ALLOW_THREADS + _cffi_restore_errno(); + { result = foo42(x0, x1); } + _cffi_save_errno(); + Py_END_ALLOW_THREADS + + return _cffi_from_c_int(result, int); +} +#else +static int _cffi_f_foo42(int x0, int *x1) +{ + return foo42(x0, x1); +} +#endif + +#ifndef PYPY_VERSION +static PyObject * +_cffi_f_foo64(PyObject *self, PyObject *arg0) +{ + int x0; + int result; + + x0 = _cffi_to_c_int(arg0, int); + if (x0 == (int)-1 && PyErr_Occurred()) + return NULL; + + Py_BEGIN_ALLOW_THREADS + _cffi_restore_errno(); + { result = foo64(x0); } + _cffi_save_errno(); + Py_END_ALLOW_THREADS + + return _cffi_from_c_int(result, int); +} +#else +static int _cffi_f_foo64(int x0) +{ + return foo64(x0); +} +#endif + +static int _cffi_const_AA(unsigned long long *output) +{ + *output = (unsigned long long)((AA) << 0); // integer + return (AA) <= 0; +} + +static void _cffi_const_BB(char *output) +{ + *(int **)output = BB; +} + +static const struct _cffi_global_s _cffi_globals[] = { + { "AA", &_cffi_const_AA, _CFFI_OP(_CFFI_OP_CONSTANT_INT, 0) }, + { "BB", &_cffi_const_BB, _CFFI_OP(_CFFI_OP_CONSTANT, 2) }, + { "bb", &bb, _CFFI_OP(_CFFI_OP_GLOBAL_VAR, 1) }, + { "foo42", &_cffi_f_foo42, _CFFI_OP(_CFFI_OP_CPYTHON_BLTN_V, 0) }, + { "foo64", &_cffi_f_foo64, _CFFI_OP(_CFFI_OP_CPYTHON_BLTN_O, 4) }, +}; + +struct _cffi_align_foo_s { char x; struct foo_s y; }; + +static const struct _cffi_struct_union_s _cffi_struct_unions[] = { + { "foo_s", 7, 0, + sizeof(struct foo_s), + offsetof(struct _cffi_align_foo_s, y), + 1, 0 }, +}; + +static const struct _cffi_field_s _cffi_fields[] = { + { "a", offsetof(struct foo_s, a), sizeof(((struct foo_s *)0)->a), + _CFFI_OP(_CFFI_OP_NOOP, 1) }, +}; + +static const struct _cffi_type_context_s _cffi_type_context = { + _cffi_types, + _cffi_globals, + _cffi_fields, + _cffi_struct_unions, + NULL, + NULL, + 5, /* num_globals */ + 1, /* num_struct_unions */ + 0, + 0, + NULL, + 8, /* num_types */ +}; + +#ifndef PYPY_VERSION +PyMODINIT_FUNC +initmanual(void) +{ + if (_cffi_init() < 0) + return; + + _cffi_init_module("manual", &_cffi_type_context); +} +#else +PyMODINIT_FUNC +_cffi_pypyinit_manual(const void *p[]) +{ + p[0] = (const void *)0x10000f0; + p[1] = &_cffi_type_context; +} +#endif diff --git a/demo/manual2.py b/demo/manual2.py new file mode 100644 index 0000000..d5c49b0 --- /dev/null +++ b/demo/manual2.py @@ -0,0 +1,34 @@ +import _cffi_backend + +ffi = _cffi_backend.FFI(b"manual2", + _version = 0x2600, + _types = b'\x00\x00\x01\x0D\x00\x00\x07\x01\x00\x00\x00\x0F\x00\x00\x00\x09\x00\x00\x00\x0B\x00\x00\x01\x03', + _globals = (b'\xff\xff\xff\x0bAA',0,b'\xff\xff\xff\x0bBB',-1,b'\xff\xff\xff\x0bCC',2,b'\xff\xff\xff\x1fFOO',0x9999999999999999,b'\x00\x00\x00#close',0,b'\x00\x00\x05#stdout',0), + _struct_unions = ((b'\x00\x00\x00\x03\x00\x00\x00\x00point_s',b'\x00\x00\x01\x11\xff\xff\xff\xffx',b'\x00\x00\x01\x11\xff\xff\xff\xffy'),), + _enums = (b'\x00\x00\x00\x04\x00\x00\x00\x07myenum_e\x00AA,BB,CC',), + _typenames = (b'\x00\x00\x00\x01myint_t',), +) + + + +# trying it out +lib = ffi.dlopen(None) +assert lib.AA == 0 +assert lib.BB == -1 +assert lib.FOO == 0x9999999999999999 +x = lib.close(-42) +assert x == -1 + +print lib.stdout + +print ffi.new("struct point_s *") +print ffi.offsetof("struct point_s", "x") +print ffi.offsetof("struct point_s", "y") +print ffi.new("struct point_s[CC]") +assert ffi.sizeof("struct point_s[CC]") == 2 * ffi.sizeof("struct point_s") + +print ffi.cast("enum myenum_e", 2) +print ffi.cast("myint_t", -2) +assert ffi.typeof("myint_t") == ffi.typeof("int") + +del ffi, lib diff --git a/demo/setup_manual.py b/demo/setup_manual.py new file mode 100644 index 0000000..2569bb4 --- /dev/null +++ b/demo/setup_manual.py @@ -0,0 +1,5 @@ +from distutils.core import setup +from distutils.extension import Extension +setup(name='manual', + ext_modules=[Extension(name='manual', + sources=['manual.c'])]) |