1

I'm trying to create a python module using my C++ code and I want to declare a function with multiple arguments. (3 in this case) I've read the docs and it says that I must declare METH_VARARGS which I did, but I think I also must change something inside my function to actually receive the arguments. Otherwise it gives me "too many arguments" error when I use my function in python.

Here is the code snippet I'm using:

...
// This function can be called inside a python file.
static PyObject *
call_opencl(PyObject *self, PyObject *args)
{
    const char *command;
    int sts;

    // We except at least one argument to this function
    // Not sure how to accept more than one.
    if (!PyArg_ParseTuple(args, "s", &command))
        return NULL;

    OpenCL kernel = OpenCL();
    kernel.init();

    std::cout << "This message is called from our C code: " << std::string(command) << std::endl;

    sts = 21;

    return PyLong_FromLong(sts);
}

static PyMethodDef NervebloxMethods[] = {
    {"call_kernel",          call_opencl,         METH_VARARGS,              "Creates an opencv instance."},
    {NULL, NULL, 0, NULL}        /* Sentinel */
};

...

1 Answer 1

2

You are still expecting one argument.

if (!PyArg_ParseTuple(args, "s", &command))

the documentation defines how you can expect optional or additional arguments, for example "s|dd" will expect a string and two optional numbers, you still have to pass two doubles to the function for when the numbers are available.

double a = 0; // initial value
double b = 0;
if (!PyArg_ParseTuple(args, "s|dd", &command, &a, &b))
Sign up to request clarification or add additional context in comments.

Comments

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.