annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
54
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents:
diff changeset
1 # callback of python function to qsort(3) some numbers - this is just a example
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents:
diff changeset
2 # using an existing libc function that uses a callback; it's not practical for
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents:
diff changeset
3 # real world use as it comes with a huge overhead:
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents:
diff changeset
4 # - sorting requires many calls of the comparison function
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents:
diff changeset
5 # - each such callback back into python comes with a lot of overhead
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents:
diff changeset
6 # - on top of that, for this example, 2 memcpy(3)s are needed to access the
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents:
diff changeset
7 # data to compare, further adding to the overhead
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents:
diff changeset
8
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents:
diff changeset
9 from pydc import *
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents:
diff changeset
10 import sys
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents:
diff changeset
11 import platform
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents:
diff changeset
12 import struct
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents:
diff changeset
13
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents:
diff changeset
14 if sys.platform == "win32":
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents:
diff changeset
15 libc = load("msvcrt")
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents:
diff changeset
16 elif sys.platform == "darwin":
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents:
diff changeset
17 libc = load("/usr/lib/libc.dylib")
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents:
diff changeset
18 elif "bsd" in sys.platform:
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents:
diff changeset
19 #libc = load("/usr/lib/libc.so")
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents:
diff changeset
20 libc = load("/lib/libc.so.7")
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents:
diff changeset
21 elif platform.architecture()[0] == "64bit":
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents:
diff changeset
22 libc = load("/lib64/libc.so.6")
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents:
diff changeset
23 else:
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents:
diff changeset
24 libc = load("/lib/libc.so.6")
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents:
diff changeset
25
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents:
diff changeset
26
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents:
diff changeset
27
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents:
diff changeset
28 fp_qsort = find(libc,"qsort") # void qsort(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *));
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents:
diff changeset
29 fp_memcpy = find(libc,"memcpy") # void * memcpy(void *dst, const void *src, size_t len);
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents:
diff changeset
30
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents:
diff changeset
31
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents:
diff changeset
32
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents:
diff changeset
33 nums = bytearray(struct.pack("i"*8, 12, 3, 5, 99, 3, -9, -9, 0))
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents:
diff changeset
34 es = int(len(nums)/8) # element size
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents:
diff changeset
35
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents:
diff changeset
36
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents:
diff changeset
37 def compar(a, b):
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents:
diff changeset
38 ba = bytearray(es)
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents:
diff changeset
39 call(fp_memcpy,"ppi)p", ba, a, es)
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents:
diff changeset
40 a = struct.unpack("i", ba)[0]
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents:
diff changeset
41 call(fp_memcpy,"ppi)p", ba, b, es)
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents:
diff changeset
42 b = struct.unpack("i", ba)[0]
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents:
diff changeset
43 return a - b
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents:
diff changeset
44
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents:
diff changeset
45 cb = new_callback("pp)i", compar)
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents:
diff changeset
46
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents:
diff changeset
47 # --------
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents:
diff changeset
48
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents:
diff changeset
49 print(*struct.unpack("i"*8, nums))
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents:
diff changeset
50
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents:
diff changeset
51 print('... qsort ...')
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents:
diff changeset
52 call(fp_qsort,"piip)v", nums, 8, es, cb)
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents:
diff changeset
53
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents:
diff changeset
54 print(*struct.unpack("i"*8, nums))
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents:
diff changeset
55
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents:
diff changeset
56
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents:
diff changeset
57 free_callback(cb)
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents:
diff changeset
58