Hello,
I'm trying to write some part of my code in c++ and wrap it to use in a code written in python.
(Because my code has many loops so it takes forever to run only with python).
Environment: windows 10 64bit, python 3.7.6 32bit, visual studio 2019
However, it suddenly stopped (without any error nor warning message) even if it works well in python.
I tried to find where it crashes, and it becomes silent when I try to extract data from pyObject using PyObject_GetItem(a,key); in the for loop which is indicated by // ★. Looks like PyObject_GetItem doesn't work, or should I do something with item_a? There's no error/warning message and I still have plenty of memory(seems there's no memory leak..) so I don't know what's going on here.
Please let me know if you have any idea.
Here is a part of my code. I generated *.pyd with this code and used it in my python code. I called it hundreds of times in some loop in python code.
I'm not good at both python and c++, so there must be a stupid error in my code..
Best wishes,
J.E.Suh
I'm trying to write some part of my code in c++ and wrap it to use in a code written in python.
(Because my code has many loops so it takes forever to run only with python).
Environment: windows 10 64bit, python 3.7.6 32bit, visual studio 2019
However, it suddenly stopped (without any error nor warning message) even if it works well in python.
I tried to find where it crashes, and it becomes silent when I try to extract data from pyObject using PyObject_GetItem(a,key); in the for loop which is indicated by // ★. Looks like PyObject_GetItem doesn't work, or should I do something with item_a? There's no error/warning message and I still have plenty of memory(seems there's no memory leak..) so I don't know what's going on here.
Please let me know if you have any idea.
Here is a part of my code. I generated *.pyd with this code and used it in my python code. I called it hundreds of times in some loop in python code.
I'm not good at both python and c++, so there must be a stupid error in my code..
#include <algorithm>
#include <iostream>
#include <Python.h>
#include <ndarraytypes.h>
int some_func(args)
{
...
}
static PyObject* c_func(PyObject* self, PyObject* args)
{
PyObject* a; // 512 x 512 ndarray
PyObject* b; // 3 x 60 ndarray
PyObject* key;
double* a_double; // want to translate ndarray a to an array that c++ can understand
double* b_double; // want to translate ndarray b to an array that c++ can understand
PyObject* item_a = NULL;
PyObject* item_b = NULL;
if (!PyArgs_ParseTuple(args, "OO", &a, &b)
{
return NULL;
}
int lena = PyObject_Size(a);
int lenb = PyObject_Size(b);
a_double = (double *)malloc(sizeof(double) * lena);
b_double = (double *)malloc(sizeof(double) * lenb);
for (int i=0; i<lena; i++)
{
key = Py_BuildValue("i", i);
item_a = PyObject_GetItem(a, key); // ★
a_double[i] = PyFloat_AsDouble(item_a);
}
for (int i=0; i<lenb; i++)
{
key = Py_BuildValue("i", i);
item_b = PyObject_GetItem(b, key);
b_double[i] = PyFloat_AsDouble(item_b);
}
int something = some_func(a_double, b_double);
PyObject* b_return = PyTuple_New(lenb);
for (int i=0; i<lenb; i++)
{
key = Py_BuildValue("i", i);
PyTuple_SetItem(b_return, i, PyFloat_FromDouble(b[i]));
}
free(a_double);
free(b_double);
return Py_BuildValue("iO", something, b_return);
}
static PyMethodDef myMethod[] ={
...
};
static struct PyModuleDef myModule = {
...
};
PyMODINIT_FUNC PyInit_myModule(void)
{
return PyModule_Create(&myModule);
}Thank you so much!Best wishes,
J.E.Suh
