GithubHelp home page GithubHelp logo

Comments (9)

yuce avatar yuce commented on May 26, 2024

You can pass maxresult=1 to retrieve only a single answer, like:

solu = prolog.query("get_list(3, X)", maxresult=1)

from pyswip.

moqingyan avatar moqingyan commented on May 26, 2024

But this does not help solve the segfault though. It seems the segfault occurs at some clean up stage:

from pyswip import *

prolog = Prolog()
prolog.consult("test_list.pl")

solu = prolog.query("get_list(3, X)", maxresult=1)
for s in solu:
    print(s)
    break

print(s['X'][1])
print("prog end")

The corresponding output is:

{'X': [3, 2, 1]}
2
prog end
fish: 'python spec_verify.py' terminated by signal SIGSEGV (Address boundary error)

If I change to bash:

{'X': [3, 2, 1]}
2
prog end
Segmentation fault: 11

from pyswip.

yuce avatar yuce commented on May 26, 2024

Can you remove the break in the loop and try again?

from pyswip.

moqingyan avatar moqingyan commented on May 26, 2024

The program either stuck or quit unexpectedly. The terminal window closed itself when the program quits.

from pyswip.

yuce avatar yuce commented on May 26, 2024

This is the code I've tried:

from pyswip import Prolog                                             
prolog = Prolog()
prolog.consult("test_list.pl")
solu = prolog.query("get_list(3, X)", maxresult=1)
for i in solu:
    print (i)

This is what I get when running it:

$ python test_list.py                       
{'X': [3, 2, 1]}
$

No segfaults, etc. (of course that doesn't rule out a bug in PySwip). This is on Ubuntu 18.04, Python 3.6.8, SWI 7.6.4. All of those using Ubuntu's packages.

Did you try your code using SWI-Prolog? It doesn't terminate after the first solution.

from pyswip.

moqingyan avatar moqingyan commented on May 26, 2024

Yes, I have tried it with the SWI-Prolog and it worked normally. I think I can try to install the packages again on Ubuntu system and see whether the problem is MacOSX specific. :)

from pyswip.

yuce avatar yuce commented on May 26, 2024

This is what I've tried with SWI-Prolog:

?- [test_list].                                                       
true.                                                                 

?- get_list(3, X).                                                    
X = [3, 2, 1] ;  <-- doesn't terminate after that

What do you get after [3, 2, 1] ?

from pyswip.

fpopineau avatar fpopineau commented on May 26, 2024

Hi there,
I can confirm that there is definitely some problem here. I encountered a similar situation with a segv on both Windows and MacOSX, with SWI-PL 7.6 and 8.0. I get this result on the test case:

$ python test_list.py
{'X': [3, 2, 1]}
Exception ignored in: <generator object Prolog._QueryWrapper.__call__ at 0x0000019B3385F580>
Traceback (most recent call last):
  File "C:\Local\PyConfig\envs\aienv\lib\site-packages\pyswip\prolog.py", line 130, in __call__
OSError: exception: access violation reading 0x0000000000000110

It seems that an exception occurs inside the finally clause of the call to Prolog, precisely in the call to PL_cut_query(swipl_qid).
I tried to replace this call with PL_close_query(swipl_qid) but the result is the same.

Any idea of what can cause that ?
Regards,

from pyswip.

fpopineau avatar fpopineau commented on May 26, 2024

More on this issue. I get a constant segfault if I run the query this way:

q = prolog.query("get_list(3, X)", maxresult=1)
solu = next(q)
print(solu['X'])

but everything is perfect if I run it this way:

solu = next(prolog.query("get_list(3, X)", maxresult=1))
print(solu['X'])

The difference is about when the finally clause is called. In the later case, the finally clause is called before printing the term, whereas it is called after printing it int the former case.

And actually the reason lies in a not so well known behavior of Python generators:
https://stackoverflow.com/questions/56062909/try-finally-in-python-3-generator

from pyswip.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.