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