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

Discussion in 'Python' started by lipingffeng@gmail.com, Apr 4, 2008.

  1. Guest

    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 = 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
     
    , Apr 4, 2008
    #1
    1. Advertising

  2. En Thu, 03 Apr 2008 20:52:31 -0300, <> 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
     
    Gabriel Genellina, Apr 7, 2008
    #2
    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. Colin Paul Gloster
    Replies:
    48
    Views:
    1,931
    Colin Paul Gloster
    Apr 10, 2007
  2. Thomas Dodds

    Databind an embedded control in an embedded datagrid

    Thomas Dodds, Jul 26, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    419
    Thomas Dodds
    Jul 26, 2004
  3. Trans
    Replies:
    11
    Views:
    327
    micathom
    Sep 5, 2007
  4. Num GG
    Replies:
    2
    Views:
    391
    Num GG
    Nov 17, 2008
  5. Isaac Won
    Replies:
    9
    Views:
    419
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page