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