[pypy-commit] pypy length-hint: move length_hint to space now that it's PEP'd

pjenvey noreply at buildbot.pypy.org
Fri Sep 14 21:24:59 CEST 2012


Author: Philip Jenvey <pjenvey at underboss.org>
Branch: length-hint
Changeset: r57352:737ed2bca4fc
Date: 2012-09-13 21:38 -0700
http://bitbucket.org/pypy/pypy/changeset/737ed2bca4fc/

Log:	move length_hint to space now that it's PEP'd

diff --git a/pypy/interpreter/baseobjspace.py b/pypy/interpreter/baseobjspace.py
--- a/pypy/interpreter/baseobjspace.py
+++ b/pypy/interpreter/baseobjspace.py
@@ -860,9 +860,8 @@
         list.
         """
         # If we can guess the expected length we can preallocate.
-        from pypy.objspace.std.iterobject import length_hint
         try:
-            items = newlist_hint(length_hint(self, w_iterable, 0))
+            items = newlist_hint(self.length_hint(w_iterable, 0))
         except MemoryError:
             items = [] # it might have lied
 
@@ -925,6 +924,31 @@
         return self._unpackiterable_known_length_jitlook(w_iterator,
                                                          expected_length)
 
+    def length_hint(self, w_obj, default):
+        """Return the length of an object, consulting its __length_hint__
+        method if necessary.
+        """
+        try:
+            return self.len_w(w_obj)
+        except OperationError, e:
+            if not (e.match(self, self.w_TypeError) or
+                    e.match(self, self.w_AttributeError)):
+                raise
+
+        w_descr = self.lookup(w_obj, '__length_hint__')
+        if w_descr is None:
+            return default
+        try:
+            w_hint = self.get_and_call_function(w_descr, w_obj)
+        except OperationError, e:
+            if not (e.match(self, self.w_TypeError) or
+                    e.match(self, self.w_AttributeError)):
+                raise
+            return default
+
+        hint = self.int_w(w_hint)
+        return default if hint < 0 else hint
+
     def fixedview(self, w_iterable, expected_length=-1):
         """ A fixed list view of w_iterable. Don't modify the result
         """
diff --git a/pypy/objspace/std/iterobject.py b/pypy/objspace/std/iterobject.py
--- a/pypy/objspace/std/iterobject.py
+++ b/pypy/objspace/std/iterobject.py
@@ -4,32 +4,6 @@
 from pypy.objspace.std.register_all import register_all
 
 
-def length_hint(space, w_obj, default):
-    """Return the length of an object, consulting its __length_hint__
-    method if necessary.
-    """
-    try:
-        return space.len_w(w_obj)
-    except OperationError, e:
-        if not (e.match(space, space.w_TypeError) or
-                e.match(space, space.w_AttributeError)):
-            raise
-
-    w_descr = space.lookup(w_obj, '__length_hint__')
-    if w_descr is None:
-        return default
-    try:
-        w_hint = space.get_and_call_function(w_descr, w_obj)
-    except OperationError, e:
-        if not (e.match(space, space.w_TypeError) or
-                e.match(space, space.w_AttributeError)):
-            raise
-        return default
-
-    hint = space.int_w(w_hint)
-    return default if hint < 0 else hint
-
-
 class W_AbstractIterObject(W_Object):
     __slots__ = ()
 
diff --git a/pypy/objspace/std/listobject.py b/pypy/objspace/std/listobject.py
--- a/pypy/objspace/std/listobject.py
+++ b/pypy/objspace/std/listobject.py
@@ -3,7 +3,6 @@
 from pypy.objspace.std.multimethod import FailedToImplement
 from pypy.interpreter.error import OperationError, operationerrfmt
 from pypy.objspace.std.inttype import wrapint
-from pypy.objspace.std.iterobject import length_hint
 from pypy.objspace.std.listtype import get_list_index
 from pypy.objspace.std.sliceobject import W_SliceObject, normalize_simple_slice
 from pypy.objspace.std import slicetype
@@ -802,7 +801,7 @@
         if isinstance(w_any, W_ListObject):
             self._extend_list(w_list, w_any)
         else:
-            newlen = w_list.length() + length_hint(self.space, w_any, 0)
+            newlen = w_list.length() + self.space.length_hint(w_any, 0)
             resizelist_hint(self.unerase(w_list.lstorage), newlen)
             for item in self.space.iteriterable(w_any):
                 w_list.append(item)
diff --git a/pypy/objspace/std/test/test_lengthhint.py b/pypy/objspace/std/test/test_lengthhint.py
--- a/pypy/objspace/std/test/test_lengthhint.py
+++ b/pypy/objspace/std/test/test_lengthhint.py
@@ -1,6 +1,3 @@
-from pypy.objspace.std.iterobject import length_hint
-
-
 class TestLengthHint:
 
     SIZE = 4
@@ -8,15 +5,15 @@
 
     def _test_length_hint(self, w_obj):
         space = self.space
-        assert length_hint(space, w_obj, 8) == self.SIZE
+        assert space.length_hint(w_obj, 8) == self.SIZE
 
         w_iter = space.iter(w_obj)
         assert space.int_w(
             space.call_method(w_iter, '__length_hint__')) == self.SIZE
-        assert length_hint(space, w_iter, 8) == self.SIZE
+        assert space.length_hint(w_iter, 8) == self.SIZE
 
         space.next(w_iter)
-        assert length_hint(space, w_iter, 8) == self.SIZE - 1
+        assert space.length_hint(w_iter, 8) == self.SIZE - 1
 
     def test_list(self):
         self._test_length_hint(self.space.newlist(self.ITEMS))
@@ -40,7 +37,7 @@
 
     def test_default(self):
         space = self.space
-        assert length_hint(space, space.w_False, 3) == 3
+        assert space.length_hint(space.w_False, 3) == 3
 
     def test_exc(self):
         from pypy.interpreter.error import OperationError
@@ -52,7 +49,7 @@
             return Foo()
         """)
         try:
-            length_hint(space, w_foo, 3)
+            space.length_hint(w_foo, 3)
         except OperationError, e:
             assert e.match(space, space.w_ZeroDivisionError)
         else:


More information about the pypy-commit mailing list