[pypy-dev] XRange Object [Fixed Patch]

Moshe Zadka m at moshez.org
Tue Jul 8 10:58:20 CEST 2003


OK, I fixed the problem of not registering in the builtins, and now

moshez at green:~/devel/pypy/src/pypy$ python2.2 interpreter/py.py  -S
Python 2.2.2 (#1, Nov 21 2002, 08:18:14)
[GCC 2.95.4 20011002 (Debian prerelease)] in pypy
PyPyConsole / StdObjSpace
>>> for i in xrange(0,1,1):
...     print i
...
0

[Though, this being my first playing around with PyPy, I must say I
was a bit taken aback by just how slow it is :)]

Diff still vs. 1116

Thanks and sorry for the slight spamming,
Moshe

diff -urN -x.svn -x'*.pyc' src.old/pypy/objspace/std/objspace.py src/pypy/objspace/std/objspace.py
--- src.old/pypy/objspace/std/objspace.py	2003-07-08 08:36:48.000000000 +0000
+++ src/pypy/objspace/std/objspace.py	2003-07-08 08:53:16.000000000 +0000
@@ -67,6 +67,7 @@
             from stringtype import W_StringType
             from typetype   import W_TypeType
             from slicetype  import W_SliceType
+            from xrangetype  import W_XRangeType
         return [value for key, value in result.__dict__.items()
                       if not key.startswith('_')]   # don't look
 
@@ -260,6 +261,11 @@
         import moduleobject
         return moduleobject.W_ModuleObject(self, w_name)
 
+    def newxrange(self, w_start, w_end, w_step):
+        # w_step may be a real None
+        import xrangeobject
+        return xrangeobject.W_XRangeObject(self, w_start, w_end, w_step)
+
     def newstring(self, chars_w):
         try:
             chars = [chr(self.unwrap(w_c)) for w_c in chars_w]
diff -urN -x.svn -x'*.pyc' src.old/pypy/objspace/std/test/test_xrangeobject.py src/pypy/objspace/std/test/test_xrangeobject.py
--- src.old/pypy/objspace/std/test/test_xrangeobject.py	1970-01-01 00:00:00.000000000 +0000
+++ src/pypy/objspace/std/test/test_xrangeobject.py	2003-07-08 08:35:28.000000000 +0000
@@ -0,0 +1,70 @@
+import autopath
+from pypy.objspace.std.xrangeobject import W_XRangeObject
+from pypy.objspace.std.intobject import W_IntObject
+from pypy.objspace.std.objspace import NoValue
+from pypy.tool import test
+
+class TestW_XRangeObject(test.TestCase):
+
+    def setUp(self):
+        self.space = test.objspace('std')
+
+    def tearDown(self):
+        pass
+
+    def test_is_true(self):
+        w = self.space.wrap
+        w_range = W_XRangeObject(self.space, w(0), w(0), w(1))
+        self.assertEqual(self.space.is_true(w_range), False)
+        w_range = W_XRangeObject(self.space, w(0), w(1), w(1))
+        self.assertEqual(self.space.is_true(w_range), True)
+        w_range = W_XRangeObject(self.space, w(0), w(5), w(1))
+        self.assertEqual(self.space.is_true(w_range), True)
+
+    def test_len(self):
+        w = self.space.wrap
+        w_range = W_XRangeObject(self.space, w(0), w(0), w(1))
+        self.assertEqual_w(self.space.len(w_range), w(0))
+        w_range = W_XRangeObject(self.space, w(0), w(1), w(1))
+        self.assertEqual_w(self.space.len(w_range), w(1))
+        w_range = W_XRangeObject(self.space, w(0), w(5), w(1))
+        self.assertEqual_w(self.space.len(w_range), w(5))
+ 
+    def test_getitem(self):
+        w = self.space.wrap
+        w_range = W_XRangeObject(self.space, w(0), w(2), w(1))
+        self.assertEqual_w(self.space.getitem(w_range, w(0)), w(0))
+        self.assertEqual_w(self.space.getitem(w_range, w(1)), w(1))
+        self.assertEqual_w(self.space.getitem(w_range, w(-2)), w(0))
+        self.assertEqual_w(self.space.getitem(w_range, w(-1)), w(1))
+        self.assertRaises_w(self.space.w_IndexError,
+                            self.space.getitem, w_range, w(2))
+        self.assertRaises_w(self.space.w_IndexError,
+                            self.space.getitem, w_range, w(42))
+        self.assertRaises_w(self.space.w_IndexError,
+                            self.space.getitem, w_range, w(-3))
+
+    def test_iter(self):
+        w = self.space.wrap
+        w_range = W_XRangeObject(self.space, w(0), w(3), w(1))
+        w_iter = self.space.iter(w_range)
+        self.assertEqual_w(self.space.next(w_iter), w(0))
+        self.assertEqual_w(self.space.next(w_iter), w(1))
+        self.assertEqual_w(self.space.next(w_iter), w(2))
+        self.assertRaises(NoValue, self.space.next, w_iter)
+        self.assertRaises(NoValue, self.space.next, w_iter)
+
+    def test_contains(self):
+        w = self.space.wrap
+        w_range = W_XRangeObject(self.space, w(0), w(3), w(1))
+        self.assertEqual_w(self.space.contains(w_range, w(0)),
+                           self.space.w_True)
+        self.assertEqual_w(self.space.contains(w_range, w(1)),
+                           self.space.w_True)
+        self.assertEqual_w(self.space.contains(w_range, w(11)),
+                           self.space.w_False)
+        self.assertEqual_w(self.space.contains(w_range, w_range),
+                           self.space.w_False)
+
+if __name__ == '__main__':
+    test.main()
diff -urN -x.svn -x'*.pyc' src.old/pypy/objspace/std/xrangeobject.py src/pypy/objspace/std/xrangeobject.py
--- src.old/pypy/objspace/std/xrangeobject.py	1970-01-01 00:00:00.000000000 +0000
+++ src/pypy/objspace/std/xrangeobject.py	2003-07-08 08:32:53.000000000 +0000
@@ -0,0 +1,88 @@
+"""
+Reviewed 03-06-21
+
+slice object construction   tested, OK
+indices method              tested, OK
+"""
+
+from pypy.objspace.std.objspace import *
+from pypy.interpreter.appfile import AppFile
+from pypy.interpreter.extmodule import make_builtin_func
+from pypy.objspace.std.instmethobject import W_InstMethObject
+from xrangetype import W_XRangeType
+from intobject import W_IntObject
+
+#appfile = AppFile(__name__, ["objspace.std"])
+
+
+class W_XRangeObject(W_Object):
+    statictype = W_XRangeType
+    
+    def __init__(w_self, space, w_start, w_stop, w_step):
+        W_Object.__init__(w_self, space)
+        w_self.w_start = w_start or W_IntObject(space, 0)
+        w_self.w_stop = w_stop
+        w_self.w_step = w_step or W_IntObject(space, 1)
+
+    def tolist(w_self):
+        stop = w_self.space.unwrap(w_self.w_stop)
+        start = w_self.space.unwrap(w_self.w_start)
+        step = w_self.space.unwrap(w_self.w_step)
+        num = (stop-start)//step
+        # does this count as "using range in a loop"?
+        # RPython is poorly speced
+        return w_self.space.newlist([w_self.space.wrap(i*w_step+w_start)
+                                          for i in range(num)])
+
+registerimplementation(W_XRangeObject)
+
+def getattr__XRange_ANY(space, w_range, w_attr):
+    if space.is_true(space.eq(w_attr, space.wrap('start'))):
+        if w_range.w_start is None:
+            return space.w_None
+        else:
+            return w_range.w_start
+    if space.is_true(space.eq(w_attr, space.wrap('stop'))):
+        if w_range.w_stop is None:
+            return space.w_None
+        else:
+            return w_range.w_stop
+    if space.is_true(space.eq(w_attr, space.wrap('step'))):
+        if w_range.w_step is None:
+            return space.w_None
+        else:
+            return w_range.w_step
+    if space.is_true(space.eq(w_attr, space.wrap('tolist'))):
+        w_builtinfn = make_builtin_func(space, W_XRangeObject.tolist)
+        return W_InstMethObject(space, w_builtinfn, w_range, w_range.statictype)
+    
+    raise FailedToImplement(space.w_AttributeError)
+
+def len__XRange(space, w_range):
+    stop = space.unwrap(w_range.w_stop)
+    start = space.unwrap(w_range.w_start)
+    step =  space.unwrap(w_range.w_step)
+    num = (stop-start)//step
+    return W_IntObject(space, num)
+
+def getitem__XRange_Int(space, w_range, w_index):
+    stop = space.unwrap(w_range.w_stop)
+    start = space.unwrap(w_range.w_start)
+    step =  space.unwrap(w_range.w_step)
+    num = (stop-start)//step
+    idx = w_index.intval
+    if idx < 0:
+        idx += num
+    if idx < 0 or idx >= num:
+        raise OperationError(space.w_IndexError,
+                             space.wrap("xrange index out of range"))
+    return W_IntObject(space, start+step*idx)
+
+def iter__XRange(space, w_range):
+    # Someday, I'll write a smarter iterator
+    # This is like the old 2.2 slow iterator, rather than the new
+    # 2.3 fast iterator
+    import iterobject
+    return iterobject.W_SeqIterObject(space, w_range)
+
+register_all(vars())
diff -urN -x.svn -x'*.pyc' src.old/pypy/objspace/std/xrangetype.py src/pypy/objspace/std/xrangetype.py
--- src.old/pypy/objspace/std/xrangetype.py	1970-01-01 00:00:00.000000000 +0000
+++ src/pypy/objspace/std/xrangetype.py	2003-07-08 08:55:04.000000000 +0000
@@ -0,0 +1,33 @@
+from pypy.objspace.std.objspace import *
+from typeobject import W_TypeObject
+
+class W_XRangeType(W_TypeObject):
+
+    typename = 'xrange'
+
+registerimplementation(W_XRangeType)
+
+
+def type_new__XRangeType_XRangeType_ANY_ANY(space, w_basetype, w_xrangetype, w_args, w_kwds):
+    if space.is_true(w_kwds):
+        raise OperationError(space.w_TypeError,
+                             space.wrap("no keyword arguments expected"))
+    args = space.unpackiterable(w_args)
+    start = space.w_None
+    stop = space.w_None
+    step = space.w_None
+    if len(args) == 1:
+        stop, = args
+    elif len(args) == 2:
+        start, stop = args
+    elif len(args) == 3:
+        start, stop, step = args        
+    elif len(args) > 3:
+        raise OperationError(space.w_TypeError,
+                             space.wrap("xrange() takes at most 3 arguments"))
+    else:
+        raise OperationError(space.w_TypeError,
+                             space.wrap("xrange() takes at least 1 argument"))
+    return space.newxrange(start, stop, step), True
+
+register_all(vars())

-- 
Moshe Zadka -- http://moshez.org/
Buffy: I don't like you hanging out with someone that... short.
Riley: Yeah, a lot of young people nowadays are experimenting with shortness.
Agile Programming Language -- http://www.python.org/


More information about the Pypy-dev mailing list