Problem passing args from C to Python... 
Author Message
 Problem passing args from C to Python...

In the following code sample I am trying to build an array of the =
fibonacci sequence using a python function 'fib' defined in module =
'fibo.py'.  I can interactively use the fib() code but when trying the =
following:

#include <Python.h>
#include <stdio.h>
#define  MAX_SIZE  10

int main(int argc, char** argv)
{
    int i;
    PyObject*  module;          /* module to be loaded */
    PyObject*  function;        /* function to be used */
    PyObject*  value;           /* value returned by function */
    int        array[MAX_SIZE]; /* array of fibonacci sequence */

    /*
    ** Initialize Python interpreter, set sys.path, and load my module
    */
    Py_Initialize();
    PySys_SetArgv(argc, argv);
    module =3D PyImport_ImportModule("fibo");
    if (!module) PyErr_Print();
    function =3D PyObject_GetAttrString(module, "fib");
    if (!function) PyErr_Print();
   =20
    /*
    ** Construct array of fibonacci sequence
    */
    printf("Constructing Fibonacci sequence...\n");
    for (i=3D0; i<MAX_SIZE; i++)
    {
        value =3D PyObject_CallFunction(function, "i", i);
        printf("%s\n", PyString_AsString(value));
        /*
        ** I will remove this comment when PySring_AsString(value)=20
        ** returns something other than (null) :-/
        sscanf(PyString_AsString(value), "%d", &array[i]);
        */
        Py_DECREF(value);
    }
   =20

Quote:
}

I get the following prints:
(null)
(null)
(null)
(null)
(null)
(null)
(null)
(null)
(null)
(null)

What am I doing wrong?  The source for abstract.h says that =
PyObject_CallFunction takes a mkvalue-style format string. I can't find =
any docs on this format, but the Lutz book refers to it on pg. 523. =20
Any help is appreciated,

Allen Gooch



Fri, 31 Mar 2000 03:00:00 GMT  
 Problem passing args from C to Python...

Quote:

>In the following code sample I am trying to build an array of the =
>fibonacci sequence using a python function 'fib' defined in module =
>'fibo.py'.  I can interactively use the fib() code but when trying the =
>following:

        Is fibo.fib() written to actually return a Python string?  If
it returns an integer, then use PyInt_AsLong(value) to get the integer
value.  (PyString_AsString returns NULL if the object it's passed
isn't a string, so that's why I suspect a type mismatch.)

Quote:
>        value = PyObject_CallFunction(function, "i", i);
>        printf("%s\n", PyString_AsString(value));

        If your Python function raises an exception, value will be
NULL, so that should be checked, too.

        Andrew Kuchling

        http://starship.skyport.net/crew/amk/



Fri, 31 Mar 2000 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Are method args pass-by-value or pass-by-reference

2. Problems passing a function and its args around.

3. NT args passing problem

4. Problem: args don't pass at exec (tkcvs)

5. How to pass multible args to main

6. passing data/info/args to other programs....

7. Passing args to DLL - How Many?

8. Verilog - PLI passing args.

9. Newbie: how to pass args to C from asm

10. args passed from C (doing this without the ARG directive)

11. passing args via command line on win xp

12. Passing args C <-> Fortran

 

 
Powered by phpBB® Forum Software