xref: /petsc/src/sys/python/pythonsys.c (revision 095e973b2c58763689a1329707b168c72e080354)
1af0996ceSBarry Smith #include <petsc/private/petscimpl.h>       /*I "petscsys.h" I*/
2c4aff060SBarry Smith 
3c4aff060SBarry Smith /* ---------------------------------------------------------------- */
4c4aff060SBarry Smith 
5c4aff060SBarry Smith #if !defined(PETSC_PYTHON_EXE)
6c4aff060SBarry Smith #define PETSC_PYTHON_EXE "python"
7c4aff060SBarry Smith #endif
8c4aff060SBarry Smith 
9589a23caSBarry Smith static PetscErrorCode PetscPythonFindExecutable(char pythonexe[],size_t len)
10c4aff060SBarry Smith {
11ace3abfcSBarry Smith   PetscBool      flag;
126e111a19SKarl Rupp 
13c4aff060SBarry Smith   PetscFunctionBegin;
14c4aff060SBarry Smith   /* get the path for the Python interpreter executable */
159566063dSJacob Faibussowitsch   PetscCall(PetscStrncpy(pythonexe,PETSC_PYTHON_EXE,len));
169566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetString(NULL,NULL,"-python",pythonexe,len,&flag));
17c4aff060SBarry Smith   if (!flag || pythonexe[0]==0) {
189566063dSJacob Faibussowitsch     PetscCall(PetscStrncpy(pythonexe,PETSC_PYTHON_EXE,len));
19c4aff060SBarry Smith   }
20c4aff060SBarry Smith   PetscFunctionReturn(0);
21c4aff060SBarry Smith }
22c4aff060SBarry Smith 
230dfec4cbSBarry Smith /*
240dfec4cbSBarry Smith     Python does not appear to have a universal way to indicate the location of Python dynamic library so try several possibilities
250dfec4cbSBarry Smith */
26589a23caSBarry Smith static PetscErrorCode PetscPythonFindLibraryName(const char pythonexe[],const char attempt[],char pythonlib[],size_t pl,PetscBool *found)
27c4aff060SBarry Smith {
280dfec4cbSBarry Smith   char           command[2*PETSC_MAX_PATH_LEN];
29c4aff060SBarry Smith   FILE           *fp = NULL;
300dfec4cbSBarry Smith   char           *eol;
310dfec4cbSBarry Smith 
320dfec4cbSBarry Smith   PetscFunctionBegin;
330dfec4cbSBarry Smith   /* call Python to find out the name of the Python dynamic library */
349566063dSJacob Faibussowitsch   PetscCall(PetscStrncpy(command,pythonexe,sizeof(command)));
359566063dSJacob Faibussowitsch   PetscCall(PetscStrlcat(command," ",sizeof(command)));
369566063dSJacob Faibussowitsch   PetscCall(PetscStrlcat(command,attempt,sizeof(command)));
370dfec4cbSBarry Smith #if defined(PETSC_HAVE_POPEN)
389566063dSJacob Faibussowitsch   PetscCall(PetscPOpen(PETSC_COMM_SELF,NULL,command,"r",&fp));
3908401ef6SPierre Jolivet   PetscCheck(fgets(pythonlib,pl,fp),PETSC_COMM_SELF,PETSC_ERR_PLIB,"Python: bad output from executable: %s\nRunning: %s",pythonexe,command);
409566063dSJacob Faibussowitsch   PetscCall(PetscPClose(PETSC_COMM_SELF,fp));
410dfec4cbSBarry Smith #else
42691b26d3SBarry Smith   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Python: Aborted due to missing popen()");
430dfec4cbSBarry Smith #endif
440dfec4cbSBarry Smith   /* remove newlines */
459566063dSJacob Faibussowitsch   PetscCall(PetscStrchr(pythonlib,'\n',&eol));
460dfec4cbSBarry Smith   if (eol) eol[0] = 0;
479566063dSJacob Faibussowitsch   PetscCall(PetscTestFile(pythonlib,'r',found));
480dfec4cbSBarry Smith   PetscFunctionReturn(0);
490dfec4cbSBarry Smith }
500dfec4cbSBarry Smith 
51589a23caSBarry Smith static PetscErrorCode PetscPythonFindLibrary(const char pythonexe[],char pythonlib[],size_t pl)
52c4aff060SBarry Smith {
53d118d7f3SJed Brown   const char     cmdline1[] = "-c 'import os, sysconfig; print(os.path.join(sysconfig.get_config_var(\"LIBDIR\"),sysconfig.get_config_var(\"LDLIBRARY\")))'";
5482bdbe8dSSatish Balay   const char     cmdline2[] = "-c 'import os, sysconfig; print(os.path.join(sysconfig.get_path(\"stdlib\"),os.path.pardir,\"libpython\"+sysconfig.get_python_version()+\".dylib\"))'";
55d118d7f3SJed Brown   const char     cmdline3[] = "-c 'import os, sysconfig; print(os.path.join(sysconfig.get_config_var(\"LIBPL\"),sysconfig.get_config_var(\"LDLIBRARY\")))'";
56d118d7f3SJed Brown   const char     cmdline4[] = "-c 'import sysconfig; print(sysconfig.get_config_var(\"LIBPYTHON\"))'";
57d118d7f3SJed Brown   const char     cmdline5[] = "-c 'import os, sysconfig; import sys;print(os.path.join(sysconfig.get_config_var(\"LIBDIR\"),\"libpython\"+sys.version[:3]+\".so\"))'";
58702f9f58SSatish Balay 
59ace3abfcSBarry Smith   PetscBool      found = PETSC_FALSE;
60c4aff060SBarry Smith 
616e111a19SKarl Rupp   PetscFunctionBegin;
62c4aff060SBarry Smith #if defined(PETSC_PYTHON_LIB)
639566063dSJacob Faibussowitsch   PetscCall(PetscStrncpy(pythonlib,PETSC_PYTHON_LIB,pl));
64c4aff060SBarry Smith   PetscFunctionReturn(0);
65c4aff060SBarry Smith #endif
66c4aff060SBarry Smith 
679566063dSJacob Faibussowitsch   PetscCall(PetscPythonFindLibraryName(pythonexe,cmdline1,pythonlib,pl,&found));
6884b215baSBarry Smith   if (!found) {
699566063dSJacob Faibussowitsch     PetscCall(PetscPythonFindLibraryName(pythonexe,cmdline2,pythonlib,pl,&found));
70c4aff060SBarry Smith   }
7149a6f2e5SLisandro Dalcin   if (!found) {
729566063dSJacob Faibussowitsch     PetscCall(PetscPythonFindLibraryName(pythonexe,cmdline3,pythonlib,pl,&found));
7349a6f2e5SLisandro Dalcin   }
74cb07359aSSatish Balay   if (!found) {
759566063dSJacob Faibussowitsch     PetscCall(PetscPythonFindLibraryName(pythonexe,cmdline4,pythonlib,pl,&found));
76cb07359aSSatish Balay   }
77cb497662SVaclav Hapla   if (!found) {
789566063dSJacob Faibussowitsch     PetscCall(PetscPythonFindLibraryName(pythonexe,cmdline5,pythonlib,pl,&found));
79cb497662SVaclav Hapla   }
809566063dSJacob Faibussowitsch   PetscCall(PetscInfo(NULL,"Python library  %s found %d\n",pythonlib,found));
81c4aff060SBarry Smith   PetscFunctionReturn(0);
82c4aff060SBarry Smith }
83c4aff060SBarry Smith 
84c4aff060SBarry Smith /* ---------------------------------------------------------------- */
85c4aff060SBarry Smith 
86c4aff060SBarry Smith typedef struct _Py_object_t PyObject; /* fake definition */
87c4aff060SBarry Smith 
8802c9f0b5SLisandro Dalcin static PyObject* Py_None = NULL;
89e0ab9aedSLisandro Dalcin 
909ac80d5eSLisandro Dalcin static const char* (*Py_GetVersion)(void);
919ac80d5eSLisandro Dalcin 
92c4aff060SBarry Smith static int       (*Py_IsInitialized)(void);
93c4aff060SBarry Smith static void      (*Py_InitializeEx)(int);
94c4aff060SBarry Smith static void      (*Py_Finalize)(void);
95c4aff060SBarry Smith 
9649a6f2e5SLisandro Dalcin static void      (*PySys_SetArgv)(int,void*);
972f2e82b0SLisandro Dalcin static PyObject* (*PySys_GetObject)(const char*);
982f2e82b0SLisandro Dalcin static PyObject* (*PyObject_CallMethod)(PyObject*,const char*, const char*, ...);
99c4aff060SBarry Smith static PyObject* (*PyImport_ImportModule)(const char*);
100c4aff060SBarry Smith 
101c4aff060SBarry Smith static void      (*Py_IncRef)(PyObject*);
102c4aff060SBarry Smith static void      (*Py_DecRef)(PyObject*);
103c4aff060SBarry Smith 
104c4aff060SBarry Smith static void      (*PyErr_Clear)(void);
105c4aff060SBarry Smith static PyObject* (*PyErr_Occurred)(void);
106e0ab9aedSLisandro Dalcin static void      (*PyErr_Fetch)(PyObject**,PyObject**,PyObject**);
107e0ab9aedSLisandro Dalcin static void      (*PyErr_NormalizeException)(PyObject**,PyObject**, PyObject**);
108e0ab9aedSLisandro Dalcin static void      (*PyErr_Display)(PyObject*,PyObject*,PyObject*);
109e0ab9aedSLisandro Dalcin static void      (*PyErr_Restore)(PyObject*,PyObject*,PyObject*);
110c4aff060SBarry Smith 
111c4aff060SBarry Smith #define PetscDLPyLibOpen(libname) \
112d44a1e48SBarry Smith   PetscDLLibraryAppend(PETSC_COMM_SELF,&PetscDLLibrariesLoaded,libname)
113c4aff060SBarry Smith #define PetscDLPyLibSym(symbol, value) \
1140298fd71SBarry Smith   PetscDLLibrarySym(PETSC_COMM_SELF,&PetscDLLibrariesLoaded,NULL,symbol,(void**)value)
115c4aff060SBarry Smith #define PetscDLPyLibClose(comm) \
116c4aff060SBarry Smith   do { } while (0)
117c4aff060SBarry Smith 
118c4aff060SBarry Smith static PetscErrorCode PetscPythonLoadLibrary(const char pythonlib[])
119c4aff060SBarry Smith {
1206e111a19SKarl Rupp   PetscFunctionBegin;
121c4aff060SBarry Smith   /* open the Python dynamic library */
1229566063dSJacob Faibussowitsch   PetscCall(PetscDLPyLibOpen(pythonlib));
1239566063dSJacob Faibussowitsch   PetscCall(PetscInfo(NULL,"Python: loaded dynamic library %s\n", pythonlib));
124c4aff060SBarry Smith   /* look required symbols from the Python C-API */
1259566063dSJacob Faibussowitsch   PetscCall(PetscDLPyLibSym("_Py_NoneStruct"        , &Py_None));
1269566063dSJacob Faibussowitsch   PetscCall(PetscDLPyLibSym("Py_GetVersion"         , &Py_GetVersion));
1279566063dSJacob Faibussowitsch   PetscCall(PetscDLPyLibSym("Py_IsInitialized"      , &Py_IsInitialized));
1289566063dSJacob Faibussowitsch   PetscCall(PetscDLPyLibSym("Py_InitializeEx"       , &Py_InitializeEx));
1299566063dSJacob Faibussowitsch   PetscCall(PetscDLPyLibSym("Py_Finalize"           , &Py_Finalize));
1309566063dSJacob Faibussowitsch   PetscCall(PetscDLPyLibSym("PySys_GetObject"       , &PySys_GetObject));
1319566063dSJacob Faibussowitsch   PetscCall(PetscDLPyLibSym("PySys_SetArgv"         , &PySys_SetArgv));
1329566063dSJacob Faibussowitsch   PetscCall(PetscDLPyLibSym("PyObject_CallMethod"   , &PyObject_CallMethod));
1339566063dSJacob Faibussowitsch   PetscCall(PetscDLPyLibSym("PyImport_ImportModule" , &PyImport_ImportModule));
1349566063dSJacob Faibussowitsch   PetscCall(PetscDLPyLibSym("Py_IncRef"             , &Py_IncRef));
1359566063dSJacob Faibussowitsch   PetscCall(PetscDLPyLibSym("Py_DecRef"             , &Py_DecRef));
1369566063dSJacob Faibussowitsch   PetscCall(PetscDLPyLibSym("PyErr_Clear"           , &PyErr_Clear));
1379566063dSJacob Faibussowitsch   PetscCall(PetscDLPyLibSym("PyErr_Occurred"        , &PyErr_Occurred));
1389566063dSJacob Faibussowitsch   PetscCall(PetscDLPyLibSym("PyErr_Fetch"             , &PyErr_Fetch));
1399566063dSJacob Faibussowitsch   PetscCall(PetscDLPyLibSym("PyErr_NormalizeException", &PyErr_NormalizeException));
1409566063dSJacob Faibussowitsch   PetscCall(PetscDLPyLibSym("PyErr_Display",            &PyErr_Display));
1419566063dSJacob Faibussowitsch   PetscCall(PetscDLPyLibSym("PyErr_Restore",            &PyErr_Restore));
142c4aff060SBarry Smith   /* XXX TODO: check that ALL symbols were there !!! */
14328b400f6SJacob Faibussowitsch   PetscCheck(Py_None,PETSC_COMM_SELF,PETSC_ERR_LIB,"Python: failed to load symbols from Python dynamic library %s",pythonlib);
14428b400f6SJacob Faibussowitsch   PetscCheck(Py_GetVersion,PETSC_COMM_SELF,PETSC_ERR_LIB,"Python: failed to load symbols from Python dynamic library %s",pythonlib);
14528b400f6SJacob Faibussowitsch   PetscCheck(Py_IsInitialized,PETSC_COMM_SELF,PETSC_ERR_LIB,"Python: failed to load symbols from Python dynamic library %s",pythonlib);
14628b400f6SJacob Faibussowitsch   PetscCheck(Py_InitializeEx,PETSC_COMM_SELF,PETSC_ERR_LIB,"Python: failed to load symbols from Python dynamic library %s",pythonlib);
14728b400f6SJacob Faibussowitsch   PetscCheck(Py_Finalize,PETSC_COMM_SELF,PETSC_ERR_LIB,"Python: failed to load symbols from Python dynamic library %s",pythonlib);
1489566063dSJacob Faibussowitsch   PetscCall(PetscInfo(NULL,"Python: all required symbols loaded from Python dynamic library %s\n",pythonlib));
149c4aff060SBarry Smith   PetscFunctionReturn(0);
150c4aff060SBarry Smith }
151c4aff060SBarry Smith 
152c4aff060SBarry Smith /* ---------------------------------------------------------------- */
153c4aff060SBarry Smith 
154c4aff060SBarry Smith static char      PetscPythonExe[PETSC_MAX_PATH_LEN] = { 0 };
155c4aff060SBarry Smith static char      PetscPythonLib[PETSC_MAX_PATH_LEN] = { 0 };
156ace3abfcSBarry Smith static PetscBool PetscBeganPython = PETSC_FALSE;
157c4aff060SBarry Smith 
158c4aff060SBarry Smith /*@C
159c4aff060SBarry Smith   PetscPythonFinalize - Finalize Python.
160c4aff060SBarry Smith 
161c4aff060SBarry Smith   Level: intermediate
162c4aff060SBarry Smith 
163c4aff060SBarry Smith @*/
1647087cfbeSBarry Smith PetscErrorCode  PetscPythonFinalize(void)
165c4aff060SBarry Smith {
166c4aff060SBarry Smith   PetscFunctionBegin;
167c4aff060SBarry Smith   if (PetscBeganPython) { if (Py_IsInitialized()) Py_Finalize(); }
168c4aff060SBarry Smith   PetscBeganPython = PETSC_FALSE;
169c4aff060SBarry Smith   PetscFunctionReturn(0);
170c4aff060SBarry Smith }
171c4aff060SBarry Smith 
172c4aff060SBarry Smith /*@C
173c4aff060SBarry Smith   PetscPythonInitialize - Initialize Python and import petsc4py.
174c4aff060SBarry Smith 
1751179163eSBarry Smith    Input Parameters:
1760298fd71SBarry Smith +  pyexe - path to the Python interpreter executable, or NULL.
1770298fd71SBarry Smith -  pylib - full path to the Python dynamic library, or NULL.
178c4aff060SBarry Smith 
179c4aff060SBarry Smith   Level: intermediate
180c4aff060SBarry Smith 
181c4aff060SBarry Smith @*/
1827087cfbeSBarry Smith PetscErrorCode  PetscPythonInitialize(const char pyexe[],const char pylib[])
183c4aff060SBarry Smith {
18402c9f0b5SLisandro Dalcin   PyObject       *module = NULL;
1856e111a19SKarl Rupp 
186c4aff060SBarry Smith   PetscFunctionBegin;
187c4aff060SBarry Smith   if (PetscBeganPython) PetscFunctionReturn(0);
188c4aff060SBarry Smith   /* Python executable */
189c4aff060SBarry Smith   if (pyexe && pyexe[0] != 0) {
1909566063dSJacob Faibussowitsch     PetscCall(PetscStrncpy(PetscPythonExe,pyexe,sizeof(PetscPythonExe)));
191c4aff060SBarry Smith   } else {
1929566063dSJacob Faibussowitsch     PetscCall(PetscPythonFindExecutable(PetscPythonExe,sizeof(PetscPythonExe)));
193c4aff060SBarry Smith   }
194c4aff060SBarry Smith   /* Python dynamic library */
195c4aff060SBarry Smith   if (pylib && pylib[0] != 0) {
1969566063dSJacob Faibussowitsch     PetscCall(PetscStrncpy(PetscPythonLib,pylib,sizeof(PetscPythonLib)));
197c4aff060SBarry Smith   } else {
1989566063dSJacob Faibussowitsch     PetscCall(PetscPythonFindLibrary(PetscPythonExe,PetscPythonLib,sizeof(PetscPythonLib)));
199c4aff060SBarry Smith   }
200c4aff060SBarry Smith   /* dynamically load Python library */
2019566063dSJacob Faibussowitsch   PetscCall(PetscPythonLoadLibrary(PetscPythonLib));
202c4aff060SBarry Smith   /* initialize Python */
203c4aff060SBarry Smith   PetscBeganPython = PETSC_FALSE;
204c4aff060SBarry Smith   if (!Py_IsInitialized()) {
2052f2e82b0SLisandro Dalcin     static PetscBool registered = PETSC_FALSE;
2062f2e82b0SLisandro Dalcin     const char       *py_version;
2072f2e82b0SLisandro Dalcin     PyObject         *sys_path;
2082f2e82b0SLisandro Dalcin     char             path[PETSC_MAX_PATH_LEN] = { 0 };
209a297a907SKarl Rupp 
210*095e973bSBarry Smith     /* initialize Python. Py_InitializeEx() prints an error and EXITS the program if it is not successful! */
211*095e973bSBarry Smith     PetscCall(PetscInfo(NULL,"Calling Py_InitializeEx(0);\n"));
2122f2e82b0SLisandro Dalcin     Py_InitializeEx(0); /* 0: do not install signal handlers */
213*095e973bSBarry Smith     PetscCall(PetscInfo(NULL,"Py_InitializeEx(0) called successfully;\n"));
214*095e973bSBarry Smith 
2152f2e82b0SLisandro Dalcin     /*  build 'sys.argv' list */
2162f2e82b0SLisandro Dalcin     py_version = Py_GetVersion();
2179ac80d5eSLisandro Dalcin     if (py_version[0] == '2') {
21849a6f2e5SLisandro Dalcin       int argc = 0; char *argv[1] = {NULL};
2192f2e82b0SLisandro Dalcin       PySys_SetArgv(argc,argv);
2202f2e82b0SLisandro Dalcin     }
2212f2e82b0SLisandro Dalcin     if (py_version[0] == '3') {
22249a6f2e5SLisandro Dalcin       int argc = 0; wchar_t *argv[1] = {NULL};
22349a6f2e5SLisandro Dalcin       PySys_SetArgv(argc,argv);
2242f2e82b0SLisandro Dalcin     }
2252f2e82b0SLisandro Dalcin     /* add PETSC_LIB_DIR in front of 'sys.path' */
2262f2e82b0SLisandro Dalcin     sys_path = PySys_GetObject("path");
2272f2e82b0SLisandro Dalcin     if (sys_path) {
2289566063dSJacob Faibussowitsch       PetscCall(PetscStrreplace(PETSC_COMM_SELF,"${PETSC_LIB_DIR}",path,sizeof(path)));
2292f2e82b0SLisandro Dalcin       Py_DecRef(PyObject_CallMethod(sys_path,"insert","is",(int)0,(char*)path));
23058c0e507SSatish Balay #if defined(PETSC_PETSC4PY_INSTALL_PATH)
23158c0e507SSatish Balay       {
23258c0e507SSatish Balay         char *rpath;
2339566063dSJacob Faibussowitsch         PetscCall(PetscStrallocpy(PETSC_PETSC4PY_INSTALL_PATH,&rpath));
23458c0e507SSatish Balay         Py_DecRef(PyObject_CallMethod(sys_path,"insert","is",(int)0,rpath));
2359566063dSJacob Faibussowitsch         PetscCall(PetscFree(rpath));
23658c0e507SSatish Balay       }
23758c0e507SSatish Balay #endif
2389ac80d5eSLisandro Dalcin     }
239c4aff060SBarry Smith     /* register finalizer */
240c4aff060SBarry Smith     if (!registered) {
2419566063dSJacob Faibussowitsch       PetscCall(PetscRegisterFinalize(PetscPythonFinalize));
242c4aff060SBarry Smith       registered = PETSC_TRUE;
243c4aff060SBarry Smith     }
244c4aff060SBarry Smith     PetscBeganPython = PETSC_TRUE;
245*095e973bSBarry Smith     PetscCall(PetscInfo(NULL,"Python initialize completed.\n"));
246c4aff060SBarry Smith   }
247c4aff060SBarry Smith   /* import 'petsc4py.PETSc' module */
248c4aff060SBarry Smith   module = PyImport_ImportModule("petsc4py.PETSc");
249c4aff060SBarry Smith   if (module) {
2509566063dSJacob Faibussowitsch     PetscCall(PetscInfo(NULL,"Python: successfully imported  module 'petsc4py.PETSc'\n"));
25102c9f0b5SLisandro Dalcin     Py_DecRef(module); module = NULL;
252c4aff060SBarry Smith   } else {
253e0ab9aedSLisandro Dalcin     PetscPythonPrintError();
254546078acSJacob Faibussowitsch     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Python: could not import module 'petsc4py.PETSc', perhaps your PYTHONPATH does not contain it");
255c4aff060SBarry Smith   }
256c4aff060SBarry Smith   PetscFunctionReturn(0);
257c4aff060SBarry Smith }
258c4aff060SBarry Smith 
259e0ab9aedSLisandro Dalcin /*@C
260e0ab9aedSLisandro Dalcin   PetscPythonPrintError - Print Python errors.
261e0ab9aedSLisandro Dalcin 
262e0ab9aedSLisandro Dalcin   Level: developer
263e0ab9aedSLisandro Dalcin 
264e0ab9aedSLisandro Dalcin @*/
2657087cfbeSBarry Smith PetscErrorCode  PetscPythonPrintError(void)
266e0ab9aedSLisandro Dalcin {
26702c9f0b5SLisandro Dalcin   PyObject *exc=NULL, *val=NULL, *tb=NULL;
2686e111a19SKarl Rupp 
269e0ab9aedSLisandro Dalcin   PetscFunctionBegin;
270e0ab9aedSLisandro Dalcin   if (!PetscBeganPython) PetscFunctionReturn(0);
271e0ab9aedSLisandro Dalcin   if (!PyErr_Occurred()) PetscFunctionReturn(0);
272e0ab9aedSLisandro Dalcin   PyErr_Fetch(&exc,&val,&tb);
273e0ab9aedSLisandro Dalcin   PyErr_NormalizeException(&exc,&val,&tb);
274589a23caSBarry Smith   PyErr_Display(exc ? exc : Py_None, val ? val : Py_None, tb  ? tb  : Py_None);
275e0ab9aedSLisandro Dalcin   PyErr_Restore(exc,val,tb);
276e0ab9aedSLisandro Dalcin   PetscFunctionReturn(0);
277e0ab9aedSLisandro Dalcin }
278e0ab9aedSLisandro Dalcin 
279c4aff060SBarry Smith /* ---------------------------------------------------------------- */
2805180491cSLisandro Dalcin 
2818cc058d9SJed Brown PETSC_EXTERN PetscErrorCode (*PetscPythonMonitorSet_C)(PetscObject,const char[]);
2820298fd71SBarry Smith PetscErrorCode (*PetscPythonMonitorSet_C)(PetscObject,const char[]) = NULL;
2835180491cSLisandro Dalcin 
2845180491cSLisandro Dalcin /*@C
2855180491cSLisandro Dalcin   PetscPythonMonitorSet - Set Python monitor
2865180491cSLisandro Dalcin 
2875180491cSLisandro Dalcin   Level: developer
2885180491cSLisandro Dalcin 
2895180491cSLisandro Dalcin @*/
2905180491cSLisandro Dalcin PetscErrorCode PetscPythonMonitorSet(PetscObject obj, const char url[])
2915180491cSLisandro Dalcin {
2925180491cSLisandro Dalcin   PetscFunctionBegin;
2935180491cSLisandro Dalcin   PetscValidHeader(obj,1);
2945180491cSLisandro Dalcin   PetscValidCharPointer(url,2);
2956c4ed002SBarry Smith   if (!PetscPythonMonitorSet_C) {
2969566063dSJacob Faibussowitsch     PetscCall(PetscPythonInitialize(NULL,NULL));
29728b400f6SJacob Faibussowitsch     PetscCheck(PetscPythonMonitorSet_C,PETSC_COMM_SELF,PETSC_ERR_PLIB,"Couldn't initialize Python support for monitors");
2985180491cSLisandro Dalcin   }
2999566063dSJacob Faibussowitsch   PetscCall(PetscPythonMonitorSet_C(obj,url));
3005180491cSLisandro Dalcin   PetscFunctionReturn(0);
3015180491cSLisandro Dalcin }
3025180491cSLisandro Dalcin 
3035180491cSLisandro Dalcin /* ---------------------------------------------------------------- */
304