[pypy-svn] r77730 - in pypy/branch/fast-forward/pypy/module/__builtin__: . test

afa at codespeak.net afa at codespeak.net
Fri Oct 8 18:50:55 CEST 2010


Author: afa
Date: Fri Oct  8 18:50:53 2010
New Revision: 77730

Modified:
   pypy/branch/fast-forward/pypy/module/__builtin__/__init__.py
   pypy/branch/fast-forward/pypy/module/__builtin__/operation.py
   pypy/branch/fast-forward/pypy/module/__builtin__/test/test_buffer.py
Log:
Very very basic start of a memoryview
I can't get my mind around the PyPy's buffer interface.


Modified: pypy/branch/fast-forward/pypy/module/__builtin__/__init__.py
==============================================================================
--- pypy/branch/fast-forward/pypy/module/__builtin__/__init__.py	(original)
+++ pypy/branch/fast-forward/pypy/module/__builtin__/__init__.py	Fri Oct  8 18:50:53 2010
@@ -52,7 +52,8 @@
         'object'        : '(space.w_object)',
         'bytes'         : '(space.w_str)',
         'unicode'       : '(space.w_unicode)',
-        'buffer'        : 'operation.Buffer',
+        'buffer'        : 'operation.W_Buffer',
+        'memoryview'    : 'operation.W_Memoryview',
 
         'file'          : 'state.get(space).w_file',
         'open'          : 'state.get(space).w_file',

Modified: pypy/branch/fast-forward/pypy/module/__builtin__/operation.py
==============================================================================
--- pypy/branch/fast-forward/pypy/module/__builtin__/operation.py	(original)
+++ pypy/branch/fast-forward/pypy/module/__builtin__/operation.py	Fri Oct  8 18:50:53 2010
@@ -3,13 +3,32 @@
 """
 
 from pypy.interpreter import gateway, buffer
-from pypy.interpreter.baseobjspace import ObjSpace
+from pypy.interpreter.baseobjspace import ObjSpace, W_Root
 from pypy.interpreter.error import OperationError
+from pypy.interpreter.gateway import interp2app, unwrap_spec
+from pypy.interpreter.typedef import TypeDef
 from pypy.rlib.runicode import UNICHR
 import __builtin__
 NoneNotWrapped = gateway.NoneNotWrapped
 
-Buffer = buffer.Buffer
+W_Buffer = buffer.Buffer
+
+class W_Memoryview(buffer.StringLikeBuffer):
+    @unwrap_spec(ObjSpace, W_Root, W_Root)
+    def descr_new(space, w_subtype, w_object):
+        self = space.allocate_instance(W_Memoryview, w_subtype)
+        buffer.StringLikeBuffer.__init__(self, space, w_object)
+        return space.wrap(self)
+
+    @unwrap_spec('self', ObjSpace)
+    def to_bytes_w(self, space):
+        return space.wrap(self.as_str())
+
+W_Memoryview.typedef = TypeDef(
+    "memoryview",
+    __new__=interp2app(W_Memoryview.descr_new.im_func),
+    to_bytes=interp2app(W_Memoryview.to_bytes_w),
+    )
 
 def abs(space, w_val):
     "abs(number) -> number\n\nReturn the absolute value of the argument."

Modified: pypy/branch/fast-forward/pypy/module/__builtin__/test/test_buffer.py
==============================================================================
--- pypy/branch/fast-forward/pypy/module/__builtin__/test/test_buffer.py	(original)
+++ pypy/branch/fast-forward/pypy/module/__builtin__/test/test_buffer.py	Fri Oct  8 18:50:53 2010
@@ -159,3 +159,8 @@
 
         raises(ValueError, buffer, a, -1)
         raises(ValueError, buffer, a, 0, -2)
+
+class AppTestMemoryview:
+    def test_basic(self):
+        v = memoryview("abc")
+        assert v.to_bytes() == "abc"



More information about the Pypy-commit mailing list