Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6111977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:41:33+00:00 2026-05-23T14:41:33+00:00

I am calling python functions from C++. I was wondering if it is possible

  • 0

I am calling python functions from C++. I was wondering if it is possible to determine the number of parameters and the names of these parameters. I have read the link How to find the number of parameters to a Python function from C? however I do not really understand.

I have this C++ function that calls the function ‘add’ from pyFunction.py. ‘add’ takes two parameters and returns the sum.

static float CallPythonFunc( float *parameters )
{
    PyObject *pName, *pModule, *pDict, *pFunc, *pValue, *pArgs;
    float ret;

    // Initialize the python interpreter
    Py_Initialize();

    // Make sure we are getting the module from the correct place
    // ### This is where we will put the path input
    PyRun_SimpleString("import sys");
    PyRun_SimpleString("sys.path.append(\"/Developer/IsadoraSDK/IsadoraDemoMathFunction/\")");

    // Build the name object
    // ### This is where we will put the function input
    pName = PyString_FromString("pyFunction");

    // Load the module object
    pModule = PyImport_Import(pName);

    // pDict is a borrowed reference
    pDict = PyModule_GetDict(pModule);

    // pFunc is a borrowed reference
    pFunc = PyDict_GetItemString(pDict, "add");

    //
    // Somehow get the number of arguments and possible the arguments names from 'add'
    //

    if (PyCallable_Check(pFunc)) 
    {       
        // Set the number of arguments
                // This is where I would like to pass in number of arguments
        pArgs = PyTuple_New( 2 /*number of arguments*/ );

        //
        // Instead of the following if I had the arguments I could loop through them
        // and pass the correct number in
        //

        // Argument 1
        pValue = PyFloat_FromDouble((double)parameters[0]);
        PyTuple_SetItem(pArgs, 0, pValue);

        // Argument 2
        pValue = PyFloat_FromDouble((double)parameters[1]);
        PyTuple_SetItem(pArgs, 1, pValue);

            // Make the call to the function
        pValue = PyObject_CallObject(pFunc, pArgs);

        // Set return value
        ret = (float)PyFloat_AsDouble(pValue);

        // Clean up
        Py_DECREF(pArgs);
        Py_DECREF(pValue);
    }

// Clean up
Py_DECREF(pModule);
Py_DECREF(pName);

// Finish the Python Interpreter
Py_Finalize();

return ret;
}

I am really not that familiar with C/C++ so any help would be really helpful. Thanks to everyone for their time!

EDIT:
So something like the following?

PyObject *tuple, *arglist;
tuple = PyObject_CallMethod(pFunc,"inspect.getargspec","add");
arglist = PyTuple_GetItem(tuple,0);
int size = PyObject_Size(arglist);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T14:41:34+00:00Added an answer on May 23, 2026 at 2:41 pm

    This answer to the question you linked to seems to be what you want. inspect.getargspec does exactly what you want on the Python side, and as the answer states, you can use PyObject_CallMethod or one of the related functions described at that link target to call inspect.getargspec from your C++ code, get the returned tuple as a PyObject, use PyTuple_GetItem(returned_tuple, 0) to get the argument list, and then use either PyObject_Size() or PyObject_Length() on the list to get the number of arguments. You’ll also want to check the second and third elements of the returned tuple and increment the number of arguments by 1 for each of the two that is not Py_None. See the below code snippet for why.

    >>> import inspect
    >>> def testfunc(a, b, c, *d, **e):
        pass
    
    >>> inspect.getargspec(testfunc)
    ArgSpec(args=['a', 'b', 'c'], varargs='d', keywords='e', defaults=None)
    

    Here’s an example of what you should do (not all possible errors may be checked for, but it should be all of the NULL checks that may be necessary):

    PyObject *pName, *pInspect, *argspec_tuple, *arglist;
    int size;
    
    pName = PyString_FromString("inspect");
    
    if (pName)
    {
        pInspect = PyImport_Import(pName);
        Py_DECREF(pName);
    
    
        if (pInspect)
        {
            pName = PyString_FromString("getargspec");
    
            if (pName)
            {
                argspec_tuple = PyObject_CallMethodObjArgs(pInspect, pName, pFunc, NULL);
                Py_DECREF(pName);
    
                if (argspec_tuple)
                {
                    arglist = PyTuple_GetItem(argspec_tuple, 0);
    
                    if (arglist)
                    {
                        size = PyObject_Size(arglist)
                             + (PyTuple_GetItem(argspec_tuple, 1) == Py_None ? 0 : 1)
                             + (PyTuple_GetItem(argspec_tuple, 2) == Py_None ? 0 : 1);  // Haven't actually tested this, but it should work
                    }
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am calling a WSDL web service from Python using SOAPpy . The call
After asking organising my Python project and then calling from a parent file in
I have a bunch of Python functions. Let's call them foo , bar and
Lisp's APPLY is for calling functions with computed argument stored in lists.(Modified from Rainer's
I have a custom c++ module for python that exposes functions, some of which
I have a Python file I'm calling with PHP's exec function. Python then outputs
I have a python script which calls a lot of shell functions. The script
In python, is there a difference between calling clear() and assigning {} to a
I have a Python project that has the following structure: package1 class.py class2.py ...
I know this question has a similar title to this: C++: calling member functions

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.