[pypy-commit] pypy remove-string-smm: Some progress on bytearray.
hodgestar
noreply at buildbot.pypy.org
Tue Apr 16 21:16:56 CEST 2013
Author: Simon Cross <hodgestar at gmail.com>
Branch: remove-string-smm
Changeset: r63424:b2a605627eba
Date: 2013-04-16 21:16 +0200
http://bitbucket.org/pypy/pypy/changeset/b2a605627eba/
Log: Some progress on bytearray.
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,32 +1,23 @@
from pypy.interpreter.error import OperationError, operationerrfmt
-from pypy.objspace.std.model import registerimplementation, W_Object
+from pypy.objspace.std.model import registerimplementation
from pypy.objspace.std.register_all import register_all
from pypy.objspace.std.inttype import wrapint
from pypy.objspace.std.multimethod import FailedToImplement
-from pypy.objspace.std.noneobject import W_NoneObject
-from rpython.rlib.rarithmetic import intmask
from rpython.rlib.rstring import StringBuilder
-from rpython.rlib.debug import check_annotation
from pypy.objspace.std import stringobject
-from pypy.objspace.std.intobject import W_IntObject
-from pypy.objspace.std.listobject import get_positive_index, get_list_index
-from pypy.objspace.std.sliceobject import W_SliceObject, normalize_simple_slice
-from pypy.objspace.std.stringobject import W_StringObject
-from pypy.objspace.std.strutil import ParseStringError
-from pypy.objspace.std.strutil import string_to_float
-from pypy.objspace.std.tupleobject import W_TupleObject
-from pypy.objspace.std.unicodeobject import W_UnicodeObject
-from pypy.objspace.std import slicetype
-from pypy.interpreter import gateway
+from pypy.objspace.std.listobject import get_list_index
+from pypy.interpreter.gateway import unwrap_spec
from pypy.interpreter.buffer import RWBuffer
from pypy.interpreter.signature import Signature
from pypy.objspace.std.bytearraytype import (
- makebytearraydata_w, getbytevalue,
- new_bytearray
-)
-from rpython.tool.sourcetools import func_with_new_name
-from pypy.objspace.std.contiguousstring import StringMethods
+ makebytearraydata_w, new_bytearray)
from pypy.objspace.std.bytearraytype import W_AbstractBytearrayObject
+from pypy.objspace.std.unicodeobject import W_UnicodeObject # XXX: kill this whem SMMs are dead
+from pypy.objspace.std.noneobject import W_NoneObject # XXX: and this one.
+from pypy.objspace.std.stringobject import W_StringObject # XXX: and this too.
+from pypy.objspace.std.tupleobject import W_TupleObject # XXX: ...
+from pypy.objspace.std.intobject import W_IntObject # XXX: ...
+from pypy.objspace.std.sliceobject import W_SliceObject # XXX: WTF.
class W_BytearrayObject(W_AbstractBytearrayObject):
@@ -35,10 +26,74 @@
def __init__(w_self, data):
w_self.data = data
+ def descr_ljust(self, space, arg, fillchar=' '):
+ u_self = self.data
+ if len(fillchar) != 1:
+ raise OperationError(space.w_TypeError,
+ space.wrap("ljust() argument 2 must be a single character"))
+
+ d = arg - len(u_self)
+ if d > 0:
+ lst = [0] * max(arg, len(u_self))
+ fillchar = fillchar[0] # annotator hint: it's a single character
+ lst[:len(u_self)] = u_self
+ for i in range(d):
+ lst[len(u_self) + i] = fillchar
+ else:
+ lst = u_self.data[:]
+
+ return space.newbytearray(lst)
+
+ def bytearray_rjust(self, space, arg, fillchar=' '):
+ u_self = self.data
+ if len(fillchar) != 1:
+ raise OperationError(space.w_TypeError,
+ space.wrap("rjust() argument 2 must be a single character"))
+
+ d = arg - len(u_self)
+ if d > 0:
+ lst = [0] * max(arg, len(u_self))
+ fillchar = fillchar[0] # annotator hint: it's a single character
+ for i in range(d):
+ lst[i] = fillchar
+ lst[len(u_self) - 1:] = u_self
+ else:
+ lst = u_self.data[:]
+
+ return space.newbytearray(lst)
+
+ def descr_insert(self, space, index, w_val):
+ if isinstance(val, int):
+ val = chr(val)
+ elif len(val) != 1:
+ raise OperationError(space.w_ValueError,
+ space.wrap("string must be of size 1"))
+ self.data.insert(index, val)
+ return space.w_None
+
+ def descr_pop(self, space, index=-1):
+ 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, value):
+ try:
+ self.data.remove(chr(value))
+ except ValueError:
+ raise OperationError(space.w_ValueError, space.wrap(
+ "value not found in bytearray"))
+
def __repr__(w_self):
""" representation for debugging purposes """
return "%s(%s)" % (w_self.__class__.__name__, ''.join(w_self.data))
+
registerimplementation(W_BytearrayObject)
init_signature = Signature(['source', 'encoding', 'errors'], None, None)
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
@@ -1,10 +1,8 @@
-from pypy.interpreter.baseobjspace import W_Root
from pypy.objspace.std.model import W_Object
from pypy.interpreter.error import OperationError
-from pypy.interpreter.gateway import interp2app, unwrap_spec
-from pypy.objspace.std.register_all import register_all
+from pypy.interpreter.gateway import (
+ interpindirect2app, interp2app, unwrap_spec)
from pypy.objspace.std.stdtypedef import StdTypeDef, SMM
-from pypy.objspace.std.listobject import get_positive_index
from pypy.objspace.std.stringtype import (
str_decode,
@@ -17,11 +15,52 @@
str_splitlines, str_translate)
from rpython.rlib.objectmodel import newlist_hint, resizelist_hint
-from pypy.objspace.std.bytearrayinterface import bytearray_interface_methods
class W_AbstractBytearrayObject(W_Object):
- pass
+ @unwrap_spec(arg=int, fillchar=str)
+ def descr_ljust(self, space, arg, fillchar=' '):
+ """S.ljust(width[, fillchar]) -> string
+
+ Return S left justified in a string of length width. Padding
+ is done using the specified fill character (default is a space).
+ """
+ raise NotImplementedError
+
+ @unwrap_spec(arg=int, fillchar=str)
+ def descr_rjust(self, space, arg, fillchar=' '):
+ """S.rjust(width[, fillchar]) -> string
+
+ Return S right justified in a string of length width. Padding
+ is done using the specified fill character (default is a space).
+ """
+ raise NotImplementedError
+
+ @unwrap_spec(index=int)
+ def descr_insert(self, space, index, w_val):
+ """B.insert(index, int) -> None
+
+ Insert a single item into the bytearray before the given index.
+ """
+ raise NotImplementedError
+
+ @unwrap_spec(index=int)
+ def descr_pop(self, space, index=-1):
+ """B.pop([index]) -> int
+
+ Remove and return a single item from B. If no index
+ argument is given, will pop the last value.
+ """
+ raise NotImplementedError
+
+ @unwrap_spec(value='index')
+ def descr_remove(self, space, value):
+ """B.remove(int) -> None
+
+ Remove the first occurance of a value in B.
+ """
+ raise NotImplementedError
+
str_join = SMM('join', 2, defaults=(None,-1))
@@ -29,103 +68,6 @@
bytearray_extend = SMM('extend', 2)
- at unwrap_spec(w_self=W_Root, arg=int, fillchar=str)
-def bytearray_ljust(w_self, space, arg, fillchar=' '):
- """S.ljust(width[, fillchar]) -> string
-
- Return S left justified in a string of length width. Padding
- is done using the specified fill character (default is a space).
- """
- assert isinstance(w_self, W_AbstractBytearrayObject)
- u_self = w_self.data
- if len(fillchar) != 1:
- raise OperationError(space.w_TypeError,
- space.wrap("ljust() argument 2 must be a single character"))
-
- d = arg - len(u_self)
- if d > 0:
- lst = [0] * max(arg, len(u_self))
- fillchar = fillchar[0] # annotator hint: it's a single character
- lst[:len(u_self)] = u_self
- for i in range(d):
- lst[len(u_self) + i] = fillchar
- else:
- lst = u_self.data[:]
-
- return space.newbytearray(lst)
-
-
- at unwrap_spec(w_self=W_Root, arg=int, fillchar=str)
-def bytearray_rjust(w_self, space, arg, fillchar=' '):
- """S.rjust(width[, fillchar]) -> string
-
- Return S right justified in a string of length width. Padding
- is done using the specified fill character (default is a space).
- """
- u_self = w_self.data
- assert isinstance(w_self, W_AbstractBytearrayObject)
- if len(fillchar) != 1:
- raise OperationError(space.w_TypeError,
- space.wrap("rjust() argument 2 must be a single character"))
-
- d = arg - len(u_self)
- if d > 0:
- lst = [0] * max(arg, len(u_self))
- fillchar = fillchar[0] # annotator hint: it's a single character
- for i in range(d):
- lst[i] = fillchar
- lst[len(u_self)-1:] = u_self
- else:
- lst = u_self.data[:]
-
- return space.newbytearray(lst)
-
-
- at unwrap_spec(index=int, val='str_or_int')
-def bytearray_insert(w_self, space, index, val):
- """B.insert(index, int) -> None
-
- Insert a single item into the bytearray before the given index.
- """
- if isinstance(val, int):
- val = chr(val)
- elif len(val) != 1:
- raise OperationError(space.w_ValueError,
- space.wrap("string must be of size 1"))
- w_self.data.insert(index, val)
- return space.w_None
-
-
- at unwrap_spec(index=int)
-def bytearray_pop(w_self, space, index=-1):
- """B.pop([index]) -> int
-
- Remove and return a single item from B. If no index
- argument is given, will pop the last value.
- """
- try:
- result = w_self.data.pop(index)
- except IndexError:
- if not w_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))
-
-
- at unwrap_spec(value='index')
-def bytearray_remove(w_self, space, value):
- """B.remove(int) -> None
-
- Remove the first occurance of a value in B.
- """
- try:
- w_self.data.remove(chr(value))
- except ValueError:
- raise OperationError(space.w_ValueError, space.wrap(
- "value not found in bytearray"))
-
bytearray_reverse = SMM('reverse', 1,
doc="B.reverse() -> None\n\n"
@@ -267,10 +209,10 @@
__hash__ = None,
__reduce__ = interp2app(descr_bytearray__reduce__),
fromhex = interp2app(descr_fromhex, as_classmethod=True),
- ljust=interp2app(bytearray_ljust),
- rjust=interp2app(bytearray_rjust),
- insert=interp2app(bytearray_insert),
- pop=interp2app(bytearray_pop),
- remove=interp2app(bytearray_remove),
+ ljust=interpindirect2app(W_AbstractBytearrayObject.descr_ljust),
+ rjust=interpindirect2app(W_AbstractBytearrayObject.descr_rjust),
+ insert=interpindirect2app(W_AbstractBytearrayObject.descr_insert),
+ pop=interpindirect2app(W_AbstractBytearrayObject.descr_pop),
+ remove=interpindirect2app(W_AbstractBytearrayObject.descr_remove),
)
bytearray_typedef.registermethods(globals())
More information about the pypy-commit
mailing list