Actual source code: pythonsys.c

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

  3: #if !defined(PETSC_PYTHON_EXE)
  4:   #define PETSC_PYTHON_EXE "python"
  5: #endif

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

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

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

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

 51: static PetscErrorCode PetscPythonFindLibrary(const char pythonexe[], char pythonlib[], size_t pl)
 52: {
 53:   // clang-format off
 54:   const char *const cmdlines[] = {"-c 'import os, sysconfig; print(os.path.join(sysconfig.get_config_var(\"LIBDIR\"),sysconfig.get_config_var(\"LDLIBRARY\")))'",
 55:                                   "-c 'import os, sysconfig; print(os.path.join(sysconfig.get_path(\"stdlib\"),os.path.pardir,\"libpython\"+sysconfig.get_python_version()+\".dylib\"))'",
 56:                                   "-c 'import os, sysconfig; print(os.path.join(sysconfig.get_path(\"stdlib\"),os.path.pardir,\"libpython\"+sysconfig.get_python_version()+\".so\"))'",
 57:                                   "-c 'import os, sysconfig; print(os.path.join(sysconfig.get_config_var(\"LIBPL\"),sysconfig.get_config_var(\"LDLIBRARY\")))'",
 58:                                   "-c 'import sysconfig; print(sysconfig.get_config_var(\"LIBPYTHON\"))'",
 59:                                   "-c 'import os, sysconfig; print(os.path.join(sysconfig.get_config_var(\"LIBDIR\"),\"libpython\"+sysconfig.get_python_version()+\".dylib\"))'",
 60:                                   "-c 'import os, sysconfig; print(os.path.join(sysconfig.get_config_var(\"LIBDIR\"),\"libpython\"+sysconfig.get_python_version()+\".so\"))'"};
 61:   // clang-format on

 63:   PetscBool found = PETSC_FALSE;

 65:   PetscFunctionBegin;
 66: #if defined(PETSC_PYTHON_LIB)
 67:   PetscCall(PetscStrncpy(pythonlib, PETSC_PYTHON_LIB, pl));
 68:   PetscFunctionReturn(PETSC_SUCCESS);
 69: #endif

 71:   for (size_t i = 0; i < PETSC_STATIC_ARRAY_LENGTH(cmdlines); i++) {
 72:     PetscCall(PetscInfo(NULL, "Looking for Python library with \"%s %s\"\n", pythonexe, cmdlines[i]));
 73:     PetscCall(PetscPythonFindLibraryName(pythonexe, cmdlines[i], pythonlib, pl, &found));
 74:     if (found) break;
 75:   }
 76:   PetscCheck(found, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Unable to find Python dynamic library");
 77:   PetscCall(PetscInfo(NULL, "Python library %s found %d\n", pythonlib, found));
 78:   PetscFunctionReturn(PETSC_SUCCESS);
 79: }

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

 83: static PyObject *Py_None = NULL;

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

 87: static int (*Py_IsInitialized)(void);
 88: static void (*Py_InitializeEx)(int);
 89: static void (*Py_Finalize)(void);

 91: static void (*Py_SetProgramName)(const wchar_t *);
 92: static wchar_t *(*Py_DecodeLocale)(const char *, size_t *);

 94: static void (*Py_IncRef)(PyObject *);
 95: static void (*Py_DecRef)(PyObject *);

 97: static void (*PySys_SetArgv)(int, void *);
 98: static PyObject *(*PySys_GetObject)(const char *);
 99: static PyObject *(*PyObject_CallMethod)(PyObject *, const char *, const char *, ...);
100: static PyObject *(*PyImport_ImportModule)(const char *);

102: static void (*PyErr_Clear)(void);
103: static PyObject *(*PyErr_Occurred)(void);
104: static void (*PyErr_Fetch)(PyObject **, PyObject **, PyObject **);
105: static void (*PyErr_NormalizeException)(PyObject **, PyObject **, PyObject **);
106: static void (*PyErr_Display)(PyObject *, PyObject *, PyObject *);
107: static void (*PyErr_Restore)(PyObject *, PyObject *, PyObject *);

109: static void (*PyMem_RawFree)(void *);

111: #define PetscDLPyLibOpen(libname)      PetscDLLibraryAppend(PETSC_COMM_SELF, &PetscDLLibrariesLoaded, libname)
112: #define PetscDLPyLibSym(symbol, value) PetscDLLibrarySym(PETSC_COMM_SELF, &PetscDLLibrariesLoaded, NULL, symbol, (void **)value)
113: #define PetscDLPyLibClose(comm) \
114:   do { \
115:   } while (0)

117: static PetscErrorCode PetscPythonLoadLibrary(const char pythonlib[])
118: {
119:   PetscFunctionBegin;
120:   /* open the Python dynamic library */
121:   PetscCall(PetscDLPyLibOpen(pythonlib));
122:   PetscCall(PetscInfo(NULL, "Python: loaded dynamic library %s\n", pythonlib));
123:   /* look required symbols from the Python C-API */
124:   PetscCall(PetscDLPyLibSym("_Py_NoneStruct", &Py_None));
125:   PetscCall(PetscDLPyLibSym("Py_GetVersion", &Py_GetVersion));
126:   PetscCall(PetscDLPyLibSym("Py_IsInitialized", &Py_IsInitialized));
127:   PetscCall(PetscDLPyLibSym("Py_InitializeEx", &Py_InitializeEx));
128:   PetscCall(PetscDLPyLibSym("Py_SetProgramName", &Py_SetProgramName));
129:   PetscCall(PetscDLPyLibSym("Py_DecodeLocale", &Py_DecodeLocale));
130:   PetscCall(PetscDLPyLibSym("PyMem_RawFree", &PyMem_RawFree));
131:   PetscCall(PetscDLPyLibSym("Py_Finalize", &Py_Finalize));
132:   PetscCall(PetscDLPyLibSym("PySys_GetObject", &PySys_GetObject));
133:   PetscCall(PetscDLPyLibSym("PySys_SetArgv", &PySys_SetArgv));
134:   PetscCall(PetscDLPyLibSym("PyObject_CallMethod", &PyObject_CallMethod));
135:   PetscCall(PetscDLPyLibSym("PyImport_ImportModule", &PyImport_ImportModule));
136:   PetscCall(PetscDLPyLibSym("Py_IncRef", &Py_IncRef));
137:   PetscCall(PetscDLPyLibSym("Py_DecRef", &Py_DecRef));
138:   PetscCall(PetscDLPyLibSym("PyErr_Clear", &PyErr_Clear));
139:   PetscCall(PetscDLPyLibSym("PyErr_Occurred", &PyErr_Occurred));
140:   PetscCall(PetscDLPyLibSym("PyErr_Fetch", &PyErr_Fetch));
141:   PetscCall(PetscDLPyLibSym("PyErr_NormalizeException", &PyErr_NormalizeException));
142:   PetscCall(PetscDLPyLibSym("PyErr_Display", &PyErr_Display));
143:   PetscCall(PetscDLPyLibSym("PyErr_Restore", &PyErr_Restore));
144:   /* XXX TODO: check that ALL symbols were there !!! */
145:   PetscCheck(Py_None, PETSC_COMM_SELF, PETSC_ERR_LIB, "Python: failed to load symbols from Python dynamic library %s", pythonlib);
146:   PetscCheck(Py_GetVersion, PETSC_COMM_SELF, PETSC_ERR_LIB, "Python: failed to load symbols from Python dynamic library %s", pythonlib);
147:   PetscCheck(Py_IsInitialized, PETSC_COMM_SELF, PETSC_ERR_LIB, "Python: failed to load symbols from Python dynamic library %s", pythonlib);
148:   PetscCheck(Py_InitializeEx, PETSC_COMM_SELF, PETSC_ERR_LIB, "Python: failed to load symbols from Python dynamic library %s", pythonlib);
149:   PetscCheck(Py_Finalize, PETSC_COMM_SELF, PETSC_ERR_LIB, "Python: failed to load symbols from Python dynamic library %s", pythonlib);
150:   PetscCall(PetscInfo(NULL, "Python: all required symbols loaded from Python dynamic library %s\n", pythonlib));
151:   PetscFunctionReturn(PETSC_SUCCESS);
152: }

154: static char      PetscPythonExe[PETSC_MAX_PATH_LEN] = {0};
155: static char      PetscPythonLib[PETSC_MAX_PATH_LEN] = {0};
156: static PetscBool PetscBeganPython                   = PETSC_FALSE;

158: /*@
159:   PetscPythonFinalize - Finalize PETSc for use with Python.

161:   Level: intermediate

163: .seealso: `PetscPythonInitialize()`, `PetscPythonPrintError()`
164: @*/
165: PetscErrorCode PetscPythonFinalize(void)
166: {
167:   PetscFunctionBegin;
168:   if (PetscBeganPython) {
169:     if (Py_IsInitialized()) Py_Finalize();
170:   }
171:   PetscBeganPython = PETSC_FALSE;
172:   PetscFunctionReturn(PETSC_SUCCESS);
173: }

175: /*@
176:   PetscPythonInitialize - Initialize Python for use with PETSc and import petsc4py.

178:    Input Parameters:
179: +  pyexe - path to the Python interpreter executable, or `NULL`.
180: -  pylib - full path to the Python dynamic library, or `NULL`.

182:   Options Database Key:
183: . -python exe - Initializes Python, and optionally takes a Python executable name

185:   Level: intermediate

187: .seealso: `PetscPythonFinalize()`, `PetscPythonPrintError()`
188: @*/
189: PetscErrorCode PetscPythonInitialize(const char pyexe[], const char pylib[])
190: {
191:   PyObject *module = NULL;

193:   PetscFunctionBegin;
194:   if (PetscBeganPython) PetscFunctionReturn(PETSC_SUCCESS);
195:   /* Python executable */
196:   if (pyexe && pyexe[0] != 0) {
197:     PetscCall(PetscStrncpy(PetscPythonExe, pyexe, sizeof(PetscPythonExe)));
198:   } else {
199:     PetscCall(PetscPythonFindExecutable(PetscPythonExe, sizeof(PetscPythonExe)));
200:   }
201:   /* Python dynamic library */
202:   if (pylib && pylib[0] != 0) {
203:     PetscCall(PetscStrncpy(PetscPythonLib, pylib, sizeof(PetscPythonLib)));
204:   } else {
205:     PetscCall(PetscPythonFindLibrary(PetscPythonExe, PetscPythonLib, sizeof(PetscPythonLib)));
206:   }
207:   /* dynamically load Python library */
208:   PetscCall(PetscPythonLoadLibrary(PetscPythonLib));
209:   /* initialize Python */
210:   PetscBeganPython = PETSC_FALSE;
211:   if (!Py_IsInitialized()) {
212:     static PetscBool registered = PETSC_FALSE;
213:     const char      *py_version;
214:     PyObject        *sys_path;
215:     char             path[PETSC_MAX_PATH_LEN] = {0};
216:     wchar_t         *wPetscPythonExe          = NULL;

218:     /* add program name to ease find the petsc4py module with virtual environments */
219:     PetscCallExternalVoid("Py_DecodeLocale", wPetscPythonExe = Py_DecodeLocale(PetscPythonExe, NULL));
220:     PetscCallExternalVoid("Py_SetProgramName", Py_SetProgramName(wPetscPythonExe));
221:     PetscCallExternalVoid("PyMem_RawFree", PyMem_RawFree(wPetscPythonExe));

223:     /* initialize Python. Py_InitializeEx() prints an error and EXITS the program if it is not successful! */
224:     PetscCall(PetscInfo(NULL, "Calling Py_InitializeEx(0)\n"));
225:     PetscCallExternalVoid("Py_InitializeEx", Py_InitializeEx(0)); /* 0: do not install signal handlers */
226:     PetscCall(PetscInfo(NULL, "Py_InitializeEx(0) called successfully\n"));

228:     /* build 'sys.argv' list */
229:     py_version = Py_GetVersion();
230:     if (py_version[0] == '2') {
231:       int   argc    = 0;
232:       char *argv[1] = {NULL};
233:       PySys_SetArgv(argc, argv);
234:     }
235:     if (py_version[0] == '3') {
236:       int      argc    = 0;
237:       wchar_t *argv[1] = {NULL};
238:       PySys_SetArgv(argc, argv);
239:     }

241:     /* add PETSC_LIB_DIR in front of 'sys.path' */
242:     sys_path = PySys_GetObject("path");
243:     if (sys_path) {
244:       int zero = 0;
245:       PetscCall(PetscStrreplace(PETSC_COMM_SELF, "${PETSC_LIB_DIR}", path, sizeof(path)));
246:       Py_DecRef(PyObject_CallMethod(sys_path, "insert", "is", zero, (char *)path));
247: #if defined(PETSC_PETSC4PY_INSTALL_PATH)
248:       {
249:         char *rpath;
250:         PetscCall(PetscStrallocpy(PETSC_PETSC4PY_INSTALL_PATH, &rpath));
251:         Py_DecRef(PyObject_CallMethod(sys_path, "insert", "is", zero, rpath));
252:         PetscCall(PetscFree(rpath));
253:       }
254: #endif
255:     }
256:     /* register finalizer */
257:     if (!registered) {
258:       PetscCall(PetscRegisterFinalize(PetscPythonFinalize));
259:       registered = PETSC_TRUE;
260:     }
261:     PetscBeganPython = PETSC_TRUE;
262:     PetscCall(PetscInfo(NULL, "Python initialize completed\n"));
263:   }
264:   /* import 'petsc4py.PETSc' module */
265:   PetscCall(PetscFPTrapPush(PETSC_FP_TRAP_OFF));
266:   PetscCallExternalVoid("PyImport_ImportModule", module = PyImport_ImportModule("petsc4py.PETSc"));
267:   PetscCall(PetscFPTrapPop());
268:   if (module) {
269:     PetscCall(PetscInfo(NULL, "Python: successfully imported module 'petsc4py.PETSc'\n"));
270:     Py_DecRef(module);
271:     module = NULL;
272:   } else {
273:     PetscCall(PetscInfo(NULL, "Python: error when importing module 'petsc4py.PETSc'\n"));
274:     PetscCall(PetscPythonPrintError());
275:     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Python: could not import module 'petsc4py.PETSc', perhaps your PYTHONPATH does not contain it");
276:   }
277:   PetscFunctionReturn(PETSC_SUCCESS);
278: }

280: /*@C
281:   PetscPythonPrintError - Print any current Python errors.

283:   Level: developer

285: .seealso: `PetscPythonInitialize()`, `PetscPythonFinalize()`
286: @*/
287: PetscErrorCode PetscPythonPrintError(void)
288: {
289:   PyObject *exc = NULL, *val = NULL, *tb = NULL;

291:   PetscFunctionBegin;
292:   if (!PetscBeganPython) PetscFunctionReturn(PETSC_SUCCESS);
293:   if (!PyErr_Occurred()) PetscFunctionReturn(PETSC_SUCCESS);
294:   PyErr_Fetch(&exc, &val, &tb);
295:   PyErr_NormalizeException(&exc, &val, &tb);
296:   PyErr_Display(exc ? exc : Py_None, val ? val : Py_None, tb ? tb : Py_None);
297:   PyErr_Restore(exc, val, tb);
298:   PetscFunctionReturn(PETSC_SUCCESS);
299: }

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

304: /*@
305:   PetscPythonMonitorSet - Set a Python monitor for a `PetscObject`

307:   Level: developer

309: .seealso: `PetscPythonInitialize()`, `PetscPythonFinalize()`, `PetscPythonPrintError()`
310: @*/
311: PetscErrorCode PetscPythonMonitorSet(PetscObject obj, const char url[])
312: {
313:   PetscFunctionBegin;
315:   PetscAssertPointer(url, 2);
316:   if (!PetscPythonMonitorSet_C) {
317:     PetscCall(PetscPythonInitialize(NULL, NULL));
318:     PetscCheck(PetscPythonMonitorSet_C, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Couldn't initialize Python support for monitors");
319:   }
320:   PetscCall(PetscPythonMonitorSet_C(obj, url));
321:   PetscFunctionReturn(PETSC_SUCCESS);
322: }