[pypy-svn] r52276 - in pypy/branch/buffer/pypy: interpreter module/__builtin__/test

arigo at codespeak.net arigo at codespeak.net
Fri Mar 7 20:30:37 CET 2008


Author: arigo
Date: Fri Mar  7 20:30:37 2008
New Revision: 52276

Modified:
   pypy/branch/buffer/pypy/interpreter/buffer.py
   pypy/branch/buffer/pypy/module/__builtin__/test/test_buffer.py
Log:
buffer.__mul__, buffer.__rmul__.


Modified: pypy/branch/buffer/pypy/interpreter/buffer.py
==============================================================================
--- pypy/branch/buffer/pypy/interpreter/buffer.py	(original)
+++ pypy/branch/buffer/pypy/interpreter/buffer.py	Fri Mar  7 20:30:37 2008
@@ -96,6 +96,14 @@
         return space.wrap(hash(self.as_str()))
     descr_hash.unwrap_spec = ['self', ObjSpace]
 
+    def descr_mul(self, space, w_times):
+        # xxx not the most efficient implementation
+        w_string = space.wrap(self.as_str())
+        # use the __mul__ method instead of space.mul() so that we
+        # return NotImplemented instead of raising a TypeError
+        return space.call_method(w_string, '__mul__', w_times)
+    descr_mul.unwrap_spec = ['self', ObjSpace, W_Root]
+
 
 def descr_buffer__new__(space, w_subtype, w_object):  #, offset, size
     # w_subtype can only be exactly 'buffer' for now
@@ -131,6 +139,8 @@
     __gt__ = interp2app(Buffer.descr_gt),
     __ge__ = interp2app(Buffer.descr_ge),
     __hash__ = interp2app(Buffer.descr_hash),
+    __mul__ = interp2app(Buffer.descr_mul),
+    __rmul__ = interp2app(Buffer.descr_mul),
     )
 Buffer.typedef.acceptable_as_base_class = False
 

Modified: pypy/branch/buffer/pypy/module/__builtin__/test/test_buffer.py
==============================================================================
--- pypy/branch/buffer/pypy/module/__builtin__/test/test_buffer.py	(original)
+++ pypy/branch/buffer/pypy/module/__builtin__/test/test_buffer.py	Fri Mar  7 20:30:37 2008
@@ -58,3 +58,9 @@
 
     def test_hash(self):
         assert hash(buffer('hello')) == hash('hello')
+
+    def test_mul(self):
+        assert buffer('ab') * 5 == 'ababababab'
+        assert buffer('ab') * (-2) == ''
+        assert 5 * buffer('ab') == 'ababababab'
+        assert (-2) * buffer('ab') == ''



More information about the Pypy-commit mailing list