[pypy-commit] pypy default: hg merge refactor-str-types: This branch removes multimethods on str/unicode/bytearray and makes these types share code.
Manuel Jacob
noreply at buildbot.pypy.org
Fri Jan 17 22:56:40 CET 2014
Author: Manuel Jacob
Branch:
Changeset: r68752:7bdc8396c973
Date: 2014-01-17 22:55 +0100
http://bitbucket.org/pypy/pypy/changeset/7bdc8396c973/
Log: hg merge refactor-str-types: This branch removes multimethods on
str/unicode/bytearray and makes these types share code.
diff too long, truncating to 2000 out of 9425 lines
diff --git a/lib-python/2.7/test/test_memoryview.py b/lib-python/2.7/test/test_memoryview.py
--- a/lib-python/2.7/test/test_memoryview.py
+++ b/lib-python/2.7/test/test_memoryview.py
@@ -166,11 +166,18 @@
self.assertTrue(m[0:6] == m[:])
self.assertFalse(m[0:5] == m)
- # Comparison with objects which don't support the buffer API
- self.assertFalse(m == u"abcdef")
- self.assertTrue(m != u"abcdef")
- self.assertFalse(u"abcdef" == m)
- self.assertTrue(u"abcdef" != m)
+ if test_support.check_impl_detail(cpython=True):
+ # what is supported and what is not supported by memoryview is
+ # very inconsisten on CPython. In PyPy, memoryview supports
+ # the buffer interface, and thus the following comparison
+ # succeeds. See also the comment in
+ # pypy.modules.__builtin__.interp_memoryview.W_MemoryView.descr_buffer
+ #
+ # Comparison with objects which don't support the buffer API
+ self.assertFalse(m == u"abcdef", "%s %s" % (self, tp))
+ self.assertTrue(m != u"abcdef")
+ self.assertFalse(u"abcdef" == m)
+ self.assertTrue(u"abcdef" != m)
# Unordered comparisons are unimplemented, and therefore give
# arbitrary results (they raise a TypeError in py3k)
diff --git a/pypy/interpreter/baseobjspace.py b/pypy/interpreter/baseobjspace.py
--- a/pypy/interpreter/baseobjspace.py
+++ b/pypy/interpreter/baseobjspace.py
@@ -231,6 +231,11 @@
msg = "__int__ returned non-int (type '%T')"
raise operationerrfmt(space.w_TypeError, msg, w_result)
+ def ord(self, space):
+ typename = space.type(self).getname(space)
+ msg = "ord() expected string of length 1, but %s found"
+ raise operationerrfmt(space.w_TypeError, msg, typename)
+
def __spacebind__(self, space):
return self
@@ -1396,6 +1401,9 @@
# This is here mostly just for gateway.int_unwrapping_space_method().
return bool(self.int_w(w_obj))
+ def ord(self, w_obj):
+ return w_obj.ord(self)
+
# This is all interface for gateway.py.
def gateway_int_w(self, w_obj):
if self.isinstance_w(w_obj, self.w_float):
diff --git a/pypy/interpreter/gateway.py b/pypy/interpreter/gateway.py
--- a/pypy/interpreter/gateway.py
+++ b/pypy/interpreter/gateway.py
@@ -520,12 +520,13 @@
# When a BuiltinCode is stored in a Function object,
# you get the functionality of CPython's built-in function type.
- def __init__(self, func, unwrap_spec=None, self_type=None, descrmismatch=None):
+ def __init__(self, func, unwrap_spec=None, self_type=None,
+ descrmismatch=None, doc=None):
"NOT_RPYTHON"
# 'implfunc' is the interpreter-level function.
# Note that this uses a lot of (construction-time) introspection.
Code.__init__(self, func.__name__)
- self.docstring = func.__doc__
+ self.docstring = doc or func.__doc__
self.identifier = "%s-%s-%s" % (func.__module__, func.__name__,
getattr(self_type, '__name__', '*'))
@@ -832,7 +833,7 @@
instancecache = {}
def __new__(cls, f, app_name=None, unwrap_spec=None, descrmismatch=None,
- as_classmethod=False):
+ as_classmethod=False, doc=None):
"NOT_RPYTHON"
# f must be a function whose name does NOT start with 'app_'
@@ -861,7 +862,8 @@
cls.instancecache[key] = self
self._code = BuiltinCode(f, unwrap_spec=unwrap_spec,
self_type=self_type,
- descrmismatch=descrmismatch)
+ descrmismatch=descrmismatch,
+ doc=doc)
self.__name__ = f.func_name
self.name = app_name
self.as_classmethod = as_classmethod
diff --git a/pypy/interpreter/test/test_gateway.py b/pypy/interpreter/test/test_gateway.py
--- a/pypy/interpreter/test/test_gateway.py
+++ b/pypy/interpreter/test/test_gateway.py
@@ -708,6 +708,18 @@
never_called
py.test.raises(AssertionError, space.wrap, gateway.interp2app_temp(g))
+ def test_interp2app_doc(self):
+ space = self.space
+ def f(space, w_x):
+ """foo"""
+ w_f = space.wrap(gateway.interp2app_temp(f))
+ assert space.unwrap(space.getattr(w_f, space.wrap('__doc__'))) == 'foo'
+ #
+ def g(space, w_x):
+ never_called
+ w_g = space.wrap(gateway.interp2app_temp(g, doc='bar'))
+ assert space.unwrap(space.getattr(w_g, space.wrap('__doc__'))) == 'bar'
+
class AppTestPyTestMark:
@py.test.mark.unlikely_to_exist
diff --git a/pypy/interpreter/unicodehelper.py b/pypy/interpreter/unicodehelper.py
--- a/pypy/interpreter/unicodehelper.py
+++ b/pypy/interpreter/unicodehelper.py
@@ -30,7 +30,7 @@
# ____________________________________________________________
def encode(space, w_data, encoding=None, errors='strict'):
- from pypy.objspace.std.unicodetype import encode_object
+ from pypy.objspace.std.unicodeobject import encode_object
return encode_object(space, w_data, encoding, errors)
# These functions take and return unwrapped rpython strings and unicodes
diff --git a/pypy/module/__builtin__/interp_memoryview.py b/pypy/module/__builtin__/interp_memoryview.py
--- a/pypy/module/__builtin__/interp_memoryview.py
+++ b/pypy/module/__builtin__/interp_memoryview.py
@@ -68,10 +68,14 @@
return W_MemoryView(buf)
def descr_buffer(self, space):
- """Note that memoryview() objects in PyPy support buffer(), whereas
- not in CPython; but CPython supports passing memoryview() to most
- built-in functions that accept buffers, with the notable exception
- of the buffer() built-in."""
+ """
+ Note that memoryview() is very inconsistent in CPython: it does not
+ support the buffer interface but does support the new buffer
+ interface: as a result, it is possible to pass memoryview to
+ e.g. socket.send() but not to file.write(). For simplicity and
+ consistency, in PyPy memoryview DOES support buffer(), which means
+ that it is accepted in more places than CPython.
+ """
return space.wrap(self.buf)
def descr_tobytes(self, space):
diff --git a/pypy/module/_codecs/interp_codecs.py b/pypy/module/_codecs/interp_codecs.py
--- a/pypy/module/_codecs/interp_codecs.py
+++ b/pypy/module/_codecs/interp_codecs.py
@@ -709,7 +709,7 @@
@unwrap_spec(data=str, errors='str_or_None')
def escape_encode(space, data, errors='strict'):
- from pypy.objspace.std.stringobject import string_escape_encode
+ from pypy.objspace.std.bytesobject import string_escape_encode
result = string_escape_encode(data, quote="'")
start = 1
end = len(result) - 1
diff --git a/pypy/module/cpyext/unicodeobject.py b/pypy/module/cpyext/unicodeobject.py
--- a/pypy/module/cpyext/unicodeobject.py
+++ b/pypy/module/cpyext/unicodeobject.py
@@ -12,7 +12,7 @@
from pypy.module.cpyext.stringobject import PyString_Check
from pypy.module.sys.interp_encoding import setdefaultencoding
from pypy.module._codecs.interp_codecs import CodecState
-from pypy.objspace.std import unicodeobject, unicodetype
+from pypy.objspace.std import unicodeobject
from rpython.rlib import rstring, runicode
from rpython.tool.sourcetools import func_renamer
import sys
@@ -262,7 +262,7 @@
def PyUnicode_GetDefaultEncoding(space):
"""Returns the currently active default encoding."""
if default_encoding[0] == '\x00':
- encoding = unicodetype.getdefaultencoding(space)
+ encoding = unicodeobject.getdefaultencoding(space)
i = 0
while i < len(encoding) and i < DEFAULT_ENCODING_SIZE:
default_encoding[i] = encoding[i]
@@ -295,7 +295,7 @@
encoding = rffi.charp2str(llencoding)
if llerrors:
errors = rffi.charp2str(llerrors)
- return unicodetype.encode_object(space, w_unicode, encoding, errors)
+ return unicodeobject.encode_object(space, w_unicode, encoding, errors)
@cpython_api([PyObject, CONST_STRING, CONST_STRING], PyObject)
def PyUnicode_AsEncodedString(space, w_unicode, llencoding, llerrors):
@@ -318,7 +318,7 @@
if not PyUnicode_Check(space, w_unicode):
PyErr_BadArgument(space)
- return unicodetype.encode_object(space, w_unicode, 'unicode-escape', 'strict')
+ return unicodeobject.encode_object(space, w_unicode, 'unicode-escape', 'strict')
@cpython_api([CONST_WSTRING, Py_ssize_t], PyObject)
def PyUnicode_FromUnicode(space, wchar_p, length):
@@ -471,7 +471,7 @@
exception was raised by the codec."""
if not PyUnicode_Check(space, w_unicode):
PyErr_BadArgument(space)
- return unicodetype.encode_object(space, w_unicode, encoding, "strict")
+ return unicodeobject.encode_object(space, w_unicode, encoding, "strict")
@cpython_api([CONST_STRING, Py_ssize_t, CONST_STRING], PyObject)
@func_renamer('PyUnicode_Decode%s' % suffix)
diff --git a/pypy/module/micronumpy/interp_boxes.py b/pypy/module/micronumpy/interp_boxes.py
--- a/pypy/module/micronumpy/interp_boxes.py
+++ b/pypy/module/micronumpy/interp_boxes.py
@@ -2,9 +2,9 @@
from pypy.interpreter.error import operationerrfmt, OperationError
from pypy.interpreter.gateway import interp2app, unwrap_spec
from pypy.interpreter.typedef import TypeDef, GetSetProperty
+from pypy.objspace.std.bytesobject import W_BytesObject
from pypy.objspace.std.floattype import float_typedef
-from pypy.objspace.std.stringtype import str_typedef
-from pypy.objspace.std.unicodetype import unicode_typedef, unicode_from_object
+from pypy.objspace.std.unicodeobject import W_UnicodeObject
from pypy.objspace.std.inttype import int_typedef
from pypy.objspace.std.complextype import complex_typedef
from rpython.rlib.rarithmetic import LONG_BIT
@@ -510,7 +510,7 @@
from pypy.module.micronumpy.interp_dtype import new_unicode_dtype
- arg = space.unicode_w(unicode_from_object(space, w_arg))
+ arg = space.unicode_w(space.unicode_from_object(w_arg))
# XXX size computations, we need tests anyway
arr = VoidBoxStorage(len(arg), new_unicode_dtype(space, len(arg)))
# XXX not this way, we need store
@@ -773,13 +773,13 @@
__module__ = "numpy",
)
-W_StringBox.typedef = TypeDef("string_", (W_CharacterBox.typedef, str_typedef),
+W_StringBox.typedef = TypeDef("string_", (W_CharacterBox.typedef, W_BytesObject.typedef),
__module__ = "numpy",
__new__ = interp2app(W_StringBox.descr__new__string_box.im_func),
__len__ = interp2app(W_StringBox.descr_len),
)
-W_UnicodeBox.typedef = TypeDef("unicode_", (W_CharacterBox.typedef, unicode_typedef),
+W_UnicodeBox.typedef = TypeDef("unicode_", (W_CharacterBox.typedef, W_UnicodeObject.typedef),
__module__ = "numpy",
__new__ = interp2app(W_UnicodeBox.descr__new__unicode_box.im_func),
__len__ = interp2app(W_UnicodeBox.descr_len),
diff --git a/pypy/objspace/fake/objspace.py b/pypy/objspace/fake/objspace.py
--- a/pypy/objspace/fake/objspace.py
+++ b/pypy/objspace/fake/objspace.py
@@ -290,6 +290,9 @@
ec._py_repr = None
return ec
+ def unicode_from_object(self, w_obj):
+ return w_some_obj()
+
# ----------
def translates(self, func=None, argtypes=None, seeobj_w=[], **kwds):
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
@@ -1,25 +1,23 @@
"""The builtin bytearray implementation"""
+from pypy.interpreter.baseobjspace import W_Root
from pypy.interpreter.buffer import RWBuffer
from pypy.interpreter.error import OperationError, operationerrfmt
+from pypy.interpreter.gateway import interp2app, unwrap_spec, WrappedDefault
from pypy.interpreter.signature import Signature
-from pypy.objspace.std import stringobject
-from pypy.objspace.std.bytearraytype import (
- getbytevalue, makebytearraydata_w, new_bytearray)
-from pypy.objspace.std.intobject import W_IntObject
-from pypy.objspace.std.model import W_Object, registerimplementation
-from pypy.objspace.std.multimethod import FailedToImplement
-from pypy.objspace.std.noneobject import W_NoneObject
-from pypy.objspace.std.register_all import register_all
-from pypy.objspace.std.sliceobject import W_SliceObject, normalize_simple_slice
-from pypy.objspace.std.stringobject import W_StringObject
-from pypy.objspace.std.unicodeobject import W_UnicodeObject
+from pypy.objspace.std.sliceobject import W_SliceObject
+from pypy.objspace.std.stdtypedef import StdTypeDef
+from pypy.objspace.std.stringmethods import StringMethods
from pypy.objspace.std.util import get_positive_index
+from rpython.rlib.objectmodel import newlist_hint, resizelist_hint, import_from_mixin
from rpython.rlib.rstring import StringBuilder
-class W_BytearrayObject(W_Object):
- from pypy.objspace.std.bytearraytype import bytearray_typedef as typedef
+def _make_data(s):
+ return [s[i] for i in range(len(s))]
+
+class W_BytearrayObject(W_Root):
+ import_from_mixin(StringMethods)
def __init__(w_self, data):
w_self.data = data
@@ -28,310 +26,984 @@
""" representation for debugging purposes """
return "%s(%s)" % (w_self.__class__.__name__, ''.join(w_self.data))
-registerimplementation(W_BytearrayObject)
+ def _new(self, value):
+ return W_BytearrayObject(_make_data(value))
+
+ def _new_from_list(self, value):
+ return W_BytearrayObject(value)
+
+ def _empty(self):
+ return W_BytearrayObject([])
+
+ def _len(self):
+ return len(self.data)
+
+ def _val(self, space):
+ return space.bufferstr_w(self)
+
+ def _op_val(self, space, w_other):
+ return space.bufferstr_new_w(w_other)
+
+ def _chr(self, char):
+ assert len(char) == 1
+ return str(char)[0]
+
+ _builder = StringBuilder
+
+ def _newlist_unwrapped(self, space, res):
+ return space.newlist([W_BytearrayObject(_make_data(i)) for i in res])
+
+ def _isupper(self, ch):
+ return ch.isupper()
+
+ def _islower(self, ch):
+ return ch.islower()
+
+ def _istitle(self, ch):
+ return ch.isupper()
+
+ def _isspace(self, ch):
+ return ch.isspace()
+
+ def _isalpha(self, ch):
+ return ch.isalpha()
+
+ def _isalnum(self, ch):
+ return ch.isalnum()
+
+ def _isdigit(self, ch):
+ return ch.isdigit()
+
+ _iscased = _isalpha
+
+ def _islinebreak(self, ch):
+ return (ch == '\n') or (ch == '\r')
+
+ def _upper(self, ch):
+ if ch.islower():
+ o = ord(ch) - 32
+ return chr(o)
+ else:
+ return ch
+
+ def _lower(self, ch):
+ if ch.isupper():
+ o = ord(ch) + 32
+ return chr(o)
+ else:
+ return ch
+
+ _title = _upper
+
+ def _join_return_one(self, space, w_obj):
+ return False
+
+ def _join_check_item(self, space, w_obj):
+ if (space.isinstance_w(w_obj, space.w_str) or
+ space.isinstance_w(w_obj, space.w_bytearray)):
+ return 0
+ return 1
+
+ def ord(self, space):
+ if len(self.data) != 1:
+ msg = "ord() expected a character, but string of length %d found"
+ raise operationerrfmt(space.w_TypeError, msg, len(self.data))
+ return space.wrap(ord(self.data[0]))
+
+ @staticmethod
+ def descr_new(space, w_bytearraytype, __args__):
+ return new_bytearray(space, w_bytearraytype, [])
+
+ def descr_reduce(self, space):
+ assert isinstance(self, W_BytearrayObject)
+ w_dict = self.getdict(space)
+ if w_dict is None:
+ w_dict = space.w_None
+ return space.newtuple([
+ space.type(self), space.newtuple([
+ space.wrap(''.join(self.data).decode('latin-1')),
+ space.wrap('latin-1')]),
+ w_dict])
+
+ @staticmethod
+ def descr_fromhex(space, w_bytearraytype, w_hexstring):
+ "bytearray.fromhex(string) -> bytearray\n"
+ "\n"
+ "Create a bytearray object from a string of hexadecimal numbers.\n"
+ "Spaces between two numbers are accepted.\n"
+ "Example: bytearray.fromhex('B9 01EF') -> bytearray(b'\\xb9\\x01\\xef')."
+ hexstring = space.str_w(w_hexstring)
+ hexstring = hexstring.lower()
+ data = []
+ length = len(hexstring)
+ i = -2
+ while True:
+ i += 2
+ while i < length and hexstring[i] == ' ':
+ i += 1
+ if i >= length:
+ break
+ if i+1 == length:
+ raise OperationError(space.w_ValueError, space.wrap(
+ "non-hexadecimal number found in fromhex() arg at position %d" % i))
+
+ top = _hex_digit_to_int(hexstring[i])
+ if top == -1:
+ raise OperationError(space.w_ValueError, space.wrap(
+ "non-hexadecimal number found in fromhex() arg at position %d" % i))
+ bot = _hex_digit_to_int(hexstring[i+1])
+ if bot == -1:
+ raise OperationError(space.w_ValueError, space.wrap(
+ "non-hexadecimal number found in fromhex() arg at position %d" % (i+1,)))
+ data.append(chr(top*16 + bot))
+
+ # in CPython bytearray.fromhex is a staticmethod, so
+ # we ignore w_type and always return a bytearray
+ return new_bytearray(space, space.w_bytearray, data)
+
+ def descr_init(self, space, __args__):
+ # this is on the silly side
+ w_source, w_encoding, w_errors = __args__.parse_obj(
+ None, 'bytearray', init_signature, init_defaults)
+
+ if w_source is None:
+ w_source = space.wrap('')
+ if w_encoding is None:
+ w_encoding = space.w_None
+ if w_errors is None:
+ w_errors = space.w_None
+
+ # Unicode argument
+ if not space.is_w(w_encoding, space.w_None):
+ from pypy.objspace.std.unicodeobject import (
+ _get_encoding_and_errors, encode_object
+ )
+ encoding, errors = _get_encoding_and_errors(space, w_encoding, w_errors)
+
+ # if w_source is an integer this correctly raises a TypeError
+ # the CPython error message is: "encoding or errors without a string argument"
+ # ours is: "expected unicode, got int object"
+ w_source = encode_object(space, w_source, encoding, errors)
+
+ # Is it an int?
+ try:
+ count = space.int_w(w_source)
+ except OperationError, e:
+ if not e.match(space, space.w_TypeError):
+ raise
+ else:
+ if count < 0:
+ raise OperationError(space.w_ValueError,
+ space.wrap("bytearray negative count"))
+ self.data = ['\0'] * count
+ return
+
+ data = makebytearraydata_w(space, w_source)
+ self.data = data
+
+ def descr_repr(self, space):
+ s = self.data
+
+ # Good default if there are no replacements.
+ buf = StringBuilder(len("bytearray(b'')") + len(s))
+
+ buf.append("bytearray(b'")
+
+ for i in range(len(s)):
+ c = s[i]
+
+ if c == '\\' or c == "'":
+ buf.append('\\')
+ buf.append(c)
+ elif c == '\t':
+ buf.append('\\t')
+ elif c == '\r':
+ buf.append('\\r')
+ elif c == '\n':
+ buf.append('\\n')
+ elif not '\x20' <= c < '\x7f':
+ n = ord(c)
+ buf.append('\\x')
+ buf.append("0123456789abcdef"[n>>4])
+ buf.append("0123456789abcdef"[n&0xF])
+ else:
+ buf.append(c)
+
+ buf.append("')")
+
+ return space.wrap(buf.build())
+
+ def descr_str(self, space):
+ return space.wrap(''.join(self.data))
+
+ def descr_eq(self, space, w_other):
+ try:
+ return space.newbool(self._val(space) == self._op_val(space, w_other))
+ except OperationError, e:
+ if e.match(space, space.w_TypeError):
+ return space.w_NotImplemented
+ raise
+
+ def descr_ne(self, space, w_other):
+ try:
+ return space.newbool(self._val(space) != self._op_val(space, w_other))
+ except OperationError, e:
+ if e.match(space, space.w_TypeError):
+ return space.w_NotImplemented
+ raise
+
+ def descr_lt(self, space, w_other):
+ try:
+ return space.newbool(self._val(space) < self._op_val(space, w_other))
+ except OperationError, e:
+ if e.match(space, space.w_TypeError):
+ return space.w_NotImplemented
+ raise
+
+ def descr_le(self, space, w_other):
+ try:
+ return space.newbool(self._val(space) <= self._op_val(space, w_other))
+ except OperationError, e:
+ if e.match(space, space.w_TypeError):
+ return space.w_NotImplemented
+ raise
+
+ def descr_gt(self, space, w_other):
+ try:
+ return space.newbool(self._val(space) > self._op_val(space, w_other))
+ except OperationError, e:
+ if e.match(space, space.w_TypeError):
+ return space.w_NotImplemented
+ raise
+
+ def descr_ge(self, space, w_other):
+ try:
+ return space.newbool(self._val(space) >= self._op_val(space, w_other))
+ except OperationError, e:
+ if e.match(space, space.w_TypeError):
+ return space.w_NotImplemented
+ raise
+
+ def descr_buffer(self, space):
+ return BytearrayBuffer(self.data)
+
+ def descr_inplace_add(self, space, w_other):
+ if isinstance(w_other, W_BytearrayObject):
+ self.data += w_other.data
+ else:
+ self.data += self._op_val(space, w_other)
+ return self
+
+ def descr_inplace_mul(self, space, w_times):
+ try:
+ times = space.getindex_w(w_times, space.w_OverflowError)
+ except OperationError, e:
+ if e.match(space, space.w_TypeError):
+ return space.w_NotImplemented
+ raise
+ self.data *= times
+ return self
+
+ def descr_setitem(self, space, w_index, w_other):
+ if isinstance(w_index, W_SliceObject):
+ oldsize = len(self.data)
+ start, stop, step, slicelength = w_index.indices4(space, oldsize)
+ sequence2 = makebytearraydata_w(space, w_other)
+ _setitem_slice_helper(space, self.data, start, step,
+ slicelength, sequence2, empty_elem='\x00')
+ else:
+ idx = space.getindex_w(w_index, space.w_IndexError, "bytearray index")
+ try:
+ self.data[idx] = getbytevalue(space, w_other)
+ except IndexError:
+ raise OperationError(space.w_IndexError,
+ space.wrap("bytearray index out of range"))
+
+ def descr_delitem(self, space, w_idx):
+ if isinstance(w_idx, W_SliceObject):
+ start, stop, step, slicelength = w_idx.indices4(space,
+ len(self.data))
+ _delitem_slice_helper(space, self.data, start, step, slicelength)
+ else:
+ idx = space.getindex_w(w_idx, space.w_IndexError, "bytearray index")
+ try:
+ del self.data[idx]
+ except IndexError:
+ raise OperationError(space.w_IndexError,
+ space.wrap("bytearray deletion index out of range"))
+
+ def descr_append(self, space, w_item):
+ self.data.append(getbytevalue(space, w_item))
+
+ def descr_extend(self, space, w_other):
+ if isinstance(w_other, W_BytearrayObject):
+ self.data += w_other.data
+ else:
+ self.data += makebytearraydata_w(space, w_other)
+ return self
+
+ def descr_insert(self, space, w_idx, w_other):
+ where = space.int_w(w_idx)
+ length = len(self.data)
+ index = get_positive_index(where, length)
+ val = getbytevalue(space, w_other)
+ self.data.insert(index, val)
+ return space.w_None
+
+ @unwrap_spec(w_idx=WrappedDefault(-1))
+ def descr_pop(self, space, w_idx):
+ index = space.int_w(w_idx)
+ try:
+ result = self.data.pop(index)
+ except IndexError:
+ if not self.data:
+ raise OperationError(space.w_IndexError, space.wrap(
+ "pop from empty bytearray"))
+ raise OperationError(space.w_IndexError, space.wrap(
+ "pop index out of range"))
+ return space.wrap(ord(result))
+
+ def descr_remove(self, space, w_char):
+ char = space.int_w(space.index(w_char))
+ try:
+ self.data.remove(chr(char))
+ except ValueError:
+ raise OperationError(space.w_ValueError, space.wrap(
+ "value not found in bytearray"))
+
+ def descr_reverse(self, space):
+ self.data.reverse()
+
+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):
+ w_obj = space.allocate_instance(W_BytearrayObject, w_bytearraytype)
+ W_BytearrayObject.__init__(w_obj, data)
+ return w_obj
+
+
+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
+ w_iter = space.iter(w_source)
+ length_hint = space.length_hint(w_source, 0)
+ data = newlist_hint(length_hint)
+ extended = 0
+ 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)
+ extended += 1
+ if extended < length_hint:
+ resizelist_hint(data, extended)
+ return data
+
+def _hex_digit_to_int(d):
+ val = ord(d)
+ if 47 < val < 58:
+ return val - 48
+ if 96 < val < 103:
+ return val - 87
+ return -1
+
+
+class BytearrayDocstrings:
+ """bytearray(iterable_of_ints) -> bytearray
+ bytearray(string, encoding[, errors]) -> bytearray
+ bytearray(bytes_or_bytearray) -> mutable copy of bytes_or_bytearray
+ bytearray(memory_view) -> bytearray
+
+ Construct an mutable bytearray object from:
+ - an iterable yielding integers in range(256)
+ - a text string encoded using the specified encoding
+ - a bytes or a bytearray object
+ - any object implementing the buffer API.
+
+ bytearray(int) -> bytearray.
+
+ Construct a zero-initialized bytearray of the given length.
+
+ """
+
+ def __add__():
+ """x.__add__(y) <==> x+y"""
+
+ def __alloc__():
+ """B.__alloc__() -> int
+
+ Return the number of bytes actually allocated.
+ """
+
+ def __contains__():
+ """x.__contains__(y) <==> y in x"""
+
+ def __delitem__():
+ """x.__delitem__(y) <==> del x[y]"""
+
+ def __eq__():
+ """x.__eq__(y) <==> x==y"""
+
+ def __ge__():
+ """x.__ge__(y) <==> x>=y"""
+
+ def __getattribute__():
+ """x.__getattribute__('name') <==> x.name"""
+
+ def __getitem__():
+ """x.__getitem__(y) <==> x[y]"""
+
+ def __gt__():
+ """x.__gt__(y) <==> x>y"""
+
+ def __iadd__():
+ """x.__iadd__(y) <==> x+=y"""
+
+ def __imul__():
+ """x.__imul__(y) <==> x*=y"""
+
+ def __init__():
+ """x.__init__(...) initializes x; see help(type(x)) for signature"""
+
+ def __iter__():
+ """x.__iter__() <==> iter(x)"""
+
+ def __le__():
+ """x.__le__(y) <==> x<=y"""
+
+ def __len__():
+ """x.__len__() <==> len(x)"""
+
+ def __lt__():
+ """x.__lt__(y) <==> x<y"""
+
+ def __mul__():
+ """x.__mul__(n) <==> x*n"""
+
+ def __ne__():
+ """x.__ne__(y) <==> x!=y"""
+
+ def __reduce__():
+ """Return state information for pickling."""
+
+ def __repr__():
+ """x.__repr__() <==> repr(x)"""
+
+ def __rmul__():
+ """x.__rmul__(n) <==> n*x"""
+
+ def __setitem__():
+ """x.__setitem__(i, y) <==> x[i]=y"""
+
+ def __sizeof__():
+ """B.__sizeof__() -> int
+
+ Returns the size of B in memory, in bytes
+ """
+
+ def __str__():
+ """x.__str__() <==> str(x)"""
+
+ def append():
+ """B.append(int) -> None
+
+ Append a single item to the end of B.
+ """
+
+ def capitalize():
+ """B.capitalize() -> copy of B
+
+ Return a copy of B with only its first character capitalized (ASCII)
+ and the rest lower-cased.
+ """
+
+ def center():
+ """B.center(width[, fillchar]) -> copy of B
+
+ Return B centered in a string of length width. Padding is
+ done using the specified fill character (default is a space).
+ """
+
+ def count():
+ """B.count(sub[, start[, end]]) -> int
+
+ Return the number of non-overlapping occurrences of subsection sub in
+ bytes B[start:end]. Optional arguments start and end are interpreted
+ as in slice notation.
+ """
+
+ def decode():
+ """B.decode(encoding=None, errors='strict') -> unicode
+
+ Decode B using the codec registered for encoding. encoding defaults
+ to the default encoding. errors may be given to set a different error
+ handling scheme. Default is 'strict' meaning that encoding errors raise
+ a UnicodeDecodeError. Other possible values are 'ignore' and 'replace'
+ as well as any other name registered with codecs.register_error that is
+ able to handle UnicodeDecodeErrors.
+ """
+
+ def endswith():
+ """B.endswith(suffix[, start[, end]]) -> bool
+
+ Return True if B ends with the specified suffix, False otherwise.
+ With optional start, test B beginning at that position.
+ With optional end, stop comparing B at that position.
+ suffix can also be a tuple of strings to try.
+ """
+
+ def expandtabs():
+ """B.expandtabs([tabsize]) -> copy of B
+
+ Return a copy of B where all tab characters are expanded using spaces.
+ If tabsize is not given, a tab size of 8 characters is assumed.
+ """
+
+ def extend():
+ """B.extend(iterable_of_ints) -> None
+
+ Append all the elements from the iterator or sequence to the
+ end of B.
+ """
+
+ def find():
+ """B.find(sub[, start[, end]]) -> int
+
+ Return the lowest index in B where subsection sub is found,
+ such that sub is contained within B[start,end]. Optional
+ arguments start and end are interpreted as in slice notation.
+
+ Return -1 on failure.
+ """
+
+ def fromhex():
+ """bytearray.fromhex(string) -> bytearray (static method)
+
+ Create a bytearray object from a string of hexadecimal numbers.
+ Spaces between two numbers are accepted.
+ Example: bytearray.fromhex('B9 01EF') -> bytearray(b'\xb9\x01\xef').
+ """
+
+ def index():
+ """B.index(sub[, start[, end]]) -> int
+
+ Like B.find() but raise ValueError when the subsection is not found.
+ """
+
+ def insert():
+ """B.insert(index, int) -> None
+
+ Insert a single item into the bytearray before the given index.
+ """
+
+ def isalnum():
+ """B.isalnum() -> bool
+
+ Return True if all characters in B are alphanumeric
+ and there is at least one character in B, False otherwise.
+ """
+
+ def isalpha():
+ """B.isalpha() -> bool
+
+ Return True if all characters in B are alphabetic
+ and there is at least one character in B, False otherwise.
+ """
+
+ def isdigit():
+ """B.isdigit() -> bool
+
+ Return True if all characters in B are digits
+ and there is at least one character in B, False otherwise.
+ """
+
+ def islower():
+ """B.islower() -> bool
+
+ Return True if all cased characters in B are lowercase and there is
+ at least one cased character in B, False otherwise.
+ """
+
+ def isspace():
+ """B.isspace() -> bool
+
+ Return True if all characters in B are whitespace
+ and there is at least one character in B, False otherwise.
+ """
+
+ def istitle():
+ """B.istitle() -> bool
+
+ Return True if B is a titlecased string and there is at least one
+ character in B, i.e. uppercase characters may only follow uncased
+ characters and lowercase characters only cased ones. Return False
+ otherwise.
+ """
+
+ def isupper():
+ """B.isupper() -> bool
+
+ Return True if all cased characters in B are uppercase and there is
+ at least one cased character in B, False otherwise.
+ """
+
+ def join():
+ """B.join(iterable_of_bytes) -> bytearray
+
+ Concatenate any number of str/bytearray objects, with B
+ in between each pair, and return the result as a new bytearray.
+ """
+
+ def ljust():
+ """B.ljust(width[, fillchar]) -> copy of B
+
+ Return B left justified in a string of length width. Padding is
+ done using the specified fill character (default is a space).
+ """
+
+ def lower():
+ """B.lower() -> copy of B
+
+ Return a copy of B with all ASCII characters converted to lowercase.
+ """
+
+ def lstrip():
+ """B.lstrip([bytes]) -> bytearray
+
+ Strip leading bytes contained in the argument
+ and return the result as a new bytearray.
+ If the argument is omitted, strip leading ASCII whitespace.
+ """
+
+ def partition():
+ """B.partition(sep) -> (head, sep, tail)
+
+ Search for the separator sep in B, and return the part before it,
+ the separator itself, and the part after it. If the separator is not
+ found, returns B and two empty bytearray objects.
+ """
+
+ def pop():
+ """B.pop([index]) -> int
+
+ Remove and return a single item from B. If no index
+ argument is given, will pop the last value.
+ """
+
+ def remove():
+ """B.remove(int) -> None
+
+ Remove the first occurrence of a value in B.
+ """
+
+ def replace():
+ """B.replace(old, new[, count]) -> bytearray
+
+ Return a copy of B with all occurrences of subsection
+ old replaced by new. If the optional argument count is
+ given, only the first count occurrences are replaced.
+ """
+
+ def reverse():
+ """B.reverse() -> None
+
+ Reverse the order of the values in B in place.
+ """
+
+ def rfind():
+ """B.rfind(sub[, start[, end]]) -> int
+
+ Return the highest index in B where subsection sub is found,
+ such that sub is contained within B[start,end]. Optional
+ arguments start and end are interpreted as in slice notation.
+
+ Return -1 on failure.
+ """
+
+ def rindex():
+ """B.rindex(sub[, start[, end]]) -> int
+
+ Like B.rfind() but raise ValueError when the subsection is not found.
+ """
+
+ def rjust():
+ """B.rjust(width[, fillchar]) -> copy of B
+
+ Return B right justified in a string of length width. Padding is
+ done using the specified fill character (default is a space)
+ """
+
+ def rpartition():
+ """B.rpartition(sep) -> (head, sep, tail)
+
+ Search for the separator sep in B, starting at the end of B,
+ and return the part before it, the separator itself, and the
+ part after it. If the separator is not found, returns two empty
+ bytearray objects and B.
+ """
+
+ def rsplit():
+ """B.rsplit(sep=None, maxsplit=-1) -> list of bytearrays
+
+ Return a list of the sections in B, using sep as the delimiter,
+ starting at the end of B and working to the front.
+ If sep is not given, B is split on ASCII whitespace characters
+ (space, tab, return, newline, formfeed, vertical tab).
+ If maxsplit is given, at most maxsplit splits are done.
+ """
+
+ def rstrip():
+ """B.rstrip([bytes]) -> bytearray
+
+ Strip trailing bytes contained in the argument
+ and return the result as a new bytearray.
+ If the argument is omitted, strip trailing ASCII whitespace.
+ """
+
+ def split():
+ """B.split(sep=None, maxsplit=-1) -> list of bytearrays
+
+ Return a list of the sections in B, using sep as the delimiter.
+ If sep is not given, B is split on ASCII whitespace characters
+ (space, tab, return, newline, formfeed, vertical tab).
+ If maxsplit is given, at most maxsplit splits are done.
+ """
+
+ def splitlines():
+ """B.splitlines(keepends=False) -> list of lines
+
+ Return a list of the lines in B, breaking at line boundaries.
+ Line breaks are not included in the resulting list unless keepends
+ is given and true.
+ """
+
+ def startswith():
+ """B.startswith(prefix[, start[, end]]) -> bool
+
+ Return True if B starts with the specified prefix, False otherwise.
+ With optional start, test B beginning at that position.
+ With optional end, stop comparing B at that position.
+ prefix can also be a tuple of strings to try.
+ """
+
+ def strip():
+ """B.strip([bytes]) -> bytearray
+
+ Strip leading and trailing bytes contained in the argument
+ and return the result as a new bytearray.
+ If the argument is omitted, strip ASCII whitespace.
+ """
+
+ def swapcase():
+ """B.swapcase() -> copy of B
+
+ Return a copy of B with uppercase ASCII characters converted
+ to lowercase ASCII and vice versa.
+ """
+
+ def title():
+ """B.title() -> copy of B
+
+ Return a titlecased version of B, i.e. ASCII words start with uppercase
+ characters, all remaining cased characters have lowercase.
+ """
+
+ def translate():
+ """B.translate(table[, deletechars]) -> bytearray
+
+ Return a copy of B, where all characters occurring in the
+ optional argument deletechars are removed, and the remaining
+ characters have been mapped through the given translation
+ table, which must be a bytes object of length 256.
+ """
+
+ def upper():
+ """B.upper() -> copy of B
+
+ Return a copy of B with all ASCII characters converted to uppercase.
+ """
+
+ def zfill():
+ """B.zfill(width) -> copy of B
+
+ Pad a numeric string B with zeros on the left, to fill a field
+ of the specified width. B is never truncated.
+ """
+
+
+W_BytearrayObject.typedef = StdTypeDef(
+ "bytearray",
+ __doc__ = BytearrayDocstrings.__doc__,
+ __new__ = interp2app(W_BytearrayObject.descr_new),
+ __hash__ = None,
+ __reduce__ = interp2app(W_BytearrayObject.descr_reduce,
+ doc=BytearrayDocstrings.__reduce__.__doc__),
+ fromhex = interp2app(W_BytearrayObject.descr_fromhex, as_classmethod=True,
+ doc=BytearrayDocstrings.fromhex.__doc__),
+
+ __repr__ = interp2app(W_BytearrayObject.descr_repr,
+ doc=BytearrayDocstrings.__repr__.__doc__),
+ __str__ = interp2app(W_BytearrayObject.descr_str,
+ doc=BytearrayDocstrings.__str__.__doc__),
+
+ __eq__ = interp2app(W_BytearrayObject.descr_eq,
+ doc=BytearrayDocstrings.__eq__.__doc__),
+ __ne__ = interp2app(W_BytearrayObject.descr_ne,
+ doc=BytearrayDocstrings.__ne__.__doc__),
+ __lt__ = interp2app(W_BytearrayObject.descr_lt,
+ doc=BytearrayDocstrings.__lt__.__doc__),
+ __le__ = interp2app(W_BytearrayObject.descr_le,
+ doc=BytearrayDocstrings.__le__.__doc__),
+ __gt__ = interp2app(W_BytearrayObject.descr_gt,
+ doc=BytearrayDocstrings.__gt__.__doc__),
+ __ge__ = interp2app(W_BytearrayObject.descr_ge,
+ doc=BytearrayDocstrings.__ge__.__doc__),
+
+ __len__ = interp2app(W_BytearrayObject.descr_len,
+ doc=BytearrayDocstrings.__len__.__doc__),
+ __contains__ = interp2app(W_BytearrayObject.descr_contains,
+ doc=BytearrayDocstrings.__contains__.__doc__),
+
+ __add__ = interp2app(W_BytearrayObject.descr_add,
+ doc=BytearrayDocstrings.__add__.__doc__),
+ __mul__ = interp2app(W_BytearrayObject.descr_mul,
+ doc=BytearrayDocstrings.__mul__.__doc__),
+ __rmul__ = interp2app(W_BytearrayObject.descr_mul,
+ doc=BytearrayDocstrings.__rmul__.__doc__),
+
+ __getitem__ = interp2app(W_BytearrayObject.descr_getitem,
+ doc=BytearrayDocstrings.__getitem__.__doc__),
+
+ capitalize = interp2app(W_BytearrayObject.descr_capitalize,
+ doc=BytearrayDocstrings.capitalize.__doc__),
+ center = interp2app(W_BytearrayObject.descr_center,
+ doc=BytearrayDocstrings.center.__doc__),
+ count = interp2app(W_BytearrayObject.descr_count,
+ doc=BytearrayDocstrings.count.__doc__),
+ decode = interp2app(W_BytearrayObject.descr_decode,
+ doc=BytearrayDocstrings.decode.__doc__),
+ expandtabs = interp2app(W_BytearrayObject.descr_expandtabs,
+ doc=BytearrayDocstrings.expandtabs.__doc__),
+ find = interp2app(W_BytearrayObject.descr_find,
+ doc=BytearrayDocstrings.find.__doc__),
+ rfind = interp2app(W_BytearrayObject.descr_rfind,
+ doc=BytearrayDocstrings.rfind.__doc__),
+ index = interp2app(W_BytearrayObject.descr_index,
+ doc=BytearrayDocstrings.index.__doc__),
+ rindex = interp2app(W_BytearrayObject.descr_rindex,
+ doc=BytearrayDocstrings.rindex.__doc__),
+ isalnum = interp2app(W_BytearrayObject.descr_isalnum,
+ doc=BytearrayDocstrings.isalnum.__doc__),
+ isalpha = interp2app(W_BytearrayObject.descr_isalpha,
+ doc=BytearrayDocstrings.isalpha.__doc__),
+ isdigit = interp2app(W_BytearrayObject.descr_isdigit,
+ doc=BytearrayDocstrings.isdigit.__doc__),
+ islower = interp2app(W_BytearrayObject.descr_islower,
+ doc=BytearrayDocstrings.islower.__doc__),
+ isspace = interp2app(W_BytearrayObject.descr_isspace,
+ doc=BytearrayDocstrings.isspace.__doc__),
+ istitle = interp2app(W_BytearrayObject.descr_istitle,
+ doc=BytearrayDocstrings.istitle.__doc__),
+ isupper = interp2app(W_BytearrayObject.descr_isupper,
+ doc=BytearrayDocstrings.isupper.__doc__),
+ join = interp2app(W_BytearrayObject.descr_join,
+ doc=BytearrayDocstrings.join.__doc__),
+ ljust = interp2app(W_BytearrayObject.descr_ljust,
+ doc=BytearrayDocstrings.ljust.__doc__),
+ rjust = interp2app(W_BytearrayObject.descr_rjust,
+ doc=BytearrayDocstrings.rjust.__doc__),
+ lower = interp2app(W_BytearrayObject.descr_lower,
+ doc=BytearrayDocstrings.lower.__doc__),
+ partition = interp2app(W_BytearrayObject.descr_partition,
+ doc=BytearrayDocstrings.partition.__doc__),
+ rpartition = interp2app(W_BytearrayObject.descr_rpartition,
+ doc=BytearrayDocstrings.rpartition.__doc__),
+ replace = interp2app(W_BytearrayObject.descr_replace,
+ doc=BytearrayDocstrings.replace.__doc__),
+ split = interp2app(W_BytearrayObject.descr_split,
+ doc=BytearrayDocstrings.split.__doc__),
+ rsplit = interp2app(W_BytearrayObject.descr_rsplit,
+ doc=BytearrayDocstrings.rsplit.__doc__),
+ splitlines = interp2app(W_BytearrayObject.descr_splitlines,
+ doc=BytearrayDocstrings.splitlines.__doc__),
+ startswith = interp2app(W_BytearrayObject.descr_startswith,
+ doc=BytearrayDocstrings.startswith.__doc__),
+ endswith = interp2app(W_BytearrayObject.descr_endswith,
+ doc=BytearrayDocstrings.endswith.__doc__),
+ strip = interp2app(W_BytearrayObject.descr_strip,
+ doc=BytearrayDocstrings.strip.__doc__),
+ lstrip = interp2app(W_BytearrayObject.descr_lstrip,
+ doc=BytearrayDocstrings.lstrip.__doc__),
+ rstrip = interp2app(W_BytearrayObject.descr_rstrip,
+ doc=BytearrayDocstrings.rstrip.__doc__),
+ swapcase = interp2app(W_BytearrayObject.descr_swapcase,
+ doc=BytearrayDocstrings.swapcase.__doc__),
+ title = interp2app(W_BytearrayObject.descr_title,
+ doc=BytearrayDocstrings.title.__doc__),
+ translate = interp2app(W_BytearrayObject.descr_translate,
+ doc=BytearrayDocstrings.translate.__doc__),
+ upper = interp2app(W_BytearrayObject.descr_upper,
+ doc=BytearrayDocstrings.upper.__doc__),
+ zfill = interp2app(W_BytearrayObject.descr_zfill,
+ doc=BytearrayDocstrings.zfill.__doc__),
+
+ __init__ = interp2app(W_BytearrayObject.descr_init,
+ doc=BytearrayDocstrings.__init__.__doc__),
+ __buffer__ = interp2app(W_BytearrayObject.descr_buffer),
+
+ __iadd__ = interp2app(W_BytearrayObject.descr_inplace_add,
+ doc=BytearrayDocstrings.__iadd__.__doc__),
+ __imul__ = interp2app(W_BytearrayObject.descr_inplace_mul,
+ doc=BytearrayDocstrings.__imul__.__doc__),
+ __setitem__ = interp2app(W_BytearrayObject.descr_setitem,
+ doc=BytearrayDocstrings.__setitem__.__doc__),
+ __delitem__ = interp2app(W_BytearrayObject.descr_delitem,
+ doc=BytearrayDocstrings.__delitem__.__doc__),
+
+ append = interp2app(W_BytearrayObject.descr_append,
+ doc=BytearrayDocstrings.append.__doc__),
+ extend = interp2app(W_BytearrayObject.descr_extend,
+ doc=BytearrayDocstrings.extend.__doc__),
+ insert = interp2app(W_BytearrayObject.descr_insert,
+ doc=BytearrayDocstrings.insert.__doc__),
+ pop = interp2app(W_BytearrayObject.descr_pop,
+ doc=BytearrayDocstrings.pop.__doc__),
+ remove = interp2app(W_BytearrayObject.descr_remove,
+ doc=BytearrayDocstrings.remove.__doc__),
+ reverse = interp2app(W_BytearrayObject.descr_reverse,
+ doc=BytearrayDocstrings.reverse.__doc__),
+)
init_signature = Signature(['source', 'encoding', 'errors'], None, None)
init_defaults = [None, None, None]
-def init__Bytearray(space, w_bytearray, __args__):
- # this is on the silly side
- w_source, w_encoding, w_errors = __args__.parse_obj(
- None, 'bytearray', init_signature, init_defaults)
- if w_source is None:
- w_source = space.wrap('')
- if w_encoding is None:
- w_encoding = space.w_None
- if w_errors is None:
- w_errors = space.w_None
-
- # Unicode argument
- if not space.is_w(w_encoding, space.w_None):
- from pypy.objspace.std.unicodetype import (
- _get_encoding_and_errors, encode_object
- )
- encoding, errors = _get_encoding_and_errors(space, w_encoding, w_errors)
-
- # if w_source is an integer this correctly raises a TypeError
- # the CPython error message is: "encoding or errors without a string argument"
- # ours is: "expected unicode, got int object"
- w_source = encode_object(space, w_source, encoding, errors)
-
- # Is it an int?
- try:
- count = space.int_w(w_source)
- except OperationError, e:
- if not e.match(space, space.w_TypeError):
- raise
- else:
- if count < 0:
- raise OperationError(space.w_ValueError,
- space.wrap("bytearray negative count"))
- w_bytearray.data = ['\0'] * count
- return
-
- data = makebytearraydata_w(space, w_source)
- w_bytearray.data = data
-
-def len__Bytearray(space, w_bytearray):
- result = len(w_bytearray.data)
- return space.newint(result)
-
-def ord__Bytearray(space, w_bytearray):
- if len(w_bytearray.data) != 1:
- raise OperationError(space.w_TypeError,
- space.wrap("expected a character, but string"
- "of length %s found" % len(w_bytearray.data)))
- return space.wrap(ord(w_bytearray.data[0]))
-
-def getitem__Bytearray_ANY(space, w_bytearray, w_index):
- # getindex_w should get a second argument space.w_IndexError,
- # but that doesn't exist the first time this is called.
- try:
- w_IndexError = space.w_IndexError
- except AttributeError:
- w_IndexError = None
- index = space.getindex_w(w_index, w_IndexError, "bytearray index")
- try:
- return space.newint(ord(w_bytearray.data[index]))
- except IndexError:
- raise OperationError(space.w_IndexError,
- space.wrap("bytearray index out of range"))
-
-def getitem__Bytearray_Slice(space, w_bytearray, w_slice):
- data = w_bytearray.data
- length = len(data)
- start, stop, step, slicelength = w_slice.indices4(space, length)
- assert slicelength >= 0
- if step == 1 and 0 <= start <= stop:
- newdata = data[start:stop]
- else:
- newdata = _getitem_slice_multistep(data, start, step, slicelength)
- return W_BytearrayObject(newdata)
-
-def _getitem_slice_multistep(data, start, step, slicelength):
- return [data[start + i*step] for i in range(slicelength)]
-
-def contains__Bytearray_Int(space, w_bytearray, w_char):
- char = space.int_w(w_char)
- if not 0 <= char < 256:
- raise OperationError(space.w_ValueError,
- space.wrap("byte must be in range(0, 256)"))
- for c in w_bytearray.data:
- if ord(c) == char:
- return space.w_True
- return space.w_False
-
-def contains__Bytearray_String(space, w_bytearray, w_str):
- # XXX slow - copies, needs rewriting
- w_str2 = str__Bytearray(space, w_bytearray)
- return stringobject.contains__String_String(space, w_str2, w_str)
-
-def contains__Bytearray_ANY(space, w_bytearray, w_sub):
- # XXX slow - copies, needs rewriting
- w_str = space.wrap(space.bufferstr_new_w(w_sub))
- w_str2 = str__Bytearray(space, w_bytearray)
- return stringobject.contains__String_String(space, w_str2, w_str)
-
-def add__Bytearray_Bytearray(space, w_bytearray1, w_bytearray2):
- data1 = w_bytearray1.data
- data2 = w_bytearray2.data
- return W_BytearrayObject(data1 + data2)
-
-def add__Bytearray_ANY(space, w_bytearray1, w_other):
- data1 = w_bytearray1.data
- data2 = [c for c in space.bufferstr_new_w(w_other)]
- return W_BytearrayObject(data1 + data2)
-
-def add__String_Bytearray(space, w_str, w_bytearray):
- data2 = w_bytearray.data
- data1 = [c for c in space.str_w(w_str)]
- return W_BytearrayObject(data1 + data2)
-
-def mul_bytearray_times(space, w_bytearray, w_times):
- try:
- times = space.getindex_w(w_times, space.w_OverflowError)
- except OperationError, e:
- if e.match(space, space.w_TypeError):
- raise FailedToImplement
- raise
- data = w_bytearray.data
- return W_BytearrayObject(data * times)
-
-def mul__Bytearray_ANY(space, w_bytearray, w_times):
- return mul_bytearray_times(space, w_bytearray, w_times)
-
-def mul__ANY_Bytearray(space, w_times, w_bytearray):
- return mul_bytearray_times(space, w_bytearray, w_times)
-
-def inplace_mul__Bytearray_ANY(space, w_bytearray, w_times):
- try:
- times = space.getindex_w(w_times, space.w_OverflowError)
- except OperationError, e:
- if e.match(space, space.w_TypeError):
- raise FailedToImplement
- raise
- w_bytearray.data *= times
- return w_bytearray
-
-def eq__Bytearray_Bytearray(space, w_bytearray1, w_bytearray2):
- data1 = w_bytearray1.data
- data2 = w_bytearray2.data
- if len(data1) != len(data2):
- return space.w_False
- for i in range(len(data1)):
- if data1[i] != data2[i]:
- return space.w_False
- return space.w_True
-
-def String2Bytearray(space, w_str):
- data = [c for c in space.str_w(w_str)]
- return W_BytearrayObject(data)
-
-def eq__Bytearray_String(space, w_bytearray, w_other):
- return space.eq(str__Bytearray(space, w_bytearray), w_other)
-
-def eq__Bytearray_Unicode(space, w_bytearray, w_other):
- return space.w_False
-
-def eq__Unicode_Bytearray(space, w_other, w_bytearray):
- return space.w_False
-
-def ne__Bytearray_String(space, w_bytearray, w_other):
- return space.ne(str__Bytearray(space, w_bytearray), w_other)
-
-def ne__Bytearray_Unicode(space, w_bytearray, w_other):
- return space.w_True
-
-def ne__Unicode_Bytearray(space, w_other, w_bytearray):
- return space.w_True
-
-def _min(a, b):
- if a < b:
- return a
- return b
-
-def lt__Bytearray_Bytearray(space, w_bytearray1, w_bytearray2):
- data1 = w_bytearray1.data
- data2 = w_bytearray2.data
- ncmp = _min(len(data1), len(data2))
- # Search for the first index where items are different
- for p in range(ncmp):
- if data1[p] != data2[p]:
- return space.newbool(data1[p] < data2[p])
- # No more items to compare -- compare sizes
- return space.newbool(len(data1) < len(data2))
-
-def gt__Bytearray_Bytearray(space, w_bytearray1, w_bytearray2):
- data1 = w_bytearray1.data
- data2 = w_bytearray2.data
- ncmp = _min(len(data1), len(data2))
- # Search for the first index where items are different
- for p in range(ncmp):
- if data1[p] != data2[p]:
- return space.newbool(data1[p] > data2[p])
- # No more items to compare -- compare sizes
- return space.newbool(len(data1) > len(data2))
-
-def str_translate__Bytearray_ANY_ANY(space, w_bytearray1, w_table, w_deletechars):
- # XXX slow, copies *twice* needs proper implementation
- w_str_copy = str__Bytearray(space, w_bytearray1)
- w_res = stringobject.str_translate__String_ANY_ANY(space, w_str_copy,
- w_table, w_deletechars)
- return String2Bytearray(space, w_res)
-
-# Mostly copied from repr__String, but without the "smart quote"
-# functionality.
-def repr__Bytearray(space, w_bytearray):
- s = w_bytearray.data
-
- # Good default if there are no replacements.
- buf = StringBuilder(len("bytearray(b'')") + len(s))
-
- buf.append("bytearray(b'")
-
- for i in range(len(s)):
- c = s[i]
-
- if c == '\\' or c == "'":
- buf.append('\\')
- buf.append(c)
- elif c == '\t':
- buf.append('\\t')
- elif c == '\r':
- buf.append('\\r')
- elif c == '\n':
- buf.append('\\n')
- elif not '\x20' <= c < '\x7f':
- n = ord(c)
- buf.append('\\x')
- buf.append("0123456789abcdef"[n>>4])
- buf.append("0123456789abcdef"[n&0xF])
- else:
- buf.append(c)
-
- buf.append("')")
-
- return space.wrap(buf.build())
-
-def str__Bytearray(space, w_bytearray):
- return space.wrap(''.join(w_bytearray.data))
-
-def str_count__Bytearray_ANY_ANY_ANY(space, w_bytearray, w_char, w_start, w_stop):
- w_char = space.wrap(space.bufferstr_new_w(w_char))
- w_str = str__Bytearray(space, w_bytearray)
- return stringobject.str_count__String_String_ANY_ANY(space, w_str, w_char,
- w_start, w_stop)
-
-def str_index__Bytearray_ANY_ANY_ANY(space, w_bytearray, w_char, w_start, w_stop):
- w_char = space.wrap(space.bufferstr_new_w(w_char))
- w_str = str__Bytearray(space, w_bytearray)
- return stringobject.str_index__String_String_ANY_ANY(space, w_str, w_char,
- w_start, w_stop)
-
-def str_rindex__Bytearray_ANY_ANY_ANY(space, w_bytearray, w_char, w_start, w_stop):
- w_char = space.wrap(space.bufferstr_new_w(w_char))
- w_str = str__Bytearray(space, w_bytearray)
- return stringobject.str_rindex__String_String_ANY_ANY(space, w_str, w_char,
- w_start, w_stop)
-
-def str_find__Bytearray_ANY_ANY_ANY(space, w_bytearray, w_char, w_start, w_stop):
- w_char = space.wrap(space.bufferstr_new_w(w_char))
- w_str = str__Bytearray(space, w_bytearray)
- return stringobject.str_find__String_String_ANY_ANY(space, w_str, w_char,
- w_start, w_stop)
-
-def str_rfind__Bytearray_ANY_ANY_ANY(space, w_bytearray, w_char, w_start, w_stop):
- w_char = space.wrap(space.bufferstr_new_w(w_char))
- w_str = str__Bytearray(space, w_bytearray)
- return stringobject.str_rfind__String_String_ANY_ANY(space, w_str, w_char,
- w_start, w_stop)
-
-def str_startswith__Bytearray_ANY_ANY_ANY(space, w_bytearray, w_prefix, w_start, w_stop):
- if space.isinstance_w(w_prefix, space.w_tuple):
- w_str = str__Bytearray(space, w_bytearray)
- w_prefix = space.newtuple([space.wrap(space.bufferstr_new_w(w_entry)) for w_entry in
- space.fixedview(w_prefix)])
- return stringobject.str_startswith__String_ANY_ANY_ANY(space, w_str, w_prefix,
- w_start, w_stop)
-
- w_prefix = space.wrap(space.bufferstr_new_w(w_prefix))
- w_str = str__Bytearray(space, w_bytearray)
- return stringobject.str_startswith__String_String_ANY_ANY(space, w_str, w_prefix,
- w_start, w_stop)
-
-def str_endswith__Bytearray_ANY_ANY_ANY(space, w_bytearray, w_suffix, w_start, w_stop):
- if space.isinstance_w(w_suffix, space.w_tuple):
- w_str = str__Bytearray(space, w_bytearray)
- w_suffix = space.newtuple([space.wrap(space.bufferstr_new_w(w_entry)) for w_entry in
- space.fixedview(w_suffix)])
- return stringobject.str_endswith__String_ANY_ANY_ANY(space, w_str, w_suffix,
- w_start, w_stop)
- w_suffix = space.wrap(space.bufferstr_new_w(w_suffix))
- w_str = str__Bytearray(space, w_bytearray)
- return stringobject.str_endswith__String_String_ANY_ANY(space, w_str, w_suffix,
- w_start, w_stop)
-
+# XXX consider moving to W_BytearrayObject or remove
def str_join__Bytearray_ANY(space, w_self, w_list):
list_w = space.listview(w_list)
if not list_w:
@@ -350,251 +1022,8 @@
newdata.extend([c for c in space.bufferstr_new_w(w_s)])
return W_BytearrayObject(newdata)
-def str_decode__Bytearray_ANY_ANY(space, w_bytearray, w_encoding, w_errors):
- w_str = str__Bytearray(space, w_bytearray)
- return stringobject.str_decode__String_ANY_ANY(space, w_str, w_encoding, w_errors)
-
-def str_islower__Bytearray(space, w_bytearray):
- w_str = str__Bytearray(space, w_bytearray)
- return stringobject.str_islower__String(space, w_str)
-
-def str_isupper__Bytearray(space, w_bytearray):
- w_str = str__Bytearray(space, w_bytearray)
- return stringobject.str_isupper__String(space, w_str)
-
-def str_isalpha__Bytearray(space, w_bytearray):
- w_str = str__Bytearray(space, w_bytearray)
- return stringobject.str_isalpha__String(space, w_str)
-
-def str_isalnum__Bytearray(space, w_bytearray):
- w_str = str__Bytearray(space, w_bytearray)
- return stringobject.str_isalnum__String(space, w_str)
-
-def str_isdigit__Bytearray(space, w_bytearray):
- w_str = str__Bytearray(space, w_bytearray)
- return stringobject.str_isdigit__String(space, w_str)
-
-def str_istitle__Bytearray(space, w_bytearray):
- w_str = str__Bytearray(space, w_bytearray)
- return stringobject.str_istitle__String(space, w_str)
-
-def str_isspace__Bytearray(space, w_bytearray):
- w_str = str__Bytearray(space, w_bytearray)
- return stringobject.str_isspace__String(space, w_str)
-
-def bytearray_insert__Bytearray_Int_ANY(space, w_bytearray, w_idx, w_other):
- where = space.int_w(w_idx)
- length = len(w_bytearray.data)
- index = get_positive_index(where, length)
- val = getbytevalue(space, w_other)
- w_bytearray.data.insert(index, val)
- return space.w_None
-
-def bytearray_pop__Bytearray_Int(space, w_bytearray, w_idx):
- index = space.int_w(w_idx)
- try:
- result = w_bytearray.data.pop(index)
- except IndexError:
- if not w_bytearray.data:
- raise OperationError(space.w_IndexError, space.wrap(
- "pop from empty bytearray"))
- raise OperationError(space.w_IndexError, space.wrap(
- "pop index out of range"))
- return space.wrap(ord(result))
-
-def bytearray_remove__Bytearray_ANY(space, w_bytearray, w_char):
- char = space.int_w(space.index(w_char))
- try:
- result = w_bytearray.data.remove(chr(char))
- except ValueError:
- raise OperationError(space.w_ValueError, space.wrap(
- "value not found in bytearray"))
-
-def bytearray_reverse__Bytearray(space, w_bytearray):
- w_bytearray.data.reverse()
- return space.w_None
-
_space_chars = ''.join([chr(c) for c in [9, 10, 11, 12, 13, 32]])
-def bytearray_strip__Bytearray_None(space, w_bytearray, w_chars):
- return _strip(space, w_bytearray, _space_chars, 1, 1)
-
-def bytearray_strip__Bytearray_ANY(space, w_bytearray, w_chars):
- return _strip(space, w_bytearray, space.bufferstr_new_w(w_chars), 1, 1)
-
-def bytearray_lstrip__Bytearray_None(space, w_bytearray, w_chars):
- return _strip(space, w_bytearray, _space_chars, 1, 0)
-
-def bytearray_lstrip__Bytearray_ANY(space, w_bytearray, w_chars):
- return _strip(space, w_bytearray, space.bufferstr_new_w(w_chars), 1, 0)
-
-def bytearray_rstrip__Bytearray_None(space, w_bytearray, w_chars):
- return _strip(space, w_bytearray, _space_chars, 0, 1)
-
-def bytearray_rstrip__Bytearray_ANY(space, w_bytearray, w_chars):
- return _strip(space, w_bytearray, space.bufferstr_new_w(w_chars), 0, 1)
-
-# These methods could just delegate to the string implementation,
-# but they have to return a bytearray.
-def str_replace__Bytearray_ANY_ANY_ANY(space, w_bytearray, w_str1, w_str2, w_max):
- w_str = str__Bytearray(space, w_bytearray)
- w_res = stringobject.str_replace__String_ANY_ANY_ANY(space, w_str, w_str1,
- w_str2, w_max)
- return String2Bytearray(space, w_res)
-
-def str_upper__Bytearray(space, w_bytearray):
- w_str = str__Bytearray(space, w_bytearray)
- w_res = stringobject.str_upper__String(space, w_str)
- return String2Bytearray(space, w_res)
-
-def str_lower__Bytearray(space, w_bytearray):
- w_str = str__Bytearray(space, w_bytearray)
- w_res = stringobject.str_lower__String(space, w_str)
- return String2Bytearray(space, w_res)
-
-def str_title__Bytearray(space, w_bytearray):
- w_str = str__Bytearray(space, w_bytearray)
- w_res = stringobject.str_title__String(space, w_str)
- return String2Bytearray(space, w_res)
-
-def str_swapcase__Bytearray(space, w_bytearray):
- w_str = str__Bytearray(space, w_bytearray)
- w_res = stringobject.str_swapcase__String(space, w_str)
- return String2Bytearray(space, w_res)
-
-def str_capitalize__Bytearray(space, w_bytearray):
- w_str = str__Bytearray(space, w_bytearray)
- w_res = stringobject.str_capitalize__String(space, w_str)
- return String2Bytearray(space, w_res)
-
-def str_ljust__Bytearray_ANY_ANY(space, w_bytearray, w_width, w_fillchar):
- w_str = str__Bytearray(space, w_bytearray)
- w_res = stringobject.str_ljust__String_ANY_ANY(space, w_str, w_width,
- w_fillchar)
- return String2Bytearray(space, w_res)
-
-def str_rjust__Bytearray_ANY_ANY(space, w_bytearray, w_width, w_fillchar):
- w_str = str__Bytearray(space, w_bytearray)
- w_res = stringobject.str_rjust__String_ANY_ANY(space, w_str, w_width,
- w_fillchar)
- return String2Bytearray(space, w_res)
-
-def str_center__Bytearray_ANY_ANY(space, w_bytearray, w_width, w_fillchar):
- w_str = str__Bytearray(space, w_bytearray)
- w_res = stringobject.str_center__String_ANY_ANY(space, w_str, w_width,
- w_fillchar)
- return String2Bytearray(space, w_res)
-
-def str_zfill__Bytearray_ANY(space, w_bytearray, w_width):
- w_str = str__Bytearray(space, w_bytearray)
- w_res = stringobject.str_zfill__String_ANY(space, w_str, w_width)
- return String2Bytearray(space, w_res)
-
-def str_expandtabs__Bytearray_ANY(space, w_bytearray, w_tabsize):
- w_str = str__Bytearray(space, w_bytearray)
- w_res = stringobject.str_expandtabs__String_ANY(space, w_str, w_tabsize)
- return String2Bytearray(space, w_res)
-
-def str_splitlines__Bytearray_ANY(space, w_bytearray, w_keepends):
- 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))
- for w_entry in space.unpackiterable(w_result)
- ])
-
-def str_split__Bytearray_ANY_ANY(space, w_bytearray, w_by, w_maxsplit=-1):
- w_str = str__Bytearray(space, w_bytearray)
- if not space.is_w(w_by, space.w_None):
- w_by = space.wrap(space.bufferstr_new_w(w_by))
- w_list = space.call_method(w_str, "split", w_by, w_maxsplit)
- length = space.int_w(space.len(w_list))
- for i in range(length):
- w_i = space.wrap(i)
- space.setitem(w_list, w_i, String2Bytearray(space, space.getitem(w_list, w_i)))
- return w_list
-
-def str_rsplit__Bytearray_ANY_ANY(space, w_bytearray, w_by, w_maxsplit=-1):
- w_str = str__Bytearray(space, w_bytearray)
- if not space.is_w(w_by, space.w_None):
- w_by = space.wrap(space.bufferstr_new_w(w_by))
- w_list = space.call_method(w_str, "rsplit", w_by, w_maxsplit)
- length = space.int_w(space.len(w_list))
- for i in range(length):
- w_i = space.wrap(i)
- space.setitem(w_list, w_i, String2Bytearray(space, space.getitem(w_list, w_i)))
- return w_list
-
-def str_partition__Bytearray_ANY(space, w_bytearray, w_sub):
- w_str = str__Bytearray(space, w_bytearray)
- w_sub = space.wrap(space.bufferstr_new_w(w_sub))
- w_tuple = stringobject.str_partition__String_String(space, w_str, w_sub)
- w_a, w_b, w_c = space.fixedview(w_tuple, 3)
- return space.newtuple([
- String2Bytearray(space, w_a),
- String2Bytearray(space, w_b),
- String2Bytearray(space, w_c)])
-
-def str_rpartition__Bytearray_ANY(space, w_bytearray, w_sub):
- w_str = str__Bytearray(space, w_bytearray)
- w_sub = space.wrap(space.bufferstr_new_w(w_sub))
- w_tuple = stringobject.str_rpartition__String_String(space, w_str, w_sub)
- w_a, w_b, w_c = space.fixedview(w_tuple, 3)
- return space.newtuple([
- String2Bytearray(space, w_a),
- String2Bytearray(space, w_b),
- String2Bytearray(space, w_c)])
-
-# __________________________________________________________
-# Mutability methods
-
-def bytearray_append__Bytearray_ANY(space, w_bytearray, w_item):
- from pypy.objspace.std.bytearraytype import getbytevalue
- w_bytearray.data.append(getbytevalue(space, w_item))
-
-def bytearray_extend__Bytearray_Bytearray(space, w_bytearray, w_other):
- w_bytearray.data += w_other.data
-
-def bytearray_extend__Bytearray_ANY(space, w_bytearray, w_other):
- w_bytearray.data += makebytearraydata_w(space, w_other)
-
-def inplace_add__Bytearray_Bytearray(space, w_bytearray1, w_bytearray2):
- bytearray_extend__Bytearray_Bytearray(space, w_bytearray1, w_bytearray2)
- return w_bytearray1
-
-def inplace_add__Bytearray_ANY(space, w_bytearray1, w_iterable2):
- w_bytearray1.data += space.bufferstr_new_w(w_iterable2)
- return w_bytearray1
-
-def setitem__Bytearray_ANY_ANY(space, w_bytearray, w_index, w_item):
- from pypy.objspace.std.bytearraytype import getbytevalue
- idx = space.getindex_w(w_index, space.w_IndexError, "bytearray index")
- try:
- w_bytearray.data[idx] = getbytevalue(space, w_item)
- except IndexError:
- raise OperationError(space.w_IndexError,
- space.wrap("bytearray index out of range"))
-
-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)
- _setitem_slice_helper(space, w_bytearray.data, start, step, slicelength, sequence2, empty_elem='\x00')
-
-def delitem__Bytearray_ANY(space, w_bytearray, w_idx):
- idx = space.getindex_w(w_idx, space.w_IndexError, "bytearray index")
- 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)
-
#XXX share the code again with the stuff in listobject.py
def _delitem_slice_helper(space, items, start, step, slicelength):
if slicelength==0:
@@ -673,27 +1102,6 @@
items[start] = sequence2[i]
start += step
-def _strip(space, w_bytearray, u_chars, left, right):
- # note: mostly copied from stringobject._strip
- # should really be shared
- u_self = w_bytearray.data
-
- lpos = 0
- rpos = len(u_self)
-
- if left:
- while lpos < rpos and u_self[lpos] in u_chars:
- lpos += 1
-
- if right:
- while rpos > lpos and u_self[rpos - 1] in u_chars:
- rpos -= 1
- assert rpos >= 0
-
- return new_bytearray(space, space.w_bytearray, u_self[lpos:rpos])
-
-# __________________________________________________________
-# Buffer interface
class BytearrayBuffer(RWBuffer):
def __init__(self, data):
@@ -707,10 +1115,3 @@
def setitem(self, index, char):
self.data[index] = char
-
-def buffer__Bytearray(space, self):
- b = BytearrayBuffer(self.data)
- return space.wrap(b)
-
-from pypy.objspace.std import bytearraytype
-register_all(vars(), bytearraytype)
diff --git a/pypy/objspace/std/bytearraytype.py b/pypy/objspace/std/bytearraytype.py
deleted file mode 100644
--- a/pypy/objspace/std/bytearraytype.py
+++ /dev/null
@@ -1,176 +0,0 @@
-from pypy.interpreter.baseobjspace import ObjSpace, W_Root
-from pypy.interpreter.error import OperationError
-from pypy.interpreter.gateway import interp2app
-from pypy.objspace.std.register_all import register_all
-from pypy.objspace.std.stdtypedef import StdTypeDef, SMM
-
-from pypy.objspace.std.stringtype import (
- str_decode,
- str_count, str_index, str_rindex, str_find, str_rfind, str_replace,
- str_startswith, str_endswith, str_islower, str_isupper, str_isalpha,
- str_isalnum, str_isdigit, str_isspace, str_istitle,
- str_upper, str_lower, str_title, str_swapcase, str_capitalize,
- str_expandtabs, str_ljust, str_rjust, str_center, str_zfill,
- str_join, str_split, str_rsplit, str_partition, str_rpartition,
- str_splitlines, str_translate)
-
-from rpython.rlib.objectmodel import newlist_hint, resizelist_hint
-
-bytearray_append = SMM('append', 2)
-bytearray_extend = SMM('extend', 2)
-bytearray_insert = SMM('insert', 3,
- doc="B.insert(index, int) -> None\n\n"
- "Insert a single item into the bytearray before "
- "the given index.")
-
-bytearray_pop = SMM('pop', 2, defaults=(-1,),
- doc="B.pop([index]) -> int\n\nRemove and return a "
- "single item from B. If no index\nargument is given, "
- "will pop the last value.")
-
-bytearray_remove = SMM('remove', 2,
- doc="B.remove(int) -> None\n\n"
- "Remove the first occurance of a value in B.")
-
-bytearray_reverse = SMM('reverse', 1,
- doc="B.reverse() -> None\n\n"
- "Reverse the order of the values in B in place.")
-
-bytearray_strip = SMM('strip', 2, defaults=(None,),
- doc="B.strip([bytes]) -> bytearray\n\nStrip leading "
- "and trailing bytes contained in the argument.\nIf "
- "the argument is omitted, strip ASCII whitespace.")
-
-bytearray_lstrip = SMM('lstrip', 2, defaults=(None,),
- doc="B.lstrip([bytes]) -> bytearray\n\nStrip leading "
- "bytes contained in the argument.\nIf the argument is "
- "omitted, strip leading ASCII whitespace.")
-
-bytearray_rstrip = SMM('rstrip', 2, defaults=(None,),
- doc="'B.rstrip([bytes]) -> bytearray\n\nStrip trailing "
- "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)
- W_BytearrayObject.__init__(w_obj, data)
- return w_obj
-
-
-def descr__new__(space, w_bytearraytype, __args__):
- 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
- w_iter = space.iter(w_source)
- length_hint = space.length_hint(w_source, 0)
- data = newlist_hint(length_hint)
- extended = 0
- 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)
- extended += 1
- if extended < length_hint:
- resizelist_hint(data, extended)
- return data
-
-def descr_bytearray__reduce__(space, w_self):
- from pypy.objspace.std.bytearrayobject import W_BytearrayObject
- assert isinstance(w_self, W_BytearrayObject)
- w_dict = w_self.getdict(space)
- if w_dict is None:
- w_dict = space.w_None
- return space.newtuple([
- space.type(w_self), space.newtuple([
- space.wrap(''.join(w_self.data).decode('latin-1')),
- space.wrap('latin-1')]),
- w_dict])
-
-def _hex_digit_to_int(d):
More information about the pypy-commit
mailing list