[pypy-svn] r15213 - in pypy/dist/pypy/module/__builtin__: . test

nik at codespeak.net nik at codespeak.net
Wed Jul 27 22:21:19 CEST 2005


Author: nik
Date: Wed Jul 27 22:21:18 2005
New Revision: 15213

Added:
   pypy/dist/pypy/module/__builtin__/test/test_buffer.py
Modified:
   pypy/dist/pypy/module/__builtin__/app_buffer.py
Log:
added unicode support to buffer type (in conformance with CPython 2.4.1).
still missing: array support for buffer.


Modified: pypy/dist/pypy/module/__builtin__/app_buffer.py
==============================================================================
--- pypy/dist/pypy/module/__builtin__/app_buffer.py	(original)
+++ pypy/dist/pypy/module/__builtin__/app_buffer.py	Wed Jul 27 22:21:18 2005
@@ -1,4 +1,6 @@
 # Might probably be deprecated in Python at some point.
+import sys
+from struct import pack, unpack
 
 class buffer(object):
     """buffer(object [, offset[, size]])
@@ -12,6 +14,15 @@
     def __init__(self, object, offset=0, size=None):
         if isinstance(object, str):
             pass
+        elif isinstance(object, unicode):
+            str_object = ""
+            if sys.maxunicode == 65535:
+                pack_code = "H"
+            else:
+                pack_code = "I"
+            for char in object:
+                str_object += pack(pack_code, ord(char))
+            object = str_object
         elif isinstance(object, buffer):
             object = object.buf
         else:

Added: pypy/dist/pypy/module/__builtin__/test/test_buffer.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/module/__builtin__/test/test_buffer.py	Wed Jul 27 22:21:18 2005
@@ -0,0 +1,21 @@
+"""Tests some behaviour of the buffer type that is not tested in
+lib-python/2.4.1/test/test_types.py where the stdlib buffer tests live."""
+import autopath
+
+class AppTestBuffer:
+
+    def test_unicode_buffer(self):
+        import sys
+        b = buffer(u"ab")
+        if sys.maxunicode == 65535: # UCS2 build
+            assert len(b) == 4
+            if sys.byteorder == "big":
+                assert b[0:4] == "\x00a\x00b"
+            else:
+                assert b[0:4] == "a\x00b\x00"
+        else: # UCS4 build
+            assert len(b) == 8
+            if sys.byteorder == "big":
+                assert b[0:8] == "\x00\x00\x00a\x00\x00\x00b"
+            else:
+                assert b[0:8] == "a\x00\x00\x00b\x00\x00\x00"



More information about the Pypy-commit mailing list