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