Mercurial > pub > dyncall > bindings
annotate python/pydc/examples/callback.py @ 60:8e905c0798c7
- p2Z() helper func
- import fix for test code avoiding potential circular import
author | Tassilo Philipp |
---|---|
date | Wed, 03 Aug 2022 15:38:07 +0200 |
parents | 16151547265e |
children |
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 |
56
16151547265e
made callback example python 2 compatible, and added some randomness to numbers to sort
Tassilo Philipp
parents:
54
diff
changeset
|
12 import random |
54
918dab7a6606
- added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents:
diff
changeset
|
13 import struct |
918dab7a6606
- added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents:
diff
changeset
|
14 |
918dab7a6606
- added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents:
diff
changeset
|
15 if sys.platform == "win32": |
918dab7a6606
- added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents:
diff
changeset
|
16 libc = load("msvcrt") |
918dab7a6606
- added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents:
diff
changeset
|
17 elif sys.platform == "darwin": |
918dab7a6606
- added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents:
diff
changeset
|
18 libc = load("/usr/lib/libc.dylib") |
918dab7a6606
- added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents:
diff
changeset
|
19 elif "bsd" in sys.platform: |
918dab7a6606
- added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents:
diff
changeset
|
20 #libc = load("/usr/lib/libc.so") |
918dab7a6606
- added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents:
diff
changeset
|
21 libc = load("/lib/libc.so.7") |
918dab7a6606
- added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents:
diff
changeset
|
22 elif platform.architecture()[0] == "64bit": |
918dab7a6606
- added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents:
diff
changeset
|
23 libc = load("/lib64/libc.so.6") |
918dab7a6606
- added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents:
diff
changeset
|
24 else: |
918dab7a6606
- added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents:
diff
changeset
|
25 libc = load("/lib/libc.so.6") |
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 |
918dab7a6606
- added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents:
diff
changeset
|
29 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
|
30 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
|
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 |
56
16151547265e
made callback example python 2 compatible, and added some randomness to numbers to sort
Tassilo Philipp
parents:
54
diff
changeset
|
34 n = 8 |
16151547265e
made callback example python 2 compatible, and added some randomness to numbers to sort
Tassilo Philipp
parents:
54
diff
changeset
|
35 nums = bytearray(struct.pack("i"*n, *[random.randrange (-10, 50) for i in range (n)])) |
16151547265e
made callback example python 2 compatible, and added some randomness to numbers to sort
Tassilo Philipp
parents:
54
diff
changeset
|
36 es = int(len(nums)/n) # element size |
54
918dab7a6606
- added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents:
diff
changeset
|
37 |
918dab7a6606
- added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents:
diff
changeset
|
38 |
918dab7a6606
- added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents:
diff
changeset
|
39 def compar(a, b): |
918dab7a6606
- added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents:
diff
changeset
|
40 ba = bytearray(es) |
918dab7a6606
- added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents:
diff
changeset
|
41 call(fp_memcpy,"ppi)p", ba, a, es) |
918dab7a6606
- added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents:
diff
changeset
|
42 a = struct.unpack("i", ba)[0] |
918dab7a6606
- added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents:
diff
changeset
|
43 call(fp_memcpy,"ppi)p", ba, b, es) |
918dab7a6606
- added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents:
diff
changeset
|
44 b = struct.unpack("i", ba)[0] |
918dab7a6606
- added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents:
diff
changeset
|
45 return a - b |
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 cb = new_callback("pp)i", compar) |
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 # -------- |
918dab7a6606
- added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents:
diff
changeset
|
50 |
56
16151547265e
made callback example python 2 compatible, and added some randomness to numbers to sort
Tassilo Philipp
parents:
54
diff
changeset
|
51 print('%d '*n % struct.unpack("i"*n, nums)) |
54
918dab7a6606
- added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents:
diff
changeset
|
52 |
918dab7a6606
- added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents:
diff
changeset
|
53 print('... qsort ...') |
56
16151547265e
made callback example python 2 compatible, and added some randomness to numbers to sort
Tassilo Philipp
parents:
54
diff
changeset
|
54 call(fp_qsort,"piip)v", nums, n, es, cb) |
54
918dab7a6606
- added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents:
diff
changeset
|
55 |
56
16151547265e
made callback example python 2 compatible, and added some randomness to numbers to sort
Tassilo Philipp
parents:
54
diff
changeset
|
56 print('%d '*n % struct.unpack("i"*n, nums)) |
54
918dab7a6606
- added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents:
diff
changeset
|
57 |
918dab7a6606
- added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents:
diff
changeset
|
58 |
918dab7a6606
- added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents:
diff
changeset
|
59 free_callback(cb) |
918dab7a6606
- added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents:
diff
changeset
|
60 |