Fix refcount bugs in pyffish (#218)
authorBajusz Tamás <gbtami@users.noreply.github.com>
Fri, 18 Dec 2020 09:21:28 +0000 (10:21 +0100)
committerGitHub <noreply@github.com>
Fri, 18 Dec 2020 09:21:28 +0000 (10:21 +0100)
setup.py
src/pyffish.cpp

index aa4d900..7e99b30 100644 (file)
--- a/setup.py
+++ b/setup.py
@@ -39,7 +39,7 @@ pyffish_module = Extension(
     sources=sources,
     extra_compile_args=args)
 
-setup(name="pyffish", version="0.0.52",
+setup(name="pyffish", version="0.0.53",
       description="Fairy-Stockfish Python wrapper",
       long_description=long_description,
       long_description_content_type="text/markdown",
index 4805466..11c569a 100644 (file)
@@ -36,7 +36,9 @@ void buildPosition(Position& pos, StateListPtr& states, const char *variant, con
     int numMoves = PyList_Size(moveList);
     for (int i = 0; i < numMoves ; i++)
     {
-        std::string moveStr(PyBytes_AS_STRING(PyUnicode_AsEncodedString( PyList_GetItem(moveList, i), "UTF-8", "strict")));
+        PyObject *MoveStr = PyUnicode_AsEncodedString( PyList_GetItem(moveList, i), "UTF-8", "strict");
+        std::string moveStr(PyBytes_AS_STRING(MoveStr));
+        Py_XDECREF(MoveStr);
         Move m;
         if ((m = UCI::to_move(pos, moveStr)) != MOVE_NONE)
         {
@@ -65,7 +67,11 @@ extern "C" PyObject* pyffish_setOption(PyObject* self, PyObject *args) {
     if (!PyArg_ParseTuple(args, "sO", &name, &valueObj)) return NULL;
 
     if (Options.count(name))
-        Options[name] = std::string(PyBytes_AS_STRING(PyUnicode_AsEncodedString(PyObject_Str(valueObj), "UTF-8", "strict")));
+    {
+        PyObject *Value = PyUnicode_AsEncodedString( PyObject_Str(valueObj), "UTF-8", "strict");
+        Options[name] = std::string(PyBytes_AS_STRING(Value));
+        Py_XDECREF(Value);
+    }
     else
     {
         PyErr_SetString(PyExc_ValueError, (std::string("No such option ") + name + "'").c_str());
@@ -123,6 +129,8 @@ extern "C" PyObject* pyffish_getSAN(PyObject* self, PyObject *args) {
     StateListPtr states(new std::deque<StateInfo>(1));
     buildPosition(pos, states, variant, fen, moveList, chess960);
     std::string moveStr = move;
+
+    Py_XDECREF(moveList);
     return Py_BuildValue("s", move_to_san(pos, UCI::to_move(pos, moveStr), notation).c_str());
 }
 
@@ -144,7 +152,9 @@ extern "C" PyObject* pyffish_getSANmoves(PyObject* self, PyObject *args) {
 
     int numMoves = PyList_Size(moveList);
     for (int i=0; i<numMoves ; i++) {
-        std::string moveStr(PyBytes_AS_STRING(PyUnicode_AsEncodedString( PyList_GetItem(moveList, i), "UTF-8", "strict")));
+        PyObject *MoveStr = PyUnicode_AsEncodedString( PyList_GetItem(moveList, i), "UTF-8", "strict");
+        std::string moveStr(PyBytes_AS_STRING(MoveStr));
+        Py_XDECREF(MoveStr);
         Move m;
         if ((m = UCI::to_move(pos, moveStr)) != MOVE_NONE)
         {
@@ -163,7 +173,9 @@ extern "C" PyObject* pyffish_getSANmoves(PyObject* self, PyObject *args) {
             return NULL;
         }
     }
-    return sanMoves;
+    PyObject *Result = Py_BuildValue("O", sanMoves);  
+    Py_XDECREF(sanMoves);
+    return Result;
 }
 
 // INPUT variant, fen, move list
@@ -186,7 +198,10 @@ extern "C" PyObject* pyffish_legalMoves(PyObject* self, PyObject *args) {
         PyList_Append(legalMoves, moveStr);
         Py_XDECREF(moveStr);
     }
-    return legalMoves;
+
+    PyObject *Result = Py_BuildValue("O", legalMoves);  
+    Py_XDECREF(legalMoves);
+    return Result;
 }
 
 // INPUT variant, fen, move list