/[ascend]/trunk/models/johnpye/extpy/extpy.c
ViewVC logotype

Diff of /trunk/models/johnpye/extpy/extpy.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 872 by johnpye, Thu Oct 5 12:52:43 2006 UTC revision 873 by johnpye, Thu Oct 5 14:51:49 2006 UTC
# Line 28  Line 28 
28  #include <general/ospath.h>  #include <general/ospath.h>
29    
30  #include <compiler/importhandler.h>  #include <compiler/importhandler.h>
31    #include <compiler/extfunc.h>
32    
33  #include <Python.h>  #include <Python.h>
34    
# Line 46  ImportHandlerImportFn extpy_import; Line 47  ImportHandlerImportFn extpy_import;
47  extern ASC_EXPORT(int) extpy_register(){  extern ASC_EXPORT(int) extpy_register(){
48      int result = 0;      int result = 0;
49    
50      ERROR_REPORTER_HERE(ASC_PROG_NOTE,"Hello from EXTPY...");      CONSOLE_DEBUG("Hello...");
51    
52      struct ImportHandler *handler;      struct ImportHandler *handler;
53      handler = ASC_NEW(struct ImportHandler);      handler = ASC_NEW(struct ImportHandler);
# Line 64  extern ASC_EXPORT(int) extpy_register(){ Line 65  extern ASC_EXPORT(int) extpy_register(){
65  }  }
66    
67  /*------------------------------------------------------------------------------  /*------------------------------------------------------------------------------
68    METHODS TO EXPOSE DATA TO THE EXTERNAL SCRIPT    PYTHON METHOD INVOKER
69    */
70    
71    ExtMethodRun extpy_invokemethod;
72    
73    /** Method invoker. extpy will supply a pointer to this function whenever it
74        registers a python function as an external script method. This function will
75        then dereference the user_data field into a python function, and execute that
76        python function.
77    
78        One difficult aspect is the question of how to usefully pass the 'context'
79        argument to Python?
80    */
81    int extpy_invokemethod(struct Instance *context, struct gl_list_t *args, void *user_data){
82        PyObject *fn;
83        /* cast user data to PyObject pointer */
84        fn = (PyObject *) user_data;
85    
86        ERROR_REPORTER_HERE(ASC_USER_NOTE,"RUNNING PYTHON METHOD");
87        CONSOLE_DEBUG("RUNNING PYTHON METHOD...");
88        return 1;
89    }
90    
91    /*------------------------------------------------------------------------------
92      'EXTPY' PYTHON STATIC MODULE
93  */  */
94    
 /* Return the number of arguments of the application command line */  
95  static PyObject *extpy_getbrowser(PyObject *self, PyObject *args){  static PyObject *extpy_getbrowser(PyObject *self, PyObject *args){
96      PyObject *browser;      PyObject *browser;
97      if(args!=NULL){      if(args!=NULL){
# Line 77  static PyObject *extpy_getbrowser(PyObje Line 101  static PyObject *extpy_getbrowser(PyObje
101      return Py_BuildValue("O",browser);      return Py_BuildValue("O",browser);
102  }  }
103    
104    static PyObject *extpy_registermethod(PyObject *self, PyObject *args){
105        PyObject *fn, *name, *docstring;
106        const char *cname, *cdocstring;
107        int res;
108        int nargs = 1;
109    
110        PyArg_ParseTuple(args,"O:registermethod", &fn);
111        if(!PyCallable_Check(fn)){
112            PyErr_SetString(PyExc_TypeError,"parameter must be callable");
113            return NULL;
114        }
115        Py_INCREF(fn);
116    
117        CONSOLE_DEBUG("FOUND FN=%p",fn);
118    
119        name = PyObject_GetAttr(fn,PyString_FromString("__name__"));
120        if(name==NULL){
121            CONSOLE_DEBUG("No __name__ attribute");
122            PyErr_SetString(PyExc_TypeError,"No __name__ attribute");
123            return NULL;
124        }
125        cname = PyString_AsString(name);
126    
127        CONSOLE_DEBUG("REGISTERED METHOD '%s' HAS %d ARGS",cname,nargs);
128    
129        docstring = PyObject_GetAttr(fn,PyString_FromString("func_doc"));
130        cdocstring = "(no help)";
131        if(name!=NULL){
132            cdocstring = PyString_AsString(docstring);
133            CONSOLE_DEBUG("DOCSTRING: %s",cdocstring);
134        }
135    
136        res = CreateUserFunctionMethod(cname,extpy_invokemethod,nargs,cdocstring,fn);
137    
138        CONSOLE_DEBUG("EXTPY INVOKER IS AT %p",extpy_invokemethod);
139    
140        if(res){
141            ERROR_REPORTER_HERE(ASC_PROG_ERR,"Problem registering external script method (%d)",res);
142            PyErr_SetString(PyExc_Exception,"unable to register script method");
143            return NULL;
144        }
145    
146        ERROR_REPORTER_HERE(ASC_PROG_NOTE,"Registered python method '%s'",cname);
147    
148        /* nothing gets returned (but possibly an exception) */
149        Py_INCREF(Py_None);
150        return Py_None;
151    }
152    
153  static PyMethodDef extpymethods[] = {  static PyMethodDef extpymethods[] = {
154      {"getbrowser", extpy_getbrowser, METH_NOARGS,"Retrieve browser pointer"}      {"getbrowser", extpy_getbrowser, METH_NOARGS,"Retrieve browser pointer"}
155        ,{"registermethod", extpy_registermethod, METH_VARARGS,"Register a python method as an ASCEND script method"}
156      ,{NULL,NULL,0,NULL}      ,{NULL,NULL,0,NULL}
157  };  };
158    
159  PyMODINIT_FUNC initextpy(void){  PyMODINIT_FUNC initextpy(void){
160      PyObject *obj;      PyObject *obj;
     CONSOLE_DEBUG("registering 'extpy' module...");  
161      obj = Py_InitModule3("extpy", extpymethods,"Module for accessing shared ASCEND pointers from python");      obj = Py_InitModule3("extpy", extpymethods,"Module for accessing shared ASCEND pointers from python");
     CONSOLE_DEBUG("returned %p",obj);  
     CONSOLE_DEBUG("name %s",PyModule_GetName(obj));  
162  }  }
163    
164  /*------------------------------------------------------------------------------  /*------------------------------------------------------------------------------
# Line 128  int extpy_import(const struct FilePath * Line 199  int extpy_import(const struct FilePath *
199      FILE *f;      FILE *f;
200      PyObject *pyfile;      PyObject *pyfile;
201    
202      CONSOLE_DEBUG("IMPORTING PYTHON SCRIPT %s",name);      CONSOLE_DEBUG("Importing Python script %s",name);
203      if(Py_IsInitialized()){      if(Py_IsInitialized()){
204          CONSOLE_DEBUG("PYTHON IS ALREADY INITIALISED");          CONSOLE_DEBUG("Python was already initialised");
205      }else{      }else{
206          CONSOLE_DEBUG("INITIALISING PYTHON");          CONSOLE_DEBUG("INITIALISING PYTHON");
207          Py_Initialize();          Py_Initialize();
# Line 138  int extpy_import(const struct FilePath * Line 209  int extpy_import(const struct FilePath *
209      }      }
210    
211      if(!Py_IsInitialized()){      if(!Py_IsInitialized()){
212            ERROR_REPORTER_HERE(ASC_PROG_ERR,"Unable to initialise Python");
213          CONSOLE_DEBUG("UNABLE TO INITIALIZE PYTHON");          CONSOLE_DEBUG("UNABLE TO INITIALIZE PYTHON");
214            ASC_FREE(name);
215          return 1;          return 1;
216      }      }
     PyRun_SimpleString("print \"HELLO FROM PYTHON IN C\"");  
     CONSOLE_DEBUG("IMPORTING ASCPY...");  
     PyRun_SimpleString("import ascpy");  
     CONSOLE_DEBUG("CREATING LIBRARY OBJECT...");  
     PyRun_SimpleString("L = ascpy.Library()");  
     CONSOLE_DEBUG("PRINTING MESSAGE...");  
     PyRun_SimpleString("print \"IMPORTED ASCPY\"");  
     PyRun_SimpleString("print L");  
217    
218      initextpy();      initextpy();
219    
     CONSOLE_DEBUG("OPENING THE SCRIPT \"%s\"",name);  
220      pyfile = PyFile_FromString(name,"r");      pyfile = PyFile_FromString(name,"r");
221      if(pyfile==NULL){      if(pyfile==NULL){
222          CONSOLE_DEBUG("Failed opening script");          CONSOLE_DEBUG("Failed opening script");
223          ERROR_REPORTER_HERE(ASC_PROG_ERR,"UNABLE TO OPEN SCRIPT");          ERROR_REPORTER_HERE(ASC_PROG_ERR,"Unable to open '%s' (%s)",partialpath,name);
224            ASC_FREE(name);
225          return 1;          return 1;
226      }      }
227            
228      f = PyFile_AsFile(pyfile);            f = PyFile_AsFile(pyfile);      
229      if(f==NULL){      if(f==NULL){
230          ERROR_REPORTER_HERE(ASC_PROG_ERR,"UNABLE TO CAST TO FILE*");          ERROR_REPORTER_HERE(ASC_PROG_ERR,"Unable to cast PyObject to FILE*");
231            ASC_FREE(name);
232          return 1;          return 1;
233      }      }
234      CONSOLE_DEBUG("RUNNING THE SCRIPT");  
235      PyRun_AnyFileEx(f,name,1);      PyRun_AnyFileEx(f,name,1);
236      CONSOLE_DEBUG("FINISHED RUNNING THE SCRIPT");      /*if(PyErr_Occurred()){
237            ERROR_REPORTER_HERE(ASC_PROG_ERR,"An error occurred in the python script '%s'. Check the console for details");
238            PyErr_Print();
239            PyErr_Clear();
240            return 1;
241        }*/
242    
243        ERROR_REPORTER_HERE(ASC_PROG_NOTE,"Imported python script '%s' (check console for errors)",partialpath);
244    
245      ASC_FREE(name);      ASC_FREE(name);
246      return 1;      return 0;
247  }  }
248    

Legend:
Removed from v.872  
changed lines
  Added in v.873

john.pye@anu.edu.au
ViewVC Help
Powered by ViewVC 1.1.22