Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error "'CArgObject' has no len()" when using Prolog strings #9

Closed
morxa opened this issue Jan 18, 2017 · 2 comments
Closed

Error "'CArgObject' has no len()" when using Prolog strings #9

morxa opened this issue Jan 18, 2017 · 2 comments

Comments

@morxa
Copy link

morxa commented Jan 18, 2017

When using Prolog strings in a fact, an error is thrown.

import pyswip
p = pyswip.Prolog()
p.assertz('some_string_fact("abc")')
print(list(p.query('some_string_fact(S)')))

This results in an error:

$ python3 pyswip_string.py 
Traceback (most recent call last):
  File "pyswip_string.py", line 23, in <module>
    print(list(p.query('some_string_fact(S)')))
  File "/usr/lib/python3.5/site-packages/pyswip/prolog.py", line 111, in __call__
    t = getTerm(swipl_list)
  File "/usr/lib/python3.5/site-packages/pyswip/easy.py", line 404, in getTerm
    res = getList(t)
  File "/usr/lib/python3.5/site-packages/pyswip/easy.py", line 420, in getList
    result.append(getTerm(head))
  File "/usr/lib/python3.5/site-packages/pyswip/easy.py", line 406, in getTerm
    res = getFunctor(t)
  File "/usr/lib/python3.5/site-packages/pyswip/easy.py", line 429, in getFunctor
    return Functor.fromTerm(t)
  File "/usr/lib/python3.5/site-packages/pyswip/easy.py", line 252, in fromTerm
    args.append(getTerm(a0 + i))
  File "/usr/lib/python3.5/site-packages/pyswip/easy.py", line 402, in getTerm
    res = _getterm_router[p](t)
  File "/usr/lib/python3.5/site-packages/pyswip/easy.py", line 387, in getString
    if PL_get_string_chars(t, byref(s), byref(slen)):
  File "/usr/lib/python3.5/site-packages/pyswip/core.py", line 555, in check_and_call
    args[i] = list_to_bytes_list(arg)
  File "/usr/lib/python3.5/site-packages/pyswip/core.py", line 487, in list_to_bytes_list
    pList = c_char_p * len(strList)
TypeError: object of type 'CArgObject' has no len()

Expected result: No error, but the unification [{'S': "abc"}]

I'm using SWI-Prolog 7.2.3 on Fedora 25 with pyswip commit 72771d9.

@xpinguin
Copy link

xpinguin commented Jan 2, 2018

Apparently, check_strings shouldn't be used to decorate PL_get_string/PL_get_string_chars because char **s is actually passed as ctypes.byref, and not as a list (IMHO, it is correct):
https://github.com/yuce/pyswip/blob/master/pyswip/easy.py#L387

The problem could be fixed by commenting the following line:
https://github.com/yuce/pyswip/blob/master/pyswip/core.py#L801

While I haven't tested it thoroughly, it seems to work for my (simple) use-cases.

yuce added a commit that referenced this issue May 24, 2018
@yuce
Copy link
Owner

yuce commented May 24, 2018

This issue was fixed with: #31

@yuce yuce closed this as completed May 24, 2018
yuce added a commit that referenced this issue May 24, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants