Actual source code: pythonsys.c

  1: #include <petsc/private/petscimpl.h>

  3: /* ---------------------------------------------------------------- */

  5: #if !defined(PETSC_PYTHON_EXE)
  6:   #define PETSC_PYTHON_EXE "python"
  7: #endif

  9: static PetscErrorCode PetscPythonFindExecutable(char pythonexe[], size_t len)
 10: {
 11:   PetscBool flag;

 13:   /* get the path for the Python interpreter executable */
 14:   PetscStrncpy(pythonexe, PETSC_PYTHON_EXE, len);
 15:   PetscOptionsGetString(NULL, NULL, "-python", pythonexe, len, &flag);
 16:   if (!flag || pythonexe[0] == 0) PetscStrncpy(pythonexe, PETSC_PYTHON_EXE, len);
 17:   return 0;
 18: }

 20: /*
 21:     Python does not appear to have a universal way to indicate the location of Python dynamic library so try several possibilities
 22: */
 23: static PetscErrorCode PetscPythonFindLibraryName(const char pythonexe[], const char attempt[], char pythonlib[], size_t pl, PetscBool *found)
 24: {
 25:   char  command[2 * PETSC_MAX_PATH_LEN];
 26:   FILE *fp = NULL;
 27:   char *eol;

 29:   /* call Python to find out the name of the Python dynamic library */
 30:   PetscStrncpy(command, pythonexe, sizeof(command));
 31:   PetscStrlcat(command, " ", sizeof(command));
 32:   PetscStrlcat(command, attempt, sizeof(command));
 33: #if defined(PETSC_HAVE_POPEN)
 34:   PetscPOpen(PETSC_COMM_SELF, NULL, command, "r", &fp);
 36:   PetscPClose(PETSC_COMM_SELF, fp);
 37: #else
 38:   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_LIB, "Python: Aborted due to missing popen()");
 39: #endif
 40:   /* remove newlines */
 41:   PetscStrchr(pythonlib, '\n', &eol);
 42:   if (eol) eol[0] = 0;
 43:   PetscTestFile(pythonlib, 'r', found);
 44:   return 0;
 45: }

 47: static PetscErrorCode PetscPythonFindLibrary(const char pythonexe[], char pythonlib[], size_t pl)
 48: {
 49:   const char cmdline1[] = "-c 'import os, sysconfig; print(os.path.join(sysconfig.get_config_var(\"LIBDIR\"),sysconfig.get_config_var(\"LDLIBRARY\")))'";
 50:   const char cmdline2[] = "-c 'import os, sysconfig; print(os.path.join(sysconfig.get_path(\"stdlib\"),os.path.pardir,\"libpython\"+sysconfig.get_python_version()+\".dylib\"))'";
 51:   const char cmdline3[] = "-c 'import os, sysconfig; print(os.path.join(sysconfig.get_config_var(\"LIBPL\"),sysconfig.get_config_var(\"LDLIBRARY\")))'";
 52:   const char cmdline4[] = "-c 'import sysconfig; print(sysconfig.get_config_var(\"LIBPYTHON\"))'";
 53:   const char cmdline5[] = "-c 'import os, sysconfig; import sys;print(os.path.join(sysconfig.get_config_var(\"LIBDIR\"),\"libpython\"+sys.version[:3]+\".so\"))'";

 55:   PetscBool found = PETSC_FALSE;

 57: #if defined(PETSC_PYTHON_LIB)
 58:   PetscStrncpy(pythonlib, PETSC_PYTHON_LIB, pl);
 59:   return 0;
 60: #endif

 62:   PetscPythonFindLibraryName(pythonexe, cmdline1, pythonlib, pl, &found);
 63:   if (!found) PetscPythonFindLibraryName(pythonexe, cmdline2, pythonlib, pl, &found);
 64:   if (!found) PetscPythonFindLibraryName(pythonexe, cmdline3, pythonlib, pl, &found);
 65:   if (!found) PetscPythonFindLibraryName(pythonexe, cmdline4, pythonlib, pl, &found);
 66:   if (!found) PetscPythonFindLibraryName(pythonexe, cmdline5, pythonlib, pl, &found);
 67:   PetscInfo(NULL, "Python library  %s found %d\n", pythonlib, found);
 68:   return 0;
 69: }

 71: /* ---------------------------------------------------------------- */

 73: typedef struct _Py_object_t PyObject; /* fake definition */

 75: static PyObject *Py_None = NULL;

 77: static const char *(*Py_GetVersion)(void);

 79: static int (*Py_IsInitialized)(void);
 80: static void (*Py_InitializeEx)(int);
 81: static void (*Py_Finalize)(void);

 83: static void (*PySys_SetArgv)(int, void *);
 84: static PyObject *(*PySys_GetObject)(const char *);
 85: static PyObject *(*PyObject_CallMethod)(PyObject *, const char *, const char *, ...);
 86: static PyObject *(*PyImport_ImportModule)(const char *);

 88: static void (*Py_IncRef)(PyObject *);
 89: static void (*Py_DecRef)(PyObject *);

 91: static void (*PyErr_Clear)(void);
 92: static PyObject *(*PyErr_Occurred)(void);
 93: static void (*PyErr_Fetch)(PyObject **, PyObject **, PyObject **);
 94: static void (*PyErr_NormalizeException)(PyObject **, PyObject **, PyObject **);
 95: static void (*PyErr_Display)(PyObject *, PyObject *, PyObject *);
 96: static void (*PyErr_Restore)(PyObject *, PyObject *, PyObject *);

 98: #define PetscDLPyLibOpen(libname)      PetscDLLibraryAppend(PETSC_COMM_SELF, &PetscDLLibrariesLoaded, libname)
 99: #define PetscDLPyLibSym(symbol, value) PetscDLLibrarySym(PETSC_COMM_SELF, &PetscDLLibrariesLoaded, NULL, symbol, (void **)value)
100: #define PetscDLPyLibClose(comm) \
101:   do { \
102:   } while (0)

104: static PetscErrorCode PetscPythonLoadLibrary(const char pythonlib[])
105: {
106:   /* open the Python dynamic library */
107:   PetscDLPyLibOpen(pythonlib);
108:   PetscInfo(NULL, "Python: loaded dynamic library %s\n", pythonlib);
109:   /* look required symbols from the Python C-API */
110:   PetscDLPyLibSym("_Py_NoneStruct", &Py_None);
111:   PetscDLPyLibSym("Py_GetVersion", &Py_GetVersion);
112:   PetscDLPyLibSym("Py_IsInitialized", &Py_IsInitialized);
113:   PetscDLPyLibSym("Py_InitializeEx", &Py_InitializeEx);
114:   PetscDLPyLibSym("Py_Finalize", &Py_Finalize);
115:   PetscDLPyLibSym("PySys_GetObject", &PySys_GetObject);
116:   PetscDLPyLibSym("PySys_SetArgv", &PySys_SetArgv);
117:   PetscDLPyLibSym("PyObject_CallMethod", &PyObject_CallMethod);
118:   PetscDLPyLibSym("PyImport_ImportModule", &PyImport_ImportModule);
119:   PetscDLPyLibSym("Py_IncRef", &Py_IncRef);
120:   PetscDLPyLibSym("Py_DecRef", &Py_DecRef);
121:   PetscDLPyLibSym("PyErr_Clear", &PyErr_Clear);
122:   PetscDLPyLibSym("PyErr_Occurred", &PyErr_Occurred);
123:   PetscDLPyLibSym("PyErr_Fetch", &PyErr_Fetch);
124:   PetscDLPyLibSym("PyErr_NormalizeException", &PyErr_NormalizeException);
125:   PetscDLPyLibSym("PyErr_Display", &PyErr_Display);
126:   PetscDLPyLibSym("PyErr_Restore", &PyErr_Restore);
127:   /* XXX TODO: check that ALL symbols were there !!! */
133:   PetscInfo(NULL, "Python: all required symbols loaded from Python dynamic library %s\n", pythonlib);
134:   return 0;
135: }

137: /* ---------------------------------------------------------------- */

139: static char      PetscPythonExe[PETSC_MAX_PATH_LEN] = {0};
140: static char      PetscPythonLib[PETSC_MAX_PATH_LEN] = {0};
141: static PetscBool PetscBeganPython                   = PETSC_FALSE;

143: /*@C
144:   PetscPythonFinalize - Finalize PETSc for use with Python.

146:   Level: intermediate

148: .seealso: `PetscPythonInitialize()`, `PetscPythonPrintError()`
149: @*/
150: PetscErrorCode PetscPythonFinalize(void)
151: {
152:   if (PetscBeganPython) {
153:     if (Py_IsInitialized()) Py_Finalize();
154:   }
155:   PetscBeganPython = PETSC_FALSE;
156:   return 0;
157: }

159: /*@C
160:   PetscPythonInitialize - Initialize Python for use with PETSc and import petsc4py.

162:    Input Parameters:
163: +  pyexe - path to the Python interpreter executable, or NULL.
164: -  pylib - full path to the Python dynamic library, or NULL.

166:   Level: intermediate

168: .seealso: `PetscPythonFinalize()`, `PetscPythonPrintError()`
169: @*/
170: PetscErrorCode PetscPythonInitialize(const char pyexe[], const char pylib[])
171: {
172:   PyObject *module = NULL;

174:   if (PetscBeganPython) return 0;
175:   /* Python executable */
176:   if (pyexe && pyexe[0] != 0) {
177:     PetscStrncpy(PetscPythonExe, pyexe, sizeof(PetscPythonExe));
178:   } else {
179:     PetscPythonFindExecutable(PetscPythonExe, sizeof(PetscPythonExe));
180:   }
181:   /* Python dynamic library */
182:   if (pylib && pylib[0] != 0) {
183:     PetscStrncpy(PetscPythonLib, pylib, sizeof(PetscPythonLib));
184:   } else {
185:     PetscPythonFindLibrary(PetscPythonExe, PetscPythonLib, sizeof(PetscPythonLib));
186:   }
187:   /* dynamically load Python library */
188:   PetscPythonLoadLibrary(PetscPythonLib);
189:   /* initialize Python */
190:   PetscBeganPython = PETSC_FALSE;
191:   if (!Py_IsInitialized()) {
192:     static PetscBool registered = PETSC_FALSE;
193:     const char      *py_version;
194:     PyObject        *sys_path;
195:     char             path[PETSC_MAX_PATH_LEN] = {0};

197:     /* initialize Python. Py_InitializeEx() prints an error and EXITS the program if it is not successful! */
198:     PetscInfo(NULL, "Calling Py_InitializeEx(0);\n");
199:     Py_InitializeEx(0); /* 0: do not install signal handlers */
200:     PetscInfo(NULL, "Py_InitializeEx(0) called successfully;\n");

202:     /*  build 'sys.argv' list */
203:     py_version = Py_GetVersion();
204:     if (py_version[0] == '2') {
205:       int   argc    = 0;
206:       char *argv[1] = {NULL};
207:       PySys_SetArgv(argc, argv);
208:     }
209:     if (py_version[0] == '3') {
210:       int      argc    = 0;
211:       wchar_t *argv[1] = {NULL};
212:       PySys_SetArgv(argc, argv);
213:     }
214:     /* add PETSC_LIB_DIR in front of 'sys.path' */
215:     sys_path = PySys_GetObject("path");
216:     if (sys_path) {
217:       PetscStrreplace(PETSC_COMM_SELF, "${PETSC_LIB_DIR}", path, sizeof(path));
218:       Py_DecRef(PyObject_CallMethod(sys_path, "insert", "is", (int)0, (char *)path));
219: #if defined(PETSC_PETSC4PY_INSTALL_PATH)
220:       {
221:         char *rpath;
222:         PetscStrallocpy(PETSC_PETSC4PY_INSTALL_PATH, &rpath);
223:         Py_DecRef(PyObject_CallMethod(sys_path, "insert", "is", (int)0, rpath));
224:         PetscFree(rpath);
225:       }
226: #endif
227:     }
228:     /* register finalizer */
229:     if (!registered) {
230:       PetscRegisterFinalize(PetscPythonFinalize);
231:       registered = PETSC_TRUE;
232:     }
233:     PetscBeganPython = PETSC_TRUE;
234:     PetscInfo(NULL, "Python initialize completed.\n");
235:   }
236:   /* import 'petsc4py.PETSc' module */
237:   module = PyImport_ImportModule("petsc4py.PETSc");
238:   if (module) {
239:     PetscInfo(NULL, "Python: successfully imported  module 'petsc4py.PETSc'\n");
240:     Py_DecRef(module);
241:     module = NULL;
242:   } else {
243:     PetscPythonPrintError();
244:     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Python: could not import module 'petsc4py.PETSc', perhaps your PYTHONPATH does not contain it");
245:   }
246:   return 0;
247: }

249: /*@C
250:   PetscPythonPrintError - Print any current Python errors.

252:   Level: developer

254: .seealso: `PetscPythonInitialize()`, `PetscPythonFinalize()`
255: @*/
256: PetscErrorCode PetscPythonPrintError(void)
257: {
258:   PyObject *exc = NULL, *val = NULL, *tb = NULL;

260:   if (!PetscBeganPython) return 0;
261:   if (!PyErr_Occurred()) return 0;
262:   PyErr_Fetch(&exc, &val, &tb);
263:   PyErr_NormalizeException(&exc, &val, &tb);
264:   PyErr_Display(exc ? exc : Py_None, val ? val : Py_None, tb ? tb : Py_None);
265:   PyErr_Restore(exc, val, tb);
266:   return 0;
267: }

269: /* ---------------------------------------------------------------- */

271: PETSC_EXTERN PetscErrorCode (*PetscPythonMonitorSet_C)(PetscObject, const char[]);
272: PetscErrorCode (*PetscPythonMonitorSet_C)(PetscObject, const char[]) = NULL;

274: /*@C
275:   PetscPythonMonitorSet - Set a Python monitor for a `PetscObject`

277:   Level: developer

279: .seealso: `PetscPythonInitialize()`, `PetscPythonFinalize()`, `PetscPythonPrintError()`
280: @*/
281: PetscErrorCode PetscPythonMonitorSet(PetscObject obj, const char url[])
282: {
285:   if (!PetscPythonMonitorSet_C) {
286:     PetscPythonInitialize(NULL, NULL);
288:   }
289:   PetscPythonMonitorSet_C(obj, url);
290:   return 0;
291: }

293: /* ---------------------------------------------------------------- */