summaryrefslogtreecommitdiff
path: root/demo
diff options
context:
space:
mode:
authormattip <matti.picus@gmail.com>2015-11-24 21:28:16 +0200
committermattip <matti.picus@gmail.com>2015-11-24 21:28:16 +0200
commit5e30323cdb30a9cf386e14a8900957ada5ee9b05 (patch)
treef5b41b105eed56c1e5f521cde88c4e1039258695 /demo
parentee2e12f801b23389ef98cb17a2b630ae08f23537 (diff)
downloadcffi-5e30323cdb30a9cf386e14a8900957ada5ee9b05.tar.gz
update gmp demo
Diffstat (limited to 'demo')
-rw-r--r--demo/btrfs-snap.py3
-rw-r--r--demo/fastcsv.py2
-rw-r--r--demo/gmp.py36
-rw-r--r--demo/gmp_build.py27
-rw-r--r--demo/xclient.py8
-rw-r--r--demo/xclient_build.py10
6 files changed, 57 insertions, 29 deletions
diff --git a/demo/btrfs-snap.py b/demo/btrfs-snap.py
index 3224db4..f8e711f 100644
--- a/demo/btrfs-snap.py
+++ b/demo/btrfs-snap.py
@@ -22,9 +22,6 @@ ffi.cdef("""
};
""")
-v = ffi.verify("#include <btrfs/ioctl.h>")
-
-
parser = argparse.ArgumentParser(usage=__doc__.strip())
parser.add_argument('source', help='source subvolume')
diff --git a/demo/fastcsv.py b/demo/fastcsv.py
index 7f04107..e4f7faf 100644
--- a/demo/fastcsv.py
+++ b/demo/fastcsv.py
@@ -26,7 +26,7 @@ def _make_ffi_from_dialect(dialect):
else:
d['is_escape_char'] = '&& 0'
- lib = ffi.verify(r'''
+ ffi.set_source('_fastcsv', r'''
typedef enum {
START_RECORD, START_FIELD, ESCAPED_CHAR, IN_FIELD,
diff --git a/demo/gmp.py b/demo/gmp.py
index 9677a24..8cec6f7 100644
--- a/demo/gmp.py
+++ b/demo/gmp.py
@@ -1,33 +1,29 @@
import sys
-import cffi
-#
-# This is only a demo based on the GMP library.
-# There is a rather more complete version available at:
-# http://bazaar.launchpad.net/~tolot-solar-empire/+junk/gmpy_cffi/files
-#
+# If the build script was run immediately before this script, the cffi module
+# ends up in the current directory. Make sure we can import it.
+sys.path.append('.')
-ffi = cffi.FFI()
+try:
+ from _gmp import ffi, lib
+except ImportError:
+ print 'run gmp_build first, then make sure the shared object is on sys.path'
+ sys.exit(-1)
-ffi.cdef("""
-
- typedef struct { ...; } MP_INT;
- typedef MP_INT mpz_t[1];
-
- int mpz_init_set_str (MP_INT *dest_integer, char *src_cstring, int base);
- void mpz_add (MP_INT *sum, MP_INT *addend1, MP_INT *addend2);
- char * mpz_get_str (char *string, int base, MP_INT *integer);
-
-""")
-
-lib = ffi.verify("#include <gmp.h>",
- libraries=['gmp', 'm'])
+# ffi "knows" about the declared variables and functions from the
+# cdef parts of the module xclient_build created,
+# lib "knows" how to call the functions from the set_source parts
+# of the module.
# ____________________________________________________________
a = ffi.new("mpz_t")
b = ffi.new("mpz_t")
+if len(sys.argv) < 3:
+ print 'call as %s bigint1, bigint2' % sys.argv[0]
+ sys.exit(-1)
+
lib.mpz_init_set_str(a, sys.argv[1], 10) # Assume decimal integers
lib.mpz_init_set_str(b, sys.argv[2], 10) # Assume decimal integers
lib.mpz_add(a, a, b) # a=a+b
diff --git a/demo/gmp_build.py b/demo/gmp_build.py
new file mode 100644
index 0000000..516cbc7
--- /dev/null
+++ b/demo/gmp_build.py
@@ -0,0 +1,27 @@
+import cffi
+
+#
+# This is only a demo based on the GMP library.
+# There is a rather more complete (but outdated) version available at:
+# http://bazaar.launchpad.net/~tolot-solar-empire/+junk/gmpy_cffi/files
+#
+
+ffi = cffi.FFI()
+
+ffi.cdef("""
+
+ typedef struct { ...; } MP_INT;
+ typedef MP_INT mpz_t[1];
+
+ int mpz_init_set_str (MP_INT *dest_integer, char *src_cstring, int base);
+ void mpz_add (MP_INT *sum, MP_INT *addend1, MP_INT *addend2);
+ char * mpz_get_str (char *string, int base, MP_INT *integer);
+
+""")
+
+ffi.set_source('_gmp', "#include <gmp.h>",
+ libraries=['gmp', 'm'])
+
+if __name__ == '__main__':
+ ffi.compile()
+
diff --git a/demo/xclient.py b/demo/xclient.py
index b8c7232..37e7597 100644
--- a/demo/xclient.py
+++ b/demo/xclient.py
@@ -7,9 +7,15 @@ sys.path.append('.')
try:
from _xclient import ffi, lib
except ImportError:
- print 'run %s_build first, then make sure the shared object is on sys.path' % os.path.splitext(__file__)[0]
+ print 'run xclient_build first, then make sure the shared object is on sys.path'
sys.exit(-1)
+# ffi "knows" about the declared variables and functions from the
+# cdef parts of the module xclient_build created,
+# lib "knows" how to call the functions from the set_source parts
+# of the module.
+
+
class XError(Exception):
pass
diff --git a/demo/xclient_build.py b/demo/xclient_build.py
index b55f69f..57ec008 100644
--- a/demo/xclient_build.py
+++ b/demo/xclient_build.py
@@ -1,6 +1,6 @@
from cffi import FFI
-_ffi = FFI()
-_ffi.cdef("""
+ffi = FFI()
+ffi.cdef("""
typedef ... Display;
typedef struct { ...; } Window;
@@ -17,8 +17,10 @@ Window XCreateSimpleWindow(Display *display, Window parent, int x, int y,
int XNextEvent(Display *display, XEvent *event_return);
""")
-_ffi.set_source('_xclient', """
+ffi.set_source('_xclient', """
#include <X11/Xlib.h>
""", libraries=['X11'])
-_ffi.compile()
+
+if __name__ == '__main__':
+ ffi.compile()