diff --git a/.python-version b/.python-version new file mode 100644 index 0000000000000000000000000000000000000000..61553c0a198d5e2c3918a53e466bae1f96746877 --- /dev/null +++ b/.python-version @@ -0,0 +1 @@ +python-c-interface3.6.4 diff --git a/python-c-api_solution/Makefile b/python-c-api_solution/Makefile new file mode 100644 index 0000000000000000000000000000000000000000..375d89f733c115c1d2f2d46e56123244a70e0f98 --- /dev/null +++ b/python-c-api_solution/Makefile @@ -0,0 +1,9 @@ +#http://www.cs.colby.edu/maxwell/courses/tutorials/maketutor/ +CC=gcc +CFLAGS=-I. + +test_script: main.o demolib.o + $(CC) -o test_script main.o demolib.o + +clean: + rm -f *.o \ No newline at end of file diff --git a/python-c-api_solution/demolib.c b/python-c-api_solution/demolib.c new file mode 100644 index 0000000000000000000000000000000000000000..39d66e9af907677bc27a2a1d813ac14276572141 --- /dev/null +++ b/python-c-api_solution/demolib.c @@ -0,0 +1,25 @@ +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#include "demolib.h" + +int add_number(int number_1, int number_2) { + int result = number_1 + number_2; + return result; +} + +// https://stackoverflow.com/questions/8465006/how-do-i-concatenate-two-strings-in-c + +char* make_str(const char *s1, const char *s2) { + char *result = malloc(strlen(s1) + strlen(s2) + 1); + strcpy(result, s1); + strcat(result, s2); + return result; +} + +int add_number_using_pointer() { + +} + + + diff --git a/python-c-api_solution/demolib.h b/python-c-api_solution/demolib.h new file mode 100644 index 0000000000000000000000000000000000000000..17ce34495fadd00e04318dd51f8ff79e87c4a898 --- /dev/null +++ b/python-c-api_solution/demolib.h @@ -0,0 +1,6 @@ +// Example header file + +// char construct_string(int number); + +int add_number(int number_1, int number_2); +char* make_str(const char *s1, const char *s2); \ No newline at end of file diff --git a/python-c-api_solution/demolib.o b/python-c-api_solution/demolib.o new file mode 100644 index 0000000000000000000000000000000000000000..5896b43ecea3a7fb3097c0b062930226d5bc57dd Binary files /dev/null and b/python-c-api_solution/demolib.o differ diff --git a/python-c-api_solution/demomodule.c b/python-c-api_solution/demomodule.c new file mode 100644 index 0000000000000000000000000000000000000000..1657aca00836095a00929dff4e20ec84eb65090f --- /dev/null +++ b/python-c-api_solution/demomodule.c @@ -0,0 +1,63 @@ +// demomodule.c +#include <Python.h> +#include "demolib.h" + + +// https://stackoverflow.com/questions/51505391/how-to-parse-void-to-pyobject + + +static PyObject *DemoLib_AddNumber(PyObject *self, PyObject *args) { + int *number_1; + int *number_2; + if (!PyArg_ParseTuple(args, "ii", &number_1, &number_2)) { + return NULL; + } + + int result; + result = add_number(number_1, number_2); + + return Py_BuildValue("i", result); +} + +static PyObject *DemoLib_MakeStr(PyObject *self, PyObject *args) { + char *character_1; + char *character_2; + if (!PyArg_ParseTuple(args, "ss", &character_1, &character_2)) { + return NULL; + } + + char* result; + result = make_str(character_1, character_2); + + return Py_BuildValue("s", result); +} + +// module's function table +static PyMethodDef DemoLib_FunctionsTable[] = { + { + "add_number", // name exposed to Python + DemoLib_AddNumber, // C wrapper function + METH_VARARGS, // received variable args (but really just 1) + "adds two numbers" // documentation + }, { + "make_str", // name exposed to Python + DemoLib_MakeStr, // C wrapper function + METH_VARARGS, // received variable args (but really just 1) + "adds two strings" // documentation + }, { + NULL, NULL, 0, NULL + } +}; + +// modules definition +static struct PyModuleDef DemoLib_Module = { + PyModuleDef_HEAD_INIT, + "demo", // name of module exposed to Python + "Demo Python wrapper for custom C extension library.", // module documentation + -1, + DemoLib_FunctionsTable +}; + +PyMODINIT_FUNC PyInit_demo(void) { + return PyModule_Create(&DemoLib_Module); +} \ No newline at end of file diff --git a/python-c-api_solution/main.c b/python-c-api_solution/main.c new file mode 100644 index 0000000000000000000000000000000000000000..3e05435dd83c83c6d64510c136a6106a241cd953 --- /dev/null +++ b/python-c-api_solution/main.c @@ -0,0 +1,34 @@ +#include <stdio.h> +#include "demolib.h" + + +int main(void) { + // Testing stuff for add_number function + int getal_1=2; + int getal_2=3; + + int result; + result = add_number(getal_1, getal_2); + printf("%d\n", result); + // + + + // testing stuff for concatenate string function + char* str_1 = "a"; + char* str_2 = "b"; + + char* s = make_str(str_1, str_2); + // do things with s + printf("%s\n", s); + + free(s); // deallocate the string + // + + // testing stuff to + + + + + + return(0); +} \ No newline at end of file diff --git a/python-c-api_solution/main.o b/python-c-api_solution/main.o new file mode 100644 index 0000000000000000000000000000000000000000..e29df7dd325a7b1a79365f68f3ad84979b8f9351 Binary files /dev/null and b/python-c-api_solution/main.o differ diff --git a/python-c-api_solution/setup.py b/python-c-api_solution/setup.py new file mode 100644 index 0000000000000000000000000000000000000000..0354312c193a3abc6b4495d4ecafbf759744cbb6 --- /dev/null +++ b/python-c-api_solution/setup.py @@ -0,0 +1,11 @@ +from setuptools import Extension, setup + +module = Extension("demo", + sources=[ + 'demolib.c', + 'demomodule.c' + ]) +setup(name='demo', + version='1.0', + description='Python wrapper for custom C extension', + ext_modules=[module]) \ No newline at end of file diff --git a/python-c-api_solution/test_script b/python-c-api_solution/test_script new file mode 100755 index 0000000000000000000000000000000000000000..f97b74885b85f148dd55b72354cc0b1763672653 Binary files /dev/null and b/python-c-api_solution/test_script differ diff --git a/python-c-api_solution/todo.org b/python-c-api_solution/todo.org new file mode 100644 index 0000000000000000000000000000000000000000..8911529d323e93b8a8ac311f055887192fc71834 --- /dev/null +++ b/python-c-api_solution/todo.org @@ -0,0 +1,20 @@ + +* DONE Construct function in c that returns combines 2 strings + CLOSED: [2019-10-30 Wed 16:59] +* DONE Wrap this in python + CLOSED: [2019-10-30 Wed 16:59] + +* DONE construct a function in c that adds 2 numbers + CLOSED: [2019-10-30 Wed 16:59] +* DONE Wrap this in python + CLOSED: [2019-10-30 Wed 16:59] +* TODO Construct a function in c that uses a function pointer to combine two numbers +* TODO wrap this in python + + + + + + + +* TODO Construct a simple program that recieves a function pointer AND a value, and passes that value to that function pointer diff --git a/python-c-api_solution/todo.org~ b/python-c-api_solution/todo.org~ new file mode 100644 index 0000000000000000000000000000000000000000..86fcc941df9cbd8dd93981703f92156fd9dc8565 --- /dev/null +++ b/python-c-api_solution/todo.org~ @@ -0,0 +1,2 @@ +* TODO construct simple program in c that prints a value +* TODO Interface this function with python