diff --git a/README.org b/README.org new file mode 100644 index 0000000000000000000000000000000000000000..e90a45ffed57b481a853a092b2b555f335d08be2 --- /dev/null +++ b/README.org @@ -0,0 +1,42 @@ +# python-c-interface + +Repo for interfacing python with c. Main goal is to be able to compile a function and give the pointer to that function to a c program. + +# Leads: +# https://stackoverflow.com/questions/17980167/writing-python-ctypes-for-function-pointer-callback-function-in-c +# https://wiki.python.org/moin/IntegratingPythonWithOtherLanguages +# https://cffi.readthedocs.io/en/release-1.4/using.html?highlight=re +# http://www.swig.org/Doc1.3/SWIG.html#SWIG_nn30 +# https://stackoverflow.com/questions/51273804/returning-a-function-python-c-api +# https://docs.scipy.org/doc/numpy/reference/c-api.types-and-structures.html#c.PyArray_ArrFuncs.fillwithscalar +# https://stackoverflow.com/questions/43162622/python-passing-pointers-between-c-and-python-with-ctypes +# https://stackoverflow.com/questions/4213095/python-and-ctypes-how-to-correctly-pass-pointer-to-pointer-into-dll + +# https://stackoverflow.com/questions/41954269/create-c-function-pointers-structure-in-python +# https://stackabuse.com/enhancing-python-with-custom-c-extensions/ Read +# https://stackoverflow.com/questions/49941617/runtime-generation-and-compilation-of-cython-functions +# https://realpython.com/cpython-source-code-guide/ +# https://docs.python.org/3.6/c-api/index.html +# https://stackoverflow.com/questions/6626167/build-a-pyobject-from-a-c-function +# https://docs.python.org/3.6/extending/newtypes_tutorial.html?highlight=pointer +# https://realpython.com/cpython-source-code-guide/ +# https://diseraluca.github.io/blog/2019/03/21/wetting-feet-with-python-c-api +# https://docs.python.org/3/c-api/function.html + +# http://koichitamura.blogspot.com/2008/06/this-is-small-python-capi-tutorial.html +# https://cython.readthedocs.io/en/latest/src/userguide/external_C_code.html#including-verbatim-c-code probably works too! +# https://www.oreilly.com/library/view/python-cookbook/0596001673/ch16s07.html +# https://stackoverflow.com/questions/53215786/when-writing-a-python-extension-in-c-how-does-one-pass-a-python-function-in-to +# https://pythonextensionpatterns.readthedocs.io/en/latest/refcount.html +# https://bitbucket.org/cffi/cffi/issues/265/cffi-doesnt-allow-creating-pointers-to +# https://stackoverflow.com/questions/40166645/a-c-pointer-passed-between-c-and-python-using-capsule-is-empty +# https://www.youtube.com/watch?v=2B0CMF3cYPA +# https://stackoverflow.com/questions/16993927/using-cython-to-link-python-to-a-shared-library + + +* Current Status: +I managed to compile a shared library on runtime, and also load the function pointer memory adress of a dynamically loaded function. + +** TODO Cast that pointer value into an integer and pass it to another function that will use that int as a function pointer again +** TODO Test within c-script itself +** TODO First in the dummy script, just to try things out, and then in the script where python is the bridge between diff --git a/README.md b/README.org~ similarity index 99% rename from README.md rename to README.org~ index 4ca4a36197c3dbc7b61e32f59abf262fa15e4ecf..879fc7132baef7bcce38a1f743e3750c9715fb73 100644 --- a/README.md +++ b/README.org~ @@ -32,3 +32,6 @@ Repo for interfacing python with c. Main goal is to be able to compile a functio # https://stackoverflow.com/questions/40166645/a-c-pointer-passed-between-c-and-python-using-capsule-is-empty # https://www.youtube.com/watch?v=2B0CMF3cYPA # https://stackoverflow.com/questions/16993927/using-cython-to-link-python-to-a-shared-library + + + diff --git a/ctypes/compile.sh b/ctypes/compile.sh index 736d14689fcf31186110ae7aa2a0156014c506ef..0c12cafce7ba9b593bd29f3198db768f01299ebf 100644 --- a/ctypes/compile.sh +++ b/ctypes/compile.sh @@ -1,2 +1,3 @@ # Compile a shared library -gcc -shared -o libmean.so.1 mean.c \ No newline at end of file +gcc -shared -o libmean.so.1 -fPIC mean.c +gcc -o main -fPIC mean.c \ No newline at end of file diff --git a/ctypes/libmean.so.1 b/ctypes/libmean.so.1 index 1ede0ce64eb59a8cf19dfffde096fbdf7fe4c32d..df6280d836c719bec47c764d75c8a0222edbf955 100755 Binary files a/ctypes/libmean.so.1 and b/ctypes/libmean.so.1 differ diff --git a/ctypes/main.py b/ctypes/main.py index d5549a5d439d8c34b7361c59bba04b606ea2e2fe..67ad75ad878c39a2a4e784fd99abff14d9d4ae0c 100644 --- a/ctypes/main.py +++ b/ctypes/main.py @@ -5,6 +5,10 @@ # https://stackoverflow.com/questions/9420673/is-it-possible-to-compile-c-code-using-python # https://documentation.help/Python-3.2.1/ctypes.html + +# https://stackoverflow.com/questions/49635105/ctypes-get-the-actual-address-of-a-c-function + + import ctypes import os import subprocess @@ -14,15 +18,39 @@ def buildLibrary(): # store the return code of the c program(return 0) # and display the output s = subprocess.check_call("bash compile.sh", shell=True) - print("return code:\n{}".format(s)) + if not s==0: + print("return code:\n{}".format(s)) +# Building library buildLibrary() - +# Loading library libmean = ctypes.CDLL("libmean.so.1") # loads the shared library -libmean.mean.restype = ctypes.c_double # define mean function return type -print(libmean.mean) -print(libmean.mean(ctypes.c_double(10), ctypes.c_double(3))) # call mean function needs cast arg types -libmean.mean.argtypes = [ctypes.c_double, ctypes.c_double] # define arguments types -print(libmean.mean(10, 3)) # call mean function does not need cast arg types -print(type(libmean.mean(10, 5))) \ No newline at end of file + +# # +# Pointer +print("Function pointer of the function printing the function pointer: {}".format(libmean.print_pointer_to_mean)) + +# Memory adress +libmean.print_pointer_to_mean() + +# Get memory adress of function. mimicking a pointer +mem = ctypes.cast(libmean.mean, ctypes.c_void_p).value + +# ijij +print("pointer to mean function (via python code): {}".format(mem)) +print("Hex memory adress: {}".format(hex(mem))) +print(type(hex(mem))) + + + + + + + +# Other uses of this code +# print(libmean.mean) +# print(libmean.mean(ctypes.c_double(10), ctypes.c_double(3))) # call mean function needs cast arg types +# libmean.mean.argtypes = [ctypes.c_double, ctypes.c_double] # define arguments types +# print(libmean.mean(10, 3)) # call mean function does not need cast arg types +# print(type(libmean.mean(10, 5))) \ No newline at end of file diff --git a/ctypes/mean.c b/ctypes/mean.c index 12d80b0a402f33c1aea74f5697ecc49282a6231c..31d46677fc7bb7b9d80ffef40640fa4b3bed9ec4 100644 --- a/ctypes/mean.c +++ b/ctypes/mean.c @@ -1,5 +1,24 @@ #include "mean.h" +#include <stdio.h> +#include <stdlib.h> double mean(double a, double b) { + printf("calculating mean with %f and %f", a, b); return (a+b)/2; +} + +void print_pointer_to_mean() { + printf("pointer to mean function (via c code): %p\n", &mean); +} + +double calc_mean_using_pointer(double (*pf)(double, double), double number_1, double number_2) { + double result; + result = pf(number_1, number_2); + printf("result running mean function with pointer to the function: %f", result); + return result; +} + +void main() { + double res = double mean(double 3.1, double 6.2); + printf("result %f", res); } \ No newline at end of file diff --git a/ctypes/mean.h b/ctypes/mean.h index a52ef612da30d036480c0690f2ae20e944d3db23..3fdc9b18b0abe3304e2110e95a7ab3d665366cbf 100644 --- a/ctypes/mean.h +++ b/ctypes/mean.h @@ -1,3 +1,5 @@ // Returns the mean of passed parameters -double mean(double, double); \ No newline at end of file +double mean(double, double); +void print_pointer_to_get_value(); +double calc_mean_using_pointer(double (*pf)(double, double), double number_1, double number_2); \ No newline at end of file diff --git a/python-c-api_solution/demolib.c b/python-c-api_solution/demolib.c index fcb2dbaa941b2f1fec08a3530b27edc7989f308b..f210145af3d55cf42e273dbaa81fcc6b9e5fa7dd 100644 --- a/python-c-api_solution/demolib.c +++ b/python-c-api_solution/demolib.c @@ -27,7 +27,4 @@ int add_number_using_pointer(int (*pf)(int, int), int number_1, int number_2) { int result; result = pf(number_1, number_2); return result; -} - - - +} \ No newline at end of file