aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorManuel Jacob <me@manueljacob.de>2014-10-21 15:06:52 +0200
committerManuel Jacob <me@manueljacob.de>2014-10-21 15:06:52 +0200
commite3400e26bacec3422400ed462bdb8a27a7c25798 (patch)
tree666d9d42fa1641109729dad277b3ba3077a70bc3 /pypy/objspace/std/bytesobject.py
parenthg merge remove-remaining-smm (diff)
parenthg merge default (diff)
downloadpypy-e3400e26bacec3422400ed462bdb8a27a7c25798.tar.gz
pypy-e3400e26bacec3422400ed462bdb8a27a7c25798.tar.bz2
pypy-e3400e26bacec3422400ed462bdb8a27a7c25798.zip
hg merge remove-remaining-smm
Diffstat (limited to 'pypy/objspace/std/bytesobject.py')
-rw-r--r--pypy/objspace/std/bytesobject.py144
1 files changed, 118 insertions, 26 deletions
diff --git a/pypy/objspace/std/bytesobject.py b/pypy/objspace/std/bytesobject.py
index 0a16283954..7e948d62f7 100644
--- a/pypy/objspace/std/bytesobject.py
+++ b/pypy/objspace/std/bytesobject.py
@@ -3,10 +3,10 @@
from rpython.rlib.jit import we_are_jitted
from rpython.rlib.objectmodel import (
compute_hash, compute_unique_id, import_from_mixin)
+from rpython.rlib.buffer import StringBuffer
from rpython.rlib.rstring import StringBuilder, replace
from pypy.interpreter.baseobjspace import W_Root
-from pypy.interpreter.buffer import StringBuffer
from pypy.interpreter.error import OperationError, oefmt
from pypy.interpreter.gateway import (
WrappedDefault, interp2app, interpindirect2app, unwrap_spec)
@@ -424,15 +424,13 @@ class W_AbstractBytesObject(W_Root):
of the specified width. The string S is never truncated.
"""
- def descr_buffer(self, space):
- pass
-
class W_BytesObject(W_AbstractBytesObject):
import_from_mixin(StringMethods)
_immutable_fields_ = ['_value']
def __init__(self, str):
+ assert str is not None
self._value = str
def __repr__(self):
@@ -445,6 +443,19 @@ class W_BytesObject(W_AbstractBytesObject):
def str_w(self, space):
return self._value
+ def buffer_w(self, space, flags):
+ space.check_buf_flags(flags, True)
+ return StringBuffer(self._value)
+
+ def readbuf_w(self, space):
+ return StringBuffer(self._value)
+
+ def writebuf_w(self, space):
+ raise OperationError(space.w_TypeError, space.wrap(
+ "Cannot use string as modifiable buffer"))
+
+ charbuf_w = str_w
+
def listview_bytes(self):
return _create_list_from_bytes(self._value)
@@ -467,12 +478,22 @@ class W_BytesObject(W_AbstractBytesObject):
def _len(self):
return len(self._value)
- def _val(self, space):
- return self._value
+ _val = str_w
- def _op_val(self, space, w_other):
- return space.bufferstr_w(w_other)
- #return w_other._value
+ @staticmethod
+ def _use_rstr_ops(space, w_other):
+ from pypy.objspace.std.unicodeobject import W_UnicodeObject
+ return (isinstance(w_other, W_BytesObject) or
+ isinstance(w_other, W_UnicodeObject))
+
+ @staticmethod
+ def _op_val(space, w_other):
+ try:
+ return space.str_w(w_other)
+ except OperationError, e:
+ if not e.match(space, space.w_TypeError):
+ raise
+ return space.charbuf_w(w_other)
def _chr(self, char):
assert len(char) == 1
@@ -567,9 +588,6 @@ class W_BytesObject(W_AbstractBytesObject):
def descr_mod(self, space, w_values):
return mod_format(space, self, w_values, do_unicode=False)
- def descr_buffer(self, space):
- return space.wrap(StringBuffer(self._value))
-
def descr_eq(self, space, w_other):
if space.config.objspace.std.withstrbuf:
from pypy.objspace.std.strbufobject import W_StringBufferObject
@@ -689,19 +707,7 @@ class W_BytesObject(W_AbstractBytesObject):
new_is_unicode = space.isinstance_w(w_new, space.w_unicode)
if old_is_unicode or new_is_unicode:
self_as_uni = unicode_from_encoded_object(space, self, None, None)
- if not old_is_unicode:
- w_old = unicode_from_encoded_object(space, w_old, None, None)
- if not new_is_unicode:
- w_new = unicode_from_encoded_object(space, w_new, None, None)
- input = self_as_uni._val(space)
- sub = self_as_uni._op_val(space, w_old)
- by = self_as_uni._op_val(space, w_new)
- try:
- res = replace(input, sub, by, count)
- except OverflowError:
- raise oefmt(space.w_OverflowError,
- "replace string is too long")
- return self_as_uni._new(res)
+ return self_as_uni.descr_replace(space, w_old, w_new, count)
return self._StringMethods_descr_replace(space, w_old, w_new, count)
_StringMethods_descr_join = descr_join
@@ -713,6 +719,92 @@ class W_BytesObject(W_AbstractBytesObject):
return space.wrap(self._val(space).join(l))
return self._StringMethods_descr_join(space, w_list)
+ _StringMethods_descr_split = descr_split
+ @unwrap_spec(maxsplit=int)
+ def descr_split(self, space, w_sep=None, maxsplit=-1):
+ if w_sep is not None and space.isinstance_w(w_sep, space.w_unicode):
+ self_as_uni = unicode_from_encoded_object(space, self, None, None)
+ return self_as_uni.descr_split(space, w_sep, maxsplit)
+ return self._StringMethods_descr_split(space, w_sep, maxsplit)
+
+ _StringMethods_descr_rsplit = descr_rsplit
+ @unwrap_spec(maxsplit=int)
+ def descr_rsplit(self, space, w_sep=None, maxsplit=-1):
+ if w_sep is not None and space.isinstance_w(w_sep, space.w_unicode):
+ self_as_uni = unicode_from_encoded_object(space, self, None, None)
+ return self_as_uni.descr_rsplit(space, w_sep, maxsplit)
+ return self._StringMethods_descr_rsplit(space, w_sep, maxsplit)
+
+ _StringMethods_descr_strip = descr_strip
+ def descr_strip(self, space, w_chars=None):
+ if w_chars is not None and space.isinstance_w(w_chars, space.w_unicode):
+ self_as_uni = unicode_from_encoded_object(space, self, None, None)
+ return self_as_uni.descr_strip(space, w_chars)
+ return self._StringMethods_descr_strip(space, w_chars)
+
+ _StringMethods_descr_lstrip = descr_lstrip
+ def descr_lstrip(self, space, w_chars=None):
+ if w_chars is not None and space.isinstance_w(w_chars, space.w_unicode):
+ self_as_uni = unicode_from_encoded_object(space, self, None, None)
+ return self_as_uni.descr_lstrip(space, w_chars)
+ return self._StringMethods_descr_lstrip(space, w_chars)
+
+ _StringMethods_descr_rstrip = descr_rstrip
+ def descr_rstrip(self, space, w_chars=None):
+ if w_chars is not None and space.isinstance_w(w_chars, space.w_unicode):
+ self_as_uni = unicode_from_encoded_object(space, self, None, None)
+ return self_as_uni.descr_rstrip(space, w_chars)
+ return self._StringMethods_descr_rstrip(space, w_chars)
+
+ _StringMethods_descr_count = descr_count
+ def descr_count(self, space, w_sub, w_start=None, w_end=None):
+ if space.isinstance_w(w_sub, space.w_unicode):
+ self_as_uni = unicode_from_encoded_object(space, self, None, None)
+ return self_as_uni.descr_count(space, w_sub, w_start, w_end)
+ return self._StringMethods_descr_count(space, w_sub, w_start, w_end)
+
+ _StringMethods_descr_find = descr_find
+ def descr_find(self, space, w_sub, w_start=None, w_end=None):
+ if space.isinstance_w(w_sub, space.w_unicode):
+ self_as_uni = unicode_from_encoded_object(space, self, None, None)
+ return self_as_uni.descr_find(space, w_sub, w_start, w_end)
+ return self._StringMethods_descr_find(space, w_sub, w_start, w_end)
+
+ _StringMethods_descr_rfind = descr_rfind
+ def descr_rfind(self, space, w_sub, w_start=None, w_end=None):
+ if space.isinstance_w(w_sub, space.w_unicode):
+ self_as_uni = unicode_from_encoded_object(space, self, None, None)
+ return self_as_uni.descr_rfind(space, w_sub, w_start, w_end)
+ return self._StringMethods_descr_rfind(space, w_sub, w_start, w_end)
+
+ _StringMethods_descr_index = descr_index
+ def descr_index(self, space, w_sub, w_start=None, w_end=None):
+ if space.isinstance_w(w_sub, space.w_unicode):
+ self_as_uni = unicode_from_encoded_object(space, self, None, None)
+ return self_as_uni.descr_index(space, w_sub, w_start, w_end)
+ return self._StringMethods_descr_index(space, w_sub, w_start, w_end)
+
+ _StringMethods_descr_rindex = descr_rindex
+ def descr_rindex(self, space, w_sub, w_start=None, w_end=None):
+ if space.isinstance_w(w_sub, space.w_unicode):
+ self_as_uni = unicode_from_encoded_object(space, self, None, None)
+ return self_as_uni.descr_rindex(space, w_sub, w_start, w_end)
+ return self._StringMethods_descr_rindex(space, w_sub, w_start, w_end)
+
+ _StringMethods_descr_partition = descr_partition
+ def descr_partition(self, space, w_sub):
+ if space.isinstance_w(w_sub, space.w_unicode):
+ self_as_uni = unicode_from_encoded_object(space, self, None, None)
+ return self_as_uni.descr_partition(space, w_sub)
+ return self._StringMethods_descr_partition(space, w_sub)
+
+ _StringMethods_descr_rpartition = descr_rpartition
+ def descr_rpartition(self, space, w_sub):
+ if space.isinstance_w(w_sub, space.w_unicode):
+ self_as_uni = unicode_from_encoded_object(space, self, None, None)
+ return self_as_uni.descr_rpartition(space, w_sub)
+ return self._StringMethods_descr_rpartition(space, w_sub)
+
def _join_return_one(self, space, w_obj):
return (space.is_w(space.type(w_obj), space.w_str) or
space.is_w(space.type(w_obj), space.w_unicode))
@@ -853,13 +945,13 @@ W_BytesObject.typedef = TypeDef(
format = interpindirect2app(W_BytesObject.descr_format),
__format__ = interpindirect2app(W_BytesObject.descr__format__),
__mod__ = interpindirect2app(W_BytesObject.descr_mod),
- __buffer__ = interpindirect2app(W_AbstractBytesObject.descr_buffer),
__getnewargs__ = interpindirect2app(
W_AbstractBytesObject.descr_getnewargs),
_formatter_parser = interp2app(W_BytesObject.descr_formatter_parser),
_formatter_field_name_split =
interp2app(W_BytesObject.descr_formatter_field_name_split),
)
+W_BytesObject.typedef.flag_sequence_bug_compat = True
def string_escape_encode(s, quote):