Need some Python 3 help

P

Paul McGuire

I was teetering on the brink of releasing Pyparsing 1.5.3 (with some
nice new examples and goodies), when I saw that I had recently
introduced a bug in the Python 3 compatible version. Here is the
stacktrace as reported on SF:

Traceback (most recent call last):
File "testcase.py", line 11, in <module>
result = exp.parseFile("./pyparsing_py3.py")
File "/data/projekte/parsing/pyparsing/pyparsing_py3.py", line 1426,
in parseFile
return self.parseString(file_contents, parseAll)
File "/data/projekte/parsing/pyparsing/pyparsing_py3.py", line 1068,
in parseString
loc, tokens = self._parse( instring, 0 )
File "/data/projekte/parsing/pyparsing/pyparsing_py3.py", line 935, in
_parseNoCache
preloc = self.preParse( instring, loc )
File "/data/projekte/parsing/pyparsing/pyparsing_py3.py", line 893, in
preParse
while loc < instrlen and instring[loc] in wt:
TypeError: 'in <string>' requires string as left operand, not int

In this section of code, instring is a string, loc is an int, and wt
is a string. Any clues why instring[loc] would be evaluating as int?
(I am unfortunately dependent on the kindness of strangers when it
comes to testing my Python 3 code, as I don't have a Py3 environment
installed.)

Thanks,
-- Paul
 
B

Bryan

Paul McGuire said:
while loc < instrlen and instring[loc] in wt:
TypeError: 'in <string>' requires string as left operand, not int

In this section of code, instring is a string, loc is an int,

In Python 3, lots of things that used to return str now return bytes,
and the elements of a bytes object are ints. Something to check.
 
B

Benjamin Peterson

Paul McGuire said:
In this section of code, instring is a string, loc is an int, and wt
is a string. Any clues why instring[loc] would be evaluating as int?
(I am unfortunately dependent on the kindness of strangers when it
comes to testing my Python 3 code, as I don't have a Py3 environment
installed.)

Indexing bytes in Python 3 gives an integer.
 
P

Paul McGuire

Paul McGuire said:
In this section of code, instring is a string, loc is an int, and wt
is a string.  Any clues why instring[loc] would be evaluating as int?
(I am unfortunately dependent on the kindness of strangers when it
comes to testing my Python 3 code, as I don't have a Py3 environment
installed.)

Indexing bytes in Python 3 gives an integer.

Hrmm, I had a sneaking hunch this might be the issue. But then I
don't know how this code *ever* worked in Python 3, as it is chock
full of indexed references into the string being parsed. And yet,
I've had other folks test and confirm that pyparsing_py3 *does* work
on Python 3. It is a puzzle.

-- Paul
 
B

Bryan

Paul said:
Hrmm, I had a sneaking hunch this might be the issue.  But then I
don't know how this code *ever* worked in Python 3, as it is chock
full of indexed references into the string being parsed.  And yet,
I've had other folks test and confirm that pyparsing_py3 *does* work
on Python 3.  It is a puzzle.

I suspect in most cases you use bytes consistently. You got the
exception from:

instring[loc] in wt

If instring and wt are both bytes, that's fine. If they're both str,
also fine. If one is bytes and one is str, exception.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top