Hi everyone, I am starting to use Restricted Python a lot and I found the following problem with slicing:
from zope.security import checker l = [1, 2] l[-3:] [1, 2] lp = checker.ProxyFactory(l) lp[-3:] [2]
The problem is that -3 gets converted to 1 somewhere, but it should be a negative number signalizing the slice to start at the beginning of the sequence. The problem exists both in Python 2.4 and 2.5 and affects both Zope 2 and 3, since both Zopes use the RestrictedPython package. I suspect that the problem lies with the new slicing syntax introduced in Python 2.4. I am willing to fix the bug, but I would need some guidance and goodwill from the gods of RestrictedPython. Anyone? Regards, Stephan -- Stephan Richter Web Software Design, Development and Training Google me. "Zope Stephan Richter"
On Thursday 05 June 2008, Martijn Faassen wrote:
Stephan Richter wrote:
I am starting to use Restricted Python a lot
I'm curious to learn what you're using it for.
I cannot give you the full details yet, but Keas, the company I am working for now, has been developing a domain-specific language that is based on Python. We did some cool stuff overriding slicing and item lookup; I will be able to say more once we launched. Clearly we do not allow to run unrestricted Python and run everything in restricted Python. The slicing and getitem syntax is very central to us, so then the problem popped up pretty quickly. Regards, Stephan -- Stephan Richter Web Software Design, Development and Training Google me. "Zope Stephan Richter"
Stephan Richter wrote:
On Thursday 05 June 2008, Martijn Faassen wrote:
Stephan Richter wrote:
I am starting to use Restricted Python a lot I'm curious to learn what you're using it for.
I cannot give you the full details yet [snip hints]
Thanks for the info nonetheless. My curiosity has only increased now, but that can't be helped. :) Keas sounds interesting! Regards, Martijn
Stephan Richter wrote:
Hi everyone,
I am starting to use Restricted Python a lot and I found the following problem with slicing:
from zope.security import checker l = [1, 2] l[-3:] [1, 2] lp = checker.ProxyFactory(l) lp[-3:] [2]
The problem is that -3 gets converted to 1 somewhere, but it should be a negative number signalizing the slice to start at the beginning of the sequence.
The problem exists both in Python 2.4 and 2.5 and affects both Zope 2 and 3, since both Zopes use the RestrictedPython package.
I suspect that the problem lies with the new slicing syntax introduced in Python 2.4. I am willing to fix the bug, but I would need some guidance and goodwill from the gods of RestrictedPython. Anyone?
Are you in fact using RestrictedPython? The code snippet looks like it only uses a security proxy. RestrictedPython is a custom Python compiler; you're not using it unless your interactive Python prompt uses RestrictedPython to compile all expressions. The behavior you saw is exactly what happens when an object implements __getitem__ and __len__ but not __getslice__. If lp matches that description, and the length of lp is 2, then Python evaluates "lp[-3:]" as "lp.__getitem__(slice(-1, 2147483647, None))". I wish Python would instead evaluate it as "lp.__getitem__(slice(-3))", but maybe there are historical reasons for this. OTOH, if RestrictedPython really is involved, RP will convert the expression to "_getitem_(lp, slice(-3, None))", which would probably do what you wanted, assuming the _getitem_ function is not too clever. BTW, here's the code I used to answer this email. :-)
class itemgetter: ... def __getitem__(self, i): ... return i ... def __len__(self): ... return 2 ... itemgetter()[-3:] slice(-1, 2147483647, None)
Shane
On Thursday 05 June 2008, Shane Hathaway wrote:
Are you in fact using RestrictedPython? The code snippet looks like it only uses a security proxy. RestrictedPython is a custom Python compiler; you're not using it unless your interactive Python prompt uses RestrictedPython to compile all expressions.
You are right.
The behavior you saw is exactly what happens when an object implements __getitem__ and __len__ but not __getslice__. If lp matches that description, and the length of lp is 2, then Python evaluates "lp[-3:]" as "lp.__getitem__(slice(-1, 2147483647, None))". I wish Python would instead evaluate it as "lp.__getitem__(slice(-3))", but maybe there are historical reasons for this.
But this is still fine. the first index I got for "lp" is +1, not -1. I have tried to ready through the C code of Proxy. It clearly exposes __getslice__. See zope/proxy/_zope_proxy_proxy.c, line 630 and 746. Does anyone else have an idea? Regards, Stephan -- Stephan Richter Web Software Design, Development and Training Google me. "Zope Stephan Richter"
participants (3)
-
Martijn Faassen -
Shane Hathaway -
Stephan Richter