Advertisement
RicardasSim

interfacing python3 with C, ctypes

Mar 18th, 2019
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.58 KB | None | 0 0
  1. /*
  2.  articles:
  3.  
  4.  Interfacing Python with C using ctypes
  5.  http://blog.prashanthellina.com/interfacing-python-with-c-using-ctypes.html
  6.  
  7.  Shared objects for the object disoriented!
  8.  https://www.ibm.com/developerworks/library/l-shobj/
  9.  
  10.  ctypes — A foreign function library for Python
  11.  https://docs.python.org/3/library/ctypes.html
  12.  
  13.  Interfacing Python and C using Ctypes
  14.  http://indico.ictp.it/event/7657/session/4/contribution/19/material/1/0.pdf
  15.  
  16.  */
  17.  
  18. /*
  19.  
  20. build shared library:
  21.  
  22. gcc -fPIC -c test.c
  23. ld -shared -soname libtest.so.1 -o libtest.so.1.0 -lc test.o
  24.  
  25. file testc.py
  26.  
  27. # begin
  28.  
  29. from ctypes import *
  30.  
  31. lib = cdll.LoadLibrary('./libtest.so.1.0')
  32.  
  33. print(lib.addi(1, 2))
  34.  
  35. a = c_float(1.7)
  36. b = c_float(1.8)
  37.  
  38. print("a: %.5f b: %.5f" % (a.value,b.value))
  39.  
  40. lib.addf.restype=c_float
  41. print("%.5f" % lib.addf(c_float(1.7),c_float(1.8)))
  42.  
  43.  
  44. print("a before (1): %.5f" % a.value)
  45. lib.addfbr(byref(a))
  46. print("a after (1): %.5f" % a.value)
  47.  
  48.  
  49. f_array = (c_float * 16)(*range(16))
  50.  
  51. print(sizeof(f_array))
  52.  
  53. f_array[0] = 300.1
  54. f_array[1] = 300.2
  55.  
  56. lib.addfbrarr.restype=c_bool
  57.  
  58. retval = c_bool()
  59.  
  60. print("before (2): %.5f %.5f" % (f_array[0],f_array[1]))
  61. retval = lib.addfbrarr(byref(f_array),16)
  62. if retval:
  63.     print("ERROR: addfbrarr")
  64. else:
  65.     print("after (2): %.5f %.5f" % (f_array[0],f_array[1]))
  66.  
  67. # end
  68.  
  69. run:
  70.  
  71. python3 testc.py
  72.  
  73.  */
  74.  
  75. #include <stdio.h>
  76. #include <stdlib.h>
  77. #include <stdbool.h>
  78.  
  79. void _init()
  80. {
  81.     printf("init\n");
  82. }
  83.  
  84. void _fini()
  85. {
  86.     printf("fini\n");
  87. }
  88.  
  89. int addi(int a, int b)
  90. {
  91.    
  92.     return a + b;
  93. }
  94.  
  95. float addf(float a, float b)
  96. {
  97.    
  98.     return a + b;
  99. }
  100.  
  101. void addfbr(float* a)
  102. {
  103.     *a = *a + 1.0f;
  104. }
  105.  
  106.  
  107. bool addfbrarr(float* a, unsigned int numOfE)
  108. {
  109.    
  110.     printf("sizeof float %zu\n",sizeof(float));
  111.     printf("sizeof float* %zu\n",sizeof(float*));
  112.    
  113.     float* tmp;
  114.     unsigned int offset = 0;
  115.    
  116.     if(offset<numOfE)
  117.     {
  118.     tmp = a + offset;
  119.     printf("address of tmp: %p\n", (void*) tmp);
  120.     *tmp = *tmp + 1.0f;
  121.     }
  122.     else return 1;
  123.    
  124.     offset++;
  125.    
  126.     //tmp += 1; adds sizeof(float)
  127.    
  128.     if(offset<numOfE)
  129.     {
  130.     tmp = a + offset;
  131.     printf("address of tmp: %p\n", (void*) tmp);
  132.     *tmp = *tmp + 1.0f;
  133.     }
  134.     else return 1;
  135.  
  136.     return 0;
  137. }
  138.  
  139. /*
  140.  
  141. output:
  142.  
  143. init
  144. 3
  145. a: 1.70000 b: 1.80000
  146. 3.50000
  147. a before (1): 1.70000
  148. a after (1): 2.70000
  149. 64
  150. before (2): 300.10001 300.20001
  151. sizeof float 4
  152. sizeof float* 8
  153. address of tmp: 0x7f93fa8fc6f0
  154. address of tmp: 0x7f93fa8fc6f4
  155. after (2): 301.10001 301.20001
  156. fini
  157.  
  158. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement