[pypy-commit] pypy remove-remaining-smm: Kill slice SMMs.
Manuel Jacob
noreply at buildbot.pypy.org
Mon Feb 24 15:30:53 CET 2014
Author: Manuel Jacob
Branch: remove-remaining-smm
Changeset: r69354:069b92af2e4e
Date: 2014-02-24 15:12 +0100
http://bitbucket.org/pypy/pypy/changeset/069b92af2e4e/
Log: Kill slice SMMs.
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
@@ -16,13 +16,13 @@
interp2app)
from pypy.interpreter.generator import GeneratorIterator
from pypy.interpreter.signature import Signature
-from pypy.objspace.std import slicetype
from pypy.objspace.std.bytesobject import W_BytesObject
from pypy.objspace.std.floatobject import W_FloatObject
from pypy.objspace.std.intobject import W_IntObject
from pypy.objspace.std.iterobject import (W_FastListIterObject,
W_ReverseSeqIterObject)
-from pypy.objspace.std.sliceobject import W_SliceObject, normalize_simple_slice
+from pypy.objspace.std.sliceobject import (W_SliceObject, unwrap_start_stop,
+ normalize_simple_slice)
from pypy.objspace.std.stdtypedef import StdTypeDef
from pypy.objspace.std.tupleobject import W_AbstractTupleObject
from pypy.objspace.std.unicodeobject import W_UnicodeObject
@@ -624,8 +624,7 @@
first index of value'''
# needs to be safe against eq_w() mutating the w_list behind our back
size = self.length()
- i, stop = slicetype.unwrap_start_stop(
- space, size, w_start, w_stop, True)
+ i, stop = unwrap_start_stop(space, size, w_start, w_stop, True)
try:
i = self.find(w_value, i, stop)
except ValueError:
diff --git a/pypy/objspace/std/model.py b/pypy/objspace/std/model.py
--- a/pypy/objspace/std/model.py
+++ b/pypy/objspace/std/model.py
@@ -33,7 +33,6 @@
class result:
from pypy.objspace.std.objecttype import object_typedef
from pypy.objspace.std.typeobject import type_typedef
- from pypy.objspace.std.slicetype import slice_typedef
self.pythontypes = [value for key, value in result.__dict__.items()
if not key.startswith('_')] # don't look
@@ -82,6 +81,7 @@
self.pythontypes.append(longobject.W_LongObject.typedef)
self.pythontypes.append(floatobject.W_FloatObject.typedef)
self.pythontypes.append(complexobject.W_ComplexObject.typedef)
+ self.pythontypes.append(sliceobject.W_SliceObject.typedef)
# the set of implementation types
self.typeorder = {
diff --git a/pypy/objspace/std/sliceobject.py b/pypy/objspace/std/sliceobject.py
--- a/pypy/objspace/std/sliceobject.py
+++ b/pypy/objspace/std/sliceobject.py
@@ -1,13 +1,14 @@
"""Slice object"""
+from pypy.interpreter import gateway
+from pypy.interpreter.baseobjspace import W_Root
from pypy.interpreter.error import OperationError
-from pypy.interpreter import gateway
-from pypy.objspace.std.model import registerimplementation, W_Object
-from pypy.objspace.std.register_all import register_all
-from pypy.objspace.std.slicetype import _eval_slice_index
+from pypy.interpreter.typedef import GetSetProperty
+from pypy.objspace.std.stdtypedef import StdTypeDef
+from rpython.rlib.objectmodel import specialize
-class W_SliceObject(W_Object):
- from pypy.objspace.std.slicetype import slice_typedef as typedef
+
+class W_SliceObject(W_Root):
_immutable_fields_ = ['w_start', 'w_stop', 'w_step']
def __init__(w_self, w_start, w_stop, w_step):
@@ -83,11 +84,156 @@
return "<W_SliceObject(%r, %r, %r)>" % (
self.w_start, self.w_stop, self.w_step)
-registerimplementation(W_SliceObject)
+ @staticmethod
+ def descr__new__(space, w_slicetype, args_w):
+ from pypy.objspace.std.sliceobject import W_SliceObject
+ w_start = space.w_None
+ w_stop = space.w_None
+ w_step = space.w_None
+ if len(args_w) == 1:
+ w_stop, = args_w
+ elif len(args_w) == 2:
+ w_start, w_stop = args_w
+ elif len(args_w) == 3:
+ w_start, w_stop, w_step = args_w
+ elif len(args_w) > 3:
+ raise OperationError(space.w_TypeError,
+ space.wrap("slice() takes at most 3 arguments"))
+ else:
+ raise OperationError(space.w_TypeError,
+ space.wrap("slice() takes at least 1 argument"))
+ w_obj = space.allocate_instance(W_SliceObject, w_slicetype)
+ W_SliceObject.__init__(w_obj, w_start, w_stop, w_step)
+ return w_obj
+ def descr_repr(self, space):
+ return space.wrap("slice(%s, %s, %s)" % (
+ space.str_w(space.repr(self.w_start)),
+ space.str_w(space.repr(self.w_stop)),
+ space.str_w(space.repr(self.w_step))))
+
+ def descr__reduce__(self, space):
+ from pypy.objspace.std.sliceobject import W_SliceObject
+ assert isinstance(self, W_SliceObject)
+ return space.newtuple([
+ space.type(self),
+ space.newtuple([self.w_start, self.w_stop, self.w_step])])
+
+ def descr_eq(self, space, w_other):
+ # We need this because CPython considers that slice1 == slice1
+ # is *always* True (e.g. even if slice1 was built with non-comparable
+ # parameters
+ if space.is_w(self, w_other):
+ return space.w_True
+ if space.eq_w(self.w_start, w_other.w_start) and \
+ space.eq_w(self.w_stop, w_other.w_stop) and \
+ space.eq_w(self.w_step, w_other.w_step):
+ return space.w_True
+ else:
+ return space.w_False
+
+ def descr_lt(self, space, w_other):
+ if space.is_w(self, w_other):
+ return space.w_False # see comments in descr_eq()
+ if space.eq_w(self.w_start, w_other.w_start):
+ if space.eq_w(self.w_stop, w_other.w_stop):
+ return space.lt(self.w_step, w_other.w_step)
+ else:
+ return space.lt(self.w_stop, w_other.w_stop)
+ else:
+ return space.lt(self.w_start, w_other.w_start)
+
+ def descr_indices(self, space, w_length):
+ """S.indices(len) -> (start, stop, stride)
+
+ Assuming a sequence of length len, calculate the start and stop
+ indices, and the stride length of the extended slice described by
+ S. Out of bounds indices are clipped in a manner consistent with the
+ handling of normal slices.
+ """
+ length = space.getindex_w(w_length, space.w_OverflowError)
+ start, stop, step = self.indices3(space, length)
+ return space.newtuple([space.wrap(start), space.wrap(stop),
+ space.wrap(step)])
+
+
+def slicewprop(name):
+ def fget(space, w_obj):
+ from pypy.objspace.std.sliceobject import W_SliceObject
+ if not isinstance(w_obj, W_SliceObject):
+ raise OperationError(space.w_TypeError,
+ space.wrap("descriptor is for 'slice'"))
+ return getattr(w_obj, name)
+ return GetSetProperty(fget)
+
+W_SliceObject.typedef = StdTypeDef("slice",
+ __doc__ = '''slice([start,] stop[, step])
+
+Create a slice object. This is used for extended slicing (e.g. a[0:10:2]).''',
+ __new__ = gateway.interp2app(W_SliceObject.descr__new__),
+ __repr__ = gateway.interp2app(W_SliceObject.descr_repr),
+ __hash__ = None,
+ __reduce__ = gateway.interp2app(W_SliceObject.descr__reduce__),
+
+ __eq__ = gateway.interp2app(W_SliceObject.descr_eq),
+ __lt__ = gateway.interp2app(W_SliceObject.descr_lt),
+
+ start = slicewprop('w_start'),
+ stop = slicewprop('w_stop'),
+ step = slicewprop('w_step'),
+ indices = gateway.interp2app(W_SliceObject.descr_indices),
+)
+W_SliceObject.typedef.acceptable_as_base_class = False
+
+
+# utility functions
+def _eval_slice_index(space, w_int):
+ # note that it is the *callers* responsibility to check for w_None
+ # otherwise you can get funny error messages
+ try:
+ return space.getindex_w(w_int, None) # clamp if long integer too large
+ except OperationError, err:
+ if not err.match(space, space.w_TypeError):
+ raise
+ raise OperationError(space.w_TypeError,
+ space.wrap("slice indices must be integers or "
+ "None or have an __index__ method"))
+
+def adapt_lower_bound(space, size, w_index):
+ index = _eval_slice_index(space, w_index)
+ if index < 0:
+ index = index + size
+ if index < 0:
+ index = 0
+ assert index >= 0
+ return index
+
+def adapt_bound(space, size, w_index):
+ index = adapt_lower_bound(space, size, w_index)
+ if index > size:
+ index = size
+ assert index >= 0
+ return index
+
+ at specialize.arg(4)
+def unwrap_start_stop(space, size, w_start, w_end, upper_bound=False):
+ if space.is_none(w_start):
+ start = 0
+ elif upper_bound:
+ start = adapt_bound(space, size, w_start)
+ else:
+ start = adapt_lower_bound(space, size, w_start)
+
+ if space.is_none(w_end):
+ end = size
+ elif upper_bound:
+ end = adapt_bound(space, size, w_end)
+ else:
+ end = adapt_lower_bound(space, size, w_end)
+ return start, end
def normalize_simple_slice(space, length, w_start, w_stop):
- """Helper for the {get,set,del}slice multimethod implementations."""
+ """Helper for the {get,set,del}slice implementations."""
# this returns a pair (start, stop) which is usable for slicing
# a sequence of the given length in the most friendly way, i.e.
# guaranteeing that 0 <= start <= stop <= length.
@@ -103,45 +249,3 @@
if start > length:
start = length
return start, stop
-
-
-repr__Slice = gateway.applevel("""
- def repr__Slice(aslice):
- return 'slice(%r, %r, %r)' % (aslice.start, aslice.stop, aslice.step)
-""", filename=__file__).interphook("repr__Slice")
-
-def eq__Slice_Slice(space, w_slice1, w_slice2):
- # We need this because CPython considers that slice1 == slice1
- # is *always* True (e.g. even if slice1 was built with non-comparable
- # parameters
- if space.is_w(w_slice1, w_slice2):
- return space.w_True
- if space.eq_w(w_slice1.w_start, w_slice2.w_start) and \
- space.eq_w(w_slice1.w_stop, w_slice2.w_stop) and \
- space.eq_w(w_slice1.w_step, w_slice2.w_step):
- return space.w_True
- else:
- return space.w_False
-
-def lt__Slice_Slice(space, w_slice1, w_slice2):
- if space.is_w(w_slice1, w_slice2):
- return space.w_False # see comments in eq__Slice_Slice()
- if space.eq_w(w_slice1.w_start, w_slice2.w_start):
- if space.eq_w(w_slice1.w_stop, w_slice2.w_stop):
- return space.lt(w_slice1.w_step, w_slice2.w_step)
- else:
- return space.lt(w_slice1.w_stop, w_slice2.w_stop)
- else:
- return space.lt(w_slice1.w_start, w_slice2.w_start)
-
-# indices impl
-
-def slice_indices__Slice_ANY(space, w_slice, w_length):
- length = space.getindex_w(w_length, space.w_OverflowError)
- start, stop, step = w_slice.indices3(space, length)
- return space.newtuple([space.wrap(start), space.wrap(stop),
- space.wrap(step)])
-
-# register all methods
-from pypy.objspace.std import slicetype
-register_all(vars(), slicetype)
diff --git a/pypy/objspace/std/slicetype.py b/pypy/objspace/std/slicetype.py
deleted file mode 100644
--- a/pypy/objspace/std/slicetype.py
+++ /dev/null
@@ -1,122 +0,0 @@
-from pypy.interpreter import baseobjspace, gateway
-from pypy.interpreter.typedef import GetSetProperty
-from pypy.objspace.std.stdtypedef import StdTypeDef, SMM
-from pypy.objspace.std.register_all import register_all
-from pypy.interpreter.error import OperationError
-from rpython.rlib.objectmodel import specialize
-
-# indices multimehtod
-slice_indices = SMM('indices', 2,
- doc='S.indices(len) -> (start, stop, stride)\n\nAssuming a'
- ' sequence of length len, calculate the start and'
- ' stop\nindices, and the stride length of the extended'
- ' slice described by\nS. Out of bounds indices are'
- ' clipped in a manner consistent with the\nhandling of'
- ' normal slices.')
-
-# utility functions
-def _eval_slice_index(space, w_int):
- # note that it is the *callers* responsibility to check for w_None
- # otherwise you can get funny error messages
- try:
- return space.getindex_w(w_int, None) # clamp if long integer too large
- except OperationError, err:
- if not err.match(space, space.w_TypeError):
- raise
- raise OperationError(space.w_TypeError,
- space.wrap("slice indices must be integers or "
- "None or have an __index__ method"))
-
-def adapt_lower_bound(space, size, w_index):
- index = _eval_slice_index(space, w_index)
- if index < 0:
- index = index + size
- if index < 0:
- index = 0
- assert index >= 0
- return index
-
-def adapt_bound(space, size, w_index):
- index = adapt_lower_bound(space, size, w_index)
- if index > size:
- index = size
- assert index >= 0
- return index
-
- at specialize.arg(4)
-def unwrap_start_stop(space, size, w_start, w_end, upper_bound=False):
- if space.is_none(w_start):
- start = 0
- elif upper_bound:
- start = adapt_bound(space, size, w_start)
- else:
- start = adapt_lower_bound(space, size, w_start)
-
- if space.is_none(w_end):
- 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())
-
-# ____________________________________________________________
-
-def descr__new__(space, w_slicetype, args_w):
- from pypy.objspace.std.sliceobject import W_SliceObject
- w_start = space.w_None
- w_stop = space.w_None
- w_step = space.w_None
- if len(args_w) == 1:
- w_stop, = args_w
- elif len(args_w) == 2:
- w_start, w_stop = args_w
- elif len(args_w) == 3:
- w_start, w_stop, w_step = args_w
- elif len(args_w) > 3:
- raise OperationError(space.w_TypeError,
- space.wrap("slice() takes at most 3 arguments"))
- else:
- raise OperationError(space.w_TypeError,
- space.wrap("slice() takes at least 1 argument"))
- w_obj = space.allocate_instance(W_SliceObject, w_slicetype)
- W_SliceObject.__init__(w_obj, w_start, w_stop, w_step)
- return w_obj
-
-def descr__reduce__(space, w_self):
- from pypy.objspace.std.sliceobject import W_SliceObject
- assert isinstance(w_self, W_SliceObject)
- return space.newtuple([
- space.type(w_self),
- space.newtuple([w_self.w_start,
- w_self.w_stop,
- w_self.w_step]),
- ])
-
-# ____________________________________________________________
-
-def slicewprop(name):
- def fget(space, w_obj):
- from pypy.objspace.std.sliceobject import W_SliceObject
- if not isinstance(w_obj, W_SliceObject):
- raise OperationError(space.w_TypeError,
- space.wrap("descriptor is for 'slice'"))
- return getattr(w_obj, name)
- return GetSetProperty(fget)
-
-
-slice_typedef = StdTypeDef("slice",
- __doc__ = '''slice([start,] stop[, step])
-
-Create a slice object. This is used for extended slicing (e.g. a[0:10:2]).''',
- __new__ = gateway.interp2app(descr__new__),
- __hash__ = None,
- __reduce__ = gateway.interp2app(descr__reduce__),
- start = slicewprop('w_start'),
- stop = slicewprop('w_stop'),
- step = slicewprop('w_step'),
- )
-slice_typedef.acceptable_as_base_class = False
-slice_typedef.registermethods(globals())
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
@@ -7,8 +7,8 @@
from pypy.interpreter.error import OperationError, oefmt
from pypy.interpreter.gateway import WrappedDefault, unwrap_spec
-from pypy.objspace.std import slicetype
-from pypy.objspace.std.sliceobject import W_SliceObject, normalize_simple_slice
+from pypy.objspace.std.sliceobject import (W_SliceObject, unwrap_start_stop,
+ normalize_simple_slice)
class StringMethods(object):
@@ -24,8 +24,8 @@
def _convert_idx_params(self, space, w_start, w_end, upper_bound=False):
value = self._val(space)
lenself = len(value)
- start, end = slicetype.unwrap_start_stop(
- space, lenself, w_start, w_end, upper_bound=upper_bound)
+ start, end = unwrap_start_stop(space, lenself, w_start, w_end,
+ upper_bound=upper_bound)
return (value, start, end)
def descr_len(self, space):
diff --git a/pypy/objspace/std/tupleobject.py b/pypy/objspace/std/tupleobject.py
--- a/pypy/objspace/std/tupleobject.py
+++ b/pypy/objspace/std/tupleobject.py
@@ -6,8 +6,8 @@
from pypy.interpreter.error import OperationError
from pypy.interpreter.gateway import (
WrappedDefault, interp2app, interpindirect2app, unwrap_spec)
-from pypy.objspace.std import slicetype
-from pypy.objspace.std.sliceobject import W_SliceObject, normalize_simple_slice
+from pypy.objspace.std.sliceobject import (W_SliceObject, unwrap_start_stop,
+ normalize_simple_slice)
from pypy.objspace.std.stdtypedef import StdTypeDef
from pypy.objspace.std.util import negate
from rpython.rlib import jit
@@ -202,8 +202,7 @@
tuple
"""
length = self.length()
- start, stop = slicetype.unwrap_start_stop(space, length, w_start,
- w_stop)
+ start, stop = unwrap_start_stop(space, length, w_start, w_stop)
for i in range(start, min(stop, length)):
w_item = self.tolist()[i]
if space.eq_w(w_item, w_obj):
More information about the pypy-commit
mailing list