commit 17eda4dedb0d70c5a82edf77e9b3aaa1af5bfb19
parent ef087c3849cfbe58f4f77e09367d6fbf152e5498
Author: Sebastiano Tronto <sebastiano@tronto.net>
Date: Sat, 12 Oct 2024 19:32:20 +0200
Initial support for Python
Diffstat:
4 files changed, 88 insertions(+), 3 deletions(-)
diff --git a/Makefile b/Makefile
@@ -39,4 +39,8 @@ debugshell: debugnissy.o
shelltest: debugshell
./shell/test.sh
-.PHONY: all clean test tool debugtool shell debugshell shelltest
+python: nissy.o
+ ${CC} -shared ${PYTHON3_INCLUDES} -o nissy_python_module.so \
+ nissy.o python/nissy_module.c
+
+.PHONY: all clean test tool debugtool shell debugshell shelltest python
diff --git a/README.md b/README.md
@@ -150,6 +150,41 @@ F' U R
For a full list of available command, use `run help`.
+## Running commands from a Python shell
+
+There is a work-in-progress python module available. To build it you need
+the Python development headers installed. You can check this from the output
+of `./configure.sh`:
+
+```
+$ ./configure.sh
+...
+Python3 development libraries: version 3.12
+```
+
+Then to build the module:
+
+```
+$ make python
+```
+
+And to import it
+
+```
+$ python # In the main folder
+>>> import nissy_python_module as nissy # In the python shell
+```
+
+From here you can call the library functions directly, for example:
+
+```
+>>> nissy.compose("NEORSQLH=ZFCYUAGLHTKB", "NEORSQLH=ZFCYUAGLHTKB")
+'ASTUGFBH=DACXEZGBLIKF'
+```
+
+Please note: as this is work in progress, not all functions are currently
+available from the Python module.
+
## Cube formats
The cube is represented as a string in one of the following formats,
diff --git a/configure.sh b/configure.sh
@@ -102,21 +102,33 @@ else
fi
LIBS="-lpthread"
-CFLAGS="$STD $LIBS $WFLAGS $WNOFLAGS $AVX -O3"
-DBGFLAGS="$STD $LIBS $WFLAGS $WNOFLAGS $SAN $AVX -g3 -DDEBUG"
+CFLAGS="$STD $LIBS $WFLAGS $WNOFLAGS $AVX -O3 -fPIC"
+DBGFLAGS="$STD $LIBS $WFLAGS $WNOFLAGS $SAN $AVX -g3 -DDEBUG -fPIC"
MACROS="-DTHREADS=$THREADS -D$ARCH"
+if (command -v "python3-config" >/dev/null 2>&1) ; then
+ PYTHON3_INCLUDES="$(python3-config --includes)"
+ PYTHON3="version $(echo "$PYTHON3_INCLUDES" | sed 's/.*3\./3./')"
+else
+ PYTHON3_INCLUDES=""
+ PYTHON3="Not found, Python shell won't be available"
+fi
+
echo "Compiler: $CC"
echo "Selected architecture: $ARCH"
echo "Number of threads: $THREADS"
echo "Sanitizer options (debug build only): $SAN"
+echo "Python3 development libraries: $PYTHON3"
{
echo "ARCH = $ARCH";
echo "";
echo "CFLAGS = $CFLAGS";
+echo "";
echo "DBGFLAGS = $DBGFLAGS";
+echo "";
echo "MACROS = $MACROS"
echo "";
+echo "PYTHON3_INCLUDES = $PYTHON3_INCLUDES"
echo "CC = $CC"
} > config.mk
diff --git a/python/nissy_module.c b/python/nissy_module.c
@@ -0,0 +1,34 @@
+#define PY_SSIZE_T_CLEAN
+#include <Python.h>
+
+#include "../src/nissy.h"
+
+/* TODO from here */
+
+static PyObject *compose(PyObject *self, PyObject *args) {
+ const char *cube, *permutation;
+ char result[NISSY_SIZE_B32];
+
+ if (!PyArg_ParseTuple(args, "ss", &cube, &permutation))
+ return NULL;
+
+ nissy_compose(cube, permutation, result);
+ return PyUnicode_FromString(result);
+}
+
+static PyMethodDef NissyMethods[] = {
+ { "compose", compose, METH_VARARGS, "Compose two cubes." },
+ { NULL, NULL, 0, NULL }
+};
+
+static struct PyModuleDef nissy_python_module = {
+ PyModuleDef_HEAD_INIT,
+ "nissy",
+ NULL,
+ -1,
+ NissyMethods
+};
+
+PyMODINIT_FUNC PyInit_nissy_python_module(void) {
+ return PyModule_Create(&nissy_python_module);
+}