pyswip and utf8

101 views
Skip to first unread message

Hefaist

unread,
Aug 7, 2019, 6:45:29 AM8/7/19
to pyswip
Hi everybody,

I solve the problem of how to work with facts in UTF8 mode.
In python, it looks good.

For example:
from pyswip import Prolog


prolog
= Prolog()
prolog
.assertz(u"father('franišek')")
prolog
.assertz(u"father('bonifác')")

print(list(prolog.query("father(X)")))

Result:
[{'X': 'franišek'}, {'X': 'bonifác'}]

It is correct, but when I am loading these facts from a file TEST.PL, I have a problem.

:- set_prolog_flag(encoding,utf8).

father
('franišek').
father
('bonifác').



My program for loading looks that:
from pyswip import Prolog

prolog
= Prolog()
prolog
.consult("test.pl", catcherrors=True)
print(list(prolog.query("current_prolog_flag(encoding,X)")))
print(list(prolog.query("father(X)")))

Result:
[{'X': 'utf8'}]

and error:
File "d:\xwrk\sandbox\prolog\test_pyswip.py", line 6, in <module>
 
print(list(prolog.query("father(X)")))
File "D:\xprg\python\Conda35\Lib\site-packages\pyswip\prolog.py", line 111, in __call__
  t
= getTerm(swipl_list)
File "D:\xprg\python\Conda35\Lib\site-packages\pyswip\easy.py", line 404, in getTerm
  res
= getList(t)
File "D:\xprg\python\Conda35\Lib\site-packages\pyswip\easy.py", line 420, in getList
  result
.append(getTerm(head))
File "D:\xprg\python\Conda35\Lib\site-packages\pyswip\easy.py", line 406, in getTerm
  res
= getFunctor(t)
File "D:\xprg\python\Conda35\Lib\site-packages\pyswip\easy.py", line 429, in getFunctor
 
return Functor.fromTerm(t)
File "D:\xprg\python\Conda35\Lib\site-packages\pyswip\easy.py", line 254, in fromTerm
 
return cls(f.value, args=args, a0=a0)
File "D:\xprg\python\Conda35\Lib\site-packages\pyswip\easy.py", line 231, in __init__
 
self.__value = self.func[self.handle](self.arity, *self.args)
File "D:\xprg\python\Conda35\Lib\site-packages\pyswip\easy.py", line 295, in _unifier
 
return {args[0].value:args[1].value}
File "D:\xprg\python\Conda35\Lib\site-packages\pyswip\easy.py", line 82, in get_value
  ret
= ret.decode()

builtins
.UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe1 in position 5: invalid continuation byte


In the project SWI-prolog loading of the test.pl file works without a problem.


Thank you very much for the inspiration, how to find a solution to the problem?.

Nicolas Gallinal

unread,
Aug 7, 2019, 9:37:29 AM8/7/19
to pyswip
Hey Heifast,

Very interesting issue. Have you created an issue in the repo?
I'll take a look into it!

Nicolas Gallinal

unread,
Aug 7, 2019, 1:06:17 PM8/7/19
to pyswip
Hi again Heifast,

I could reproduce it.
The enconding that comes back from prolog in my case is latin-1 and that is why it explodes.

Please create a bug in the repo with all this information.

Cheers,
Nico

On Wednesday, 7 August 2019 07:45:29 UTC-3, Hefaist wrote:

Hefaist

unread,
Aug 9, 2019, 2:49:36 AM8/9/19
to pyswip
Hi Nicolas,

I am pleased that you are willing to solve this problem.
I prepared everything here.

Have a nice weekend
                                        Hefaist



Dne středa 7. srpna 2019 19:06:17 UTC+2 Nicolas Gallinal napsal(a):
Reply all
Reply to author
Forward
0 new messages