[pypy-svn] r52327 - in pypy/branch/buffer/pypy/objspace/std: . test

arigo at codespeak.net arigo at codespeak.net
Sun Mar 9 12:46:26 CET 2008


Author: arigo
Date: Sun Mar  9 12:46:23 2008
New Revision: 52327

Modified:
   pypy/branch/buffer/pypy/objspace/std/ropeobject.py
   pypy/branch/buffer/pypy/objspace/std/ropeunicodeobject.py
   pypy/branch/buffer/pypy/objspace/std/test/test_stringobject.py
   pypy/branch/buffer/pypy/objspace/std/test/test_unicodeobject.py
Log:
Buffer support for the ropes.  Should be improved at some point
for string ropes.


Modified: pypy/branch/buffer/pypy/objspace/std/ropeobject.py
==============================================================================
--- pypy/branch/buffer/pypy/objspace/std/ropeobject.py	(original)
+++ pypy/branch/buffer/pypy/objspace/std/ropeobject.py	Sun Mar  9 12:46:23 2008
@@ -842,6 +842,11 @@
     encoding, errors = _get_encoding_and_errors(space, w_encoding, w_errors)
     return encode_object(space, w_string, encoding, errors)
 
+def buffer__Rope(space, w_string):
+    from pypy.interpreter.buffer import StringBuffer
+    value = w_string._node.flatten_string()      # XXX inefficient
+    return space.wrap(StringBuffer(value))
+
 
 # methods of the iterator
 

Modified: pypy/branch/buffer/pypy/objspace/std/ropeunicodeobject.py
==============================================================================
--- pypy/branch/buffer/pypy/objspace/std/ropeunicodeobject.py	(original)
+++ pypy/branch/buffer/pypy/objspace/std/ropeunicodeobject.py	Sun Mar  9 12:46:23 2008
@@ -909,6 +909,18 @@
 def mod__RopeUnicode_ANY(space, w_format, w_values):
     return mod_format(space, w_format, w_values, do_unicode=True)
 
+def buffer__RopeUnicode(space, w_unicode):
+    # xxx this is a slightly strange thing...
+    from pypy.module.struct.unichar import pack_unichar
+    charlist = []
+    node = w_unicode._node
+    iter = rope.ItemIterator(node)
+    for idx in range(node.length()):
+        unich = unichr(iter.nextint())
+        pack_unichar(unich, charlist)
+    from pypy.interpreter.buffer import StringBuffer
+    return space.wrap(StringBuffer(''.join(charlist)))
+
 
 # methods of the iterator
 

Modified: pypy/branch/buffer/pypy/objspace/std/test/test_stringobject.py
==============================================================================
--- pypy/branch/buffer/pypy/objspace/std/test/test_stringobject.py	(original)
+++ pypy/branch/buffer/pypy/objspace/std/test/test_stringobject.py	Sun Mar  9 12:46:23 2008
@@ -657,6 +657,15 @@
         assert hash('hello') & 0x7fffffff == 0x347697fd
         assert hash('hello world!') & 0x7fffffff == 0x2f0bb411
 
+    def test_buffer(self):
+        x = "he"
+        x += "llo"
+        b = buffer(x)
+        assert len(b) == 5
+        assert b[-1] == "o"
+        assert b[:] == "hello"
+        raises(TypeError, "b[3] = 'x'")
+
     def test_getnewargs(self):
         assert  "foo".__getnewargs__() == ("foo",)
 

Modified: pypy/branch/buffer/pypy/objspace/std/test/test_unicodeobject.py
==============================================================================
--- pypy/branch/buffer/pypy/objspace/std/test/test_unicodeobject.py	(original)
+++ pypy/branch/buffer/pypy/objspace/std/test/test_unicodeobject.py	Sun Mar  9 12:46:23 2008
@@ -547,3 +547,10 @@
 
     def test_swapcase(self):
         assert u'\xe4\xc4\xdf'.swapcase() == u'\xc4\xe4\xdf'
+
+    def test_buffer(self):
+        buf = buffer(u'XY')
+        assert str(buf) in ['X\x00Y\x00',
+                            '\x00X\x00Y',
+                            'X\x00\x00\x00Y\x00\x00\x00',
+                            '\x00\x00\x00X\x00\x00\x00Y']



More information about the Pypy-commit mailing list