Author: Armin Rigo <[email protected]>
Branch: len_w
Changeset: r92512:6bb19d21ddea
Date: 2017-09-29 19:46 +0200
http://bitbucket.org/pypy/pypy/changeset/6bb19d21ddea/

Log:    The idea is to do this change, to make space.len_w() actually not
        allocate a temporary W_IntObject even if the call to space.len_w()
        is not JITted.

diff --git a/pypy/interpreter/baseobjspace.py b/pypy/interpreter/baseobjspace.py
--- a/pypy/interpreter/baseobjspace.py
+++ b/pypy/interpreter/baseobjspace.py
@@ -328,6 +328,11 @@
         raise oefmt(space.w_TypeError,
                     "ord() expected string of length 1, but %T found", self)
 
+    def len_w(self, space):
+        # NOTE: you still need to override __len__ in your specific
+        # subclass' typedef; this is only here for optimization.
+        return space.int_w(space.len(self))
+
     def spacebind(self, space):
         """ Return a version of the object bound to a specific object space
         instance. This is used for objects (like e.g. TypeDefs) that are
@@ -800,7 +805,7 @@
 
     def len_w(self, w_obj):
         """shortcut for space.int_w(space.len(w_obj))"""
-        return self.int_w(self.len(w_obj))
+        return w_obj.len_w(self)
 
     def contains_w(self, w_container, w_item):
         """shortcut for space.is_true(space.contains(w_container, w_item))"""
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
@@ -308,6 +308,9 @@
     def length(self):
         return self.strategy.length(self)
 
+    def len_w(self, space):
+        return self.length()
+
     def getitem(self, index):
         """Returns the wrapped object that is found in the
         list at the given index. The index must be unwrapped.
diff --git a/pypy/objspace/std/tupleobject.py b/pypy/objspace/std/tupleobject.py
--- a/pypy/objspace/std/tupleobject.py
+++ b/pypy/objspace/std/tupleobject.py
@@ -283,6 +283,9 @@
     def length(self):
         return len(self.wrappeditems)
 
+    def len_w(self, space):
+        return self.length()
+
     def descr_hash(self, space):
         if _unroll_condition(self):
             return self._descr_hash_unroll(space)
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to