[pypy-svn] r57158 - in pypy/branch/garden-call-code/pypy: interpreter module/operator
pedronis at codespeak.net
pedronis at codespeak.net
Sun Aug 10 13:17:04 CEST 2008
Author: pedronis
Date: Sun Aug 10 13:17:03 2008
New Revision: 57158
Modified:
pypy/branch/garden-call-code/pypy/interpreter/function.py
pypy/branch/garden-call-code/pypy/module/operator/__init__.py
pypy/branch/garden-call-code/pypy/module/operator/app_operator.py
pypy/branch/garden-call-code/pypy/module/operator/interp_operator.py
Log:
avoid repetion
Modified: pypy/branch/garden-call-code/pypy/interpreter/function.py
==============================================================================
--- pypy/branch/garden-call-code/pypy/interpreter/function.py (original)
+++ pypy/branch/garden-call-code/pypy/interpreter/function.py Sun Aug 10 13:17:03 2008
@@ -71,7 +71,7 @@
def funccall_valuestack(self, nargs, frame): # speed hack
code = self.getcode() # hook for the jit
fast_natural_arity = code.fast_natural_arity
- if nargs == code.fast_natural_arity:
+ if nargs == fast_natural_arity:
if nargs == 0:
return code.fastcall_0(self.space, self)
elif nargs == 1:
Modified: pypy/branch/garden-call-code/pypy/module/operator/__init__.py
==============================================================================
--- pypy/branch/garden-call-code/pypy/module/operator/__init__.py (original)
+++ pypy/branch/garden-call-code/pypy/module/operator/__init__.py Sun Aug 10 13:17:03 2008
@@ -21,15 +21,16 @@
'countOf', 'delslice', 'getslice', 'indexOf',
'isMappingType', 'isNumberType', 'isSequenceType',
'repeat', 'setslice',
+ 'attrgetter', 'itemgetter'
]
for name in app_names:
appleveldefs[name] = 'app_operator.%s' % name
- interp_names = ['index', 'abs', 'add', 'and_', 'attrgetter',
+ interp_names = ['index', 'abs', 'add', 'and_',
'concat', 'contains', 'delitem', 'div', 'eq', 'floordiv',
'ge', 'getitem', 'gt', 'inv',
- 'invert', 'is_', 'is_not', 'isCallable', 'itemgetter',
+ 'invert', 'is_', 'is_not', 'isCallable',
'le', 'lshift', 'lt', 'mod', 'mul',
'ne', 'neg', 'not_', 'or_',
'pos', 'pow', 'rshift', 'setitem', 'sequenceIncludes',
Modified: pypy/branch/garden-call-code/pypy/module/operator/app_operator.py
==============================================================================
--- pypy/branch/garden-call-code/pypy/module/operator/app_operator.py (original)
+++ pypy/branch/garden-call-code/pypy/module/operator/app_operator.py Sun Aug 10 13:17:03 2008
@@ -63,3 +63,20 @@
a[b:c] = d
__setslice__ = setslice
+class attrgetter(object):
+
+ def __init__(self, name):
+ self.name = name
+
+ def __call__(self, obj):
+ return getattr(obj, self.name)
+
+class itemgetter(object):
+
+ def __init__(self, index):
+ self.index = index
+
+ def __call__(self, obj):
+ return obj[self.index]
+
+
Modified: pypy/branch/garden-call-code/pypy/module/operator/interp_operator.py
==============================================================================
--- pypy/branch/garden-call-code/pypy/module/operator/interp_operator.py (original)
+++ pypy/branch/garden-call-code/pypy/module/operator/interp_operator.py Sun Aug 10 13:17:03 2008
@@ -157,54 +157,3 @@
def xor(space, w_a, w_b):
'xor(a, b) -- Same as a ^ b.'
return space.xor(w_a, w_b)
-
-# ____________________________________________________________
-# attrgetter and itergetter
-
-from pypy.interpreter import eval, function
-from pypy.interpreter.error import OperationError
-
-class SimpleClosureBuiltinFunction(function.BuiltinFunction):
-
- def __init__(self, space, code, w_index):
- assert isinstance(code, SimpleClosureCode)
- function.Function.__init__(self, space, code)
- self.w_index = w_index
-
-
-class SimpleClosureCode(eval.Code):
- fast_natural_arity = 1
-
- sig = (['obj'], None, None)
-
- def __init__(self, co_name, is_attrgetter):
- eval.Code.__init__(self, co_name)
- self.is_attrgetter = is_attrgetter
-
- def signature(self):
- return self.sig
-
- def funcrun(self, func, args):
- space = func.space
- [w_obj] = args.parse(func.name, self.sig)
- return self.fastcall_1(space, func, w_obj)
-
- def fastcall_1(self, space, func, w_obj):
- if not isinstance(func, SimpleClosureBuiltinFunction):
- raise OperationError(space.w_TypeError, space.wrap("bad call"))
- w_index = func.w_index
- if self.is_attrgetter:
- return space.getattr(w_obj, w_index)
- else:
- return space.getitem(w_obj, w_index)
-
-attrgetter_code = SimpleClosureCode("attrgetter", is_attrgetter=True)
-itemgetter_code = SimpleClosureCode("itemgetter", is_attrgetter=False)
-
-def attrgetter(space, w_attr):
- func = SimpleClosureBuiltinFunction(space, attrgetter_code, w_attr)
- return space.wrap(func)
-
-def itemgetter(space, w_idx):
- func = SimpleClosureBuiltinFunction(space, itemgetter_code, w_idx)
- return space.wrap(func)
More information about the Pypy-commit
mailing list