Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > how to do "load script; run script" in a loop in embedded python?

Reply
Thread Tools

how to do "load script; run script" in a loop in embedded python?

 
 
lipingffeng@gmail.com
Guest
Posts: n/a
 
      04-03-2008
Hi, all,

I am currently involved in a project that needs to load/run a python
script dynamically in a C application. The sample code is as
following:

PyObject *LoadScript(char *file, char *func)
{
PyObject *pName, *pModule, *pDict, *pFunc;

pName = PyString_FromString(file);
pModule = PyImport_Import(pName);
pDict = PyModule_GetDict(pModule);
pFunc = PyDict_GetItemString(pDict, func);
return pFunc;
}

int RunScript(PyObject *pFunc, PyObject *arglist)
{
PyObject *pValue = PyObject_CallFunction(pFunc, "O", arglist);
int ret = PyInt_AsLong(pValue);
return ret;
}

int main(int argc, char *argv[])
{
PyObject *arglist, *pFunc;
char imgData[10];
int ret;

for(int i = 0; i < 10; i++)
imgData[i] = 48 + i;

arglist = Py_BuildValue("s#", imgData, 10);

Py_SetProgramName(argv[0]);
Py_Initialize();
PySys_SetArgv(argc, argv);

for (int k = 0; k < 3; k++) // using loop to imitate dynamic
loading/running script
{
pFunc = LoadScript(argv[1], argv[2]);
ret = RunScript(pFunc, arglist);
}

Py_Finalize();
return 0;
}

The first loop is perfectly ok, but on the second loop, script loading
is successful but running will always fail.

Any ideas would be highly apprecicated.

Thanks,

Liping
 
Reply With Quote
 
 
 
 
Gabriel Genellina
Guest
Posts: n/a
 
      04-07-2008
En Thu, 03 Apr 2008 20:52:31 -0300, <(E-Mail Removed)> escribió:

> Hi, all,
>
> I am currently involved in a project that needs to load/run a python
> script dynamically in a C application. The sample code is as
> following:
>
> PyObject *LoadScript(char *file, char *func)
> {
> PyObject *pName, *pModule, *pDict, *pFunc;
>
> pName = PyString_FromString(file);
> pModule = PyImport_Import(pName);
> pDict = PyModule_GetDict(pModule);
> pFunc = PyDict_GetItemString(pDict, func);
> return pFunc;
> }


Remember to check all PyObject* return values, NULL means there was an
error.
And pay attention to reference counts! Read the section about reference
counts in both books, Extending and Embedding, and the Python API
Reference. http://docs.python.org/

> The first loop is perfectly ok, but on the second loop, script loading
> is successful but running will always fail.


"fail" in what form? A Python exception? The program freezes? A core dump?

--
Gabriel Genellina

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
Scripts run using load in "for" loop run out of order Fa Sidd Ruby 12 01-26-2008 12:06 AM
Embedded languages based on early Ada (from "Re: Preferred OS, processor family for running embedded Ada?") Colin Paul Gloster VHDL 48 04-10-2007 10:31 AM
How to display images embedded in e-mail as embedded, not attachments Jim Firefox 4 12-11-2004 05:36 AM



Advertisments