xref: /petsc/src/sys/python/pythonsys.c (revision 9566063d113dddea24716c546802770db7481bc0)
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 */
15*9566063dSJacob Faibussowitsch   PetscCall(PetscStrncpy(pythonexe,PETSC_PYTHON_EXE,len));
16*9566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetString(NULL,NULL,"-python",pythonexe,len,&flag));
17c4aff060SBarry Smith   if (!flag || pythonexe[0]==0) {
18*9566063dSJacob 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 */
34*9566063dSJacob Faibussowitsch   PetscCall(PetscStrncpy(command,pythonexe,sizeof(command)));
35*9566063dSJacob Faibussowitsch   PetscCall(PetscStrlcat(command," ",sizeof(command)));
36*9566063dSJacob Faibussowitsch   PetscCall(PetscStrlcat(command,attempt,sizeof(command)));
370dfec4cbSBarry Smith #if defined(PETSC_HAVE_POPEN)
38*9566063dSJacob Faibussowitsch   PetscCall(PetscPOpen(PETSC_COMM_SELF,NULL,command,"r",&fp));
392c71b3e2SJacob Faibussowitsch   PetscCheckFalse(!fgets(pythonlib,pl,fp),PETSC_COMM_SELF,PETSC_ERR_PLIB,"Python: bad output from executable: %s\nRunning: %s",pythonexe,command);
40*9566063dSJacob 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 */
45*9566063dSJacob Faibussowitsch   PetscCall(PetscStrchr(pythonlib,'\n',&eol));
460dfec4cbSBarry Smith   if (eol) eol[0] = 0;
47*9566063dSJacob 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\")))'";
54d118d7f3SJed Brown   const char     cmdline2[] = "-c 'import os, sysconfig; import sys;print(os.path.join(sysconfig.get_config_var(\"LIBDIR\"),\"libpython\"+sys.version[:3]+\".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)
63*9566063dSJacob Faibussowitsch   PetscCall(PetscStrncpy(pythonlib,PETSC_PYTHON_LIB,pl));
64c4aff060SBarry Smith   PetscFunctionReturn(0);
65c4aff060SBarry Smith #endif
66c4aff060SBarry Smith 
67*9566063dSJacob Faibussowitsch   PetscCall(PetscPythonFindLibraryName(pythonexe,cmdline1,pythonlib,pl,&found));
6884b215baSBarry Smith   if (!found) {
69*9566063dSJacob Faibussowitsch     PetscCall(PetscPythonFindLibraryName(pythonexe,cmdline2,pythonlib,pl,&found));
70c4aff060SBarry Smith   }
7149a6f2e5SLisandro Dalcin   if (!found) {
72*9566063dSJacob Faibussowitsch     PetscCall(PetscPythonFindLibraryName(pythonexe,cmdline3,pythonlib,pl,&found));
7349a6f2e5SLisandro Dalcin   }
74cb07359aSSatish Balay   if (!found) {
75*9566063dSJacob Faibussowitsch     PetscCall(PetscPythonFindLibraryName(pythonexe,cmdline4,pythonlib,pl,&found));
76cb07359aSSatish Balay   }
77cb497662SVaclav Hapla   if (!found) {
78*9566063dSJacob Faibussowitsch     PetscCall(PetscPythonFindLibraryName(pythonexe,cmdline5,pythonlib,pl,&found));
79cb497662SVaclav Hapla   }
80*9566063dSJacob 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 */
122*9566063dSJacob Faibussowitsch   PetscCall(PetscDLPyLibOpen(pythonlib));
123*9566063dSJacob Faibussowitsch   PetscCall(PetscInfo(NULL,"Python: loaded dynamic library %s\n", pythonlib));
124c4aff060SBarry Smith   /* look required symbols from the Python C-API */
125*9566063dSJacob Faibussowitsch   PetscCall(PetscDLPyLibSym("_Py_NoneStruct"        , &Py_None));
126*9566063dSJacob Faibussowitsch   PetscCall(PetscDLPyLibSym("Py_GetVersion"         , &Py_GetVersion));
127*9566063dSJacob Faibussowitsch   PetscCall(PetscDLPyLibSym("Py_IsInitialized"      , &Py_IsInitialized));
128*9566063dSJacob Faibussowitsch   PetscCall(PetscDLPyLibSym("Py_InitializeEx"       , &Py_InitializeEx));
129*9566063dSJacob Faibussowitsch   PetscCall(PetscDLPyLibSym("Py_Finalize"           , &Py_Finalize));
130*9566063dSJacob Faibussowitsch   PetscCall(PetscDLPyLibSym("PySys_GetObject"       , &PySys_GetObject));
131*9566063dSJacob Faibussowitsch   PetscCall(PetscDLPyLibSym("PySys_SetArgv"         , &PySys_SetArgv));
132*9566063dSJacob Faibussowitsch   PetscCall(PetscDLPyLibSym("PyObject_CallMethod"   , &PyObject_CallMethod));
133*9566063dSJacob Faibussowitsch   PetscCall(PetscDLPyLibSym("PyImport_ImportModule" , &PyImport_ImportModule));
134*9566063dSJacob Faibussowitsch   PetscCall(PetscDLPyLibSym("Py_IncRef"             , &Py_IncRef));
135*9566063dSJacob Faibussowitsch   PetscCall(PetscDLPyLibSym("Py_DecRef"             , &Py_DecRef));
136*9566063dSJacob Faibussowitsch   PetscCall(PetscDLPyLibSym("PyErr_Clear"           , &PyErr_Clear));
137*9566063dSJacob Faibussowitsch   PetscCall(PetscDLPyLibSym("PyErr_Occurred"        , &PyErr_Occurred));
138*9566063dSJacob Faibussowitsch   PetscCall(PetscDLPyLibSym("PyErr_Fetch"             , &PyErr_Fetch));
139*9566063dSJacob Faibussowitsch   PetscCall(PetscDLPyLibSym("PyErr_NormalizeException", &PyErr_NormalizeException));
140*9566063dSJacob Faibussowitsch   PetscCall(PetscDLPyLibSym("PyErr_Display",            &PyErr_Display));
141*9566063dSJacob 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);
148*9566063dSJacob 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 
175c4aff060SBarry Smith    Input Parameter:
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) {
190*9566063dSJacob Faibussowitsch     PetscCall(PetscStrncpy(PetscPythonExe,pyexe,sizeof(PetscPythonExe)));
191c4aff060SBarry Smith   } else {
192*9566063dSJacob Faibussowitsch     PetscCall(PetscPythonFindExecutable(PetscPythonExe,sizeof(PetscPythonExe)));
193c4aff060SBarry Smith   }
194c4aff060SBarry Smith   /* Python dynamic library */
195c4aff060SBarry Smith   if (pylib && pylib[0] != 0) {
196*9566063dSJacob Faibussowitsch     PetscCall(PetscStrncpy(PetscPythonLib,pylib,sizeof(PetscPythonLib)));
197c4aff060SBarry Smith   } else {
198*9566063dSJacob Faibussowitsch     PetscCall(PetscPythonFindLibrary(PetscPythonExe,PetscPythonLib,sizeof(PetscPythonLib)));
199c4aff060SBarry Smith   }
200c4aff060SBarry Smith   /* dynamically load Python library */
201*9566063dSJacob 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 
2102f2e82b0SLisandro Dalcin     /* initialize Python */
2112f2e82b0SLisandro Dalcin     Py_InitializeEx(0); /* 0: do not install signal handlers */
2122f2e82b0SLisandro Dalcin     /*  build 'sys.argv' list */
2132f2e82b0SLisandro Dalcin     py_version = Py_GetVersion();
2149ac80d5eSLisandro Dalcin     if (py_version[0] == '2') {
21549a6f2e5SLisandro Dalcin       int argc = 0; char *argv[1] = {NULL};
2162f2e82b0SLisandro Dalcin       PySys_SetArgv(argc,argv);
2172f2e82b0SLisandro Dalcin     }
2182f2e82b0SLisandro Dalcin     if (py_version[0] == '3') {
21949a6f2e5SLisandro Dalcin       int argc = 0; wchar_t *argv[1] = {NULL};
22049a6f2e5SLisandro Dalcin       PySys_SetArgv(argc,argv);
2212f2e82b0SLisandro Dalcin     }
2222f2e82b0SLisandro Dalcin     /* add PETSC_LIB_DIR in front of 'sys.path' */
2232f2e82b0SLisandro Dalcin     sys_path = PySys_GetObject("path");
2242f2e82b0SLisandro Dalcin     if (sys_path) {
225*9566063dSJacob Faibussowitsch       PetscCall(PetscStrreplace(PETSC_COMM_SELF,"${PETSC_LIB_DIR}",path,sizeof(path)));
2262f2e82b0SLisandro Dalcin       Py_DecRef(PyObject_CallMethod(sys_path,"insert","is",(int)0,(char*)path));
22758c0e507SSatish Balay #if defined(PETSC_PETSC4PY_INSTALL_PATH)
22858c0e507SSatish Balay       {
22958c0e507SSatish Balay         char *rpath;
230*9566063dSJacob Faibussowitsch         PetscCall(PetscStrallocpy(PETSC_PETSC4PY_INSTALL_PATH,&rpath));
23158c0e507SSatish Balay         Py_DecRef(PyObject_CallMethod(sys_path,"insert","is",(int)0,rpath));
232*9566063dSJacob Faibussowitsch         PetscCall(PetscFree(rpath));
23358c0e507SSatish Balay       }
23458c0e507SSatish Balay #endif
2359ac80d5eSLisandro Dalcin     }
236c4aff060SBarry Smith     /* register finalizer */
237c4aff060SBarry Smith     if (!registered) {
238*9566063dSJacob Faibussowitsch       PetscCall(PetscRegisterFinalize(PetscPythonFinalize));
239c4aff060SBarry Smith       registered = PETSC_TRUE;
240c4aff060SBarry Smith     }
241c4aff060SBarry Smith     PetscBeganPython = PETSC_TRUE;
242c4aff060SBarry Smith   }
243c4aff060SBarry Smith   /* import 'petsc4py.PETSc' module */
244c4aff060SBarry Smith   module = PyImport_ImportModule("petsc4py.PETSc");
245c4aff060SBarry Smith   if (module) {
246*9566063dSJacob Faibussowitsch     PetscCall(PetscInfo(NULL,"Python: successfully imported  module 'petsc4py.PETSc'\n"));
247a297a907SKarl Rupp 
24802c9f0b5SLisandro Dalcin     Py_DecRef(module); module = NULL;
249c4aff060SBarry Smith   } else {
250e0ab9aedSLisandro Dalcin     PetscPythonPrintError();
251546078acSJacob Faibussowitsch     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Python: could not import module 'petsc4py.PETSc', perhaps your PYTHONPATH does not contain it");
252c4aff060SBarry Smith   }
253c4aff060SBarry Smith   PetscFunctionReturn(0);
254c4aff060SBarry Smith }
255c4aff060SBarry Smith 
256e0ab9aedSLisandro Dalcin /*@C
257e0ab9aedSLisandro Dalcin   PetscPythonPrintError - Print Python errors.
258e0ab9aedSLisandro Dalcin 
259e0ab9aedSLisandro Dalcin   Level: developer
260e0ab9aedSLisandro Dalcin 
261e0ab9aedSLisandro Dalcin @*/
2627087cfbeSBarry Smith PetscErrorCode  PetscPythonPrintError(void)
263e0ab9aedSLisandro Dalcin {
26402c9f0b5SLisandro Dalcin   PyObject *exc=NULL, *val=NULL, *tb=NULL;
2656e111a19SKarl Rupp 
266e0ab9aedSLisandro Dalcin   PetscFunctionBegin;
267e0ab9aedSLisandro Dalcin   if (!PetscBeganPython) PetscFunctionReturn(0);
268e0ab9aedSLisandro Dalcin   if (!PyErr_Occurred()) PetscFunctionReturn(0);
269e0ab9aedSLisandro Dalcin   PyErr_Fetch(&exc,&val,&tb);
270e0ab9aedSLisandro Dalcin   PyErr_NormalizeException(&exc,&val,&tb);
271589a23caSBarry Smith   PyErr_Display(exc ? exc : Py_None, val ? val : Py_None, tb  ? tb  : Py_None);
272e0ab9aedSLisandro Dalcin   PyErr_Restore(exc,val,tb);
273e0ab9aedSLisandro Dalcin   PetscFunctionReturn(0);
274e0ab9aedSLisandro Dalcin }
275e0ab9aedSLisandro Dalcin 
276c4aff060SBarry Smith /* ---------------------------------------------------------------- */
2775180491cSLisandro Dalcin 
2788cc058d9SJed Brown PETSC_EXTERN PetscErrorCode (*PetscPythonMonitorSet_C)(PetscObject,const char[]);
2790298fd71SBarry Smith PetscErrorCode (*PetscPythonMonitorSet_C)(PetscObject,const char[]) = NULL;
2805180491cSLisandro Dalcin 
2815180491cSLisandro Dalcin /*@C
2825180491cSLisandro Dalcin   PetscPythonMonitorSet - Set Python monitor
2835180491cSLisandro Dalcin 
2845180491cSLisandro Dalcin   Level: developer
2855180491cSLisandro Dalcin 
2865180491cSLisandro Dalcin @*/
2875180491cSLisandro Dalcin PetscErrorCode PetscPythonMonitorSet(PetscObject obj, const char url[])
2885180491cSLisandro Dalcin {
2895180491cSLisandro Dalcin   PetscFunctionBegin;
2905180491cSLisandro Dalcin   PetscValidHeader(obj,1);
2915180491cSLisandro Dalcin   PetscValidCharPointer(url,2);
2926c4ed002SBarry Smith   if (!PetscPythonMonitorSet_C) {
293*9566063dSJacob Faibussowitsch     PetscCall(PetscPythonInitialize(NULL,NULL));
29428b400f6SJacob Faibussowitsch     PetscCheck(PetscPythonMonitorSet_C,PETSC_COMM_SELF,PETSC_ERR_PLIB,"Couldn't initialize Python support for monitors");
2955180491cSLisandro Dalcin   }
296*9566063dSJacob Faibussowitsch   PetscCall(PetscPythonMonitorSet_C(obj,url));
2975180491cSLisandro Dalcin   PetscFunctionReturn(0);
2985180491cSLisandro Dalcin }
2995180491cSLisandro Dalcin 
3005180491cSLisandro Dalcin /* ---------------------------------------------------------------- */
301