yuce / pyswip

PySwip is a Python-Prolog interface that enables querying SWI-Prolog in your Python programs.
https://pyswip.org
MIT License
476 stars 98 forks source link

Has anyone had any luck printing a backtrace? #87

Closed allComputableThings closed 4 years ago

allComputableThings commented 4 years ago

Assert

        test_backtrace2(X) :- missing(X), f(X).
        test_backtrace1(X) :- test_backtrace2(X), f(X).
        test_backtrace(X) :- test_backtrace1(X), f(X).

and querying:

        test_backtrace(X).

I was hoping to be able to see a backtrace. However, I just see:

  [0] system:$c_call_prolog/0 <no clause>

(the exception message confirms it failed at 'test_backtrace2(X) :- missing(X)...')

Here's what I did to get that:

# Caller must PL_free the frame.
PL_backtrace_string =_lib.PL_backtrace_string
PL_backtrace_string.argtypes = [c_int, c_int]
PL_backtrace_string.restype = c_void_p

PL_free =_lib.PL_free
PL_free.argtypes = [c_void_p]
PL_free.restype = None

and in QueryWrapper:

            if PL_exception(swipl_qid):
                import ctypes as ct
                bttext = PL_backtrace_string(20, 0)   # Tried 0, 1, 2, 3 --
                if bttext is not None:
                    print(f"{msg}\n{ct.cast(bttext, ct.c_char_p).value.decode('utf8')}")
                    PL_free(bttext)

Any idea how to receive the full backtrace?

allComputableThings commented 4 years ago

This is a bug with swi-prolog. The intellij environment fatally interferes with the prolog input prompt causing a segfault. Choose "Emulate terminal" in the run configuration to avoid it.