pyswip icon indicating copy to clipboard operation
pyswip copied to clipboard

catch errors / warnings on consult

Open yochem opened this issue 3 years ago • 3 comments

Dear maintainer(s),

I am using your package for automating grading of students' homework. The basic procedure is like this:

  1. consult their file
  2. query our tests
  3. verify output queries

However, sometimes the students have errors or warnings on consulting. This should be catched by our program which can give feedback about this. Unfortunately I can't get this to work. I tried using the catcherrors parameter of the consult method, but that doesn't change anything. The warnings / errors are printed to stderr.

Do you know if this would be possible? I would love something like returning it from the consult method or something like that.

yochem avatar Sep 13 '20 20:09 yochem

Hi yochem,

I have a the same issue. I need to get an exception when there is an error in a consulted file.

After some investigation, I think the problem lies in SWI prolog itself. SWI Prolog doesn't raise an exception when there is a mistake in the consulted file and hence, pyswip cannot raise any exception neither.

I'm not sure about the rationale behind this behavior. But one explanation is that even when a prolog file contains errors, it is still "successfully" consulted up to the point where the first error occurs. I.e., all clauses preceding the one containing errors is still successfully added to the DB and you can use them.

In any case, I guess that you would have to ask the SWI prolog team directly to throw an exception when there is an error in a consulted file. Once this is implemented on the SWI prolog side, I would assume that this excecption is also automatically raised on the pyswip side.

Please be aware that everything I said above is my own conclusion after experimenting with SWI Prolog and pyswip and I don't know all the technical details. Hence, my conclusions could be wrong.

tobiasgrubenmann avatar Nov 12 '20 10:11 tobiasgrubenmann

Is it still same if you switch the catch_exception parameter?

allComputableThings avatar Nov 19 '20 02:11 allComputableThings

pyswip doesn't seem to recognise that parameter:

>>> import pyswip
>>> pl = pyswip.Prolog()
>>> pl.consult('test.pl', catch_exception=True)
TypeError: consult() got an unexpected keyword argument 'catch_exception'
>>> pl.consult('test.pl', catchexception=True)
TypeError: consult() got an unexpected keyword argument 'catchexception'
>>> pl.consult('test.pl', catch_exceptions=True)
TypeError: consult() got an unexpected keyword argument 'catch_exceptions'
>>> pl.consult('test.pl', catchexceptions=True)
TypeError: consult() got an unexpected keyword argument 'catchexceptions'

yochem avatar Nov 19 '20 11:11 yochem