xref: /petsc/src/sys/python/pythonsys.c (revision d81858271d16ebdb66c21b8d5f4d041c9c887268)
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;
12c4aff060SBarry Smith   PetscErrorCode ierr;
136e111a19SKarl Rupp 
14c4aff060SBarry Smith   PetscFunctionBegin;
15c4aff060SBarry Smith   /* get the path for the Python interpreter executable */
16589a23caSBarry Smith   ierr = PetscStrncpy(pythonexe,PETSC_PYTHON_EXE,len);CHKERRQ(ierr);
17589a23caSBarry Smith   ierr = PetscOptionsGetString(NULL,NULL,"-python",pythonexe,len,&flag);CHKERRQ(ierr);
18c4aff060SBarry Smith   if (!flag || pythonexe[0]==0) {
19589a23caSBarry Smith     ierr = PetscStrncpy(pythonexe,PETSC_PYTHON_EXE,len);CHKERRQ(ierr);
20c4aff060SBarry Smith   }
21c4aff060SBarry Smith   PetscFunctionReturn(0);
22c4aff060SBarry Smith }
23c4aff060SBarry Smith 
240dfec4cbSBarry Smith /*
250dfec4cbSBarry Smith     Python does not appear to have a universal way to indicate the location of Python dynamic library so try several possibilities
260dfec4cbSBarry Smith */
27589a23caSBarry Smith static PetscErrorCode PetscPythonFindLibraryName(const char pythonexe[],const char attempt[],char pythonlib[],size_t pl,PetscBool *found)
28c4aff060SBarry Smith {
290dfec4cbSBarry Smith   char           command[2*PETSC_MAX_PATH_LEN];
30c4aff060SBarry Smith   FILE           *fp = NULL;
310dfec4cbSBarry Smith   char           *eol;
320dfec4cbSBarry Smith   PetscErrorCode ierr;
330dfec4cbSBarry Smith 
340dfec4cbSBarry Smith   PetscFunctionBegin;
350dfec4cbSBarry Smith   /* call Python to find out the name of the Python dynamic library */
36589a23caSBarry Smith   ierr = PetscStrncpy(command,pythonexe,sizeof(command));CHKERRQ(ierr);
37589a23caSBarry Smith   ierr = PetscStrlcat(command," ",sizeof(command));CHKERRQ(ierr);
38589a23caSBarry Smith   ierr = PetscStrlcat(command,attempt,sizeof(command));CHKERRQ(ierr);
390dfec4cbSBarry Smith #if defined(PETSC_HAVE_POPEN)
400dfec4cbSBarry Smith   ierr = PetscPOpen(PETSC_COMM_SELF,NULL,command,"r",&fp);CHKERRQ(ierr);
410db4d2e0SBarry Smith   if (!fgets(pythonlib,pl,fp)) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Python: bad output from executable: %s\nRunning: %s",pythonexe,command);
420dfec4cbSBarry Smith   ierr = PetscPClose(PETSC_COMM_SELF,fp);CHKERRQ(ierr);
430dfec4cbSBarry Smith #else
44691b26d3SBarry Smith   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Python: Aborted due to missing popen()");
450dfec4cbSBarry Smith #endif
460dfec4cbSBarry Smith   /* remove newlines */
470dfec4cbSBarry Smith   ierr = PetscStrchr(pythonlib,'\n',&eol);CHKERRQ(ierr);
480dfec4cbSBarry Smith   if (eol) eol[0] = 0;
490dfec4cbSBarry Smith   ierr = PetscTestFile(pythonlib,'r',found);CHKERRQ(ierr);
500dfec4cbSBarry Smith   PetscFunctionReturn(0);
510dfec4cbSBarry Smith }
520dfec4cbSBarry Smith 
530dfec4cbSBarry Smith 
54589a23caSBarry Smith static PetscErrorCode PetscPythonFindLibrary(const char pythonexe[],char pythonlib[],size_t pl)
55c4aff060SBarry Smith {
56702f9f58SSatish Balay   const char     cmdline1[] = "-c 'import os;from distutils import sysconfig; print(os.path.join(sysconfig.get_config_var(\"LIBDIR\"),sysconfig.get_config_var(\"LDLIBRARY\")))'";
57cb07359aSSatish Balay   const char     cmdline2[] = "-c 'import os;from distutils import sysconfig; import sys;print(os.path.join(sysconfig.get_config_var(\"LIBDIR\"),\"libpython\"+sys.version[:3]+\".dylib\"))'";
58cb07359aSSatish Balay   const char     cmdline3[] = "-c 'import os;from distutils import sysconfig; print(os.path.join(sysconfig.get_config_var(\"LIBPL\"),sysconfig.get_config_var(\"LDLIBRARY\")))'";
59cb07359aSSatish Balay   const char     cmdline4[] = "-c 'from distutils import sysconfig; print(sysconfig.get_config_var(\"LIBPYTHON\"))'";
60cb497662SVaclav Hapla   const char     cmdline5[] = "-c 'import os;from distutils import sysconfig; import sys;print(os.path.join(sysconfig.get_config_var(\"LIBDIR\"),\"libpython\"+sys.version[:3]+\".so\"))'";
61702f9f58SSatish Balay 
62ace3abfcSBarry Smith   PetscBool      found = PETSC_FALSE;
63c4aff060SBarry Smith   PetscErrorCode ierr;
64c4aff060SBarry Smith 
656e111a19SKarl Rupp   PetscFunctionBegin;
66c4aff060SBarry Smith #if defined(PETSC_PYTHON_LIB)
67589a23caSBarry Smith   ierr = PetscStrncpy(pythonlib,PETSC_PYTHON_LIB,pl);CHKERRQ(ierr);
68c4aff060SBarry Smith   PetscFunctionReturn(0);
69c4aff060SBarry Smith #endif
70c4aff060SBarry Smith 
71589a23caSBarry Smith   ierr = PetscPythonFindLibraryName(pythonexe,cmdline1,pythonlib,pl,&found);CHKERRQ(ierr);
7284b215baSBarry Smith   if (!found) {
73589a23caSBarry Smith     ierr = PetscPythonFindLibraryName(pythonexe,cmdline2,pythonlib,pl,&found);CHKERRQ(ierr);
74c4aff060SBarry Smith   }
7549a6f2e5SLisandro Dalcin   if (!found) {
76589a23caSBarry Smith     ierr = PetscPythonFindLibraryName(pythonexe,cmdline3,pythonlib,pl,&found);CHKERRQ(ierr);
7749a6f2e5SLisandro Dalcin   }
78cb07359aSSatish Balay   if (!found) {
79589a23caSBarry Smith     ierr = PetscPythonFindLibraryName(pythonexe,cmdline4,pythonlib,pl,&found);CHKERRQ(ierr);
80cb07359aSSatish Balay   }
81cb497662SVaclav Hapla   if (!found) {
82cb497662SVaclav Hapla     ierr = PetscPythonFindLibraryName(pythonexe,cmdline5,pythonlib,pl,&found);CHKERRQ(ierr);
83cb497662SVaclav Hapla   }
8402c9f0b5SLisandro Dalcin   ierr = PetscInfo2(NULL,"Python library  %s found %d\n",pythonlib,found);CHKERRQ(ierr);
85c4aff060SBarry Smith   PetscFunctionReturn(0);
86c4aff060SBarry Smith }
87c4aff060SBarry Smith 
88c4aff060SBarry Smith /* ---------------------------------------------------------------- */
89c4aff060SBarry Smith 
90c4aff060SBarry Smith typedef struct _Py_object_t PyObject; /* fake definition */
91c4aff060SBarry Smith 
9202c9f0b5SLisandro Dalcin static PyObject* Py_None = NULL;
93e0ab9aedSLisandro Dalcin 
949ac80d5eSLisandro Dalcin static const char* (*Py_GetVersion)(void);
959ac80d5eSLisandro Dalcin 
96c4aff060SBarry Smith static int       (*Py_IsInitialized)(void);
97c4aff060SBarry Smith static void      (*Py_InitializeEx)(int);
98c4aff060SBarry Smith static void      (*Py_Finalize)(void);
99c4aff060SBarry Smith 
10049a6f2e5SLisandro Dalcin static void      (*PySys_SetArgv)(int,void*);
1012f2e82b0SLisandro Dalcin static PyObject* (*PySys_GetObject)(const char*);
1022f2e82b0SLisandro Dalcin static PyObject* (*PyObject_CallMethod)(PyObject*,const char*, const char*, ...);
103c4aff060SBarry Smith static PyObject* (*PyImport_ImportModule)(const char*);
104c4aff060SBarry Smith 
105c4aff060SBarry Smith static void      (*Py_IncRef)(PyObject*);
106c4aff060SBarry Smith static void      (*Py_DecRef)(PyObject*);
107c4aff060SBarry Smith 
108c4aff060SBarry Smith static void      (*PyErr_Clear)(void);
109c4aff060SBarry Smith static PyObject* (*PyErr_Occurred)(void);
110e0ab9aedSLisandro Dalcin static void      (*PyErr_Fetch)(PyObject**,PyObject**,PyObject**);
111e0ab9aedSLisandro Dalcin static void      (*PyErr_NormalizeException)(PyObject**,PyObject**, PyObject**);
112e0ab9aedSLisandro Dalcin static void      (*PyErr_Display)(PyObject*,PyObject*,PyObject*);
113e0ab9aedSLisandro Dalcin static void      (*PyErr_Restore)(PyObject*,PyObject*,PyObject*);
114c4aff060SBarry Smith 
115c4aff060SBarry Smith 
116c4aff060SBarry Smith #define PetscDLPyLibOpen(libname) \
117d44a1e48SBarry Smith   PetscDLLibraryAppend(PETSC_COMM_SELF,&PetscDLLibrariesLoaded,libname)
118c4aff060SBarry Smith #define PetscDLPyLibSym(symbol, value) \
1190298fd71SBarry Smith   PetscDLLibrarySym(PETSC_COMM_SELF,&PetscDLLibrariesLoaded,NULL,symbol,(void**)value)
120c4aff060SBarry Smith #define PetscDLPyLibClose(comm) \
121c4aff060SBarry Smith   do { } while (0)
122c4aff060SBarry Smith 
123c4aff060SBarry Smith static PetscErrorCode PetscPythonLoadLibrary(const char pythonlib[])
124c4aff060SBarry Smith {
125c4aff060SBarry Smith   PetscErrorCode ierr;
126c4aff060SBarry Smith 
1276e111a19SKarl Rupp   PetscFunctionBegin;
128c4aff060SBarry Smith   /* open the Python dynamic library */
129c4aff060SBarry Smith   ierr = PetscDLPyLibOpen(pythonlib);CHKERRQ(ierr);
13002c9f0b5SLisandro Dalcin   ierr = PetscInfo1(NULL,"Python: loaded dynamic library %s\n", pythonlib);CHKERRQ(ierr);
131c4aff060SBarry Smith   /* look required symbols from the Python C-API */
132e0ab9aedSLisandro Dalcin   ierr = PetscDLPyLibSym("_Py_NoneStruct"        , &Py_None);CHKERRQ(ierr);
1339ac80d5eSLisandro Dalcin   ierr = PetscDLPyLibSym("Py_GetVersion"         , &Py_GetVersion);CHKERRQ(ierr);
134c4aff060SBarry Smith   ierr = PetscDLPyLibSym("Py_IsInitialized"      , &Py_IsInitialized);CHKERRQ(ierr);
135c4aff060SBarry Smith   ierr = PetscDLPyLibSym("Py_InitializeEx"       , &Py_InitializeEx);CHKERRQ(ierr);
136c4aff060SBarry Smith   ierr = PetscDLPyLibSym("Py_Finalize"           , &Py_Finalize);CHKERRQ(ierr);
1372f2e82b0SLisandro Dalcin   ierr = PetscDLPyLibSym("PySys_GetObject"       , &PySys_GetObject);CHKERRQ(ierr);
138c4aff060SBarry Smith   ierr = PetscDLPyLibSym("PySys_SetArgv"         , &PySys_SetArgv);CHKERRQ(ierr);
1392f2e82b0SLisandro Dalcin   ierr = PetscDLPyLibSym("PyObject_CallMethod"   , &PyObject_CallMethod);CHKERRQ(ierr);
140c4aff060SBarry Smith   ierr = PetscDLPyLibSym("PyImport_ImportModule" , &PyImport_ImportModule);CHKERRQ(ierr);
141c4aff060SBarry Smith   ierr = PetscDLPyLibSym("Py_IncRef"             , &Py_IncRef);CHKERRQ(ierr);
142c4aff060SBarry Smith   ierr = PetscDLPyLibSym("Py_DecRef"             , &Py_DecRef);CHKERRQ(ierr);
143c4aff060SBarry Smith   ierr = PetscDLPyLibSym("PyErr_Clear"           , &PyErr_Clear);CHKERRQ(ierr);
144c4aff060SBarry Smith   ierr = PetscDLPyLibSym("PyErr_Occurred"        , &PyErr_Occurred);CHKERRQ(ierr);
145e0ab9aedSLisandro Dalcin   ierr = PetscDLPyLibSym("PyErr_Fetch"             , &PyErr_Fetch);CHKERRQ(ierr);
146e0ab9aedSLisandro Dalcin   ierr = PetscDLPyLibSym("PyErr_NormalizeException", &PyErr_NormalizeException);CHKERRQ(ierr);
147e0ab9aedSLisandro Dalcin   ierr = PetscDLPyLibSym("PyErr_Display",            &PyErr_Display);CHKERRQ(ierr);
148e0ab9aedSLisandro Dalcin   ierr = PetscDLPyLibSym("PyErr_Restore",            &PyErr_Restore);CHKERRQ(ierr);
149c4aff060SBarry Smith   /* XXX TODO: check that ALL symbols were there !!! */
150*d8185827SBarry Smith   if (!Py_None)          SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"Python: failed to load symbols from Python dynamic library %s",pythonlib);
151*d8185827SBarry Smith   if (!Py_GetVersion)    SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"Python: failed to load symbols from Python dynamic library %s",pythonlib);
152*d8185827SBarry Smith   if (!Py_IsInitialized) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"Python: failed to load symbols from Python dynamic library %s",pythonlib);
153*d8185827SBarry Smith   if (!Py_InitializeEx)  SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"Python: failed to load symbols from Python dynamic library %s",pythonlib);
154*d8185827SBarry Smith   if (!Py_Finalize)      SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"Python: failed to load symbols from Python dynamic library %s",pythonlib);
15502c9f0b5SLisandro Dalcin   ierr = PetscInfo1(NULL,"Python: all required symbols loaded from Python dynamic library %s\n",pythonlib);CHKERRQ(ierr);
156c4aff060SBarry Smith   PetscFunctionReturn(0);
157c4aff060SBarry Smith }
158c4aff060SBarry Smith 
159c4aff060SBarry Smith /* ---------------------------------------------------------------- */
160c4aff060SBarry Smith 
161c4aff060SBarry Smith static char      PetscPythonExe[PETSC_MAX_PATH_LEN] = { 0 };
162c4aff060SBarry Smith static char      PetscPythonLib[PETSC_MAX_PATH_LEN] = { 0 };
163ace3abfcSBarry Smith static PetscBool PetscBeganPython = PETSC_FALSE;
164c4aff060SBarry Smith 
165c4aff060SBarry Smith /*@C
166c4aff060SBarry Smith   PetscPythonFinalize - Finalize Python.
167c4aff060SBarry Smith 
168c4aff060SBarry Smith   Level: intermediate
169c4aff060SBarry Smith 
170c4aff060SBarry Smith @*/
1717087cfbeSBarry Smith PetscErrorCode  PetscPythonFinalize(void)
172c4aff060SBarry Smith {
173c4aff060SBarry Smith   PetscFunctionBegin;
174c4aff060SBarry Smith   if (PetscBeganPython) { if (Py_IsInitialized()) Py_Finalize(); }
175c4aff060SBarry Smith   PetscBeganPython = PETSC_FALSE;
176c4aff060SBarry Smith   PetscFunctionReturn(0);
177c4aff060SBarry Smith }
178c4aff060SBarry Smith 
179c4aff060SBarry Smith /*@C
180c4aff060SBarry Smith   PetscPythonInitialize - Initialize Python and import petsc4py.
181c4aff060SBarry Smith 
182c4aff060SBarry Smith    Input Parameter:
1830298fd71SBarry Smith +  pyexe - path to the Python interpreter executable, or NULL.
1840298fd71SBarry Smith -  pylib - full path to the Python dynamic library, or NULL.
185c4aff060SBarry Smith 
186c4aff060SBarry Smith   Level: intermediate
187c4aff060SBarry Smith 
188c4aff060SBarry Smith @*/
1897087cfbeSBarry Smith PetscErrorCode  PetscPythonInitialize(const char pyexe[],const char pylib[])
190c4aff060SBarry Smith {
19102c9f0b5SLisandro Dalcin   PyObject       *module = NULL;
192c4aff060SBarry Smith   PetscErrorCode ierr;
1936e111a19SKarl Rupp 
194c4aff060SBarry Smith   PetscFunctionBegin;
195c4aff060SBarry Smith   if (PetscBeganPython) PetscFunctionReturn(0);
196c4aff060SBarry Smith   /* Python executable */
197c4aff060SBarry Smith   if (pyexe && pyexe[0] != 0) {
198c4aff060SBarry Smith     ierr = PetscStrncpy(PetscPythonExe,pyexe,sizeof(PetscPythonExe));CHKERRQ(ierr);
199c4aff060SBarry Smith   } else {
200589a23caSBarry Smith     ierr = PetscPythonFindExecutable(PetscPythonExe,sizeof(PetscPythonExe));CHKERRQ(ierr);
201c4aff060SBarry Smith   }
202c4aff060SBarry Smith   /* Python dynamic library */
203c4aff060SBarry Smith   if (pylib && pylib[0] != 0) {
204c4aff060SBarry Smith     ierr = PetscStrncpy(PetscPythonLib,pylib,sizeof(PetscPythonLib));CHKERRQ(ierr);
205c4aff060SBarry Smith   } else {
206589a23caSBarry Smith     ierr = PetscPythonFindLibrary(PetscPythonExe,PetscPythonLib,sizeof(PetscPythonLib));CHKERRQ(ierr);
207c4aff060SBarry Smith   }
208c4aff060SBarry Smith   /* dynamically load Python library */
209c4aff060SBarry Smith   ierr = PetscPythonLoadLibrary(PetscPythonLib);CHKERRQ(ierr);
210c4aff060SBarry Smith   /* initialize Python */
211c4aff060SBarry Smith   PetscBeganPython = PETSC_FALSE;
212c4aff060SBarry Smith   if (!Py_IsInitialized()) {
2132f2e82b0SLisandro Dalcin     static PetscBool registered = PETSC_FALSE;
2142f2e82b0SLisandro Dalcin     const char       *py_version;
2152f2e82b0SLisandro Dalcin     PyObject         *sys_path;
2162f2e82b0SLisandro Dalcin     char             path[PETSC_MAX_PATH_LEN] = { 0 };
217a297a907SKarl Rupp 
2182f2e82b0SLisandro Dalcin     /* initialize Python */
2192f2e82b0SLisandro Dalcin     Py_InitializeEx(0); /* 0: do not install signal handlers */
2202f2e82b0SLisandro Dalcin     /*  build 'sys.argv' list */
2212f2e82b0SLisandro Dalcin     py_version = Py_GetVersion();
2229ac80d5eSLisandro Dalcin     if (py_version[0] == '2') {
22349a6f2e5SLisandro Dalcin       int argc = 0; char *argv[1] = {NULL};
2242f2e82b0SLisandro Dalcin       PySys_SetArgv(argc,argv);
2252f2e82b0SLisandro Dalcin     }
2262f2e82b0SLisandro Dalcin     if (py_version[0] == '3') {
22749a6f2e5SLisandro Dalcin       int argc = 0; wchar_t *argv[1] = {NULL};
22849a6f2e5SLisandro Dalcin       PySys_SetArgv(argc,argv);
2292f2e82b0SLisandro Dalcin     }
2302f2e82b0SLisandro Dalcin     /* add PETSC_LIB_DIR in front of 'sys.path' */
2312f2e82b0SLisandro Dalcin     sys_path = PySys_GetObject("path");
2322f2e82b0SLisandro Dalcin     if (sys_path) {
2332f2e82b0SLisandro Dalcin       ierr = PetscStrreplace(PETSC_COMM_SELF,"${PETSC_LIB_DIR}",path,sizeof(path));CHKERRQ(ierr);
2342f2e82b0SLisandro Dalcin       Py_DecRef(PyObject_CallMethod(sys_path,"insert","is",(int)0,(char*)path));
23558c0e507SSatish Balay #if defined(PETSC_PETSC4PY_INSTALL_PATH)
23658c0e507SSatish Balay       {
23758c0e507SSatish Balay         char *rpath;
23858c0e507SSatish Balay         ierr = PetscStrallocpy(PETSC_PETSC4PY_INSTALL_PATH,&rpath);CHKERRQ(ierr);
23958c0e507SSatish Balay         Py_DecRef(PyObject_CallMethod(sys_path,"insert","is",(int)0,rpath));
24058c0e507SSatish Balay         ierr = PetscFree(rpath);CHKERRQ(ierr);
24158c0e507SSatish Balay       }
24258c0e507SSatish Balay #endif
2439ac80d5eSLisandro Dalcin     }
244c4aff060SBarry Smith     /* register finalizer */
245c4aff060SBarry Smith     if (!registered) {
246c4aff060SBarry Smith       ierr = PetscRegisterFinalize(PetscPythonFinalize);CHKERRQ(ierr);
247c4aff060SBarry Smith       registered = PETSC_TRUE;
248c4aff060SBarry Smith     }
249c4aff060SBarry Smith     PetscBeganPython = PETSC_TRUE;
250c4aff060SBarry Smith   }
251c4aff060SBarry Smith   /* import 'petsc4py.PETSc' module */
252c4aff060SBarry Smith   module = PyImport_ImportModule("petsc4py.PETSc");
253c4aff060SBarry Smith   if (module) {
25402c9f0b5SLisandro Dalcin     ierr = PetscInfo(NULL,"Python: successfully imported  module 'petsc4py.PETSc'\n");CHKERRQ(ierr);
255a297a907SKarl Rupp 
25602c9f0b5SLisandro Dalcin     Py_DecRef(module); module = NULL;
257c4aff060SBarry Smith   } else {
258e0ab9aedSLisandro Dalcin     PetscPythonPrintError();
259e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Python: could not import module 'petsc4py.PETSc', perhaps your PYTHONPATH does not contain it\n");
260c4aff060SBarry Smith   }
261c4aff060SBarry Smith   PetscFunctionReturn(0);
262c4aff060SBarry Smith }
263c4aff060SBarry Smith 
264e0ab9aedSLisandro Dalcin /*@C
265e0ab9aedSLisandro Dalcin   PetscPythonPrintError - Print Python errors.
266e0ab9aedSLisandro Dalcin 
267e0ab9aedSLisandro Dalcin   Level: developer
268e0ab9aedSLisandro Dalcin 
269e0ab9aedSLisandro Dalcin @*/
2707087cfbeSBarry Smith PetscErrorCode  PetscPythonPrintError(void)
271e0ab9aedSLisandro Dalcin {
27202c9f0b5SLisandro Dalcin   PyObject *exc=NULL, *val=NULL, *tb=NULL;
2736e111a19SKarl Rupp 
274e0ab9aedSLisandro Dalcin   PetscFunctionBegin;
275e0ab9aedSLisandro Dalcin   if (!PetscBeganPython) PetscFunctionReturn(0);
276e0ab9aedSLisandro Dalcin   if (!PyErr_Occurred()) PetscFunctionReturn(0);
277e0ab9aedSLisandro Dalcin   PyErr_Fetch(&exc,&val,&tb);
278e0ab9aedSLisandro Dalcin   PyErr_NormalizeException(&exc,&val,&tb);
279589a23caSBarry Smith   PyErr_Display(exc ? exc : Py_None, val ? val : Py_None, tb  ? tb  : Py_None);
280e0ab9aedSLisandro Dalcin   PyErr_Restore(exc,val,tb);
281e0ab9aedSLisandro Dalcin   PetscFunctionReturn(0);
282e0ab9aedSLisandro Dalcin }
283e0ab9aedSLisandro Dalcin 
284c4aff060SBarry Smith /* ---------------------------------------------------------------- */
2855180491cSLisandro Dalcin 
2868cc058d9SJed Brown PETSC_EXTERN PetscErrorCode (*PetscPythonMonitorSet_C)(PetscObject,const char[]);
2870298fd71SBarry Smith PetscErrorCode (*PetscPythonMonitorSet_C)(PetscObject,const char[]) = NULL;
2885180491cSLisandro Dalcin 
2895180491cSLisandro Dalcin /*@C
2905180491cSLisandro Dalcin   PetscPythonMonitorSet - Set Python monitor
2915180491cSLisandro Dalcin 
2925180491cSLisandro Dalcin   Level: developer
2935180491cSLisandro Dalcin 
2945180491cSLisandro Dalcin @*/
2955180491cSLisandro Dalcin PetscErrorCode PetscPythonMonitorSet(PetscObject obj, const char url[])
2965180491cSLisandro Dalcin {
2975180491cSLisandro Dalcin   PetscErrorCode ierr;
2986e111a19SKarl Rupp 
2995180491cSLisandro Dalcin   PetscFunctionBegin;
3005180491cSLisandro Dalcin   PetscValidHeader(obj,1);
3015180491cSLisandro Dalcin   PetscValidCharPointer(url,2);
3026c4ed002SBarry Smith   if (!PetscPythonMonitorSet_C) {
3030298fd71SBarry Smith     ierr = PetscPythonInitialize(NULL,NULL);CHKERRQ(ierr);
304*d8185827SBarry Smith     if (!PetscPythonMonitorSet_C) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Couldn't initialize Python support for monitors");
3055180491cSLisandro Dalcin   }
3065180491cSLisandro Dalcin   ierr = PetscPythonMonitorSet_C(obj,url);CHKERRQ(ierr);
3075180491cSLisandro Dalcin   PetscFunctionReturn(0);
3085180491cSLisandro Dalcin }
3095180491cSLisandro Dalcin 
3105180491cSLisandro Dalcin /* ---------------------------------------------------------------- */
311