0

I am trying to cythonize the class.

import pandas as pd
import os, glob

class PyClass():
    def __init__(self, csvFile):
        self.dfCSV = pd.read_csv("file.csv")

    def returnBool(self,matchInt,stampInt):
        dfMatch = self.dfCSV[self.dfCSV['a']==matchInt]
        if len(dfMatch.index)==0:
            return False
        else:
            return True

    def returnArray(self, matchInt,stampInt):
        dfMatch = self.dfCSV[self.dfCSV['a']==matchInt]
        return list(dfMatch['b'])

My cdefs are as listed below:

from .PyClass import PyClass
from libcpp cimport bool, int
cimport cython
from libc.stdlib cimport malloc, free

cdef public object createPyClass(const char *value):
    return PyClass("file.csv")

cdef public bool callReturnBool(object p, int matchInt, int stampInt):
    return p.returnBool(matchInt, stampInt)

cdef public int *callReturnArray(object p, int matchInt, int stampInt):
    result = p.returnArray(matchInt, stampInt)
    cdef int *res
    res = <int *>malloc(len(result) * cython.sizeof(int))
    if res is NULL:
        raise MemoryError()

    for i in xrange(len(result)):
        res[i] = result[i]

    return res

In my C++ code there is a constructor whose code is:

Test::Test(const std::string& var) : m_PyClass()
{
    Py_Initialize();
    PyInit_PyClass();

    m_PyClass = createPyClass(var.c_str());
}

But, after compiling the code, I faced with a segmentation fault:

Program received signal SIGSEGV, Segmentation fault.
0x000055555555924d in createPyClass (__pyx_v_value=<optimized out>) at PyClass.cpp:1190
1190      __Pyx_GetModuleGlobalName(__pyx_t_2, __pyx_n_s_PyClass); if (unlikely(!__pyx_t_2)) __PYX_ERR(0, 7, __pyx_L1_error)

Is it possible that someone could easily spot the mistake?

asla
  • 47
  • 3
  • https://cython.readthedocs.io/en/latest/src/tutorial/embedding.html#initialising-your-main-module? "DO NOT call the module init function instead of importing the module" – DavidW Dec 28 '21 at 15:23

0 Answers0