[pypy-commit] pypy py3k: bytes() now accept a list of integers

amauryfa noreply at buildbot.pypy.org
Thu Oct 13 22:40:19 CEST 2011


Author: Amaury Forgeot d'Arc <amauryfa at gmail.com>
Branch: py3k
Changeset: r48026:897c563f0090
Date: 2011-10-13 22:38 +0200
http://bitbucket.org/pypy/pypy/changeset/897c563f0090/

Log:	bytes() now accept a list of integers

diff --git a/pypy/objspace/std/bytearrayobject.py b/pypy/objspace/std/bytearrayobject.py
--- a/pypy/objspace/std/bytearrayobject.py
+++ b/pypy/objspace/std/bytearrayobject.py
@@ -22,10 +22,8 @@
 from pypy.interpreter import gateway
 from pypy.interpreter.argument import Signature
 from pypy.interpreter.buffer import RWBuffer
-from pypy.objspace.std.bytearraytype import (
-    makebytearraydata_w, getbytevalue,
-    new_bytearray
-)
+from pypy.objspace.std.stringtype import makebytesdata_w, getbytevalue
+from pypy.objspace.std.bytearraytype import new_bytearray
 from pypy.tool.sourcetools import func_with_new_name
 
 
@@ -81,7 +79,7 @@
         w_bytearray.data = ['\0'] * count
         return
 
-    data = makebytearraydata_w(space, w_source)
+    data = makebytesdata_w(space, w_source)
     w_bytearray.data = data
 
 def len__Bytearray(space, w_bytearray):
@@ -519,7 +517,7 @@
     w_str = str__Bytearray(space, w_bytearray)
     w_result = stringobject.str_splitlines__String_ANY(space, w_str, w_keepends)
     return space.newlist([
-        new_bytearray(space, space.w_bytearray, makebytearraydata_w(space, w_entry))
+        new_bytearray(space, space.w_bytearray, makebytesdata_w(space, w_entry))
                         for w_entry in space.unpackiterable(w_result)
     ])
 
@@ -576,7 +574,7 @@
     w_bytearray.data += w_other.data
 
 def list_extend__Bytearray_ANY(space, w_bytearray, w_other):
-    w_bytearray.data += makebytearraydata_w(space, w_other)
+    w_bytearray.data += makebytesdata_w(space, w_other)
 
 def inplace_add__Bytearray_Bytearray(space, w_bytearray1, w_bytearray2):
     list_extend__Bytearray_Bytearray(space, w_bytearray1, w_bytearray2)
@@ -598,7 +596,7 @@
 def setitem__Bytearray_Slice_ANY(space, w_bytearray, w_slice, w_other):
     oldsize = len(w_bytearray.data)
     start, stop, step, slicelength = w_slice.indices4(space, oldsize)
-    sequence2 = makebytearraydata_w(space, w_other)
+    sequence2 = makebytesdata_w(space, w_other)
     setitem_slice_helper(space, w_bytearray.data, start, step, slicelength, sequence2, empty_elem='\x00')
 
 def delitem__Bytearray_ANY(space, w_bytearray, w_idx):
diff --git a/pypy/objspace/std/bytearraytype.py b/pypy/objspace/std/bytearraytype.py
--- a/pypy/objspace/std/bytearraytype.py
+++ b/pypy/objspace/std/bytearraytype.py
@@ -51,21 +51,6 @@
                     "bytes contained in the argument.\nIf the argument is "
                     "omitted, strip trailing ASCII whitespace.")
 
-def getbytevalue(space, w_value):
-    if space.isinstance_w(w_value, space.w_str):
-        string = space.str_w(w_value)
-        if len(string) != 1:
-            raise OperationError(space.w_ValueError, space.wrap(
-                "string must be of size 1"))
-        return string[0]
-
-    value = space.getindex_w(w_value, None)
-    if not 0 <= value < 256:
-        # this includes the OverflowError in case the long is too large
-        raise OperationError(space.w_ValueError, space.wrap(
-            "byte must be in range(0, 256)"))
-    return chr(value)
-
 def new_bytearray(space, w_bytearraytype, data):
     from pypy.objspace.std.bytearrayobject import W_BytearrayObject
     w_obj = space.allocate_instance(W_BytearrayObject, w_bytearraytype)
@@ -77,30 +62,6 @@
     return new_bytearray(space,w_bytearraytype, [])
 
 
-def makebytearraydata_w(space, w_source):
-    # String-like argument
-    try:
-        string = space.bufferstr_new_w(w_source)
-    except OperationError, e:
-        if not e.match(space, space.w_TypeError):
-            raise
-    else:
-        return [c for c in string]
-
-    # sequence of bytes
-    data = []
-    w_iter = space.iter(w_source)
-    while True:
-        try:
-            w_item = space.next(w_iter)
-        except OperationError, e:
-            if not e.match(space, space.w_StopIteration):
-                raise
-            break
-        value = getbytevalue(space, w_item)
-        data.append(value)
-    return data
-
 def descr_bytearray__reduce__(space, w_self):
     from pypy.objspace.std.bytearrayobject import W_BytearrayObject
     assert isinstance(w_self, W_BytearrayObject)
diff --git a/pypy/objspace/std/stringobject.py b/pypy/objspace/std/stringobject.py
--- a/pypy/objspace/std/stringobject.py
+++ b/pypy/objspace/std/stringobject.py
@@ -385,7 +385,7 @@
         if self and i != 0:
             sb.append(self)
         sb.append(space.str_w(list_w[i]))
-    return space.wrap(sb.build())
+    return space.wrapbytes(sb.build())
 
 def str_rjust__String_ANY_ANY(space, w_self, w_arg, w_fillchar):
     u_arg = space.int_w(w_arg)
diff --git a/pypy/objspace/std/stringtype.py b/pypy/objspace/std/stringtype.py
--- a/pypy/objspace/std/stringtype.py
+++ b/pypy/objspace/std/stringtype.py
@@ -1,8 +1,8 @@
 from pypy.interpreter import gateway
+from pypy.interpreter.error import OperationError
 from pypy.objspace.std.stdtypedef import StdTypeDef, SMM
 from pypy.objspace.std.register_all import register_all
 
-
 from sys import maxint
 from pypy.rlib.objectmodel import specialize
 from pypy.rlib.jit import we_are_jitted
@@ -264,20 +264,50 @@
 
 # ____________________________________________________________
 
-def descr__new__(space, w_stringtype, w_object=''):
-    # NB. the default value of w_object is really a *wrapped* empty string:
-    #     there is gateway magic at work
-    from pypy.objspace.std.stringobject import W_StringObject
-    w_obj = space.str(w_object)
-    if space.is_w(w_stringtype, space.w_str):
-        return w_obj  # XXX might be reworked when space.str() typechecks
-    value = space.str_w(w_obj)
+def getbytevalue(space, w_value):
+    value = space.getindex_w(w_value, None)
+    if not 0 <= value < 256:
+        # this includes the OverflowError in case the long is too large
+        raise OperationError(space.w_ValueError, space.wrap(
+            "byte must be in range(0, 256)"))
+    return chr(value)
+
+def makebytesdata_w(space, w_source):
+    # String-like argument
+    try:
+        string = space.bufferstr_new_w(w_source)
+    except OperationError, e:
+        if not e.match(space, space.w_TypeError):
+            raise
+    else:
+        return [c for c in string]
+
+    # sequence of bytes
+    data = []
+    w_iter = space.iter(w_source)
+    while True:
+        try:
+            w_item = space.next(w_iter)
+        except OperationError, e:
+            if not e.match(space, space.w_StopIteration):
+                raise
+            break
+        value = getbytevalue(space, w_item)
+        data.append(value)
+    return data
+
+def descr__new__(space, w_stringtype, w_source=gateway.NoneNotWrapped):
+    if (w_source and space.is_w(space.type(w_source), space.w_bytes) and
+        space.is_w(w_stringtype, space.w_bytes)):
+        return w_source
+    value = ''.join(makebytesdata_w(space, w_source))
     if space.config.objspace.std.withrope:
         from pypy.objspace.std.ropeobject import rope, W_RopeObject
         w_obj = space.allocate_instance(W_RopeObject, w_stringtype)
         W_RopeObject.__init__(w_obj, rope.LiteralStringNode(value))
         return w_obj
     else:
+        from pypy.objspace.std.stringobject import W_StringObject
         w_obj = space.allocate_instance(W_StringObject, w_stringtype)
         W_StringObject.__init__(w_obj, value)
         return w_obj


More information about the pypy-commit mailing list