Author: Philip Jenvey <pjen...@underboss.org> Branch: py3k Changeset: r68928:a27d22674995 Date: 2014-01-24 16:52 -0800 http://bitbucket.org/pypy/pypy/changeset/a27d22674995/
Log: merge default 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 @@ -389,6 +389,9 @@ class W_Float64Box(W_FloatingBox, PrimitiveBox): descr__new__, _get_dtype, descr_reduce = new_dtype_getter("float64") + def descr_as_integer_ratio(self, space): + return space.call_method(self.item(space), 'as_integer_ratio') + class W_ComplexFloatingBox(W_InexactBox): def descr_get_real(self, space): dtype = self._COMPONENTS_BOX._get_dtype(space) @@ -715,6 +718,7 @@ __module__ = "numpy", __new__ = interp2app(W_Float64Box.descr__new__.im_func), __reduce__ = interp2app(W_Float64Box.descr_reduce), + as_integer_ratio = interp2app(W_Float64Box.descr_as_integer_ratio), ) W_ComplexFloatingBox.typedef = TypeDef("complexfloating", W_InexactBox.typedef, diff --git a/pypy/module/micronumpy/test/test_scalar.py b/pypy/module/micronumpy/test/test_scalar.py --- a/pypy/module/micronumpy/test/test_scalar.py +++ b/pypy/module/micronumpy/test/test_scalar.py @@ -181,6 +181,11 @@ s = np.dtype([('a', 'int64'), ('b', 'int64')]).type('a' * 16) assert s.view('S16') == 'a' * 16 + def test_as_integer_ratio(self): + import numpy as np + raises(AttributeError, 'np.float32(1.5).as_integer_ratio()') + assert np.float64(1.5).as_integer_ratio() == (3, 2) + def test_complex_scalar_complex_cast(self): import numpy as np for tp in [np.csingle, np.cdouble, np.clongdouble]: 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,21 +1,22 @@ """The builtin bytearray implementation""" +from rpython.rlib.objectmodel import ( + import_from_mixin, newlist_hint, resizelist_hint) +from rpython.rlib.rstring import StringBuilder + from pypy.interpreter.baseobjspace import W_Root from pypy.interpreter.buffer import RWBuffer from pypy.interpreter.error import OperationError, operationerrfmt from pypy.objspace.std.bytesobject import ( getbytevalue, makebytesdata_w, newbytesdata_w) -from pypy.interpreter.gateway import interp2app, unwrap_spec, WrappedDefault +from pypy.interpreter.gateway import WrappedDefault, interp2app, unwrap_spec 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 import_from_mixin -from rpython.rlib.rstring import StringBuilder +NON_HEX_MSG = "non-hexadecimal number found in fromhex() arg at position %d" -def _make_data(s): - return [s[i] for i in range(len(s))] class W_BytearrayObject(W_Root): import_from_mixin(StringMethods) @@ -24,7 +25,7 @@ w_self.data = data def __repr__(w_self): - """ representation for debugging purposes """ + """representation for debugging purposes""" return "%s(%s)" % (w_self.__class__.__name__, ''.join(w_self.data)) def _new(self, value): @@ -126,11 +127,6 @@ @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')." if not space.is_w(space.type(w_hexstring), space.w_unicode): raise operationerrfmt(space.w_TypeError, "must be str, not %T", w_hexstring) @@ -168,8 +164,8 @@ elif not '\x20' <= c < '\x7f': n = ord(c) buf.append('\\x') - buf.append("0123456789abcdef"[n>>4]) - buf.append("0123456789abcdef"[n&0xF]) + buf.append("0123456789abcdef"[n >> 4]) + buf.append("0123456789abcdef"[n & 0xF]) else: buf.append(c) @@ -185,51 +181,60 @@ def descr_eq(self, space, w_other): try: - return space.newbool(self._val(space) == self._op_val(space, w_other)) - except OperationError, e: + res = self._val(space) == self._op_val(space, w_other) + except OperationError as e: if e.match(space, space.w_TypeError): return space.w_NotImplemented raise + return space.newbool(res) def descr_ne(self, space, w_other): try: - return space.newbool(self._val(space) != self._op_val(space, w_other)) - except OperationError, e: + res = self._val(space) != self._op_val(space, w_other) + except OperationError as e: if e.match(space, space.w_TypeError): return space.w_NotImplemented raise + return space.newbool(res) def descr_lt(self, space, w_other): try: - return space.newbool(self._val(space) < self._op_val(space, w_other)) - except OperationError, e: + res = self._val(space) < self._op_val(space, w_other) + except OperationError as e: if e.match(space, space.w_TypeError): return space.w_NotImplemented raise + return space.newbool(res) def descr_le(self, space, w_other): try: - return space.newbool(self._val(space) <= self._op_val(space, w_other)) - except OperationError, e: + res = self._val(space) <= self._op_val(space, w_other) + except OperationError as e: if e.match(space, space.w_TypeError): return space.w_NotImplemented raise + return space.newbool(res) def descr_gt(self, space, w_other): try: - return space.newbool(self._val(space) > self._op_val(space, w_other)) - except OperationError, e: + res = self._val(space) > self._op_val(space, w_other) + except OperationError as e: if e.match(space, space.w_TypeError): return space.w_NotImplemented raise + return space.newbool(res) def descr_ge(self, space, w_other): try: - return space.newbool(self._val(space) >= self._op_val(space, w_other)) - except OperationError, e: + res = self._val(space) >= self._op_val(space, w_other) + except OperationError as e: if e.match(space, space.w_TypeError): return space.w_NotImplemented raise + return space.newbool(res) + + def descr_iter(self, space): + return space.newseqiter(self) def descr_buffer(self, space): return BytearrayBuffer(self.data) @@ -244,7 +249,7 @@ def descr_inplace_mul(self, space, w_times): try: times = space.getindex_w(w_times, space.w_OverflowError) - except OperationError, e: + except OperationError as e: if e.match(space, space.w_TypeError): return space.w_NotImplemented raise @@ -259,12 +264,13 @@ _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") + 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")) + raise operationerrfmt(space.w_IndexError, + "bytearray index out of range") def descr_delitem(self, space, w_idx): if isinstance(w_idx, W_SliceObject): @@ -272,12 +278,13 @@ len(self.data)) _delitem_slice_helper(space, self.data, start, step, slicelength) else: - idx = space.getindex_w(w_idx, space.w_IndexError, "bytearray index") + 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")) + raise operationerrfmt(space.w_IndexError, + "bytearray deletion index out of range") def descr_append(self, space, w_item): self.data.append(getbytevalue(space, w_item)) @@ -304,10 +311,9 @@ 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")) + raise operationerrfmt(space.w_IndexError, + "pop from empty bytearray") + raise operationerrfmt(space.w_IndexError, "pop index out of range") return space.wrap(ord(result)) def descr_remove(self, space, w_char): @@ -315,12 +321,39 @@ try: self.data.remove(chr(char)) except ValueError: - raise OperationError(space.w_ValueError, space.wrap( - "value not found in bytearray")) + raise operationerrfmt(space.w_ValueError, + "value not found in bytearray") + + _StringMethods_descr_contains = descr_contains + def descr_contains(self, space, w_sub): + if space.isinstance_w(w_sub, space.w_int): + char = space.int_w(w_sub) + return _descr_contains_bytearray(self.data, space, char) + return self._StringMethods_descr_contains(space, w_sub) def descr_reverse(self, space): self.data.reverse() + +# ____________________________________________________________ +# helpers for slow paths, moved out because they contain loops + +def _make_data(s): + return [s[i] for i in range(len(s))] + + +def _descr_contains_bytearray(data, space, char): + if not 0 <= char < 256: + raise operationerrfmt(space.w_ValueError, + "byte must be in range(0, 256)") + for c in data: + if ord(c) == char: + return space.w_True + return space.w_False + +# ____________________________________________________________ + + def new_bytearray(space, w_bytearraytype, data): w_obj = space.allocate_instance(W_BytearrayObject, w_bytearraytype) W_BytearrayObject.__init__(w_obj, data) @@ -490,12 +523,12 @@ 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. + 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(): @@ -532,7 +565,7 @@ """ def fromhex(): - """bytearray.fromhex(string) -> bytearray (static method) + r"""bytearray.fromhex(string) -> bytearray (static method) Create a bytearray object from a string of hexadecimal numbers. Spaces between two numbers are accepted. @@ -816,6 +849,8 @@ __ge__ = interp2app(W_BytearrayObject.descr_ge, doc=BytearrayDocstrings.__ge__.__doc__), + __iter__ = interp2app(W_BytearrayObject.descr_iter, + doc=BytearrayDocstrings.__iter__.__doc__), __len__ = interp2app(W_BytearrayObject.descr_len, doc=BytearrayDocstrings.__len__.__doc__), __contains__ = interp2app(W_BytearrayObject.descr_contains, @@ -953,9 +988,10 @@ _space_chars = ''.join([chr(c) for c in [9, 10, 11, 12, 13, 32]]) -#XXX share the code again with the stuff in listobject.py + +# XXX share the code again with the stuff in listobject.py def _delitem_slice_helper(space, items, start, step, slicelength): - if slicelength==0: + if slicelength == 0: return if step < 0: @@ -985,6 +1021,7 @@ assert start >= 0 # annotator hint del items[start:] + def _setitem_slice_helper(space, items, start, step, slicelength, sequence2, empty_elem): assert slicelength >= 0 diff --git a/pypy/objspace/std/bytesobject.py b/pypy/objspace/std/bytesobject.py --- a/pypy/objspace/std/bytesobject.py +++ b/pypy/objspace/std/bytesobject.py @@ -1,17 +1,19 @@ """The builtin bytes implementation""" -from pypy.interpreter.baseobjspace import W_Root -from pypy.interpreter.buffer import StringBuffer -from pypy.interpreter.error import OperationError, operationerrfmt -from pypy.interpreter.gateway import interp2app, unwrap_spec, WrappedDefault, interpindirect2app -from pypy.objspace.std.stdtypedef import StdTypeDef -from pypy.objspace.std.stringmethods import StringMethods from rpython.rlib.jit import we_are_jitted from rpython.rlib.objectmodel import ( compute_hash, compute_unique_id, import_from_mixin, newlist_hint, resizelist_hint) from rpython.rlib.rstring import StringBuilder +from pypy.interpreter.baseobjspace import W_Root +from pypy.interpreter.buffer import StringBuffer +from pypy.interpreter.error import OperationError, operationerrfmt +from pypy.interpreter.gateway import ( + WrappedDefault, interp2app, interpindirect2app, unwrap_spec) +from pypy.objspace.std.stdtypedef import StdTypeDef +from pypy.objspace.std.stringmethods import StringMethods + class W_AbstractBytesObject(W_Root): __slots__ = () @@ -273,8 +275,8 @@ """S.rpartition(sep) -> (head, sep, tail) Search for the separator sep in S, starting at the end of S, and return - the part before it, the separator itself, and the part after it. If the - separator is not found, return two empty strings and S. + the part before it, the separator itself, and the part after it. If + the separator is not found, return two empty strings and S. """ @unwrap_spec(maxsplit=int) @@ -387,7 +389,7 @@ self._value = str def __repr__(self): - """ representation for debugging purposes """ + """representation for debugging purposes""" return "%s(%r)" % (self.__class__.__name__, self._value) def unwrap(self, space): @@ -586,7 +588,7 @@ from pypy.objspace.std.strbufobject import W_StringBufferObject try: other = self._op_val(space, w_other) - except OperationError, e: + except OperationError as e: if e.match(space, space.w_TypeError): return space.w_NotImplemented raise @@ -613,11 +615,14 @@ return space.newbool(self._value.find(chr(char)) >= 0) return self._StringMethods_descr_contains(space, w_sub) - def descr_lower(self, space): - return W_BytesObject(self._value.lower()) - - def descr_upper(self, space): - return W_BytesObject(self._value.upper()) + _StringMethods_descr_join = descr_join + def descr_join(self, space, w_list): + l = space.listview_bytes(w_list) + if l is not None: + if len(l) == 1: + return space.wrap(l[0]) + return space.wrap(self._val(space).join(l)) + return self._StringMethods_descr_join(space, w_list) def _join_return_one(self, space, w_obj): return space.is_w(space.type(w_obj), space.w_str) @@ -631,6 +636,12 @@ return True return False + def descr_lower(self, space): + return W_BytesObject(self._value.lower()) + + def descr_upper(self, space): + return W_BytesObject(self._value.upper()) + def _create_list_from_bytes(value): # need this helper function to allow the jit to look inside and inline @@ -658,6 +669,7 @@ return W_BytesObject.EMPTY return W_BytesObject(s) + def wrapchar(space, c): if space.config.objspace.std.withprebuiltchar and not we_are_jitted(): return W_BytesObject.PREBUILT[ord(c)] @@ -822,7 +834,8 @@ zfill = interpindirect2app(W_AbstractBytesObject.descr_zfill), __buffer__ = interpindirect2app(W_AbstractBytesObject.descr_buffer), - __getnewargs__ = interpindirect2app(W_AbstractBytesObject.descr_getnewargs), + __getnewargs__ = interpindirect2app( + W_AbstractBytesObject.descr_getnewargs), fromhex = interp2app(W_BytesObject.descr_fromhex, as_classmethod=True), maketrans = interp2app(W_BytesObject.descr_maketrans, as_classmethod=True), @@ -864,8 +877,8 @@ buf.append_slice(s, startslice, i) startslice = i + 1 buf.append('\\x') - buf.append("0123456789abcdef"[n>>4]) - buf.append("0123456789abcdef"[n&0xF]) + buf.append("0123456789abcdef"[n >> 4]) + buf.append("0123456789abcdef"[n & 0xF]) if use_bs_char: if i != startslice: diff --git a/pypy/objspace/std/listobject.py b/pypy/objspace/std/listobject.py --- a/pypy/objspace/std/listobject.py +++ b/pypy/objspace/std/listobject.py @@ -166,9 +166,9 @@ # XXX: BytesListStrategy is currently broken #@staticmethod - #def newlist_bytes(space, list_s): + #def newlist_bytes(space, list_b): # strategy = space.fromcache(BytesListStrategy) - # storage = strategy.erase(list_s) + # storage = strategy.erase(list_b) # return W_ListObject.from_storage_and_strategy(space, storage, strategy) @staticmethod diff --git a/pypy/objspace/std/stringmethods.py b/pypy/objspace/std/stringmethods.py --- a/pypy/objspace/std/stringmethods.py +++ b/pypy/objspace/std/stringmethods.py @@ -1,18 +1,22 @@ -from pypy.interpreter.error import OperationError, operationerrfmt -from pypy.interpreter.gateway import unwrap_spec, WrappedDefault -from pypy.objspace.std import slicetype -from pypy.objspace.std.sliceobject import W_SliceObject +"""Functionality shared between bytes/bytearray/unicode""" + from rpython.rlib import jit from rpython.rlib.objectmodel import specialize from rpython.rlib.rarithmetic import ovfcheck -from rpython.rlib.rstring import split, rsplit, replace, startswith, endswith +from rpython.rlib.rstring import endswith, replace, rsplit, split, startswith + +from pypy.interpreter.error import OperationError, operationerrfmt +from pypy.interpreter.gateway import WrappedDefault, unwrap_spec +from pypy.objspace.std import slicetype +from pypy.objspace.std.sliceobject import W_SliceObject class StringMethods(object): def _sliced(self, space, s, start, stop, orig_obj): assert start >= 0 assert stop >= 0 - #if start == 0 and stop == len(s) and space.is_w(space.type(orig_obj), space.w_str): + #if start == 0 and stop == len(s) and space.is_w(space.type(orig_obj), + # space.w_str): # return orig_obj return self._new(s[start:stop]) @@ -21,7 +25,7 @@ value = self._val(space) lenself = len(value) start, end = slicetype.unwrap_start_stop( - space, lenself, w_start, w_end, upper_bound=upper_bound) + space, lenself, w_start, w_end, upper_bound=upper_bound) return (value, start, end) @staticmethod @@ -57,17 +61,14 @@ # pass def descr_contains(self, space, w_sub): - from pypy.objspace.std.bytearrayobject import W_BytearrayObject - if (isinstance(self, W_BytearrayObject) and - space.isinstance_w(w_sub, space.w_int)): - char = space.int_w(w_sub) - return _descr_contains_bytearray(self.data, space, char) - return space.newbool(self._val(space).find(self._op_val(space, w_sub)) >= 0) + value = self._val(space) + other = self._op_val(space, w_sub) + return space.newbool(value.find(other) >= 0) def descr_add(self, space, w_other): try: other = self._op_val(space, w_other) - except OperationError, e: + except OperationError as e: if e.match(space, space.w_TypeError): return space.w_NotImplemented raise @@ -76,7 +77,7 @@ def descr_mul(self, space, w_times): try: times = space.getindex_w(w_times, space.w_OverflowError) - except OperationError, e: + except OperationError as e: if e.match(space, space.w_TypeError): return space.w_NotImplemented raise @@ -108,13 +109,12 @@ if index < 0: index += selflen if index < 0 or index >= selflen: - raise OperationError(space.w_IndexError, - space.wrap("string index out of range")) + raise operationerrfmt(space.w_IndexError, + "string index out of range") from pypy.objspace.std.bytesobject import W_BytesObject from pypy.objspace.std.bytearrayobject import W_BytearrayObject if isinstance(self, W_BytesObject) or isinstance(self, W_BytearrayObject): return space.wrap(ord(selfvalue[index])) - #return wrapchar(space, selfvalue[index]) return self._new(selfvalue[index]) def descr_capitalize(self, space): @@ -133,27 +133,30 @@ value = self._val(space) fillchar = self._op_val(space, w_fillchar) if len(fillchar) != 1: - raise OperationError(space.w_TypeError, - space.wrap("center() argument 2 must be a single character")) + raise operationerrfmt(space.w_TypeError, + "center() argument 2 must be a single " + "character") d = width - len(value) - if d>0: + if d > 0: offset = d//2 + (d & width & 1) fillchar = fillchar[0] # annotator hint: it's a single character - u_centered = offset * fillchar + value + (d - offset) * fillchar + centered = offset * fillchar + value + (d - offset) * fillchar else: - u_centered = value + centered = value - return self._new(u_centered) + return self._new(centered) def descr_count(self, space, w_sub, w_start=None, w_end=None): value, start, end = self._convert_idx_params(space, w_start, w_end) - return space.newint(value.count(self._op_val(space, w_sub), start, end)) + return space.newint(value.count(self._op_val(space, w_sub), start, + end)) def descr_decode(self, space, w_encoding=None, w_errors=None): from pypy.objspace.std.unicodeobject import ( _get_encoding_and_errors, decode_object) - encoding, errors = _get_encoding_and_errors(space, w_encoding, w_errors) + encoding, errors = _get_encoding_and_errors(space, w_encoding, + w_errors) return decode_object(space, self, encoding, errors) @unwrap_spec(tabsize=int) @@ -166,18 +169,19 @@ try: ovfcheck(len(splitted) * tabsize) except OverflowError: - raise OperationError(space.w_OverflowError, - space.wrap("new string is too long")) + raise operationerrfmt(space.w_OverflowError, + "new string is too long") expanded = oldtoken = splitted.pop(0) for token in splitted: - expanded += self._chr(' ') * self._tabindent(oldtoken, tabsize) + token + expanded += self._chr(' ') * self._tabindent(oldtoken, + tabsize) + token oldtoken = token return self._new(expanded) def _tabindent(self, token, tabsize): - "calculates distance behind the token to the next tabstop" + """calculates distance behind the token to the next tabstop""" if tabsize <= 0: return tabsize @@ -216,8 +220,8 @@ (value, start, end) = self._convert_idx_params(space, w_start, w_end) res = value.find(self._op_val(space, w_sub), start, end) if res < 0: - raise OperationError(space.w_ValueError, - space.wrap("substring not found in string.index")) + raise operationerrfmt(space.w_ValueError, + "substring not found in string.index") return space.wrap(res) @@ -225,8 +229,8 @@ (value, start, end) = self._convert_idx_params(space, w_start, w_end) res = value.rfind(self._op_val(space, w_sub), start, end) if res < 0: - raise OperationError(space.w_ValueError, - space.wrap("substring not found in string.rindex")) + raise operationerrfmt(space.w_ValueError, + "substring not found in string.rindex") return space.wrap(res) @@ -320,15 +324,6 @@ return space.newbool(cased) def descr_join(self, space, w_list): - from pypy.objspace.std.unicodeobject import W_UnicodeObject - - if isinstance(self, W_UnicodeObject): - l = space.listview_unicode(w_list) - if l is not None: - if len(l) == 1: - return space.wrap(l[0]) - return space.wrap(self._val(space).join(l)) - list_w = space.listview(w_list) size = len(list_w) @@ -370,9 +365,9 @@ value = self._val(space) fillchar = self._op_val(space, w_fillchar) if len(fillchar) != 1: - raise OperationError(space.w_TypeError, - space.wrap("ljust() argument 2 must be a single character")) - + raise operationerrfmt(space.w_TypeError, + "ljust() argument 2 must be a single " + "character") d = width - len(value) if d > 0: fillchar = fillchar[0] # annotator hint: it's a single character @@ -385,9 +380,9 @@ value = self._val(space) fillchar = self._op_val(space, w_fillchar) if len(fillchar) != 1: - raise OperationError(space.w_TypeError, - space.wrap("rjust() argument 2 must be a single character")) - + raise operationerrfmt(space.w_TypeError, + "rjust() argument 2 must be a single " + "character") d = width - len(value) if d > 0: fillchar = fillchar[0] # annotator hint: it's a single character @@ -406,8 +401,7 @@ value = self._val(space) sub = self._op_val(space, w_sub) if not sub: - raise OperationError(space.w_ValueError, - space.wrap("empty separator")) + raise operationerrfmt(space.w_ValueError, "empty separator") pos = value.find(sub) if pos == -1: from pypy.objspace.std.bytearrayobject import W_BytearrayObject @@ -426,8 +420,7 @@ value = self._val(space) sub = self._op_val(space, w_sub) if not sub: - raise OperationError(space.w_ValueError, - space.wrap("empty separator")) + raise operationerrfmt(space.w_ValueError, "empty separator") pos = value.rfind(sub) if pos == -1: from pypy.objspace.std.bytearrayobject import W_BytearrayObject @@ -450,8 +443,8 @@ try: res = replace(input, sub, by, count) except OverflowError: - raise OperationError(space.w_OverflowError, - space.wrap("replace string is too long")) + raise operationerrfmt(space.w_OverflowError, + "replace string is too long") return self._new(res) @unwrap_spec(maxsplit=int) @@ -466,7 +459,7 @@ by = self._op_val(space, w_sep) bylen = len(by) if bylen == 0: - raise OperationError(space.w_ValueError, space.wrap("empty separator")) + raise operationerrfmt(space.w_ValueError, "empty separator") res = split(value, by, maxsplit) return self._newlist_unwrapped(space, res) @@ -481,7 +474,7 @@ by = self._op_val(space, w_sep) bylen = len(by) if bylen == 0: - raise OperationError(space.w_ValueError, space.wrap("empty separator")) + raise operationerrfmt(space.w_ValueError, "empty separator") res = rsplit(value, by, maxsplit) return self._newlist_unwrapped(space, res) @@ -533,9 +526,8 @@ return startswith(value, self._op_val(space, w_prefix), start, end) def descr_endswith(self, space, w_suffix, w_start=None, w_end=None): - (value, start, end) = self._convert_idx_params(space, w_start, - w_end, True) - + (value, start, end) = self._convert_idx_params(space, w_start, w_end, + True) if space.isinstance_w(w_suffix, space.w_tuple): for w_suffix in space.fixedview(w_suffix): if self._endswith(space, value, w_suffix, start, end): @@ -558,18 +550,17 @@ def _strip(self, space, w_chars, left, right): "internal function called by str_xstrip methods" value = self._val(space) - u_chars = self._op_val(space, w_chars) + chars = self._op_val(space, w_chars) lpos = 0 rpos = len(value) if left: - #print "while %d < %d and -%s- in -%s-:"%(lpos, rpos, value[lpos],w_chars) - while lpos < rpos and value[lpos] in u_chars: + while lpos < rpos and value[lpos] in chars: lpos += 1 if right: - while rpos > lpos and value[rpos - 1] in u_chars: + while rpos > lpos and value[rpos - 1] in chars: rpos -= 1 assert rpos >= lpos # annotator hint, don't remove @@ -583,13 +574,12 @@ rpos = len(value) if left: - #print "while %d < %d and -%s- in -%s-:"%(lpos, rpos, value[lpos],w_chars) while lpos < rpos and self._isspace(value[lpos]): - lpos += 1 + lpos += 1 if right: while rpos > lpos and self._isspace(value[rpos - 1]): - rpos -= 1 + rpos -= 1 assert rpos >= lpos # annotator hint, don't remove return self._sliced(space, value, lpos, rpos, self) @@ -650,9 +640,9 @@ else: table = self._op_val(space, w_table) if len(table) != 256: - raise OperationError( + raise operationerrfmt( space.w_ValueError, - space.wrap("translation table must be 256 characters long")) + "translation table must be 256 characters long") string = self._val(space) deletechars = self._op_val(space, w_deletechars) @@ -704,15 +694,6 @@ # ____________________________________________________________ # helpers for slow paths, moved out because they contain loops -def _descr_contains_bytearray(data, space, char): - if not 0 <= char < 256: - raise OperationError(space.w_ValueError, - space.wrap("byte must be in range(0, 256)")) - for c in data: - if ord(c) == char: - return space.w_True - return space.w_False - @specialize.argtype(0) def _descr_getslice_slowpath(selfvalue, start, step, sl): return [selfvalue[start + i*step] for i in range(sl)] diff --git a/pypy/objspace/std/test/test_bytearrayobject.py b/pypy/objspace/std/test/test_bytearrayobject.py --- a/pypy/objspace/std/test/test_bytearrayobject.py +++ b/pypy/objspace/std/test/test_bytearrayobject.py @@ -130,6 +130,7 @@ def test_iter(self): assert list(bytearray(b'hello')) == [104, 101, 108, 108, 111] + assert list(bytearray(b'hello').__iter__()) == [104, 101, 108, 108, 111] def test_compare(self): assert bytearray(b'hello') == bytearray(b'hello') diff --git a/pypy/objspace/std/unicodeobject.py b/pypy/objspace/std/unicodeobject.py --- a/pypy/objspace/std/unicodeobject.py +++ b/pypy/objspace/std/unicodeobject.py @@ -1,18 +1,21 @@ """The builtin str implementation""" +from rpython.rlib.objectmodel import ( + compute_hash, compute_unique_id, import_from_mixin) +from rpython.rlib.rstring import UnicodeBuilder +from rpython.rlib.runicode import ( + make_unicode_escape_function, str_decode_ascii, str_decode_utf_8, + unicode_encode_ascii, unicode_encode_utf_8) + from pypy.interpreter import unicodehelper from pypy.interpreter.baseobjspace import W_Root from pypy.interpreter.error import OperationError, operationerrfmt -from pypy.interpreter.gateway import interp2app, unwrap_spec, WrappedDefault +from pypy.interpreter.gateway import WrappedDefault, interp2app, unwrap_spec from pypy.module.unicodedata import unicodedb from pypy.objspace.std import newformat from pypy.objspace.std.formatting import mod_format from pypy.objspace.std.stdtypedef import StdTypeDef from pypy.objspace.std.stringmethods import StringMethods -from rpython.rlib.objectmodel import compute_hash, compute_unique_id, import_from_mixin -from rpython.rlib.rstring import UnicodeBuilder -from rpython.rlib.runicode import (str_decode_utf_8, str_decode_ascii, - unicode_encode_utf_8, unicode_encode_ascii, make_unicode_escape_function) __all__ = ['W_UnicodeObject', 'wrapunicode', 'plain_str2unicode', 'encode_object', 'decode_object', 'unicode_from_object', @@ -29,7 +32,7 @@ w_self._utf8 = None def __repr__(w_self): - """ representation for debugging purposes """ + """representation for debugging purposes""" return "%s(%r)" % (w_self.__class__.__name__, w_self._value) def unwrap(w_self, space): @@ -158,19 +161,20 @@ return space.newlist_unicode(lst) @staticmethod - @unwrap_spec(w_object = WrappedDefault(u'')) + @unwrap_spec(w_object=WrappedDefault(u'')) def descr_new(space, w_unicodetype, w_object=None, w_encoding=None, w_errors=None): # NB. the default value of w_obj is really a *wrapped* empty string: # there is gateway magic at work w_obj = w_object - encoding, errors = _get_encoding_and_errors(space, w_encoding, w_errors) + encoding, errors = _get_encoding_and_errors(space, w_encoding, + w_errors) if encoding is None and errors is None: w_value = unicode_from_object(space, w_obj) else: - w_value = unicode_from_encoded_object(space, w_obj, - encoding, errors) + w_value = unicode_from_encoded_object(space, w_obj, encoding, + errors) if space.is_w(w_unicodetype, space.w_unicode): return w_value @@ -265,51 +269,57 @@ def descr_eq(self, space, w_other): try: - return space.newbool(self._val(space) == self._op_val(space, w_other)) - except OperationError, e: + res = self._val(space) == self._op_val(space, w_other) + except OperationError as e: if e.match(space, space.w_TypeError): return space.w_NotImplemented raise + return space.newbool(res) def descr_ne(self, space, w_other): try: - return space.newbool(self._val(space) != self._op_val(space, w_other)) - except OperationError, e: + res = self._val(space) != self._op_val(space, w_other) + except OperationError as e: if e.match(space, space.w_TypeError): return space.w_NotImplemented raise + return space.newbool(res) def descr_lt(self, space, w_other): try: - return space.newbool(self._val(space) < self._op_val(space, w_other)) - except OperationError, e: + res = self._val(space) < self._op_val(space, w_other) + except OperationError as e: if e.match(space, space.w_TypeError): return space.w_NotImplemented raise + return space.newbool(res) def descr_le(self, space, w_other): try: - return space.newbool(self._val(space) <= self._op_val(space, w_other)) - except OperationError, e: + res = self._val(space) <= self._op_val(space, w_other) + except OperationError as e: if e.match(space, space.w_TypeError): return space.w_NotImplemented raise + return space.newbool(res) def descr_gt(self, space, w_other): try: - return space.newbool(self._val(space) > self._op_val(space, w_other)) - except OperationError, e: + res = self._val(space) > self._op_val(space, w_other) + except OperationError as e: if e.match(space, space.w_TypeError): return space.w_NotImplemented raise + return space.newbool(res) def descr_ge(self, space, w_other): try: - return space.newbool(self._val(space) >= self._op_val(space, w_other)) - except OperationError, e: + res = self._val(space) >= self._op_val(space, w_other) + except OperationError as e: if e.match(space, space.w_TypeError): return space.w_NotImplemented raise + return space.newbool(res) def descr_format(self, space, __args__): w_kwds = space.newdict() @@ -336,12 +346,13 @@ def descr_translate(self, space, w_table): selfvalue = self._value w_sys = space.getbuiltinmodule('sys') - maxunicode = space.int_w(space.getattr(w_sys, space.wrap("maxunicode"))) + maxunicode = space.int_w(space.getattr(w_sys, + space.wrap("maxunicode"))) result = [] for unichar in selfvalue: try: w_newval = space.getitem(w_table, space.wrap(ord(unichar))) - except OperationError, e: + except OperationError as e: if e.match(space, space.w_LookupError): result.append(unichar) else: @@ -352,22 +363,32 @@ elif space.isinstance_w(w_newval, space.w_int): newval = space.int_w(w_newval) if newval < 0 or newval > maxunicode: - raise OperationError( - space.w_TypeError, - space.wrap("character mapping must be in range(0x%x)" % (maxunicode + 1,))) + msg = ("character mapping must be in range(0x%x)" % + (maxunicode + 1,)) + raise operationerrfmt(space.w_TypeError, msg) result.append(unichr(newval)) elif space.isinstance_w(w_newval, space.w_unicode): result.append(space.unicode_w(w_newval)) else: - raise operationerrfmt( - space.w_TypeError, - "character mapping must return integer, None or str") + raise operationerrfmt(space.w_TypeError, + "character mapping must return " + "integer, None or str") return W_UnicodeObject(u''.join(result)) def descr_encode(self, space, w_encoding=None, w_errors=None): - encoding, errors = _get_encoding_and_errors(space, w_encoding, w_errors) + encoding, errors = _get_encoding_and_errors(space, w_encoding, + w_errors) return encode_object(space, self, encoding, errors) + _StringMethods_descr_join = descr_join + def descr_join(self, space, w_list): + l = space.listview_unicode(w_list) + if l is not None: + if len(l) == 1: + return space.wrap(l[0]) + return space.wrap(self._val(space).join(l)) + return self._StringMethods_descr_join(space, w_list) + def _join_return_one(self, space, w_obj): return space.is_w(space.type(w_obj), space.w_unicode) @@ -424,6 +445,7 @@ def wrapunicode(space, uni): return W_UnicodeObject(uni) + def plain_str2unicode(space, s): try: return unicode(s) @@ -468,17 +490,13 @@ def getdefaultencoding(space): return space.sys.defaultencoding + def _get_encoding_and_errors(space, w_encoding, w_errors): - if space.is_none(w_encoding): - encoding = None - else: - encoding = space.str_w(w_encoding) - if space.is_none(w_errors): - errors = None - else: - errors = space.str_w(w_errors) + encoding = None if space.is_none(w_encoding) else space.str_w(w_encoding) + errors = None if space.is_none(w_errors) else space.str_w(w_errors) return encoding, errors + def encode_object(space, w_object, encoding, errors): if encoding is None: # Get the encoder functions as a wrapped object. @@ -505,10 +523,12 @@ w_restuple = space.call_function(w_encoder, w_object, w_errors) w_retval = space.getitem(w_restuple, space.wrap(0)) if not space.isinstance_w(w_retval, space.w_bytes): - msg = "encoder did not return a bytes string (type '%T')" - raise operationerrfmt(space.w_TypeError, msg, w_retval) + raise operationerrfmt( + space.w_TypeError, + "encoder did not return a bytes object (type '%T')", w_retval) return w_retval + def decode_object(space, w_obj, encoding, errors): if encoding is None: encoding = getdefaultencoding(space) @@ -543,6 +563,7 @@ assert isinstance(w_retval, W_UnicodeObject) return w_retval + def unicode_from_object(space, w_obj): if space.is_w(space.type(w_obj), space.w_unicode): return w_obj @@ -714,8 +735,8 @@ def format(): """S.format(*args, **kwargs) -> unicode - Return a formatted version of S, using substitutions from args and kwargs. - The substitutions are identified by braces ('{' and '}'). + Return a formatted version of S, using substitutions from args and + kwargs. The substitutions are identified by braces ('{' and '}'). """ def format_map(): @@ -874,16 +895,16 @@ def rjust(): """S.rjust(width[, fillchar]) -> unicode - Return S right-justified in a Unicode string of length width. Padding is - done using the specified fill character (default is a space). + Return S right-justified in a Unicode string of length width. Padding + is done using the specified fill character (default is a space). """ def rpartition(): """S.rpartition(sep) -> (head, sep, tail) Search for the separator sep in S, starting at the end of S, and return - the part before it, the separator itself, and the part after it. If the - separator is not found, return two empty strings and S. + the part before it, the separator itself, and the part after it. If + the separator is not found, return two empty strings and S. """ def rsplit(): diff --git a/rpython/jit/metainterp/pyjitpl.py b/rpython/jit/metainterp/pyjitpl.py --- a/rpython/jit/metainterp/pyjitpl.py +++ b/rpython/jit/metainterp/pyjitpl.py @@ -594,9 +594,11 @@ if tobox is not None: # sanity check: see whether the current struct value # corresponds to what the cache thinks the value is - resbox = executor.execute(self.metainterp.cpu, self.metainterp, - rop.GETFIELD_GC, fielddescr, box) - assert resbox.constbox().same_constant(tobox.constbox()) + # XXX pypy with the following check fails on micronumpy, + # XXX investigate + #resbox = executor.execute(self.metainterp.cpu, self.metainterp, + # rop.GETFIELD_GC, fielddescr, box) + #assert resbox.constbox().same_constant(tobox.constbox()) return tobox resbox = self.execute_with_descr(opnum, fielddescr, box) self.metainterp.heapcache.getfield_now_known(box, fielddescr, resbox) _______________________________________________ pypy-commit mailing list pypy-commit@python.org https://mail.python.org/mailman/listinfo/pypy-commit