[pypy-svn] r39860 - in pypy/branch/pypy-2.5/pypy: interpreter interpreter/test lib module/operator objspace objspace/flow objspace/std

gbrandl at codespeak.net gbrandl at codespeak.net
Sun Mar 4 12:19:31 CET 2007


Author: gbrandl
Date: Sun Mar  4 12:19:28 2007
New Revision: 39860

Added:
   pypy/branch/pypy-2.5/pypy/module/operator/
   pypy/branch/pypy-2.5/pypy/module/operator/__init__.py
   pypy/branch/pypy-2.5/pypy/module/operator/app_operator.py
      - copied, changed from r39846, pypy/dist/pypy/lib/operator.py
   pypy/branch/pypy-2.5/pypy/module/operator/interp_operator.py
Removed:
   pypy/branch/pypy-2.5/pypy/lib/operator.py
Modified:
   pypy/branch/pypy-2.5/pypy/interpreter/baseobjspace.py
   pypy/branch/pypy-2.5/pypy/interpreter/test/test_objspace.py
   pypy/branch/pypy-2.5/pypy/lib/_classobj.py
   pypy/branch/pypy-2.5/pypy/objspace/descroperation.py
   pypy/branch/pypy-2.5/pypy/objspace/flow/operation.py
   pypy/branch/pypy-2.5/pypy/objspace/std/intobject.py
   pypy/branch/pypy-2.5/pypy/objspace/std/longobject.py
   pypy/branch/pypy-2.5/pypy/objspace/std/stringobject.py
Log:
Add space.index, space.getindex_w, start moving operator module to interplevel.



Modified: pypy/branch/pypy-2.5/pypy/interpreter/baseobjspace.py
==============================================================================
--- pypy/branch/pypy-2.5/pypy/interpreter/baseobjspace.py	(original)
+++ pypy/branch/pypy-2.5/pypy/interpreter/baseobjspace.py	Sun Mar  4 12:19:28 2007
@@ -5,7 +5,7 @@
 from pypy.interpreter.miscutils import ThreadLocals
 from pypy.tool.cache import Cache
 from pypy.tool.uid import HUGEVAL_BYTES
-import os
+import os, sys
 
 __all__ = ['ObjSpace', 'OperationError', 'Wrappable', 'W_Root']
 
@@ -785,6 +785,28 @@
             step  = 0
         return start, stop, step
 
+    def getindex_w(self, w_obj, exception=None):
+        w_index = self.index(w_obj)
+        try:
+            index = self.int_w(w_index)
+        except OperationError, err:
+            if not err.match(self, self.w_OverflowError):
+                raise
+            if not exception:
+                # w_index is a long object
+                if w_index.get_sign() < 0:
+                    return -sys.maxint-1
+                else:
+                    return sys.maxint
+            else:
+                raise OperationError(
+                    exception, self.wrap(
+                    "cannot fit '%s' into an index-sized "
+                    "integer" % self.type(w_obj).getname(self, '?')))
+        else:
+            return index
+
+
 class AppExecCache(SpaceCache):
     def build(cache, source):
         """ NOT_RPYTHON """
@@ -841,6 +863,7 @@
     ('or_',             '|',         2, ['__or__', '__ror__']),
     ('xor',             '^',         2, ['__xor__', '__rxor__']),
     ('int',             'int',       1, ['__int__']),
+    ('index',           'index',     1, ['__index__']),
     ('float',           'float',     1, ['__float__']),
     ('long',            'long',      1, ['__long__']),
     ('inplace_add',     '+=',        2, ['__iadd__']),

Modified: pypy/branch/pypy-2.5/pypy/interpreter/test/test_objspace.py
==============================================================================
--- pypy/branch/pypy-2.5/pypy/interpreter/test/test_objspace.py	(original)
+++ pypy/branch/pypy-2.5/pypy/interpreter/test/test_objspace.py	Sun Mar  4 12:19:28 2007
@@ -1,6 +1,7 @@
 from py.test import raises
 from pypy.interpreter.function import Function
 from pypy.interpreter.pycode import PyCode
+import sys
 
 # this test isn't so much to test that the objspace interface *works*
 # -- it's more to test that it's *there*
@@ -139,6 +140,23 @@
         res = self.space.interp_w(Function, w(None), can_be_None=True)
         assert res is None
 
+    def test_getindex_w(self):
+        w_instance1 = self.space.appexec([], """():
+            class X(object):
+                def __index__(self): return 42
+            return X()""")
+        w_instance2 = self.space.appexec([], """():
+            class Y(object):
+                def __index__(self): return 2**70
+            return Y()""")
+        first = self.space.getindex_w(w_instance1)
+        assert first == 42
+        second = self.space.getindex_w(w_instance2)
+        assert second == sys.maxint
+        self.space.raises_w(self.space.w_IndexError,
+                            self.space.getindex_w, w_instance2, self.space.w_IndexError)
+
+
 class TestModuleMinimal: 
     def test_sys_exists(self):
         assert self.space.sys 

Modified: pypy/branch/pypy-2.5/pypy/lib/_classobj.py
==============================================================================
--- pypy/branch/pypy-2.5/pypy/lib/_classobj.py	(original)
+++ pypy/branch/pypy-2.5/pypy/lib/_classobj.py	Sun Mar  4 12:19:28 2007
@@ -399,6 +399,14 @@
 """, {"op": op})
     del op
 
+    def __index__(self):
+        func = instance_getattr1(self, '__index__', False)
+        if func:
+            return func()
+        else:
+            raise AttributeError('object cannot be interpreted as an index')
+
+
     # coerce
     def __coerce__(self, other):
         func = instance_getattr1(self, '__coerce__', False)

Added: pypy/branch/pypy-2.5/pypy/module/operator/__init__.py
==============================================================================
--- (empty file)
+++ pypy/branch/pypy-2.5/pypy/module/operator/__init__.py	Sun Mar  4 12:19:28 2007
@@ -0,0 +1,32 @@
+from pypy.interpreter.mixedmodule import MixedModule 
+from pypy.interpreter.error import OperationError 
+
+class Module(MixedModule):
+    """Operator Builtin Module. """
+
+    appleveldefs = {} 
+    
+    names = ['__abs__', '__add__', '__and__',
+             '__concat__', '__contains__', '__delitem__', '__delslice__',
+             '__div__', '__doc__', '__eq__', '__floordiv__',
+             '__ge__', '__getitem__', '__getslice__', '__gt__', '__inv__',
+             '__invert__', '__le__', '__lshift__', '__lt__', '__mod__',
+             '__mul__', '__name__', '__ne__', '__neg__', '__not__', '__or__',
+             '__pos__', '__pow__', '__repeat__', '__rshift__', '__setitem__',
+             '__setslice__', '__sub__', '__truediv__', '__xor__', 'abs', 'add',
+             'and_', 'attrgetter', 'concat', 'contains', 'countOf', 'delitem',
+             'delslice', 'div', 'division', 'eq', 'floordiv', 'ge', 'getitem',
+             'getslice', 'gt', 'indexOf', 'inv', 'invert', 'isCallable',
+             'isMappingType', 'isNumberType', 'isSequenceType', 'is_',
+             'is_not', 'itemgetter', 'le', 'lshift', 'lt', 'mod', 'mul',
+             'ne', 'neg', 'not_', 'or_', 'pos', 'pow', 'repeat', 'rshift',
+             'sequenceIncludes', 'setitem', 'setslice', 'sub', 'truediv',
+             'truth', 'xor']
+
+    for name in names:
+        appleveldefs[name] = 'app_operator.%s' % name
+        
+        
+    interpleveldefs = {
+        'index': 'interp_operator.index'
+    }

Added: pypy/branch/pypy-2.5/pypy/module/operator/interp_operator.py
==============================================================================
--- (empty file)
+++ pypy/branch/pypy-2.5/pypy/module/operator/interp_operator.py	Sun Mar  4 12:19:28 2007
@@ -0,0 +1,9 @@
+
+def index(space, w_a):
+    return space.index(w_a)
+
+def abs(space, w_obj):
+    'abs(a) -- Same as abs(a).'
+    return space.abs(w_obj)
+
+__abs__ = abs

Modified: pypy/branch/pypy-2.5/pypy/objspace/descroperation.py
==============================================================================
--- pypy/branch/pypy-2.5/pypy/objspace/descroperation.py	(original)
+++ pypy/branch/pypy-2.5/pypy/objspace/descroperation.py	Sun Mar  4 12:19:28 2007
@@ -514,6 +514,7 @@
 
 for targetname, specialname, checkerspec in [
     ('int', '__int__', ("space.w_int", "space.w_long")), 
+    ('index', '__index__', ("space.w_int", "space.w_long")),
     ('long', '__long__', ("space.w_int", "space.w_long")), 
     ('float', '__float__', ("space.w_float",))]:
 
@@ -537,7 +538,7 @@
         assert not hasattr(DescrOperation, %(targetname)r)
         DescrOperation.%(targetname)s = %(targetname)s
         del %(targetname)s 
-        \n""" % locals() 
+        \n""" % locals()
     exec compile2(source) 
 
 for targetname, specialname in [

Modified: pypy/branch/pypy-2.5/pypy/objspace/flow/operation.py
==============================================================================
--- pypy/branch/pypy-2.5/pypy/objspace/flow/operation.py	(original)
+++ pypy/branch/pypy-2.5/pypy/objspace/flow/operation.py	Sun Mar  4 12:19:28 2007
@@ -23,6 +23,9 @@
 def do_int(x):
     return x.__int__()
 
+def do_index(x):
+    return x.__index__()
+
 def do_float(x):
     return x.__float__()
 
@@ -162,6 +165,7 @@
     ('divmod',          divmod),
     ('pow',             pow),
     ('int',             do_int),
+    ('index',           do_index),
     ('float',           do_float),
     ('long',            do_long),
     ('inplace_add',     inplace_add),

Modified: pypy/branch/pypy-2.5/pypy/objspace/std/intobject.py
==============================================================================
--- pypy/branch/pypy-2.5/pypy/objspace/std/intobject.py	(original)
+++ pypy/branch/pypy-2.5/pypy/objspace/std/intobject.py	Sun Mar  4 12:19:28 2007
@@ -326,6 +326,9 @@
     a = w_int1.intval
     return wrapint(space, a)
 
+def index__Int(space, w_int1):
+    return int__Int(space, w_int1)
+
 def float__Int(space, w_int1):
     a = w_int1.intval
     x = float(a)

Modified: pypy/branch/pypy-2.5/pypy/objspace/std/longobject.py
==============================================================================
--- pypy/branch/pypy-2.5/pypy/objspace/std/longobject.py	(original)
+++ pypy/branch/pypy-2.5/pypy/objspace/std/longobject.py	Sun Mar  4 12:19:28 2007
@@ -50,6 +50,9 @@
     def is_odd(self):
         return self.num.is_odd()
 
+    def get_sign(self):
+        return self.num.sign
+
 registerimplementation(W_LongObject)
 
 # bool-to-long
@@ -79,6 +82,9 @@
     except OverflowError:
         return long__Long(space, w_value)
 
+def index__Long(space, w_value):
+    return long__Long(space, w_value)
+
 def float__Long(space, w_longobj):
     try:
         return space.newfloat(w_longobj.num.tofloat())

Modified: pypy/branch/pypy-2.5/pypy/objspace/std/stringobject.py
==============================================================================
--- pypy/branch/pypy-2.5/pypy/objspace/std/stringobject.py	(original)
+++ pypy/branch/pypy-2.5/pypy/objspace/std/stringobject.py	Sun Mar  4 12:19:28 2007
@@ -810,6 +810,9 @@
 def getnewargs__String(space, w_str):
     return space.newtuple([wrapstr(space, w_str._value)])
 
+def index__String(space, w_str):
+    return space.wrap(42)
+
 def repr__String(space, w_str):
     s = w_str._value
 



More information about the Pypy-commit mailing list