summaryrefslogtreecommitdiffstatshomepage
path: root/examples/unix/ffi_example.py
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2014-01-28 14:53:12 -0800
committerDamien George <damien.p.george@gmail.com>2014-01-28 14:53:12 -0800
commit4729a0cceaa241f8104d482c97ac4b2edb0c824a (patch)
tree4ef9beeffc9768ef1a077e0e5d29ac0ea566322e /examples/unix/ffi_example.py
parent7f11c794a53b78dc90d924b8c44e6d8165d5b4af (diff)
parent809eaa2679f59a74f394aa09515fd484ff3f0ad8 (diff)
downloadmicropython-4729a0cceaa241f8104d482c97ac4b2edb0c824a.tar.gz
micropython-4729a0cceaa241f8104d482c97ac4b2edb0c824a.zip
Merge pull request #235 from pfalcon/modffi
unix: Initial FFI module implementation
Diffstat (limited to 'examples/unix/ffi_example.py')
-rw-r--r--examples/unix/ffi_example.py38
1 files changed, 38 insertions, 0 deletions
diff --git a/examples/unix/ffi_example.py b/examples/unix/ffi_example.py
new file mode 100644
index 0000000000..0ac12203e6
--- /dev/null
+++ b/examples/unix/ffi_example.py
@@ -0,0 +1,38 @@
+import ffi
+
+libc = ffi.open("libc.so.6")
+print("libc:", libc)
+print()
+
+# Declare few functions
+perror = libc.func("v", "perror", ["s"])
+time = libc.func("i", "time", "p")
+open = libc.func("i", "open", ["s", "i"])
+qsort = libc.func("v", "qsort", "piip")
+# And one variable
+errno = libc.var("i", "errno")
+
+print("time:", time)
+print("UNIX time is:", time(None))
+print()
+
+perror("ffi before error")
+open("somethingnonexistent__", 0)
+print(errno)
+perror("ffi after error")
+print()
+
+def cmp(pa, pb):
+ a = ffi.as_bytearray(pa, 1)
+ b = ffi.as_bytearray(pb, 1)
+ print("cmp:", a, b)
+ return a[0] - b[0]
+
+cmp_c = ffi.callback("i", cmp, "pp")
+print("callback:", cmp_c)
+
+# TODO: violates Py semantics, pass bytearray
+s = "foobar"
+print("org string:", s)
+qsort(s, len(s), 1, cmp_c)
+print("qsort'ed:", s)