xref: /petsc/src/binding/petsc4py/src/petsc4py/PETSc/Error.pyx (revision 382a0339f0e5821666c1f569aee99d547d21b39e)
1# --------------------------------------------------------------------
2
3class Error(RuntimeError):
4    """PETSc Error.
5
6    Attributes
7    ----------
8    ierr : int
9        PETSc error code.
10
11    """
12
13    def __init__(self, int ierr=0):
14        self.ierr = ierr
15        self._traceback = []
16        RuntimeError.__init__(self, self.ierr)
17
18    def __bool__(self):
19        cdef int ierr = self.ierr
20        return ierr != 0
21
22    def __repr__(self):
23        return 'PETSc.Error(%d)' % self.ierr
24
25    def __str__(self):
26        cdef int csize=1, crank=0
27        if not (<int>PetscFinalizeCalled):
28            MPI_Comm_size(PETSC_COMM_WORLD, &csize)
29            MPI_Comm_rank(PETSC_COMM_WORLD, &crank)
30        width, rank = len(str(csize-1)), crank
31        tblist = ['error code %d' % self.ierr]
32        for entry in self._traceback:
33            tbline = '[%*d] %s' % (width, rank, entry)
34            tblist.append(tbline)
35        return '\n'.join(tblist)
36
37# --------------------------------------------------------------------
38