invoking interpeter in another interpeter

Anand K Rayudu anand at easi.soft.net
Wed Jan 21 13:32:49 CET 2004


Hi

I have extended/embedded python into my application, it is working 
great, The documentation is too good,
I could replace existing scripting tool to python in no time.


I have one problem with invoking python interpreter from another python 
interpreter.
This is how it is.

I have one C API which executes python interpreter passing the file name,
So there are chances that in that file one command it self is the API to 
execute python.
It is hanging in the second invocation, could some one help.

Thanks in advance
Anand


-------------------------------------------------------------------
Python code:

First python file

from Vdb import *
# This is the invocation of another python interpeter from one
ExecutePythonInterpeter("another.py"  )


The application first calls the ExecutePythonInterpter C API, which in 
turn calls same API for different file,
from python,

Following is the C implementation of ExecutePythonInterpeter.

void ExecutePythonInterpeter(
                char            *file
                )
{
    FILE        *fp;
    char        *msg ;
    PyObject    *exc, *val, *trb, *obj ;
    PyThreadState       *gtstate = NULL;

    if( Py_IsInitialized() == FALSE)
    {
       Py_Initialize();
       PyEval_InitThreads();
       gtstate = PyEval_SaveThread();
    }
    ExecutePythonInterpeterThread(file);
    if(gtstate)
    {
        PyEval_AcquireThread(gtstate);
        Py_Finalize();
        gtstate=NULL;
    }
    return ;
}

void ExecutePythonInterpeterThread(
                char            *file
                )
{
    FILE        *fp;
    char        file_name[256]  ;
    PyThreadState       *tstate ;
    PyEval_AcquireLock();
    tstate = Py_NewInterpreter();
    initVdb();
    initVdbPythonVgPoint2() ;
    initVdbPythonVgPoint3();
    if(tstate == NULL) {
        fprintf(stderr, "Can't create an interpreter\n");
        return ;
    }
    sprintf(file_name,"%s",file);
    fp=fopen(file_name,"r");
    if(!fp)
    {
       fprintf(stderr,"FATAL:File open for %s failed\n",file);
       return ;
    }
    PyRun_SimpleFile(fp,file_name) ;
    Py_EndInterpreter(tstate);
    PyEval_ReleaseLock();
    fclose(fp);
    return ;
}






More information about the Python-list mailing list