I have created a python extension with C++. Generally, it works fine, and I can use the extension from Python to call C++ functions. When I define a method using the METH_O or METH_NOARGS macros, the method is able to call cout << [something] << endl;
without any problems. If I define a method using METH_VARARGS or METH_VARARGS | METH_KEYWORDS, the function operates as expected unless I try to cout something; if I use cout in the method (or in any other method called by that method), then the function halts and returns an error. I really rely on cout to help me program, so this is a challenge. What do I need to do to make cout work with these functions?
Here is some sample code that illustrates the problem and the error.
First, here is my PyMethodDef:
static PyMethodDef CPPLibrary_methods[] = {
{ "CPP_Cum_Prod", (PyCFunction)CumProd, METH_O, nullptr },
{ "MyPrintFunc", (PyCFunction)MyPrintFunct, METH_NOARGS, nullptr },
{ "MyTestArgsFunction", (PyCFunction)MyTestArgsFunction, METH_VARARGS | METH_KEYWORDS, nullptr },
{ "MyOtherTestArgsFunction", (PyCFunction)MyOtherTestArgsFunction, METH_VARARGS, nullptr },
{ nullptr, nullptr, 0, nullptr }
};
The first two functions call cout and work just great. For example, MyPrintFunc is as follows:
void MyPrintFunct()
{
cout << "Printing from Library 2!" << endl;
return;
}
The third and fourth functions both have a cout in them, but halt the program with an error, but they work fine and do not return an error if I eliminate the cout. For example:
static PyObject* MyTestArgsFunction(PyObject* self, PyObject* args, PyObject *keywds)
{
int voltage;
const char* state = "a stiff";
const char* action = "voom";
const char* parrot_type = "Norwegian Blue";
const char* kwlist[] = { "voltage", "state", "action", "type", NULL };
if (!PyArg_ParseTupleAndKeywords(args, keywds, "i|sss", const_cast<char **>(kwlist),
&voltage, &state, &action, &parrot_type))
return NULL;
cout << "My string" << endl;
voltage += 1;
PyObject* obj = Py_BuildValue("i", voltage);
return obj;
}
This MyTestArgsFunction returns an error that says: "Could not convert to integer: 3221225477. Path 'exitCode'. Value was either too large or too small for an Int32." That's a pretty useless error, btw, but I think it just means that something went wrong and the program exited with an error.
In any event, it seems weird to me that (a) cout works fine in my other functions; (b) these functions work fine when I don't use cout; and (c) these functions crash with cout.
Any help is much appreciated!