view python/pydc/examples/callback.py @ 54:918dab7a6606

- added callback support (comes with some bigger refactoring) - allow CPython's Py{CObject,Capsule} to be used as 'p'ointers
author Tassilo Philipp
date Tue, 02 Feb 2021 20:42:02 +0100
parents
children 16151547265e
line wrap: on
line source

# callback of python function to qsort(3) some numbers - this is just a example
# using an existing libc function that uses a callback; it's not practical for
# real world use as it comes with a huge overhead:
# - sorting requires many calls of the comparison function
# - each such callback back into python comes with a lot of overhead
# - on top of that, for this example, 2 memcpy(3)s are needed to access the
#   data to compare, further adding to the overhead

from pydc import *
import sys
import platform
import struct

if sys.platform == "win32":
  libc = load("msvcrt")
elif sys.platform == "darwin":
  libc = load("/usr/lib/libc.dylib")
elif "bsd" in sys.platform:
  #libc = load("/usr/lib/libc.so")
  libc = load("/lib/libc.so.7")
elif platform.architecture()[0] == "64bit":
  libc = load("/lib64/libc.so.6")
else:
  libc = load("/lib/libc.so.6")



fp_qsort  = find(libc,"qsort")  # void qsort(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *));
fp_memcpy = find(libc,"memcpy") # void * memcpy(void *dst, const void *src, size_t len);



nums = bytearray(struct.pack("i"*8, 12, 3, 5, 99, 3, -9, -9, 0))
es = int(len(nums)/8)  # element size


def compar(a, b):
    ba = bytearray(es)
    call(fp_memcpy,"ppi)p", ba, a, es)
    a = struct.unpack("i", ba)[0]
    call(fp_memcpy,"ppi)p", ba, b, es)
    b = struct.unpack("i", ba)[0]
    return a - b

cb = new_callback("pp)i", compar)

# --------

print(*struct.unpack("i"*8, nums))

print('... qsort ...')
call(fp_qsort,"piip)v", nums, 8, es, cb)

print(*struct.unpack("i"*8, nums))


free_callback(cb)