1try: 2 import ffi 3except ImportError: 4 print("SKIP") 5 raise SystemExit 6 7 8def ffi_open(names): 9 err = None 10 for n in names: 11 try: 12 mod = ffi.open(n) 13 return mod 14 except OSError as e: 15 err = e 16 raise err 17 18 19libc = ffi_open(("libc.so", "libc.so.0", "libc.so.6", "libc.dylib")) 20 21qsort = libc.func("v", "qsort", "piip") 22 23 24def cmp(pa, pb): 25 a = ffi.as_bytearray(pa, 1) 26 b = ffi.as_bytearray(pb, 1) 27 # print("cmp:", a, b) 28 return a[0] - b[0] 29 30 31cmp_c = ffi.callback("i", cmp, "pp") 32 33s = bytearray(b"foobar") 34print("org string:", s) 35qsort(s, len(s), 1, cmp_c) 36print("qsort'ed:", s) 37