[pypy-svn] pypy default: (mfoord, holger) unify implementation of bytearray extend/new (thanks amaury), also fix line endings another time
hpk42
commits-noreply at bitbucket.org
Mon Jan 17 19:27:53 CET 2011
Author: holger krekel <holger at merlinux.eu>
Branch:
Changeset: r40813:f39cefd9cd81
Date: 2011-01-17 19:27 +0100
http://bitbucket.org/pypy/pypy/changeset/f39cefd9cd81/
Log: (mfoord, holger) unify implementation of bytearray extend/new
(thanks amaury), also fix line endings another time
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
@@ -5,8 +5,8 @@
from pypy.objspace.std.multimethod import FailedToImplement
from pypy.rlib.rarithmetic import intmask
from pypy.rlib.rstring import StringBuilder
-from pypy.objspace.std.intobject import W_IntObject
-from pypy.objspace.std.listobject import _delitem_slice_helper
+from pypy.objspace.std.intobject import W_IntObject
+from pypy.objspace.std.listobject import _delitem_slice_helper
from pypy.objspace.std.listtype import get_list_index
from pypy.objspace.std.stringobject import W_StringObject
from pypy.objspace.std.unicodeobject import W_UnicodeObject
@@ -14,9 +14,9 @@
from pypy.objspace.std import slicetype
from pypy.interpreter import gateway
from pypy.interpreter.buffer import RWBuffer
-
-from pypy.tool.sourcetools import func_with_new_name
-
+from pypy.objspace.std.bytearraytype import makebytearraydata_w
+from pypy.tool.sourcetools import func_with_new_name
+
class W_BytearrayObject(W_Object):
from pypy.objspace.std.bytearraytype import bytearray_typedef as typedef
@@ -380,36 +380,8 @@
def list_extend__Bytearray_ANY(space, w_bytearray, w_other):
if space.isinstance_w(w_other, space.w_unicode):
raise OperationError(space.w_TypeError, space.wrap(
- "bytes string or buffer expected"))
- try:
- other = space.bufferstr_w(w_other)
- except OperationError, e:
- if not e.match(space, space.w_TypeError):
- raise
- # We now try treating w_other as an iterable
- l = []
- for w_item in space.unpackiterable(w_other):
- if space.isinstance_w(w_item, space.w_str):
- res = space.str_w(w_item)
- if len(res) != 1:
- raise OperationError(
- space.w_ValueError,
- space.wrap("string must be of size 1")
- )
- else:
- i = space.int_w(w_item)
- try:
- res = chr(i)
- except ValueError:
- raise OperationError(
- space.w_ValueError,
- space.wrap("byte must be in range(0, 256)")
- )
-
- l.append(res)
- w_bytearray.data += l
- else:
- w_bytearray.data += [c for c in other]
+ "bytes string or buffer expected"))
+ w_bytearray.data += makebytearraydata_w(space, w_other)
def inplace_add__Bytearray_Bytearray(space, w_bytearray1, w_bytearray2):
list_extend__Bytearray_Bytearray(space, w_bytearray1, w_bytearray2)
@@ -443,24 +415,24 @@
space.wrap("fixme: only step=1 for the moment"))
_setitem_helper(w_bytearray, start, stop, slicelength,
space.str_w(w_other))
-
-def delitem__Bytearray_ANY(space, w_bytearray, w_idx):
- idx = get_list_index(space, w_idx)
- try:
- del w_bytearray.data[idx]
- except IndexError:
- raise OperationError(space.w_IndexError,
- space.wrap("bytearray deletion index out of range"))
- return space.w_None
-
-def delitem__Bytearray_Slice(space, w_bytearray, w_slice):
- start, stop, step, slicelength = w_slice.indices4(space,
- len(w_bytearray.data))
- delitem_slice_helper(space, w_bytearray.data, start, step, slicelength)
-
-# create new helper function with different list type specialisation
-delitem_slice_helper = func_with_new_name(_delitem_slice_helper,
- 'delitem_slice_helper')
+
+def delitem__Bytearray_ANY(space, w_bytearray, w_idx):
+ idx = get_list_index(space, w_idx)
+ try:
+ del w_bytearray.data[idx]
+ except IndexError:
+ raise OperationError(space.w_IndexError,
+ space.wrap("bytearray deletion index out of range"))
+ return space.w_None
+
+def delitem__Bytearray_Slice(space, w_bytearray, w_slice):
+ start, stop, step, slicelength = w_slice.indices4(space,
+ len(w_bytearray.data))
+ delitem_slice_helper(space, w_bytearray.data, start, step, slicelength)
+
+# create new helper function with different list type specialisation
+delitem_slice_helper = func_with_new_name(_delitem_slice_helper,
+ 'delitem_slice_helper')
def _setitem_helper(w_bytearray, start, stop, slicelength, data):
assert start >= 0
@@ -506,4 +478,4 @@
return space.wrap(b)
from pypy.objspace.std import bytearraytype
-register_all(vars(), bytearraytype)
\ No newline at end of file
+register_all(vars(), bytearraytype)
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
@@ -42,7 +42,6 @@
@gateway.unwrap_spec(ObjSpace, W_Root, W_Root, W_Root, W_Root)
def descr__new__(space, w_bytearraytype,
w_source='', w_encoding=None, w_errors=None):
- data = []
# Unicode argument
if not space.is_w(w_encoding, space.w_None):
from pypy.objspace.std.unicodetype import (
@@ -55,16 +54,6 @@
# ours is: "expected unicode, got int object"
w_source = encode_object(space, w_source, encoding, errors)
- # String-like argument
- try:
- string = space.str_w(w_source)
- except OperationError, e:
- if not e.match(space, space.w_TypeError):
- raise
- else:
- data = [c for c in string]
- return new_bytearray(space, w_bytearraytype, data)
-
# Is it an int?
try:
count = space.int_w(w_source)
@@ -75,7 +64,21 @@
data = ['\0'] * count
return new_bytearray(space, w_bytearraytype, data)
+ data = makebytearraydata_w(space, w_source)
+ return new_bytearray(space, w_bytearraytype, data)
+
+def makebytearraydata_w(space, w_source):
+ # String-like argument
+ try:
+ string = space.str_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:
@@ -86,8 +89,7 @@
break
value = getbytevalue(space, w_item)
data.append(value)
-
- return new_bytearray(space, w_bytearraytype, data)
+ return data
@gateway.unwrap_spec(gateway.ObjSpace, gateway.W_Root)
def descr_bytearray__reduce__(space, w_self):
More information about the Pypy-commit
mailing list