jplevyak / dparser

A Scannerless GLR parser/parser generater.
https://github.com/jplevyak/dparser
BSD 3-Clause "New" or "Revised" License
105 stars 14 forks source link

Apparent incompatibility with bytes objects in Python 3 #18

Open bkovitz opened 4 years ago

bkovitz commented 4 years ago

I just tried installing dparser 1.31 under Python 3.7.6 and Swig 3.0.8 and ran into this error when running make test in the python directory:

    running test tests/test2.py
Traceback (most recent call last):
  File "tests/test2.py", line 53, in <module>
    if Parser().parse('1  +2* (3+ 4+5)', ambiguity_fn=ambiguity_func,
  File "/home/bkovitz/github/dparser/python/build/lib.linux-x86_64-3.7/dparser.py", line 290, in __init__
    self.tables.load_tables(grammar_str, self.filename, make_grammar_file)
  File "/home/bkovitz/github/dparser/python/build/lib.linux-x86_64-3.7/dparser.py", line 190, in load_tables
    dparser_swigc.make_tables(grammar_str, filename.encode())
TypeError: in method 'make_tables', argument 1 of type 'char *'

The problem appears to be that dparser.py is passing a bytes object but the Swig wrapper expects an ordinary string. Perhaps this is an incompatibility with Python 3 (which made bytes objects distinct from strings)?

I got the tests to pass by editing dparser.py to call .decode('ascii') on various arguments passed through Swig, like this (line 190):

        dparser_swigc.make_tables(grammar_str.decode('ascii'), filename.encode().decode('ascii'))

in a few places. I can submit a pull request with these changes if you like, but I don't know enough to say if this is the correct solution or not (especially for Python 2).

jplevyak commented 4 years ago

So I tried this with python 3.8.2 and swig 4.0.1 with python 3.6.9 and swig 3.0.12 and neither of them had this particular problem. I did fix a signedness issue and pushed that.