Consult on windows, problem with backslash in path

14 views
Skip to first unread message

Alain Coetmeur

unread,
May 29, 2025, 10:03:02 AMMay 29
to pyswip
Hello,

I'm beginning to use pyswip and I've encountered an error when consulting a file.

On windows, the python Path contains backslash (even if I replace them in the parameters), and single backslash in the consult query are interpreted as escape.
ERROR: Syntax error: Unknown character escape in quoted atom or string: `\D'
ERROR: consult('C:
ERROR: ** here **
ERROR: \Develop\pyswip-mastermind\src\prolog\mastermind.pl') .
Traceback (most recent call last):
  File "C:\Develop\pyswip-mastermind\src\main.py", line 20, in <module>
    prolog.consult(relative_to=prolog_relative_to,path='prolog/mastermind.pl')
    ~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "c:\Develop\pyswip-mastermind\.venv\Lib\site-packages\pyswip\prolog.py", line 393, in consult
    next(cls.query(str(path).join(["consult('", "')"]), catcherrors=catcherrors))
    ~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "c:\Develop\pyswip-mastermind\.venv\Lib\site-packages\pyswip\prolog.py", line 171, in __call__
    raise PrologError(
    ...<10 lines>...
    )
pyswip.prolog.PrologError: Caused by: 'consult('C:\Develop\pyswip-mastermind\src\prolog\mastermind.pl')'. Returned: 'error(syntax_error(undefined_char_escape(D)), string(b"consult('C:\\Develop\\pyswip-mastermind\\src\\prolog\\mastermind.pl') . ", 11))'.

I've tested with the master gron git, installed by pip install (not sur I dit it well, I'm a beginner)

In fact the solution should be simple.
the backslash should be doubled

in prolog.consult()

path = resolve_path(path, relative_to)
        next(cls.query(str(path).join(["consult('", "')"]), catcherrors=catcherrors))

it should become
path = resolve_path(path, relative_to)
        next(cls.query(str(path).replace('\\','\\\\').join(["consult('", "')"]), catcherrors=catcherrors))

What do yo think ? 
I'm a bit noob I admit.

yuce

unread,
May 30, 2025, 12:43:42 AMMay 30
to pyswip
Hi Alain,

Could you try to pass a raw string to Prolog.consult by prefixing the string with "r"?
Like: Prolog.consult(r"C:\Develop\pyswip-mastermind\src\prolog\mastermind.p")

Hope that it helps,
Yüce

Alain Coetmeur

unread,
May 30, 2025, 1:12:48 PMMay 30
to pys...@googlegroups.com
Thanks, but does not seems to improve

I've tried
prolog.consult(path=r'C:\Develop\python-prolog-demo\src\prolog\family.pl')

but it moans the same

pyswip.prolog.PrologError: Caused by: 'consult('C:\Develop\python-prolog-demo\src\prolog\family.pl')'. Returned: 'error(syntax_error(undefined_char_escape(D)), string(b"consult('C:\\Develop\\python-prolog-demo\\src\\prolog\\family.pl') . ", 11))'.

in fact even redoubling the backslash does the same, as the pathlib.Path rebuild the path with windows standard...

prolog.consult(path=r'C:\\Develop\\python-prolog-demo\\src\\prolog\\family.pl')


ERROR: Syntax error: Unknown character escape in quoted atom or string: `\D'
ERROR: consult('C:
ERROR: ** here **
ERROR: \Develop\python-prolog-demo\src\prolog\family.pl') .
Traceback (most recent call last):
  File "C:\Develop\python-prolog-demo\src\main_old.py", line 16, in <module>
    prolog.consult(path=r'C:\\Develop\\python-prolog-demo\\src\\prolog\\family.pl')
    ~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Develop\python-prolog-demo\.venv\Lib\site-packages\pyswip\prolog.py", line 393, in consult

    next(cls.query(str(path).join(["consult('", "')"]), catcherrors=catcherrors))
    ~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Develop\python-prolog-demo\.venv\Lib\site-packages\pyswip\prolog.py", line 171, in __call__

    raise PrologError(
    ...<10 lines>...
    )
pyswip.prolog.PrologError: Caused by: 'consult('C:\Develop\python-prolog-demo\src\prolog\family.pl')'. Returned: 'error(syntax_error(undefined_char_escape(D)), string(b"consult('C:\\Develop\\python-prolog-demo\\src\\prolog\\family.pl') . ", 11))'.

whatever text I give, the pathlib library rebuilds a windows path with single backslash that annoy PROLOG.

I could make it work by replicating the code, in a hack class, just adding a redoubling of the backslash
.replace('\\','\\\\')

def consult(self,prolog:Prolog,path,catcherrors: bool = False):
        """patch the pyswip consult method which dont manage backlash in windows path"""
        #prolog.consult(relative_to=prolog_relative_to,path=path,catcherrors=catcherrors)
        path = prolog_relative_to  / path
        next(prolog.query(str(path).replace('\\','\\\\').join(["consult('", "')"])))
I don't know if it works on linux and mac in case some file name contain strange characters

maybe there is better method to escape a string for prolog. my way does not escape quotes for example... but it's forbidden on windows, so i cannot test.
maybe I should also add .replace("\"","\\\"")  
?

--
You received this message because you are subscribed to the Google Groups "pyswip" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pyswip+un...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/pyswip/5e56dd65-4680-4c81-bd94-0e7c5c37406cn%40googlegroups.com.

yuce

unread,
Jun 2, 2025, 12:42:37 AMJun 2
to pyswip
I don't have a Windows box to test this ATM, but I think you should reverse the backslashes: "C:/Develop/python-prolog-demo/src/prolog/family.pl".
If you are calling family.pl from a python file in the same directory, you can also try this: Prolog.consult("family.pl", relative_to=__file__)

If either of those don't work, could you file am issue at https://github.com/yuce/pyswip/issues ?

Alain Coetmeur

unread,
Jun 2, 2025, 6:15:39 PMJun 2
to pys...@googlegroups.com
I have tested, but nothing better.
The '/' are transformed into  backslashes by pathlib.Path
and the relativeto, also result in a windows path with backslash...

I will fill an issue

thanks

Reply all
Reply to author
Forward
0 new messages