diff options
| author | Sebastiano Tronto <sebastiano@tronto.net> | 2024-10-12 19:32:20 +0200 |
|---|---|---|
| committer | Sebastiano Tronto <sebastiano@tronto.net> | 2024-10-12 19:32:20 +0200 |
| commit | 17eda4dedb0d70c5a82edf77e9b3aaa1af5bfb19 (patch) | |
| tree | e6b7257708cadba242006202345f9b7caf094594 | |
| parent | ef087c3849cfbe58f4f77e09367d6fbf152e5498 (diff) | |
| download | nissy-core-17eda4dedb0d70c5a82edf77e9b3aaa1af5bfb19.tar.gz nissy-core-17eda4dedb0d70c5a82edf77e9b3aaa1af5bfb19.zip | |
Initial support for Python
| -rw-r--r-- | Makefile | 6 | ||||
| -rw-r--r-- | README.md | 35 | ||||
| -rwxr-xr-x | configure.sh | 16 | ||||
| -rw-r--r-- | python/nissy_module.c | 34 |
4 files changed, 88 insertions, 3 deletions
| @@ -39,4 +39,8 @@ debugshell: debugnissy.o | |||
| 39 | shelltest: debugshell | 39 | shelltest: debugshell |
| 40 | ./shell/test.sh | 40 | ./shell/test.sh |
| 41 | 41 | ||
| 42 | .PHONY: all clean test tool debugtool shell debugshell shelltest | 42 | python: nissy.o |
| 43 | ${CC} -shared ${PYTHON3_INCLUDES} -o nissy_python_module.so \ | ||
| 44 | nissy.o python/nissy_module.c | ||
| 45 | |||
| 46 | .PHONY: all clean test tool debugtool shell debugshell shelltest python | ||
| @@ -150,6 +150,41 @@ F' U R | |||
| 150 | 150 | ||
| 151 | For a full list of available command, use `run help`. | 151 | For a full list of available command, use `run help`. |
| 152 | 152 | ||
| 153 | ## Running commands from a Python shell | ||
| 154 | |||
| 155 | There is a work-in-progress python module available. To build it you need | ||
| 156 | the Python development headers installed. You can check this from the output | ||
| 157 | of `./configure.sh`: | ||
| 158 | |||
| 159 | ``` | ||
| 160 | $ ./configure.sh | ||
| 161 | ... | ||
| 162 | Python3 development libraries: version 3.12 | ||
| 163 | ``` | ||
| 164 | |||
| 165 | Then to build the module: | ||
| 166 | |||
| 167 | ``` | ||
| 168 | $ make python | ||
| 169 | ``` | ||
| 170 | |||
| 171 | And to import it | ||
| 172 | |||
| 173 | ``` | ||
| 174 | $ python # In the main folder | ||
| 175 | >>> import nissy_python_module as nissy # In the python shell | ||
| 176 | ``` | ||
| 177 | |||
| 178 | From here you can call the library functions directly, for example: | ||
| 179 | |||
| 180 | ``` | ||
| 181 | >>> nissy.compose("NEORSQLH=ZFCYUAGLHTKB", "NEORSQLH=ZFCYUAGLHTKB") | ||
| 182 | 'ASTUGFBH=DACXEZGBLIKF' | ||
| 183 | ``` | ||
| 184 | |||
| 185 | Please note: as this is work in progress, not all functions are currently | ||
| 186 | available from the Python module. | ||
| 187 | |||
| 153 | ## Cube formats | 188 | ## Cube formats |
| 154 | 189 | ||
| 155 | The cube is represented as a string in one of the following formats, | 190 | The cube is represented as a string in one of the following formats, |
diff --git a/configure.sh b/configure.sh index 51e1089..ae51189 100755 --- a/configure.sh +++ b/configure.sh | |||
| @@ -102,21 +102,33 @@ else | |||
| 102 | fi | 102 | fi |
| 103 | LIBS="-lpthread" | 103 | LIBS="-lpthread" |
| 104 | 104 | ||
| 105 | CFLAGS="$STD $LIBS $WFLAGS $WNOFLAGS $AVX -O3" | 105 | CFLAGS="$STD $LIBS $WFLAGS $WNOFLAGS $AVX -O3 -fPIC" |
| 106 | DBGFLAGS="$STD $LIBS $WFLAGS $WNOFLAGS $SAN $AVX -g3 -DDEBUG" | 106 | DBGFLAGS="$STD $LIBS $WFLAGS $WNOFLAGS $SAN $AVX -g3 -DDEBUG -fPIC" |
| 107 | MACROS="-DTHREADS=$THREADS -D$ARCH" | 107 | MACROS="-DTHREADS=$THREADS -D$ARCH" |
| 108 | 108 | ||
| 109 | if (command -v "python3-config" >/dev/null 2>&1) ; then | ||
| 110 | PYTHON3_INCLUDES="$(python3-config --includes)" | ||
| 111 | PYTHON3="version $(echo "$PYTHON3_INCLUDES" | sed 's/.*3\./3./')" | ||
| 112 | else | ||
| 113 | PYTHON3_INCLUDES="" | ||
| 114 | PYTHON3="Not found, Python shell won't be available" | ||
| 115 | fi | ||
| 116 | |||
| 109 | echo "Compiler: $CC" | 117 | echo "Compiler: $CC" |
| 110 | echo "Selected architecture: $ARCH" | 118 | echo "Selected architecture: $ARCH" |
| 111 | echo "Number of threads: $THREADS" | 119 | echo "Number of threads: $THREADS" |
| 112 | echo "Sanitizer options (debug build only): $SAN" | 120 | echo "Sanitizer options (debug build only): $SAN" |
| 121 | echo "Python3 development libraries: $PYTHON3" | ||
| 113 | 122 | ||
| 114 | { | 123 | { |
| 115 | echo "ARCH = $ARCH"; | 124 | echo "ARCH = $ARCH"; |
| 116 | echo ""; | 125 | echo ""; |
| 117 | echo "CFLAGS = $CFLAGS"; | 126 | echo "CFLAGS = $CFLAGS"; |
| 127 | echo ""; | ||
| 118 | echo "DBGFLAGS = $DBGFLAGS"; | 128 | echo "DBGFLAGS = $DBGFLAGS"; |
| 129 | echo ""; | ||
| 119 | echo "MACROS = $MACROS" | 130 | echo "MACROS = $MACROS" |
| 120 | echo ""; | 131 | echo ""; |
| 132 | echo "PYTHON3_INCLUDES = $PYTHON3_INCLUDES" | ||
| 121 | echo "CC = $CC" | 133 | echo "CC = $CC" |
| 122 | } > config.mk | 134 | } > config.mk |
diff --git a/python/nissy_module.c b/python/nissy_module.c new file mode 100644 index 0000000..b627300 --- /dev/null +++ b/python/nissy_module.c | |||
| @@ -0,0 +1,34 @@ | |||
| 1 | #define PY_SSIZE_T_CLEAN | ||
| 2 | #include <Python.h> | ||
| 3 | |||
| 4 | #include "../src/nissy.h" | ||
| 5 | |||
| 6 | /* TODO from here */ | ||
| 7 | |||
| 8 | static PyObject *compose(PyObject *self, PyObject *args) { | ||
| 9 | const char *cube, *permutation; | ||
| 10 | char result[NISSY_SIZE_B32]; | ||
| 11 | |||
| 12 | if (!PyArg_ParseTuple(args, "ss", &cube, &permutation)) | ||
| 13 | return NULL; | ||
| 14 | |||
| 15 | nissy_compose(cube, permutation, result); | ||
| 16 | return PyUnicode_FromString(result); | ||
| 17 | } | ||
| 18 | |||
| 19 | static PyMethodDef NissyMethods[] = { | ||
| 20 | { "compose", compose, METH_VARARGS, "Compose two cubes." }, | ||
| 21 | { NULL, NULL, 0, NULL } | ||
| 22 | }; | ||
| 23 | |||
| 24 | static struct PyModuleDef nissy_python_module = { | ||
| 25 | PyModuleDef_HEAD_INIT, | ||
| 26 | "nissy", | ||
| 27 | NULL, | ||
| 28 | -1, | ||
| 29 | NissyMethods | ||
| 30 | }; | ||
| 31 | |||
| 32 | PyMODINIT_FUNC PyInit_nissy_python_module(void) { | ||
| 33 | return PyModule_Create(&nissy_python_module); | ||
| 34 | } | ||
