Commit 809eaa26 authored by Paul Sokolovsky's avatar Paul Sokolovsky
Browse files

Add FFI module example.

parent 60a9fac8
import ffi
libc ="")
print("libc:", libc)
# 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))
perror("ffi before error")
open("somethingnonexistent__", 0)
perror("ffi after error")
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)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment