1 #define PETSC_DLL 2 3 #include "petscsys.h" /*I "petscsys.h" I*/ 4 5 /* ---------------------------------------------------------------- */ 6 7 #if !defined(PETSC_PYTHON_EXE) 8 #define PETSC_PYTHON_EXE "python" 9 #endif 10 11 #undef __FUNCT__ 12 #define __FUNCT__ "PetscPythonFindExecutable" 13 static PetscErrorCode PetscPythonFindExecutable(char pythonexe[PETSC_MAX_PATH_LEN]) 14 { 15 PetscBool flag; 16 PetscErrorCode ierr; 17 PetscFunctionBegin; 18 /* get the path for the Python interpreter executable */ 19 ierr = PetscStrncpy(pythonexe,PETSC_PYTHON_EXE,PETSC_MAX_PATH_LEN);CHKERRQ(ierr); 20 ierr = PetscOptionsGetString(PETSC_NULL,"-python",pythonexe,PETSC_MAX_PATH_LEN,&flag);CHKERRQ(ierr); 21 if (!flag || pythonexe[0]==0) { 22 ierr = PetscStrncpy(pythonexe,PETSC_PYTHON_EXE,PETSC_MAX_PATH_LEN);CHKERRQ(ierr); 23 } 24 PetscFunctionReturn(0); 25 } 26 27 #undef __FUNCT__ 28 #define __FUNCT__ "PetscPythonFindLibrary" 29 static PetscErrorCode PetscPythonFindLibrary(char pythonexe[PETSC_MAX_PATH_LEN], 30 char pythonlib[PETSC_MAX_PATH_LEN]) 31 { 32 const char cmdline[] = "-c 'import sys; print(sys.exec_prefix); print(sys.version[:3])'"; 33 char command[PETSC_MAX_PATH_LEN+1+sizeof(cmdline)+1]; 34 char prefix[PETSC_MAX_PATH_LEN],version[8],sep[2]={PETSC_DIR_SEPARATOR, 0},*eol; 35 FILE* fp = NULL; 36 char path[PETSC_MAX_PATH_LEN+1]; 37 PetscBool found = PETSC_FALSE; 38 PetscErrorCode ierr; 39 PetscFunctionBegin; 40 41 #if defined(PETSC_PYTHON_LIB) 42 ierr = PetscStrcpy(pythonlib,PETSC_PYTHON_LIB);CHKERRQ(ierr); 43 PetscFunctionReturn(0); 44 #endif 45 46 /* call Python to find out the name of the Python dynamic library */ 47 ierr = PetscStrncpy(command,pythonexe,PETSC_MAX_PATH_LEN);CHKERRQ(ierr); 48 ierr = PetscStrcat(command," ");CHKERRQ(ierr); 49 ierr = PetscStrcat(command,cmdline);CHKERRQ(ierr); 50 #if defined(PETSC_HAVE_POPEN) 51 ierr = PetscPOpen(PETSC_COMM_SELF,PETSC_NULL,command,"r",&fp);CHKERRQ(ierr); 52 if (!fgets(prefix,sizeof(prefix),fp)) 53 { SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Python: bad output from executable: %s",pythonexe); } 54 if (!fgets(version,sizeof(version),fp)) 55 { SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Python: bad output from executable: %s",pythonexe); } 56 ierr = PetscPClose(PETSC_COMM_SELF,fp);CHKERRQ(ierr); 57 #else 58 SETERRQ(PETSC_COMM_SELF,1,"Python: Aborted due to missing popen()"); 59 #endif 60 /* remove newlines */ 61 ierr = PetscStrchr(prefix,'\n',&eol);CHKERRQ(ierr); 62 if (eol) eol[0] = 0; 63 ierr = PetscStrchr(version,'\n',&eol);CHKERRQ(ierr); 64 if (eol) eol[0] = 0; 65 66 /* test for $prefix/lib64/libpythonX.X[.so]*/ 67 ierr = PetscStrcpy(pythonlib,prefix);CHKERRQ(ierr); 68 ierr = PetscStrcat(pythonlib,sep);CHKERRQ(ierr); 69 ierr = PetscStrcat(pythonlib,"lib64");CHKERRQ(ierr); 70 ierr = PetscTestDirectory(pythonlib,'r',&found);CHKERRQ(ierr); 71 if (found) { 72 ierr = PetscStrcat(pythonlib,sep);CHKERRQ(ierr); 73 ierr = PetscStrcat(pythonlib,"libpython");CHKERRQ(ierr); 74 ierr = PetscStrcat(pythonlib,version);CHKERRQ(ierr); 75 ierr = PetscDLLibraryRetrieve(PETSC_COMM_SELF,pythonlib,path,PETSC_MAX_PATH_LEN,&found);CHKERRQ(ierr); 76 if (found) PetscFunctionReturn(0); 77 } 78 79 /* test for $prefix/lib/libpythonX.X[.so]*/ 80 ierr = PetscStrcpy(pythonlib,prefix);CHKERRQ(ierr); 81 ierr = PetscStrcat(pythonlib,sep);CHKERRQ(ierr); 82 ierr = PetscStrcat(pythonlib,"lib");CHKERRQ(ierr); 83 ierr = PetscTestDirectory(pythonlib,'r',&found);CHKERRQ(ierr); 84 if (found) { 85 ierr = PetscStrcat(pythonlib,sep);CHKERRQ(ierr); 86 ierr = PetscStrcat(pythonlib,"libpython");CHKERRQ(ierr); 87 ierr = PetscStrcat(pythonlib,version);CHKERRQ(ierr); 88 ierr = PetscDLLibraryRetrieve(PETSC_COMM_SELF,pythonlib,path,PETSC_MAX_PATH_LEN,&found);CHKERRQ(ierr); 89 if (found) PetscFunctionReturn(0); 90 } 91 92 /* nothing good found */ 93 ierr = PetscMemzero(pythonlib,PETSC_MAX_PATH_LEN);CHKERRQ(ierr); 94 ierr = PetscInfo(0,"Python dynamic library not found\n");CHKERRQ(ierr); 95 96 PetscFunctionReturn(0); 97 } 98 99 /* ---------------------------------------------------------------- */ 100 101 typedef struct _Py_object_t PyObject; /* fake definition */ 102 103 static PyObject* Py_None = 0; 104 105 static const char* (*Py_GetVersion)(void); 106 107 static int (*Py_IsInitialized)(void); 108 static void (*Py_InitializeEx)(int); 109 static void (*Py_Finalize)(void); 110 111 static void (*PySys_SetArgv)(int, char **); 112 static PyObject* (*PyImport_ImportModule)(const char *); 113 114 static void (*Py_IncRef)(PyObject *); 115 static void (*Py_DecRef)(PyObject *); 116 117 static void (*PyErr_Clear)(void); 118 static PyObject* (*PyErr_Occurred)(void); 119 static void (*PyErr_Fetch)(PyObject **, PyObject **, PyObject **); 120 static void (*PyErr_NormalizeException)(PyObject **, PyObject **, PyObject **); 121 static void (*PyErr_Display)(PyObject *, PyObject *, PyObject *); 122 static void (*PyErr_Restore)(PyObject *, PyObject *, PyObject *); 123 124 125 #define PetscDLPyLibOpen(libname) \ 126 PetscDLLibraryAppend(PETSC_COMM_SELF,&DLLibrariesLoaded,libname) 127 #define PetscDLPyLibSym(symbol, value) \ 128 PetscDLLibrarySym(PETSC_COMM_SELF,&DLLibrariesLoaded,PETSC_NULL,symbol,(void**)value) 129 #define PetscDLPyLibClose(comm) \ 130 do { } while(0) 131 132 #undef __FUNCT__ 133 #define __FUNCT__ "PetscPythonLoadLibrary" 134 static PetscErrorCode PetscPythonLoadLibrary(const char pythonlib[]) 135 { 136 PetscErrorCode ierr; 137 PetscFunctionBegin; 138 139 /* open the Python dynamic library */ 140 ierr = PetscDLPyLibOpen(pythonlib);CHKERRQ(ierr); 141 ierr = PetscInfo1(0,"Python: loaded dynamic library %s\n", pythonlib);CHKERRQ(ierr); 142 /* look required symbols from the Python C-API */ 143 ierr = PetscDLPyLibSym("_Py_NoneStruct" , &Py_None );CHKERRQ(ierr); 144 ierr = PetscDLPyLibSym("Py_GetVersion" , &Py_GetVersion );CHKERRQ(ierr); 145 ierr = PetscDLPyLibSym("Py_IsInitialized" , &Py_IsInitialized );CHKERRQ(ierr); 146 ierr = PetscDLPyLibSym("Py_InitializeEx" , &Py_InitializeEx );CHKERRQ(ierr); 147 ierr = PetscDLPyLibSym("Py_Finalize" , &Py_Finalize );CHKERRQ(ierr); 148 ierr = PetscDLPyLibSym("PySys_SetArgv" , &PySys_SetArgv );CHKERRQ(ierr); 149 ierr = PetscDLPyLibSym("PyImport_ImportModule" , &PyImport_ImportModule );CHKERRQ(ierr); 150 ierr = PetscDLPyLibSym("Py_IncRef" , &Py_IncRef );CHKERRQ(ierr); 151 ierr = PetscDLPyLibSym("Py_DecRef" , &Py_DecRef );CHKERRQ(ierr); 152 ierr = PetscDLPyLibSym("PyErr_Clear" , &PyErr_Clear );CHKERRQ(ierr); 153 ierr = PetscDLPyLibSym("PyErr_Occurred" , &PyErr_Occurred );CHKERRQ(ierr); 154 ierr = PetscDLPyLibSym("PyErr_Fetch" , &PyErr_Fetch );CHKERRQ(ierr); 155 ierr = PetscDLPyLibSym("PyErr_NormalizeException", &PyErr_NormalizeException);CHKERRQ(ierr); 156 ierr = PetscDLPyLibSym("PyErr_Display", &PyErr_Display );CHKERRQ(ierr); 157 ierr = PetscDLPyLibSym("PyErr_Restore", &PyErr_Restore );CHKERRQ(ierr); 158 /* XXX TODO: check that ALL symbols were there !!! */ 159 if (!Py_None) SETERRQ(PETSC_COMM_SELF,1,"Python: failed to load symbols from dynamic library"); 160 if (!Py_GetVersion) SETERRQ(PETSC_COMM_SELF,1,"Python: failed to load symbols from dynamic library"); 161 if (!Py_IsInitialized) SETERRQ(PETSC_COMM_SELF,1,"Python: failed to load symbols from dynamic library"); 162 if (!Py_InitializeEx) SETERRQ(PETSC_COMM_SELF,1,"Python: failed to load symbols from dynamic library"); 163 if (!Py_Finalize) SETERRQ(PETSC_COMM_SELF,1,"Python: failed to load symbols from dynamic library"); 164 ierr = PetscInfo(0,"Python: all required symbols loaded from Python dynamic library\n");CHKERRQ(ierr); 165 166 PetscFunctionReturn(0); 167 } 168 169 /* ---------------------------------------------------------------- */ 170 171 static char PetscPythonExe[PETSC_MAX_PATH_LEN] = { 0 }; 172 static char PetscPythonLib[PETSC_MAX_PATH_LEN] = { 0 }; 173 static PetscBool PetscBeganPython = PETSC_FALSE; 174 175 #undef __FUNCT__ 176 #define __FUNCT__ "PetscPythonFinalize" 177 /*@C 178 PetscPythonFinalize - Finalize Python. 179 180 Level: intermediate 181 182 .keywords: Python 183 @*/ 184 PetscErrorCode PetscPythonFinalize(void) 185 { 186 PetscFunctionBegin; 187 if (PetscBeganPython) { if (Py_IsInitialized()) Py_Finalize(); } 188 PetscBeganPython = PETSC_FALSE; 189 PetscFunctionReturn(0); 190 } 191 192 #undef __FUNCT__ 193 #define __FUNCT__ "PetscPythonInitialize" 194 /*@C 195 PetscPythonInitialize - Initialize Python and import petsc4py. 196 197 Input Parameter: 198 + pyexe - path to the Python interpreter executable, or PETSC_NULL. 199 - pylib - full path to the Python dynamic library, or PETSC_NULL. 200 201 Level: intermediate 202 203 .keywords: Python 204 205 @*/ 206 PetscErrorCode PetscPythonInitialize(const char pyexe[],const char pylib[]) 207 { 208 PyObject *module = 0; 209 static PetscBool registered = PETSC_FALSE; 210 PetscErrorCode ierr; 211 PetscFunctionBegin; 212 if (PetscBeganPython) PetscFunctionReturn(0); 213 /* Python executable */ 214 if (pyexe && pyexe[0] != 0) { 215 ierr = PetscStrncpy(PetscPythonExe,pyexe,sizeof(PetscPythonExe));CHKERRQ(ierr); 216 } else { 217 ierr = PetscPythonFindExecutable(PetscPythonExe);CHKERRQ(ierr); 218 } 219 /* Python dynamic library */ 220 if (pylib && pylib[0] != 0) { 221 ierr = PetscStrncpy(PetscPythonLib,pylib,sizeof(PetscPythonLib));CHKERRQ(ierr); 222 } else { 223 ierr = PetscPythonFindLibrary(PetscPythonExe,PetscPythonLib);CHKERRQ(ierr); 224 } 225 /* dynamically load Python library */ 226 ierr = PetscPythonLoadLibrary(PetscPythonLib);CHKERRQ(ierr); 227 /* initialize Python */ 228 PetscBeganPython = PETSC_FALSE; 229 if (!Py_IsInitialized()) { 230 const char *py_version = Py_GetVersion(); 231 /* call below does not install signal handlers */ 232 Py_InitializeEx(0); 233 /* call below required to build 'sys.argv' list */ 234 if (py_version[0] == '2') { 235 int argc = 0; 236 char **argv = 0; 237 ierr = PetscGetArgs(&argc,&argv);CHKERRQ(ierr); 238 if (argc && argv) PySys_SetArgv(argc,argv); 239 } 240 /* register finalizer */ 241 if (!registered) { 242 ierr = PetscRegisterFinalize(PetscPythonFinalize);CHKERRQ(ierr); 243 registered = PETSC_TRUE; 244 } 245 PetscBeganPython = PETSC_TRUE; 246 } 247 /* import 'petsc4py.PETSc' module */ 248 module = PyImport_ImportModule("petsc4py.PETSc"); 249 if (module) { 250 ierr = PetscInfo(0,"Python: successfully imported module 'petsc4py.PETSc'\n");CHKERRQ(ierr); 251 Py_DecRef(module); module = 0; 252 } else { 253 PetscPythonPrintError(); 254 SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Python: could not import module 'petsc4py.PETSc', perhaps your PYTHONPATH does not contain it\n"); 255 } 256 PetscFunctionReturn(0); 257 } 258 259 #undef __FUNCT__ 260 #define __FUNCT__ "PetscPythonPrintError" 261 /*@C 262 PetscPythonPrintError - Print Python errors. 263 264 Level: developer 265 266 .keywords: Python 267 268 @*/ 269 PetscErrorCode PetscPythonPrintError(void) 270 { 271 PyObject *exc=0, *val=0, *tb=0; 272 PetscFunctionBegin; 273 if (!PetscBeganPython) PetscFunctionReturn(0); 274 if (!PyErr_Occurred()) PetscFunctionReturn(0); 275 PyErr_Fetch(&exc,&val,&tb); 276 PyErr_NormalizeException(&exc,&val,&tb); 277 PyErr_Display(exc ? exc : Py_None, 278 val ? val : Py_None, 279 tb ? tb : Py_None); 280 PyErr_Restore(exc,val,tb); 281 PetscFunctionReturn(0); 282 } 283 284 /* ---------------------------------------------------------------- */ 285