Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python C++ wrapper problem
#1
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. Huh
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
Reply
#2
I'd suggest trying it with a debugger attached, so you can inspect what key is, maybe check the return value of PyObject_GetItem() (it might be null?)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  SystemError: <method-wrapper '__getattribute__' of EnumMeta object sciloop 4 1,455 Jul-23-2022, 07:57 PM
Last Post: sciloop
  Using C++/CLI wrapper functions by Python Ales1000 2 1,731 Jun-21-2022, 04:50 AM
Last Post: Ales1000
  Python executable Script (Wrapper) Hwang 2 1,845 Jan-12-2022, 06:53 PM
Last Post: Hwang
  Sqla wrapper Slojure 1 1,504 Nov-01-2021, 04:36 PM
Last Post: Larz60+
  C++ Wrapper for python yamifm0f 6 3,018 Nov-07-2019, 06:40 PM
Last Post: Gribouillis
  generating ctypes wrapper for a c library? brighteningeyes 9 6,946 Nov-04-2018, 02:31 AM
Last Post: brighteningeyes
  how to retrieve datas with Overpass API python wrapper apollo 0 2,888 Oct-15-2017, 02:27 PM
Last Post: apollo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020