title: Passing Tuple Embedded Python
body: <p>So I'm writing a C++ application that uses embedded calls to Python to run some computations.
  I have code working for running any script in a specified directory, as well as passing and returning one value between the C++ application and the Python interpreter.</p>
  
  <p>I am, however, having a problem when I try to pass more than one value as a parameter to Python.
  The following is my code for running a Python script:</p>
  
  <pre><code>double Script::run_script(string moduleName, string funcName, double *params, 
                int numberOfParams)
  {
    PyObject *pName, *pModule, *pDict, *pFunc;
    PyObject *pArgs, *pValue;
    int i;
  
    // Set aside memory for the path to my scripts to add to sys.path
    char dir[255];
    memset(dir, 0, sizeof(dir));
    /* ********************************************* */
    sprintf(dir, "/home/bmalone/workspace/NumericalAnalysis/Testing"); //Put path into dir
     /* ********************************************* */
  
    Py_Initialize();
  
    PyObject *dictModule = PyImport_GetModuleDict();
    PyObject *sys = PyDict_GetItemString(dictModule, "sys");
  
    // Sys NULL?
    if (sys == NULL)
      {
        cerr &lt;&lt; "Cannot find sys in dict!" &lt;&lt; endl;
        return -1;
      }
  
    dictModule = PyModule_GetDict(sys);
    PyObject *path = PyDict_GetItemString(dictModule, "path");
  
    PyObject *pPath = PyUnicode_FromString(dir);
    PyList_Append(path, pPath);
  
    if (pPath);
      //cerr &lt;&lt; "PATH &gt; " &lt;&lt; PyString_AsString(PyObject_Repr(path)) &lt;&lt; endl;
    else
      cerr &lt;&lt; "pPath IS NULL" &lt;&lt; endl;
  
  
    /* Import the module that contains the function/script */
    pModule = PyImport_ImportModule(moduleName.c_str());
  
    if (pModule);
      //cerr &lt;&lt; "pModule ~ " &lt;&lt; PyString_AsString(PyObject_Repr(pModule)) &lt;&lt; endl;
    else
      cerr &lt;&lt; "pModule is NULL!!" &lt;&lt; endl;
  
    //cerr &lt;&lt; "Getting dict for argv[1]..." &lt;&lt; endl;
    dictModule = PyModule_GetDict(pModule);
    if (dictModule);
      //cerr &lt;&lt; "DictModule ~ " &lt;&lt; PyString_AsString(PyObject_Repr(dictModule))  &lt;&lt; endl;
    else
      cerr &lt;&lt; "DictModule is NULL!!" &lt;&lt; endl;
  
    // CALL THE FUNCTION!
    /*
     * argv[1] is the MODULE name, but argv[2] SHOULD BE the function name!
     */
    /* Get a PyObject referencing the function from the module 'pModule' */
    //pFunc = PyObject_GetAttrString(pModule, funcName.c_str());
    pFunc = PyDict_GetItemString(dictModule, funcName.c_str());
  
    if (pFunc)
      {
        PyObject *objRep = PyObject_Repr(pFunc);
        const char* str = PyString_AsString(objRep);
        cerr &lt;&lt; "Func is a real boy, too! - " &lt;&lt; str  &lt;&lt; endl;
      }
    else
      cerr &lt;&lt; "pFunc is NULL!" &lt;&lt; endl;
  
    /* Setup PyArgs */
    pArgs = PyTuple_New(numberOfParams);
    // Add the data as doubles
    cerr &lt;&lt; "&lt;parameters&gt; ~ params size is " &lt;&lt; numberOfParams  &lt;&lt; endl;
    for (int i = 0; i &lt; numberOfParams; i++)
      {
        if (params)
      {
        /* **************************** */
        //pValue = PyDouble_AsDouble(params[i]);
        pValue = PyFloat_FromDouble(params[i]);
        /* **************************** */
  
        cerr &lt;&lt; " " &lt;&lt; params[i] &lt;&lt; endl;
        PyObject* objRep = PyObject_Repr(pValue);
        const char* strRep = PyString_AsString(objRep);
        cerr &lt;&lt; "&gt;&gt; pValue = " &lt;&lt; strRep &lt;&lt; endl;
        PyTuple_SetItem(pArgs, i, pValue);
      }
        else
      break;
      }
  
    cerr &lt;&lt; "&lt;Module&gt; - " &lt;&lt; moduleName &lt;&lt; ", is null: " &lt;&lt; (pModule == NULL) &lt;&lt; endl;
    cerr &lt;&lt; "&lt;Function&gt; - " &lt;&lt; funcName &lt;&lt; ", is null: " &lt;&lt; (pFunc == NULL) &lt;&lt; endl;
  
    // Get the retun value
    cerr &lt;&lt; "&lt;Calling Function&gt;" &lt;&lt; endl;
    //pValue = PyObject_CallFunction(pFunc, "f", 15.0); // **WORKS**
    pValue = PyObject_CallObject(pFunc, pArgs); // **WORKS**
  
    //pValue = PyObject_CallObject(pFunc, NULL);
    //pValue = PyEval_CallObject(pFunc, NULL);
    //pValue = PyObject_CallFunction(pFunc, NULL);
    //pValue = PyObject_CallObject(pFunc, args);
  
    //pValue = PyEval_CallObject(pFunc, args);
  
    //cerr &lt;&lt; "ANSWER ---&gt; " &lt;&lt; PyFloat_AsDouble(pValue) &lt;&lt; endl;
  
    return PyFloat_AsDouble(pValue);
    Py_Finalize();
  }
  </code></pre>
  
  <p>EDIT:
  In my original post, I made a change I forgot I made, and it was a bug that prevented any script from running. The above code correctly runs any script, <em>except</em> it only works if pArgs has --1-- element. Any time I try to pass more than one element, even to the same script just to be printed out, the script never even runs.
  But if it's just 1 then it works perfectly.</p>
  
  <p>I can't seem to figure out what I'm doing wrong to pass more than 1 parameter to my scripts.
  This is the script I'm trying to run from C++ (the one that works for 1 parameter):</p>
  
  <pre><code>#!/usr/bin/python
  '''
  Created on Jul 12, 2012
  
  @author: bmalone
  '''
  import sys
  
  
  def doubleTwo(x, y):
      print str(sys.argv)
      print "Can haz doubling?\n"
  
  
  if __name__ == '__main__':
      doubleTwo()
  </code></pre>
  
  <p>Any help would be appreciated!</p>