Executing script with embedded python

Discussion in 'Python' started by Andrew Ayre, Jan 25, 2006.

  1. Andrew Ayre

    Andrew Ayre Guest

    Hi, I have a script that I want to execute from C. I don't want to call any
    functions, just execute the script. Below is a code snippet.

    The problem is that PyObject_CallObject always returns NULL. Is this the
    correct return value for simply executing a script, as there is no function
    return value involved?

    // load script
    pname = PyString_FromString(SCRIPTFILENAME);
    pmodule = PyImport_Import(pname);
    Py_DECREF(pname);

    if (pmodule != NULL)
    {
    // execute script
    presult = PyObject_CallObject(pmodule, NULL);
    if (presult == NULL)
    {
    Py_DECREF(pmodule);
    return;
    }
    Py_DECREF(presult);
    Py_DECREF(pmodule);
    }
    else
    {
    return;
    }


    regards, Andy
    (remove year to reply)
     
    Andrew Ayre, Jan 25, 2006
    #1
    1. Advertising

  2. > The problem is that PyObject_CallObject always returns NULL. Is this the
    > correct return value for simply executing a script, as there is no function
    > return value involved?


    The documentation for PyObject_CallObject states the following:

    "Returns the result of the call on success, or NULL on failure".

    So it seems like the call is failing. My guess would be that modules are
    not callable objects. Also, this seems somewhat redundant since your
    module is effectively executed when you import it using the
    PyImport_Import function.

    -Farshid
     
    Farshid Lashkari, Jan 26, 2006
    #2
    1. Advertising

  3. Andrew Ayre

    Andrew Ayre Guest

    "Farshid Lashkari" <> wrote in message
    news:cE_Bf.11329$ZA5.11001@fed1read05...
    > > The problem is that PyObject_CallObject always returns NULL. Is this the
    > > correct return value for simply executing a script, as there is no

    function
    > > return value involved?

    >
    > The documentation for PyObject_CallObject states the following:
    >
    > "Returns the result of the call on success, or NULL on failure".
    >
    > So it seems like the call is failing. My guess would be that modules are
    > not callable objects. Also, this seems somewhat redundant since your
    > module is effectively executed when you import it using the
    > PyImport_Import function.
    >
    > -Farshid
    >


    Thanks for the reply. The script does execute and do what I want it to do,
    without any problems. The only problem is that I get the NULL result. So I
    think it is callable.

    Andy
     
    Andrew Ayre, Jan 26, 2006
    #3
  4. Andrew Ayre

    Andrew Ayre Guest

    "Farshid Lashkari" <> wrote in message
    news:cE_Bf.11329$ZA5.11001@fed1read05...
    > > The problem is that PyObject_CallObject always returns NULL. Is this the
    > > correct return value for simply executing a script, as there is no

    function
    > > return value involved?

    >
    > The documentation for PyObject_CallObject states the following:
    >
    > "Returns the result of the call on success, or NULL on failure".
    >
    > So it seems like the call is failing. My guess would be that modules are
    > not callable objects. Also, this seems somewhat redundant since your
    > module is effectively executed when you import it using the
    > PyImport_Import function.
    >
    > -Farshid
    >


    I redirected stderr to my application and it is reporting that "module" is
    not callable. Pretty odd, considering it is executing the script anyway.
    I'll look into this further. Thanks.

    Andy
     
    Andrew Ayre, Jan 26, 2006
    #4
  5. Andrew Ayre

    Andrew Ayre Guest

    "Farshid Lashkari" <> wrote in message
    news:cE_Bf.11329$ZA5.11001@fed1read05...
    > > The problem is that PyObject_CallObject always returns NULL. Is this the
    > > correct return value for simply executing a script, as there is no

    function
    > > return value involved?

    >
    > The documentation for PyObject_CallObject states the following:
    >
    > "Returns the result of the call on success, or NULL on failure".
    >
    > So it seems like the call is failing. My guess would be that modules are
    > not callable objects. Also, this seems somewhat redundant since your
    > module is effectively executed when you import it using the
    > PyImport_Import function.
    >
    > -Farshid
    >


    Yes, you were 100% correct. Thanks!

    Andy
     
    Andrew Ayre, Jan 26, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sarmin kho
    Replies:
    1
    Views:
    386
    Cameron Laird
    Jun 4, 2004
  2. Replies:
    3
    Views:
    1,089
    James Stroud
    Dec 13, 2005
  3. Replies:
    1
    Views:
    488
    Gabriel Genellina
    Apr 7, 2008
  4. ashish
    Replies:
    4
    Views:
    304
    ashish
    Sep 20, 2012
  5. ashish
    Replies:
    5
    Views:
    540
    Piet van Oostrum
    Sep 20, 2012
Loading...

Share This Page