Author: Richard Plangger <[email protected]>
Branch: memoryview-attributes
Changeset: r86414:4cd49fa0d0d9
Date: 2016-08-22 12:06 +0200
http://bitbucket.org/pypy/pypy/changeset/4cd49fa0d0d9/

Log:    renamed W_StringBufferObject to W_StringBuilderObject (really a
        string builder is passed), the new object W_StringBufferObject now
        takes and wrapps a rlib.buffer:Buffer object!

diff --git a/pypy/doc/objspace.rst b/pypy/doc/objspace.rst
--- a/pypy/doc/objspace.rst
+++ b/pypy/doc/objspace.rst
@@ -431,7 +431,7 @@
 :source:`pypy/objspace/std/bytesobject.py` defines ``W_AbstractBytesObject``,
 which contains everything needed to build the ``str`` app-level type;
 and there are subclasses ``W_BytesObject`` (the usual string) and
-``W_StringBufferObject`` (a special implementation tweaked for repeated
+``W_StringBuilderObject`` (a special implementation tweaked for repeated
 additions, in :source:`pypy/objspace/std/strbufobject.py`).  For mutable data
 types like lists and dictionaries, we have a single class
 ``W_ListObject`` or ``W_DictMultiObject`` which has an indirection to
diff --git a/pypy/objspace/std/bytesobject.py b/pypy/objspace/std/bytesobject.py
--- a/pypy/objspace/std/bytesobject.py
+++ b/pypy/objspace/std/bytesobject.py
@@ -603,8 +603,8 @@
 
     def descr_eq(self, space, w_other):
         if space.config.objspace.std.withstrbuf:
-            from pypy.objspace.std.strbufobject import W_StringBufferObject
-            if isinstance(w_other, W_StringBufferObject):
+            from pypy.objspace.std.strbufobject import W_StringBuilderObject
+            if isinstance(w_other, W_StringBuilderObject):
                 return space.newbool(self._value == w_other.force())
         if not isinstance(w_other, W_BytesObject):
             return space.w_NotImplemented
@@ -612,8 +612,8 @@
 
     def descr_ne(self, space, w_other):
         if space.config.objspace.std.withstrbuf:
-            from pypy.objspace.std.strbufobject import W_StringBufferObject
-            if isinstance(w_other, W_StringBufferObject):
+            from pypy.objspace.std.strbufobject import W_StringBuilderObject
+            if isinstance(w_other, W_StringBuilderObject):
                 return space.newbool(self._value != w_other.force())
         if not isinstance(w_other, W_BytesObject):
             return space.w_NotImplemented
@@ -621,8 +621,8 @@
 
     def descr_lt(self, space, w_other):
         if space.config.objspace.std.withstrbuf:
-            from pypy.objspace.std.strbufobject import W_StringBufferObject
-            if isinstance(w_other, W_StringBufferObject):
+            from pypy.objspace.std.strbufobject import W_StringBuilderObject
+            if isinstance(w_other, W_StringBuilderObject):
                 return space.newbool(self._value < w_other.force())
         if not isinstance(w_other, W_BytesObject):
             return space.w_NotImplemented
@@ -630,8 +630,8 @@
 
     def descr_le(self, space, w_other):
         if space.config.objspace.std.withstrbuf:
-            from pypy.objspace.std.strbufobject import W_StringBufferObject
-            if isinstance(w_other, W_StringBufferObject):
+            from pypy.objspace.std.strbufobject import W_StringBuilderObject
+            if isinstance(w_other, W_StringBuilderObject):
                 return space.newbool(self._value <= w_other.force())
         if not isinstance(w_other, W_BytesObject):
             return space.w_NotImplemented
@@ -639,8 +639,8 @@
 
     def descr_gt(self, space, w_other):
         if space.config.objspace.std.withstrbuf:
-            from pypy.objspace.std.strbufobject import W_StringBufferObject
-            if isinstance(w_other, W_StringBufferObject):
+            from pypy.objspace.std.strbufobject import W_StringBuilderObject
+            if isinstance(w_other, W_StringBuilderObject):
                 return space.newbool(self._value > w_other.force())
         if not isinstance(w_other, W_BytesObject):
             return space.w_NotImplemented
@@ -648,8 +648,8 @@
 
     def descr_ge(self, space, w_other):
         if space.config.objspace.std.withstrbuf:
-            from pypy.objspace.std.strbufobject import W_StringBufferObject
-            if isinstance(w_other, W_StringBufferObject):
+            from pypy.objspace.std.strbufobject import W_StringBuilderObject
+            if isinstance(w_other, W_StringBuilderObject):
                 return space.newbool(self._value >= w_other.force())
         if not isinstance(w_other, W_BytesObject):
             return space.w_NotImplemented
@@ -669,7 +669,7 @@
             self_as_bytearray = W_BytearrayObject(_make_data(self._value))
             return space.add(self_as_bytearray, w_other)
         if space.config.objspace.std.withstrbuf:
-            from pypy.objspace.std.strbufobject import W_StringBufferObject
+            from pypy.objspace.std.strbufobject import W_StringBuilderObject
             try:
                 other = self._op_val(space, w_other)
             except OperationError as e:
@@ -679,7 +679,7 @@
             builder = StringBuilder()
             builder.append(self._value)
             builder.append(other)
-            return W_StringBufferObject(builder)
+            return W_StringBuilderObject(builder)
         return self._StringMethods_descr_add(space, w_other)
 
     _StringMethods__startswith = _startswith
diff --git a/pypy/objspace/std/strbufobject.py 
b/pypy/objspace/std/strbufobject.py
--- a/pypy/objspace/std/strbufobject.py
+++ b/pypy/objspace/std/strbufobject.py
@@ -8,8 +8,34 @@
 from pypy.interpreter.error import OperationError
 from rpython.rlib.rstring import StringBuilder
 
+class W_StringBufferObject(W_AbstractBytesObject):
+    # this wraps an rpython/rlib/buffer.py:Buffer object, not a StringBuilder
+    # NOTE the W_StringBuilderObject *was* named W_StringBufferObject
+    w_str = None
 
-class W_StringBufferObject(W_AbstractBytesObject):
+    def __init__(self, buffer):
+        self.buffer = buffer # Buffer
+        self.length = buffer.getlength()
+
+    def __repr__(self):
+        """ representation for debugging purposes """
+        return "%s(%r[:%d])" % (
+            self.__class__.__name__, self.buffer, self.length)
+
+    def unwrap(self, space):
+        return self.buffer.value
+
+    def str_w(self, space):
+        return self.buffer.value
+
+    def buffer_w(self, space, flags):
+        return self.buffer
+
+    def readbuf_w(self, space):
+        return self.buffer
+
+
+class W_StringBuilderObject(W_AbstractBytesObject):
     w_str = None
 
     def __init__(self, builder):
@@ -59,11 +85,11 @@
         else:
             builder = self.builder
         builder.append(other)
-        return W_StringBufferObject(builder)
+        return W_StringBuilderObject(builder)
 
     def descr_str(self, space):
-        # you cannot get subclasses of W_StringBufferObject here
-        assert type(self) is W_StringBufferObject
+        # you cannot get subclasses of W_StringBuilderObject here
+        assert type(self) is W_StringBuilderObject
         return self
 
 
@@ -94,6 +120,6 @@
     unwrap_spec_ = getattr(func, 'unwrap_spec', None)
     if unwrap_spec_ is not None:
         f = unwrap_spec(**unwrap_spec_)(f)
-    setattr(W_StringBufferObject, func.func_name, f)
+    setattr(W_StringBuilderObject, func.func_name, f)
 
-W_StringBufferObject.typedef = W_BytesObject.typedef
+W_StringBuilderObject.typedef = W_BytesObject.typedef
diff --git a/pypy/objspace/std/test/test_strbufobject.py 
b/pypy/objspace/std/test/test_strbufobject.py
--- a/pypy/objspace/std/test/test_strbufobject.py
+++ b/pypy/objspace/std/test/test_strbufobject.py
@@ -11,7 +11,7 @@
         # away on AST level
         s = "Hello, ".__add__("World!")
         assert type(s) is str
-        assert 'W_StringBufferObject' in __pypy__.internal_repr(s)
+        assert 'W_StringBuilderObject' in __pypy__.internal_repr(s)
 
     def test_add_twice(self):
         x = "a".__add__("b")
@@ -25,7 +25,7 @@
         all = ""
         for i in range(20):
             all += str(i)
-        assert 'W_StringBufferObject' in __pypy__.internal_repr(all)
+        assert 'W_StringBuilderObject' in __pypy__.internal_repr(all)
         assert all == "012345678910111213141516171819"
 
     def test_hash(self):
@@ -33,7 +33,7 @@
         def join(s): return s[:len(s) // 2] + s[len(s) // 2:]
         t = 'a' * 101
         s = join(t)
-        assert 'W_StringBufferObject' in __pypy__.internal_repr(s)
+        assert 'W_StringBuilderObject' in __pypy__.internal_repr(s)
         assert hash(s) == hash(t)
 
     def test_len(self):
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to