How to catch a usefull error message ?

Chris Angelico rosuav at gmail.com
Tue Apr 23 14:54:16 EDT 2019


On Wed, Apr 24, 2019 at 4:47 AM Vincent Vande Vyvre
<vincent.vande.vyvre at telenet.be> wrote:
>
> Le 23/04/19 à 19:23, Chris Angelico a écrit :
> > On Wed, Apr 24, 2019 at 3:18 AM Vincent Vande Vyvre
> > <vincent.vande.vyvre at telenet.be> wrote:
> >> Hi,
> >>
> >> In a CPython lib I have an _init() method wich take one argument, a file
> >> name.
> >>
> >>       char *fname;
> >>
> >>       if (!PyArg_ParseTuple(args, "s", &fname))
> >>           return NULL;
> >>
> >> So, if I instanciate my object with a bad argument I've a good error
> >> message:
> >>
> >> tif = ImgProc(123)
> >> TypeError: argument 1 must be str, not int
> >> (followed by the traceback)
> >>
> >> But if I do:
> >> try:
> >>       tif = ImgProc(123)
> >> except Exception as why:
> >>       print("Error:", why)
> >>
> >> I get just:
> >>
> >> Error: <class '_liboqapy.ImgProc'> returned a result with an error set
> >>
> > It looks like there's an internal problem in the C function. Are you
> > sure it's hitting the PyArg_ParseTuple and then immediately returning
> > NULL? Post a bit more of your code; this error looks like something is
> > leaving an error state but then carrying on with the code.
> >
> > ChrisA
>
> Into the lib:
>
> static int
> ImgProc_init(ImgProc *self, PyObject *args, PyObject *kwds)
> {
>      PyObject *tmp;
>      char *fname;
>
>      if (!PyArg_ParseTuple(args, "s", &fname))
>          return NULL;
>
>      tmp = self->src;
>      self->src = PyUnicode_FromString(fname);
>      Py_XDECREF(tmp);
>      return 0;
> }
>
> If i do:
>      try:
>          tif = ImgProc(123)
>      except Exception as why:
>          print(sys.exc_info())
>          raise
> I get:
> (<class 'SystemError'>, SystemError("<class '_liboqapy.ImgProc'>
> returned a result with an error set",), <traceback object at
> 0x7f3bcac748c8>)
> TypeError: argument 1 must be str, not int
>
> The above exception was the direct cause of the following exception:
>
> Traceback (most recent call last):
>    File "/home/vincent/oqapy-3/trunk/filters/ui_lenscorrection.py", line
> 104, in on_main_cursor_changed
>      self.prepare_preview_process()
>    File "/home/vincent/oqapy-3/trunk/filters/ui_lenscorrection.py", line
> 137, in prepare_preview_process
>      self.main.process_on_preview(params)
>    File "/home/vincent/oqapy-3/trunk/filters/lenscorrection.py", line
> 56, in process_on_preview
>      tif = ImgProc(123)
> SystemError: <class '_liboqapy.ImgProc'> returned a result with an error set
> --------------------------------------------------------------------------------
> Why a SystemError ?

The SystemError means that you're using the Python C API in a way that
doesn't make sense to the interpreter. You're leaving a marker saying
"hey, I need you to throw an exception" but then you're also returning
a value. You'll need to figure out where that's happening and exactly
what is being called. How are you setting up your class?

ChrisA



More information about the Python-list mailing list