[pypy-commit] pypy default: some of the _convert_idx_params implementations are rather nonsensical (and of
cfbolz
noreply at buildbot.pypy.org
Fri Nov 4 10:50:20 CET 2011
Author: Carl Friedrich Bolz <cfbolz at gmx.de>
Branch:
Changeset: r48730:c0f86162108b
Date: 2011-11-03 20:50 +0100
http://bitbucket.org/pypy/pypy/changeset/c0f86162108b/
Log: some of the _convert_idx_params implementations are rather
nonsensical (and of course they are copied here and there, with
slight variations). fix that by putting one version into
slicetype.py
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
@@ -283,17 +283,9 @@
return space.wrap(''.join(w_bytearray.data))
def _convert_idx_params(space, w_self, w_start, w_stop):
- start = slicetype.eval_slice_index(space, w_start)
- stop = slicetype.eval_slice_index(space, w_stop)
length = len(w_self.data)
- if start < 0:
- start += length
- if start < 0:
- start = 0
- if stop < 0:
- stop += length
- if stop < 0:
- stop = 0
+ start, stop = slicetype.unwrap_start_stop(
+ space, length, w_start, w_stop, False)
return start, stop, length
def str_count__Bytearray_Int_ANY_ANY(space, w_bytearray, w_char, w_start, w_stop):
diff --git a/pypy/objspace/std/ropeobject.py b/pypy/objspace/std/ropeobject.py
--- a/pypy/objspace/std/ropeobject.py
+++ b/pypy/objspace/std/ropeobject.py
@@ -357,16 +357,8 @@
self = w_self._node
sub = w_sub._node
- if space.is_w(w_start, space.w_None):
- w_start = space.wrap(0)
- if space.is_w(w_end, space.w_None):
- w_end = space.len(w_self)
- if upper_bound:
- start = slicetype.adapt_bound(space, self.length(), w_start)
- end = slicetype.adapt_bound(space, self.length(), w_end)
- else:
- start = slicetype.adapt_lower_bound(space, self.length(), w_start)
- end = slicetype.adapt_lower_bound(space, self.length(), w_end)
+ start, end = slicetype.unwrap_start_stop(
+ space, self.length(), w_start, w_end, upper_bound)
return (self, sub, start, end)
_convert_idx_params._annspecialcase_ = 'specialize:arg(5)'
diff --git a/pypy/objspace/std/slicetype.py b/pypy/objspace/std/slicetype.py
--- a/pypy/objspace/std/slicetype.py
+++ b/pypy/objspace/std/slicetype.py
@@ -3,6 +3,7 @@
from pypy.objspace.std.stdtypedef import StdTypeDef, SMM
from pypy.objspace.std.register_all import register_all
from pypy.interpreter.error import OperationError
+from pypy.rlib.objectmodel import specialize
# indices multimehtod
slice_indices = SMM('indices', 2,
@@ -40,6 +41,23 @@
assert index >= 0
return index
+ at specialize.arg(4)
+def unwrap_start_stop(space, size, w_start, w_end, upper_bound):
+ if space.is_w(w_start, space.w_None):
+ start = 0
+ elif upper_bound:
+ start = adapt_bound(space, size, w_start)
+ else:
+ start = adapt_lower_bound(space, size, w_start)
+
+ if space.is_w(w_end, space.w_None):
+ end = size
+ elif upper_bound:
+ end = adapt_bound(space, size, w_end)
+ else:
+ end = adapt_lower_bound(space, size, w_end)
+ return start, end
+
register_all(vars(), globals())
# ____________________________________________________________
diff --git a/pypy/objspace/std/stringobject.py b/pypy/objspace/std/stringobject.py
--- a/pypy/objspace/std/stringobject.py
+++ b/pypy/objspace/std/stringobject.py
@@ -422,18 +422,11 @@
def _convert_idx_params(space, w_self, w_sub, w_start, w_end, upper_bound=False):
self = w_self._value
+ lenself = len(self)
sub = w_sub._value
- if space.is_w(w_start, space.w_None):
- w_start = space.wrap(0)
- if space.is_w(w_end, space.w_None):
- w_end = space.len(w_self)
- if upper_bound:
- start = slicetype.adapt_bound(space, len(self), w_start)
- end = slicetype.adapt_bound(space, len(self), w_end)
- else:
- start = slicetype.adapt_lower_bound(space, len(self), w_start)
- end = slicetype.adapt_lower_bound(space, len(self), w_end)
+ start, end = slicetype.unwrap_start_stop(
+ space, lenself, w_start, w_end, upper_bound=upper_bound)
return (self, sub, start, end)
_convert_idx_params._annspecialcase_ = 'specialize:arg(5)'
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
@@ -479,18 +479,8 @@
assert isinstance(w_sub, W_UnicodeObject)
self = w_self._value
sub = w_sub._value
-
- if space.is_w(w_start, space.w_None):
- w_start = space.wrap(0)
- if space.is_w(w_end, space.w_None):
- w_end = space.len(w_self)
-
- if upper_bound:
- start = slicetype.adapt_bound(space, len(self), w_start)
- end = slicetype.adapt_bound(space, len(self), w_end)
- else:
- start = slicetype.adapt_lower_bound(space, len(self), w_start)
- end = slicetype.adapt_lower_bound(space, len(self), w_end)
+ start, end = slicetype.unwrap_start_stop(
+ space, len(self), w_start, w_end, upper_bound)
return (self, sub, start, end)
_convert_idx_params._annspecialcase_ = 'specialize:arg(5)'
More information about the pypy-commit
mailing list