native-stockfish 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 [Your Name]
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,4 @@
1
+ include README.md
2
+ include native_stockfish.cpp
3
+ include pyproject.toml
4
+ include LICENSE
@@ -0,0 +1,103 @@
1
+ Metadata-Version: 2.4
2
+ Name: native_stockfish
3
+ Version: 0.1.0
4
+ Summary: The fastest client for the Stockfish chess engine using C++ extension.
5
+ Home-page: https://github.com/tawfiqkhalilieh/stockfish-native
6
+ Author: Tawfiq Khalilieh
7
+ Author-email: taw.coding@gmail.com
8
+ Classifier: Programming Language :: Python :: 3
9
+ Classifier: Programming Language :: C++
10
+ Classifier: License :: OSI Approved :: MIT License
11
+ Classifier: Operating System :: POSIX :: Linux
12
+ Requires-Python: >=3.8
13
+ Description-Content-Type: text/markdown
14
+ License-File: LICENSE
15
+ Dynamic: author
16
+ Dynamic: author-email
17
+ Dynamic: classifier
18
+ Dynamic: description
19
+ Dynamic: description-content-type
20
+ Dynamic: home-page
21
+ Dynamic: license-file
22
+ Dynamic: requires-python
23
+ Dynamic: summary
24
+
25
+ # Native Stockfish Python Client
26
+
27
+ Native Stockfish is a minimal-overhead Python interface to the Stockfish chess engine.
28
+ Stockfish runs as a native process, with all UCI communication handled in C to avoid
29
+ Python becoming a performance bottleneck.
30
+
31
+ ## Prerequisites
32
+
33
+ - Python 3.8+
34
+ - A compiled Stockfish binary. You can compile Stockfish from the official source or use the submodule included in this repository.
35
+
36
+ ### Compiling Stockfish
37
+
38
+ If you have the `Stockfish` submodule checked out:
39
+
40
+ ```bash
41
+ cd Stockfish/src
42
+ make -j profile-build
43
+ ```
44
+
45
+ > Or you can use use the binray from the website.
46
+
47
+
48
+ ## Installation
49
+
50
+ ### From Source
51
+
52
+ ```bash
53
+ pip install .
54
+ ```
55
+
56
+ ## Usage
57
+
58
+ See `examples/basic_usage.py` for a complete example.
59
+
60
+ ```python
61
+ import native_stockfish
62
+
63
+ # Path to your compiled Stockfish binary
64
+ stockfish_path = "./Stockfish/src/stockfish"
65
+
66
+ client = native_stockfish.StockfishClient(stockfish_path)
67
+ client.start()
68
+
69
+ # Analyze a position (FEN string)
70
+ # top_moves(fen, depth, count)
71
+ moves = client.top_moves("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1", 15, 3)
72
+ print(moves)
73
+
74
+ client.stop()
75
+ ```
76
+
77
+ ## API
78
+
79
+ ### `StockfishClient(path)`
80
+
81
+ Initializes the client with the path to the Stockfish executable.
82
+
83
+ ### `client.start()`
84
+
85
+ Starts the Stockfish engine process.
86
+
87
+ ### `client.status()`
88
+
89
+ Returns the status of the engine (e.g., "running", "stopped").
90
+
91
+ ### `client.top_moves(fen, depth, count)`
92
+
93
+ Analyzes the given FEN position.
94
+
95
+ - `fen`: The FEN string of the position to analyze.
96
+ - `depth`: The search depth.
97
+ - `count`: The number of top moves to return.
98
+
99
+ Returns a list of top moves.
100
+
101
+ ### `client.stop()`
102
+
103
+ Stops the Stockfish engine process.
@@ -0,0 +1,79 @@
1
+ # Native Stockfish Python Client
2
+
3
+ Native Stockfish is a minimal-overhead Python interface to the Stockfish chess engine.
4
+ Stockfish runs as a native process, with all UCI communication handled in C to avoid
5
+ Python becoming a performance bottleneck.
6
+
7
+ ## Prerequisites
8
+
9
+ - Python 3.8+
10
+ - A compiled Stockfish binary. You can compile Stockfish from the official source or use the submodule included in this repository.
11
+
12
+ ### Compiling Stockfish
13
+
14
+ If you have the `Stockfish` submodule checked out:
15
+
16
+ ```bash
17
+ cd Stockfish/src
18
+ make -j profile-build
19
+ ```
20
+
21
+ > Or you can use use the binray from the website.
22
+
23
+
24
+ ## Installation
25
+
26
+ ### From Source
27
+
28
+ ```bash
29
+ pip install .
30
+ ```
31
+
32
+ ## Usage
33
+
34
+ See `examples/basic_usage.py` for a complete example.
35
+
36
+ ```python
37
+ import native_stockfish
38
+
39
+ # Path to your compiled Stockfish binary
40
+ stockfish_path = "./Stockfish/src/stockfish"
41
+
42
+ client = native_stockfish.StockfishClient(stockfish_path)
43
+ client.start()
44
+
45
+ # Analyze a position (FEN string)
46
+ # top_moves(fen, depth, count)
47
+ moves = client.top_moves("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1", 15, 3)
48
+ print(moves)
49
+
50
+ client.stop()
51
+ ```
52
+
53
+ ## API
54
+
55
+ ### `StockfishClient(path)`
56
+
57
+ Initializes the client with the path to the Stockfish executable.
58
+
59
+ ### `client.start()`
60
+
61
+ Starts the Stockfish engine process.
62
+
63
+ ### `client.status()`
64
+
65
+ Returns the status of the engine (e.g., "running", "stopped").
66
+
67
+ ### `client.top_moves(fen, depth, count)`
68
+
69
+ Analyzes the given FEN position.
70
+
71
+ - `fen`: The FEN string of the position to analyze.
72
+ - `depth`: The search depth.
73
+ - `count`: The number of top moves to return.
74
+
75
+ Returns a list of top moves.
76
+
77
+ ### `client.stop()`
78
+
79
+ Stops the Stockfish engine process.
@@ -0,0 +1,212 @@
1
+ #include <Python.h>
2
+ #include <string>
3
+ #include <unistd.h>
4
+ #include <vector>
5
+ #include <signal.h>
6
+ #include <sys/wait.h>
7
+
8
+ typedef struct {
9
+ // might find some use for this in the future, maybe in handling exceptions
10
+ PyObject_HEAD int engine_running;
11
+ const char *stockfish_path;
12
+ pid_t stockfish_process;
13
+ int (*dprintf)(int, const char *format, ...);
14
+ int write_fd;
15
+ FILE *output;
16
+ int in[2];
17
+ int out[2];
18
+ } StockfishClient;
19
+
20
+ static PyObject *Stockfish_start(StockfishClient *self, PyObject *args) {
21
+ self->engine_running = 1;
22
+ Py_RETURN_NONE;
23
+ }
24
+
25
+ static PyObject *Stockfish_stop(StockfishClient *self, PyObject *args) {
26
+ self->engine_running = 0;
27
+ Py_RETURN_NONE;
28
+ }
29
+
30
+ static PyObject *Stockfish_status(StockfishClient *self, PyObject *args) {
31
+ if (self->engine_running)
32
+ return PyUnicode_FromString("running");
33
+ else
34
+ return PyUnicode_FromString("stopped");
35
+ }
36
+
37
+ static int Stockfish_init(StockfishClient *self, PyObject *args,
38
+ PyObject *kwds) {
39
+
40
+ const char *stockfish_path_param;
41
+
42
+ if (!PyArg_ParseTuple(args, "s", &stockfish_path_param)) {
43
+ return 0; // Python exception is automatically set
44
+ }
45
+
46
+ int in[2], out[2];
47
+ if (pipe(in) || pipe(out)) {
48
+ return 0;
49
+ }
50
+
51
+ pid_t pid = fork();
52
+ if (pid == 0) {
53
+ // Child process
54
+ dup2(in[0], STDIN_FILENO);
55
+ dup2(out[1], STDOUT_FILENO);
56
+ // Close unused pipe ends
57
+ close(in[0]);
58
+ close(in[1]);
59
+ close(out[0]);
60
+ close(out[1]);
61
+
62
+ execl(stockfish_path_param, "stockfish", NULL);
63
+ _exit(1); // Properly exit child if exec fails
64
+ }
65
+
66
+ // Parent process
67
+ close(in[0]); // Close read end of input pipe
68
+ close(out[1]); // Close write end of output pipe
69
+
70
+ self->dprintf = dprintf;
71
+ self->write_fd = in[1];
72
+ self->output = fdopen(out[0], "r");
73
+ self->stockfish_process = pid;
74
+ self->engine_running = 0;
75
+ self->stockfish_path = stockfish_path_param;
76
+ self->in[0] = in[0];
77
+ self->in[1] = in[1];
78
+ self->out[0] = out[0];
79
+ self->out[1] = out[1];
80
+ return 0;
81
+ }
82
+
83
+ static void Stockfish_dealloc(StockfishClient *self) {
84
+ if (self->output) {
85
+ fclose(self->output);
86
+ self->output = NULL;
87
+ }
88
+ if (self->write_fd > 0) {
89
+ close(self->write_fd);
90
+ self->write_fd = -1;
91
+ }
92
+
93
+ if (self->stockfish_process > 0) {
94
+ kill(self->stockfish_process, SIGTERM);
95
+ waitpid(self->stockfish_process, NULL, 0);
96
+ self->stockfish_process = 0;
97
+ }
98
+
99
+ Py_TYPE(self)->tp_free((PyObject *)self);
100
+ }
101
+
102
+ static PyObject *top_moves(StockfishClient *self, PyObject *args) {
103
+ const char *fen;
104
+ int depth;
105
+ int multiv;
106
+
107
+ if (!PyArg_ParseTuple(args, "sii", &fen, &depth, &multiv)) {
108
+ return NULL; // Python exception is automatically set
109
+ }
110
+
111
+ std::string cmd = "uci\nisready\nsetoption name MultiPV value " +
112
+ std::to_string(multiv) + "\n";
113
+
114
+ dprintf(self->in[1], "%s", cmd.c_str());
115
+
116
+ char buf[2048];
117
+ while (fgets(buf, sizeof(buf), self->output)) {
118
+ std::string line(buf);
119
+ if (line.find("readyok") != std::string::npos)
120
+ break;
121
+ }
122
+
123
+ char cmd2[200];
124
+ snprintf(cmd2, sizeof(cmd2), "position fen %s\ngo depth %d\n", fen, depth);
125
+
126
+ dprintf(self->in[1], "%s", cmd2);
127
+
128
+ std::vector<std::string> found_moves(multiv, "");
129
+
130
+ while (fgets(buf, sizeof(buf), self->output)) {
131
+ std::string line(buf);
132
+ std::string search_str = "info depth " + std::to_string(depth);
133
+ if (line.find(search_str) != std::string::npos) {
134
+ int pv_index = 1;
135
+ size_t multipv_pos = line.find(" multipv ");
136
+ if (multipv_pos != std::string::npos) {
137
+ try {
138
+ pv_index = std::stoi(line.substr(multipv_pos + 9));
139
+ } catch (...) {
140
+ pv_index = 1;
141
+ }
142
+ }
143
+
144
+ if (pv_index >= 1 && pv_index <= multiv) {
145
+ size_t pv_pos = line.find(" pv ");
146
+ if (pv_pos != std::string::npos) {
147
+ size_t move_start = pv_pos + 4; // Length of " pv " is 4
148
+ size_t move_end = line.find(" ", move_start);
149
+ std::string move_str;
150
+ if (move_end != std::string::npos) {
151
+ move_str = line.substr(move_start, move_end - move_start);
152
+ } else {
153
+ move_str = line.substr(move_start);
154
+ if (!move_str.empty() && move_str.back() == '\n') {
155
+ move_str.pop_back();
156
+ }
157
+ }
158
+ found_moves[pv_index - 1] = move_str;
159
+ }
160
+ }
161
+ }
162
+
163
+ if (line.find("bestmove") != std::string::npos)
164
+ break;
165
+ }
166
+
167
+ PyObject *result_tuple = PyTuple_New(multiv);
168
+ for (int i = 0; i < multiv; ++i) {
169
+ PyObject *str_obj = PyUnicode_FromString(found_moves[i].c_str());
170
+ PyTuple_SetItem(result_tuple, i, str_obj);
171
+ }
172
+
173
+ return result_tuple;
174
+ }
175
+
176
+ static PyMethodDef Stockfish_methods[] = {
177
+ {"start", (PyCFunction)Stockfish_start, METH_NOARGS, "Start engine"},
178
+ {"stop", (PyCFunction)Stockfish_stop, METH_NOARGS, "Stop engine"},
179
+ {"status", (PyCFunction)Stockfish_status, METH_NOARGS, "Get status"},
180
+ {"top_moves", (PyCFunction)top_moves, METH_VARARGS, "Get top moves"},
181
+ {NULL}};
182
+
183
+ static PyTypeObject StockfishType = {PyVarObject_HEAD_INIT(NULL, 0)};
184
+
185
+ static int init_stockfish_type() {
186
+ StockfishType.tp_name = "native_stockfish.StockfishClient";
187
+ StockfishType.tp_basicsize = sizeof(StockfishClient);
188
+ StockfishType.tp_flags = Py_TPFLAGS_DEFAULT;
189
+ StockfishType.tp_new = PyType_GenericNew;
190
+ StockfishType.tp_init = (initproc)Stockfish_init;
191
+ StockfishType.tp_dealloc = (destructor)Stockfish_dealloc;
192
+ StockfishType.tp_methods = Stockfish_methods;
193
+
194
+ return PyType_Ready(&StockfishType);
195
+ }
196
+
197
+ static PyModuleDef moduledef = {PyModuleDef_HEAD_INIT, "native_stockfish",
198
+ "Stockfish C++ client", -1, NULL};
199
+
200
+ PyMODINIT_FUNC PyInit_native_stockfish(void) {
201
+ if (init_stockfish_type() < 0)
202
+ return NULL;
203
+
204
+ PyObject *m = PyModule_Create(&moduledef);
205
+ if (!m)
206
+ return NULL;
207
+
208
+ Py_INCREF(&StockfishType);
209
+ PyModule_AddObject(m, "StockfishClient", (PyObject *)&StockfishType);
210
+
211
+ return m;
212
+ }
@@ -0,0 +1,103 @@
1
+ Metadata-Version: 2.4
2
+ Name: native_stockfish
3
+ Version: 0.1.0
4
+ Summary: The fastest client for the Stockfish chess engine using C++ extension.
5
+ Home-page: https://github.com/tawfiqkhalilieh/stockfish-native
6
+ Author: Tawfiq Khalilieh
7
+ Author-email: taw.coding@gmail.com
8
+ Classifier: Programming Language :: Python :: 3
9
+ Classifier: Programming Language :: C++
10
+ Classifier: License :: OSI Approved :: MIT License
11
+ Classifier: Operating System :: POSIX :: Linux
12
+ Requires-Python: >=3.8
13
+ Description-Content-Type: text/markdown
14
+ License-File: LICENSE
15
+ Dynamic: author
16
+ Dynamic: author-email
17
+ Dynamic: classifier
18
+ Dynamic: description
19
+ Dynamic: description-content-type
20
+ Dynamic: home-page
21
+ Dynamic: license-file
22
+ Dynamic: requires-python
23
+ Dynamic: summary
24
+
25
+ # Native Stockfish Python Client
26
+
27
+ Native Stockfish is a minimal-overhead Python interface to the Stockfish chess engine.
28
+ Stockfish runs as a native process, with all UCI communication handled in C to avoid
29
+ Python becoming a performance bottleneck.
30
+
31
+ ## Prerequisites
32
+
33
+ - Python 3.8+
34
+ - A compiled Stockfish binary. You can compile Stockfish from the official source or use the submodule included in this repository.
35
+
36
+ ### Compiling Stockfish
37
+
38
+ If you have the `Stockfish` submodule checked out:
39
+
40
+ ```bash
41
+ cd Stockfish/src
42
+ make -j profile-build
43
+ ```
44
+
45
+ > Or you can use use the binray from the website.
46
+
47
+
48
+ ## Installation
49
+
50
+ ### From Source
51
+
52
+ ```bash
53
+ pip install .
54
+ ```
55
+
56
+ ## Usage
57
+
58
+ See `examples/basic_usage.py` for a complete example.
59
+
60
+ ```python
61
+ import native_stockfish
62
+
63
+ # Path to your compiled Stockfish binary
64
+ stockfish_path = "./Stockfish/src/stockfish"
65
+
66
+ client = native_stockfish.StockfishClient(stockfish_path)
67
+ client.start()
68
+
69
+ # Analyze a position (FEN string)
70
+ # top_moves(fen, depth, count)
71
+ moves = client.top_moves("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1", 15, 3)
72
+ print(moves)
73
+
74
+ client.stop()
75
+ ```
76
+
77
+ ## API
78
+
79
+ ### `StockfishClient(path)`
80
+
81
+ Initializes the client with the path to the Stockfish executable.
82
+
83
+ ### `client.start()`
84
+
85
+ Starts the Stockfish engine process.
86
+
87
+ ### `client.status()`
88
+
89
+ Returns the status of the engine (e.g., "running", "stopped").
90
+
91
+ ### `client.top_moves(fen, depth, count)`
92
+
93
+ Analyzes the given FEN position.
94
+
95
+ - `fen`: The FEN string of the position to analyze.
96
+ - `depth`: The search depth.
97
+ - `count`: The number of top moves to return.
98
+
99
+ Returns a list of top moves.
100
+
101
+ ### `client.stop()`
102
+
103
+ Stops the Stockfish engine process.
@@ -0,0 +1,10 @@
1
+ LICENSE
2
+ MANIFEST.in
3
+ README.md
4
+ native_stockfish.cpp
5
+ pyproject.toml
6
+ setup.py
7
+ native_stockfish.egg-info/PKG-INFO
8
+ native_stockfish.egg-info/SOURCES.txt
9
+ native_stockfish.egg-info/dependency_links.txt
10
+ native_stockfish.egg-info/top_level.txt
@@ -0,0 +1 @@
1
+ native_stockfish
@@ -0,0 +1,3 @@
1
+ [build-system]
2
+ requires = ["setuptools>=42", "wheel"]
3
+ build-backend = "setuptools.build_meta"
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,31 @@
1
+ from setuptools import setup, Extension
2
+
3
+ with open("README.md", "r", encoding="utf-8") as fh:
4
+ long_description = fh.read()
5
+
6
+ module = Extension(
7
+ "native_stockfish",
8
+ sources=["native_stockfish.cpp"],
9
+ language="c++",
10
+ extra_compile_args=["-std=c++17"],
11
+ )
12
+
13
+ setup(
14
+ name="native_stockfish",
15
+ version="0.1.0",
16
+ author="Tawfiq Khalilieh",
17
+ author_email="taw.coding@gmail.com",
18
+ description="The fastest client for the Stockfish chess engine using C++ extension.",
19
+ long_description=long_description,
20
+ long_description_content_type="text/markdown",
21
+ url="https://github.com/tawfiqkhalilieh/stockfish-native",
22
+ ext_modules=[module],
23
+ classifiers=[
24
+ "Programming Language :: Python :: 3",
25
+ "Programming Language :: C++",
26
+ "License :: OSI Approved :: MIT License",
27
+ "Operating System :: POSIX :: Linux",
28
+ ],
29
+ python_requires=">=3.8",
30
+ )
31
+