0

I'm trying to do integrate with python and c++ now and what I'm gonna do is using c++ function in python.

The feature of function is read all text in a text file.

I know I have to make c++ code to c style, so I made c++ code like below:

void ReadTextFile(string fileName)
{
    string str;
    string line;
    
    ifstream file(fileName);
    while(getline(file, line))
    {
        vector<string> result = split(line, ':');
        if (result.size() > 1)
        {
            result[1].erase(std::remove(result[1].begin(), result[1].end(), ' '), result[1].end());
            cout << result[1] << endl;
        }
    }
}



extern "C" {
    void ReadTextFileCpp(string fileName) {return ReadTextFile(fileName);
}

So I can get right result what I want to when I just build this c++ file.

But the problem is when I build this cpp file to .so file and call this function in python file. this is what I call the function in python:

lib2 = cdll.LoadLibrary('./ReadTextFile.so')
lib2.ReadTextFileCpp('0166_405504.txt')

The error occurs when I call the ReadTextFile function and this is the error message.

terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc

Why this error occurs only when I call in python?

p.s) I have changed 'std::string' to 'const char*'. This code runs but it doesn't get inside of while loop.

void ReadTextFile(const char* fileName)
{
    string str;
    string line;
    
    ifstream file(fileName);
    while(getline(file, line))
    {
        vector<string> result = split(line, ':');
        if (result.size() > 1)
        {
            result[1].erase(std::remove(result[1].begin(), result[1].end(), ' '), result[1].end());
            cout << result[1] << endl;
        }
    }
}



extern "C" {
    void ReadTextFileCpp(const char* fileName) {return ReadTextFile(fileName);
}
8
  • 3
    You can't make an extern "C" function that accepts a C++ type like std::string (some compiler implementations may allow it, but Python doesn't know how to call such things). To be legal extern "C", it has to have a prototype (arguments and return values) that are C legal. The body of the function can do C++ things, but the interface must be C-legal. In this case that should be doable at least; just accept const char* instead of std::string. Commented Jun 16, 2022 at 2:37
  • I tried change parameter std::string to const char*, but it doesn't work as well... Commented Jun 16, 2022 at 2:41
  • Have you read the ctypes docs? You need to declare the prototypes for the function, or it assumes everything is ints (which is kind of a problem when passing a pointer). It's also unclear what's split is here; it could hide all sorts of issues. I'd suggest trying to test this code purely in C++ with a debugger, to make sure it works there, before trying to interface to it with Python. Commented Jun 16, 2022 at 3:37
  • @ShadowRanger I've changed parameter to 'const char*' and it works right when I run in purely C++ with a debugger. But it doesn't get inside of while loop when I call the function in python. Commented Jun 16, 2022 at 4:43
  • Did you define the argtypes and restype for the function at the Python layer? Again, read the ctypes docs, you can't just call a function by name and hope. Commented Jun 16, 2022 at 4:44

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.