Executing script with embedded python

A

Andrew Ayre

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
(e-mail address removed) (remove year to reply)
 
F

Farshid Lashkari

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
 
A

Andrew Ayre

Farshid Lashkari said:
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
 
A

Andrew Ayre

Farshid Lashkari said:
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
 
A

Andrew Ayre

Farshid Lashkari said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top