Export to GitHub

ply - issue #30

Python 3 slicing no longer uses __getslice__


Posted on Feb 28, 2011 by Grumpy Dog

What steps will reproduce the problem? 1. Use a slice in a production rule, eg:

def p_toplevel(p): """ toplevel : alpha beta gamma TOKEN delta """ p[0] = p[1:4] + p[5:]

What is the expected output? What do you see instead? Expected: p[0] is a list of the nonterminals alpha, beta, gamma, and delta. Got: File "/usr/lib/python3.1/site-packages/ply/yacc.py", line 198, in getitem if n >= 0: return self.slice[n].value TypeError: unorderable types: slice() >= int()

What version of the product are you using? On what operating system?

This is python 3.1, but this has been true since python 3.0: http://docs.python.org/py3k/whatsnew/3.0.html#operators-and-special-methods

Namely, instead of getslice being called as it was in python 2.x, getitem is called with a slice object, which is being compared with an int in YaccProduction's case. (Similarly, my testing shows that even in Python 2.7, a slice that includes a step, eg. a[1:4:2], sends a slice object to getitem instead of to getslice.)

Simple patch included (Python 2 compatible) to fix this, but I note it allows the use of negative indices in slices in a way that doesn't match how negative indexing currently works.

Attachments

Comment #1

Posted on Dec 21, 2011 by Quick Panda

I ran into this problem as well, but my fix was the following (also plays nice with negative indices):

def getitem(self,n): if isinstance(n,slice): return [self[i] for i in range(*(n.indices(len(self.slice))))] else: # for integer indices, do what we used to if n >= 0: return self.slice[n].value else: return self.stack[n].value

Comment #2

Posted on Dec 21, 2011 by Happy Wombat

I'm not sure if using slices is really documented in PLY, but I'll make a fix in PLY-3.5. --Dave

Comment #3

Posted on Dec 21, 2011 by Quick Panda

Thanks! That would be great :)

Our project has been using slices in ply for years, and now that I tried porting it to python3 everything broke :)

Regards,

Gašper

Comment #4

Posted on Dec 21, 2011 by Happy Wombat

For what it's worth, I'm doing a bunch of compiler work with PLY right now in Python 3. I don't use slices so I haven't encountered this, but I'm hoping to put out a new PLY release sometime in the near future (4-5 weeks). If you find other bugs, please let me know.

Comment #5

Posted on Feb 19, 2013 by Grumpy Hippo

Just wanted to chime in that I experienced this issue today with Python 3... It has been over a year since it was reported and the attached patch fixes the issue. Are there any plans to release 3.5 any time soon? If not could we get a 3.4.1 release out that includes this patch? Thanks!

Comment #6

Posted on Apr 25, 2013 by Grumpy Hippo

I just upgraded my system and I'm running into this issue myself. Any chance we can get a new version out with the patch applied?

Comment #7

Posted on Dec 3, 2013 by Massive Camel

Anything new concerning releasing Ply 3.5 fixing this bug? Or do we have to avoid using slices in rules?

Comment #8

Posted on Dec 4, 2013 by Happy Rhino

I'm pretty sure that this has already been fixed in github.

Status: New

Labels:
Type-Defect Priority-Medium