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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 867 - (hide annotations) (download) (as text)
Fri Sep 29 11:25:47 2006 UTC (14 years ago) by johnpye
File MIME type: text/x-csrc
File size: 3079 byte(s)
more diagnostic output, trying to find reason for crash on Windows
1 johnpye 862 /* ASCEND modelling environment
2     Copyright (C) 2006 Carnegie Mellon University
3    
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2, or (at your option)
7     any later version.
8    
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12     GNU General Public License for more details.
13    
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 59 Temple Place - Suite 330,
17     Boston, MA 02111-1307, USA.
18     */
19    
20     #include <stdio.h>
21     #include <string.h>
22    
23     #include <utilities/ascConfig.h>
24     #include <utilities/error.h>
25     #include <general/ospath.h>
26    
27     #include <compiler/importhandler.h>
28    
29 johnpye 865 #include <Python.h>
30    
31 johnpye 862 ImportHandlerCreateFilenameFn extpy_filename;
32     ImportHandlerImportFn extpy_import;
33    
34     #ifndef ASC_EXPORT
35     # error "Where is ASC_EXPORT?"
36     #endif
37    
38     /**
39     This is the function called from "IMPORT extpy"
40    
41     It sets up the functions in this external function library
42     */
43     extern ASC_EXPORT(int) extpy_register(){
44     int result = 0;
45    
46     ERROR_REPORTER_HERE(ASC_PROG_NOTE,"Hello from EXTPY...");
47    
48     struct ImportHandler *handler;
49     handler = ASC_NEW(struct ImportHandler);
50    
51     handler->name = "extpy";
52     handler->filenamefn = extpy_filename;
53     handler->importfn = extpy_import;
54    
55     result = importhandler_add(handler);
56    
57     if(result){
58     ERROR_REPORTER_HERE(ASC_PROG_ERR,"Failed to register import handler (error = %d)",result);
59     }
60     return result;
61     }
62    
63     /**
64     Create a filename base on a partial filename. In that case of python, this
65     just means adding '.py' to the end.
66 johnpye 867
67 johnpye 862 @param partialname the filename without suffix, as specified in the user's "IMPORT" command
68     @return new filename, or NULL on failure
69     */
70     char *extpy_filename(const char *partialname){
71     char *name;
72     int len;
73     if(partialname==NULL){
74     ERROR_REPORTER_HERE(ASC_PROG_ERR,"Partial name is NULL, can't work out filename");
75     return NULL;
76     }
77 johnpye 867
78 johnpye 862 len = strlen(partialname);
79     name = ASC_NEW_ARRAY_CLEAR(char,len+4);
80     strcpy(name,partialname);
81     strcat(name,".py");
82 johnpye 865 CONSOLE_DEBUG("New filename is '%s'",name);
83 johnpye 862 return name;
84     }
85    
86     /**
87     Import a python script located at the path indicated.
88    
89     @return 0 on success, else error codes (TBD)
90     */
91 johnpye 864 int extpy_import(const struct FilePath *fp, const char *initfunc, const char *partialpath){
92 johnpye 862 char *name;
93     name = ospath_str(fp);
94 johnpye 865 FILE *f;
95    
96 johnpye 862 CONSOLE_DEBUG("IMPORTING PYTHON SCRIPT %s",name);
97 johnpye 865 if(Py_IsInitialized()){
98     CONSOLE_DEBUG("PYTHON IS ALREADY INITIALISED");
99     }else{
100     CONSOLE_DEBUG("INITIALISING PYTHON");
101     Py_Initialize();
102 johnpye 867 CONSOLE_DEBUG("COMPLETED ATTEMPT TO INITIALISE PYTHON");
103 johnpye 865 }
104    
105 johnpye 867 if(!Py_IsInitialized()){
106     CONSOLE_DEBUG("UNABLE TO INITIALIZE PYTHON");
107     return 1;
108     }
109     PyRun_SimpleString("print \"HELLO FROM PYTHON IN C\"");
110    
111 johnpye 865 PyRun_SimpleString("import ascpy");
112     f = fopen(name,"r");
113     PyRun_AnyFile(f,name);
114     fclose(f);
115    
116 johnpye 862 ASC_FREE(name);
117 johnpye 865 return 1;
118 johnpye 862 }
119    

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