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 (*PySys_SetArgv)(int, void *);
92: static PyObject *(*PySys_GetObject)(const char *);
93: static PyObject *(*PyObject_CallMethod)(PyObject *, const char *, const char *, ...);
94: static PyObject *(*PyImport_ImportModule)(const char *);
96: static void (*Py_IncRef)(PyObject *);
97: static void (*Py_DecRef)(PyObject *);
99: static void (*PyErr_Clear)(void);
100: static PyObject *(*PyErr_Occurred)(void);
101: static void (*PyErr_Fetch)(PyObject **, PyObject **, PyObject **);
102: static void (*PyErr_NormalizeException)(PyObject **, PyObject **, PyObject **);
103: static void (*PyErr_Display)(PyObject *, PyObject *, PyObject *);
104: static void (*PyErr_Restore)(PyObject *, PyObject *, PyObject *);
106: #define PetscDLPyLibOpen(libname) PetscDLLibraryAppend(PETSC_COMM_SELF, &PetscDLLibrariesLoaded, libname)
107: #define PetscDLPyLibSym(symbol, value) PetscDLLibrarySym(PETSC_COMM_SELF, &PetscDLLibrariesLoaded, NULL, symbol, (void **)value)
108: #define PetscDLPyLibClose(comm) \
109: do { \
110: } while (0)
112: static PetscErrorCode PetscPythonLoadLibrary(const char pythonlib[])
113: {
114: PetscFunctionBegin;
115: /* open the Python dynamic library */
116: PetscCall(PetscDLPyLibOpen(pythonlib));
117: PetscCall(PetscInfo(NULL, "Python: loaded dynamic library %s\n", pythonlib));
118: /* look required symbols from the Python C-API */
119: PetscCall(PetscDLPyLibSym("_Py_NoneStruct", &Py_None));
120: PetscCall(PetscDLPyLibSym("Py_GetVersion", &Py_GetVersion));
121: PetscCall(PetscDLPyLibSym("Py_IsInitialized", &Py_IsInitialized));
122: PetscCall(PetscDLPyLibSym("Py_InitializeEx", &Py_InitializeEx));
123: PetscCall(PetscDLPyLibSym("Py_Finalize", &Py_Finalize));
124: PetscCall(PetscDLPyLibSym("PySys_GetObject", &PySys_GetObject));
125: PetscCall(PetscDLPyLibSym("PySys_SetArgv", &PySys_SetArgv));
126: PetscCall(PetscDLPyLibSym("PyObject_CallMethod", &PyObject_CallMethod));
127: PetscCall(PetscDLPyLibSym("PyImport_ImportModule", &PyImport_ImportModule));
128: PetscCall(PetscDLPyLibSym("Py_IncRef", &Py_IncRef));
129: PetscCall(PetscDLPyLibSym("Py_DecRef", &Py_DecRef));
130: PetscCall(PetscDLPyLibSym("PyErr_Clear", &PyErr_Clear));
131: PetscCall(PetscDLPyLibSym("PyErr_Occurred", &PyErr_Occurred));
132: PetscCall(PetscDLPyLibSym("PyErr_Fetch", &PyErr_Fetch));
133: PetscCall(PetscDLPyLibSym("PyErr_NormalizeException", &PyErr_NormalizeException));
134: PetscCall(PetscDLPyLibSym("PyErr_Display", &PyErr_Display));
135: PetscCall(PetscDLPyLibSym("PyErr_Restore", &PyErr_Restore));
136: /* XXX TODO: check that ALL symbols were there !!! */
137: PetscCheck(Py_None, PETSC_COMM_SELF, PETSC_ERR_LIB, "Python: failed to load symbols from Python dynamic library %s", pythonlib);
138: PetscCheck(Py_GetVersion, PETSC_COMM_SELF, PETSC_ERR_LIB, "Python: failed to load symbols from Python dynamic library %s", pythonlib);
139: PetscCheck(Py_IsInitialized, PETSC_COMM_SELF, PETSC_ERR_LIB, "Python: failed to load symbols from Python dynamic library %s", pythonlib);
140: PetscCheck(Py_InitializeEx, PETSC_COMM_SELF, PETSC_ERR_LIB, "Python: failed to load symbols from Python dynamic library %s", pythonlib);
141: PetscCheck(Py_Finalize, PETSC_COMM_SELF, PETSC_ERR_LIB, "Python: failed to load symbols from Python dynamic library %s", pythonlib);
142: PetscCall(PetscInfo(NULL, "Python: all required symbols loaded from Python dynamic library %s\n", pythonlib));
143: PetscFunctionReturn(PETSC_SUCCESS);
144: }
146: static char PetscPythonExe[PETSC_MAX_PATH_LEN] = {0};
147: static char PetscPythonLib[PETSC_MAX_PATH_LEN] = {0};
148: static PetscBool PetscBeganPython = PETSC_FALSE;
150: /*@
151: PetscPythonFinalize - Finalize PETSc for use with Python.
153: Level: intermediate
155: .seealso: `PetscPythonInitialize()`, `PetscPythonPrintError()`
156: @*/
157: PetscErrorCode PetscPythonFinalize(void)
158: {
159: PetscFunctionBegin;
160: if (PetscBeganPython) {
161: if (Py_IsInitialized()) Py_Finalize();
162: }
163: PetscBeganPython = PETSC_FALSE;
164: PetscFunctionReturn(PETSC_SUCCESS);
165: }
167: /*@
168: PetscPythonInitialize - Initialize Python for use with PETSc and import petsc4py.
170: Input Parameters:
171: + pyexe - path to the Python interpreter executable, or `NULL`.
172: - pylib - full path to the Python dynamic library, or `NULL`.
174: Options Database Key:
175: . -python exe - Initializes Python, and optionally takes a Python executable name
177: Level: intermediate
179: .seealso: `PetscPythonFinalize()`, `PetscPythonPrintError()`
180: @*/
181: PetscErrorCode PetscPythonInitialize(const char pyexe[], const char pylib[])
182: {
183: PyObject *module = NULL;
185: PetscFunctionBegin;
186: if (PetscBeganPython) PetscFunctionReturn(PETSC_SUCCESS);
187: /* Python executable */
188: if (pyexe && pyexe[0] != 0) {
189: PetscCall(PetscStrncpy(PetscPythonExe, pyexe, sizeof(PetscPythonExe)));
190: } else {
191: PetscCall(PetscPythonFindExecutable(PetscPythonExe, sizeof(PetscPythonExe)));
192: }
193: /* Python dynamic library */
194: if (pylib && pylib[0] != 0) {
195: PetscCall(PetscStrncpy(PetscPythonLib, pylib, sizeof(PetscPythonLib)));
196: } else {
197: PetscCall(PetscPythonFindLibrary(PetscPythonExe, PetscPythonLib, sizeof(PetscPythonLib)));
198: }
199: /* dynamically load Python library */
200: PetscCall(PetscPythonLoadLibrary(PetscPythonLib));
201: /* initialize Python */
202: PetscBeganPython = PETSC_FALSE;
203: if (!Py_IsInitialized()) {
204: static PetscBool registered = PETSC_FALSE;
205: const char *py_version;
206: PyObject *sys_path;
207: char path[PETSC_MAX_PATH_LEN] = {0};
209: /* initialize Python. Py_InitializeEx() prints an error and EXITS the program if it is not successful! */
210: PetscCall(PetscInfo(NULL, "Calling Py_InitializeEx(0)\n"));
211: PetscCallExternalVoid("Py_InitializeEx", Py_InitializeEx(0)); /* 0: do not install signal handlers */
212: PetscCall(PetscInfo(NULL, "Py_InitializeEx(0) called successfully\n"));
214: /* build 'sys.argv' list */
215: py_version = Py_GetVersion();
216: if (py_version[0] == '2') {
217: int argc = 0;
218: char *argv[1] = {NULL};
219: PySys_SetArgv(argc, argv);
220: }
221: if (py_version[0] == '3') {
222: int argc = 0;
223: wchar_t *argv[1] = {NULL};
224: PySys_SetArgv(argc, argv);
225: }
226: /* add PETSC_LIB_DIR in front of 'sys.path' */
227: sys_path = PySys_GetObject("path");
228: if (sys_path) {
229: int zero = 0;
230: PetscCall(PetscStrreplace(PETSC_COMM_SELF, "${PETSC_LIB_DIR}", path, sizeof(path)));
231: Py_DecRef(PyObject_CallMethod(sys_path, "insert", "is", zero, (char *)path));
232: #if defined(PETSC_PETSC4PY_INSTALL_PATH)
233: {
234: char *rpath;
235: PetscCall(PetscStrallocpy(PETSC_PETSC4PY_INSTALL_PATH, &rpath));
236: Py_DecRef(PyObject_CallMethod(sys_path, "insert", "is", zero, rpath));
237: PetscCall(PetscFree(rpath));
238: }
239: #endif
240: }
241: /* register finalizer */
242: if (!registered) {
243: PetscCall(PetscRegisterFinalize(PetscPythonFinalize));
244: registered = PETSC_TRUE;
245: }
246: PetscBeganPython = PETSC_TRUE;
247: PetscCall(PetscInfo(NULL, "Python initialize completed\n"));
248: }
249: /* import 'petsc4py.PETSc' module */
250: PetscCall(PetscFPTrapPush(PETSC_FP_TRAP_OFF));
251: PetscCallExternalVoid("PyImport_ImportModule", module = PyImport_ImportModule("petsc4py.PETSc"));
252: PetscCall(PetscFPTrapPop());
253: if (module) {
254: PetscCall(PetscInfo(NULL, "Python: successfully imported module 'petsc4py.PETSc'\n"));
255: Py_DecRef(module);
256: module = NULL;
257: } else {
258: PetscCall(PetscInfo(NULL, "Python: error when importing module 'petsc4py.PETSc'\n"));
259: PetscCall(PetscPythonPrintError());
260: SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Python: could not import module 'petsc4py.PETSc', perhaps your PYTHONPATH does not contain it");
261: }
262: PetscFunctionReturn(PETSC_SUCCESS);
263: }
265: /*@C
266: PetscPythonPrintError - Print any current Python errors.
268: Level: developer
270: .seealso: `PetscPythonInitialize()`, `PetscPythonFinalize()`
271: @*/
272: PetscErrorCode PetscPythonPrintError(void)
273: {
274: PyObject *exc = NULL, *val = NULL, *tb = NULL;
276: PetscFunctionBegin;
277: if (!PetscBeganPython) PetscFunctionReturn(PETSC_SUCCESS);
278: if (!PyErr_Occurred()) PetscFunctionReturn(PETSC_SUCCESS);
279: PyErr_Fetch(&exc, &val, &tb);
280: PyErr_NormalizeException(&exc, &val, &tb);
281: PyErr_Display(exc ? exc : Py_None, val ? val : Py_None, tb ? tb : Py_None);
282: PyErr_Restore(exc, val, tb);
283: PetscFunctionReturn(PETSC_SUCCESS);
284: }
286: PETSC_EXTERN PetscErrorCode (*PetscPythonMonitorSet_C)(PetscObject, const char[]);
287: PetscErrorCode (*PetscPythonMonitorSet_C)(PetscObject, const char[]) = NULL;
289: /*@
290: PetscPythonMonitorSet - Set a Python monitor for a `PetscObject`
292: Level: developer
294: .seealso: `PetscPythonInitialize()`, `PetscPythonFinalize()`, `PetscPythonPrintError()`
295: @*/
296: PetscErrorCode PetscPythonMonitorSet(PetscObject obj, const char url[])
297: {
298: PetscFunctionBegin;
300: PetscAssertPointer(url, 2);
301: if (!PetscPythonMonitorSet_C) {
302: PetscCall(PetscPythonInitialize(NULL, NULL));
303: PetscCheck(PetscPythonMonitorSet_C, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Couldn't initialize Python support for monitors");
304: }
305: PetscCall(PetscPythonMonitorSet_C(obj, url));
306: PetscFunctionReturn(PETSC_SUCCESS);
307: }