Get object from Python to C++

  args, c++, cpython, pyobject, python

When I run this code there is a problem
when trying to run this line of code: Board board = *(((BoardObject*)(_board))->board);
That’s because this function: PyArg_ParseTuple(args, "O!", &BoardType, &_board); fills the variable _board with nullptr.

So how can I get all the member variables from an object of the class Board_acc in the C++ class Board?

Here is the whole code:

Python:

class BoardState:
    def __init__(self):
        self.state_array = numpy.zeros((8,8), Color)
        self.state_array.fill(Color.NOTHING)

class Board_acc:
    def __init__(self, start_board_state: BoardState = BoardState()):
        self.board_state = copy.deepcopy(start_board_state)
        self.pass_counter = 0

    def place(self):
        ogwa_acc.place(self) # the C++ function

C++:

struct BoardState
{
    Color state_array[8][8]/* = { {C0, C0, C0, C0, C0, C0, C0, C0},
                                {C0, C0, C0, C0, C0, C0, C0, C0},
                                {C0, C0, C0, C0, C0, C0, C0, C0},
                                {C0, C0, C0, C0, C0, C0, C0, C0},
                                {C0, C0, C0, C0, C0, C0, C0, C0},
                                {C0, C0, C0, C0, C0, C0, C0, C0},
                                {C0, C0, C0, C0, C0, C0, C0, C0},
                                {C0, C0, C0, C0, C0, C0, C0, C0},
    }*/;
};


static class Board
{
    public:
        static PyObject* place(PyObject* self, PyObject* args);

        BoardState mBoardState;

        int pass_counter;
};

typedef struct {
    PyObject_HEAD
    Board* board;
} BoardObject;

static PyTypeObject BoardType = {
    PyObject_HEAD_INIT(NULL, 0)
    ColorType.tp_name = "board",
    ColorType.tp_basicsize = sizeof(BoardObject),
    ColorType.tp_flags = Py_TPFLAGS_DEFAULT,
};

PyObject* Board::place(PyObject* self, PyObject* args)
{
    PyObject* _board = nullptr;
    PyArg_ParseTuple(args, "O!", &BoardType, &_board);

    Board board = *(((BoardObject*)(_board))->board);

    Py_RETURN_NONE;
}

The code as seen here is shortened in many places so I don’t bother you with 300 lines of code. Some things may seem strange because of this.

Source: Windows Questions C++

LEAVE A COMMENT