From 5e30323cdb30a9cf386e14a8900957ada5ee9b05 Mon Sep 17 00:00:00 2001 From: mattip Date: Tue, 24 Nov 2015 21:28:16 +0200 Subject: update gmp demo --- demo/btrfs-snap.py | 3 --- demo/fastcsv.py | 2 +- demo/gmp.py | 36 ++++++++++++++++-------------------- demo/gmp_build.py | 27 +++++++++++++++++++++++++++ demo/xclient.py | 8 +++++++- demo/xclient_build.py | 10 ++++++---- 6 files changed, 57 insertions(+), 29 deletions(-) create mode 100644 demo/gmp_build.py (limited to 'demo') 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 ") - - 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 ", - 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 ", + 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 """, libraries=['X11']) -_ffi.compile() + +if __name__ == '__main__': + ffi.compile() -- cgit v1.2.1