Author: mattip <[email protected]>
Branch: demo-cleanup
Changeset: r2437:854dc393589a
Date: 2015-11-24 21:28 +0200
http://bitbucket.org/cffi/cffi/changeset/854dc393589a/
Log: update gmp demo
diff --git a/demo/btrfs-snap.py b/demo/btrfs-snap.py
--- a/demo/btrfs-snap.py
+++ b/demo/btrfs-snap.py
@@ -22,9 +22,6 @@
};
""")
-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
--- a/demo/fastcsv.py
+++ b/demo/fastcsv.py
@@ -26,7 +26,7 @@
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
--- 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
--- /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
--- a/demo/xclient.py
+++ b/demo/xclient.py
@@ -7,9 +7,15 @@
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
--- 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 @@
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()
+
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit