[pypy-svn] r5029 - in pypy/trunk/src/pypy: interpreter module objspace/std

arigo at codespeak.net arigo at codespeak.net
Thu Jun 10 14:50:52 CEST 2004


Author: arigo
Date: Thu Jun 10 14:50:51 2004
New Revision: 5029

Added:
   pypy/trunk/src/pypy/objspace/std/longobject.py
   pypy/trunk/src/pypy/objspace/std/longtype.py
Modified:
   pypy/trunk/src/pypy/interpreter/baseobjspace.py
   pypy/trunk/src/pypy/module/__builtin__module.py
   pypy/trunk/src/pypy/objspace/std/cpythonobject.py
   pypy/trunk/src/pypy/objspace/std/intobject.py
   pypy/trunk/src/pypy/objspace/std/objspace.py
Log:
Draft for 'long' objects.  Currently delegates all operations to CPython
longs.  Having this at interp-level helps a lot for multimethod coherence,
e.g. 'lst[2L:5L]' works now.


Modified: pypy/trunk/src/pypy/interpreter/baseobjspace.py
==============================================================================
--- pypy/trunk/src/pypy/interpreter/baseobjspace.py	(original)
+++ pypy/trunk/src/pypy/interpreter/baseobjspace.py	Thu Jun 10 14:50:51 2004
@@ -225,6 +225,7 @@
     ('xor',             '^',         2, ['__xor__', '__rxor__']),
     ('int',             'int',       1, ['__int__']),
     ('float',           'float',     1, ['__float__']),
+    ('long',            'long',      1, ['__long__']),
     ('inplace_add',     '+=',        2, ['__iadd__']),
     ('inplace_sub',     '-=',        2, ['__isub__']),
     ('inplace_mul',     '*=',        2, ['__imul__']),

Modified: pypy/trunk/src/pypy/module/__builtin__module.py
==============================================================================
--- pypy/trunk/src/pypy/module/__builtin__module.py	(original)
+++ pypy/trunk/src/pypy/module/__builtin__module.py	Thu Jun 10 14:50:51 2004
@@ -946,9 +946,6 @@
 # add radd, rsub, rmul, rdiv...
 
 
-class long:
-    pass   # XXX do it
-
 # ________________________________________________________________________
 ##    def app___import__(*args):
 ##        # NOTE: No import statements can be done in this function,

Modified: pypy/trunk/src/pypy/objspace/std/cpythonobject.py
==============================================================================
--- pypy/trunk/src/pypy/objspace/std/cpythonobject.py	(original)
+++ pypy/trunk/src/pypy/objspace/std/cpythonobject.py	Thu Jun 10 14:50:51 2004
@@ -46,18 +46,18 @@
 
 def cpython_unwrap(space, w_obj):
     cpyobj = w_obj.cpyobj
-    if hasattr(type(cpyobj), '__unwrap__'):
-        cpyobj = cpyobj.__unwrap__()
+    #if hasattr(type(cpyobj), '__unwrap__'):
+    #    cpyobj = cpyobj.__unwrap__()
     return cpyobj
 
 StdObjSpace.unwrap.register(cpython_unwrap, W_CPythonObject)
 
 # XXX we hack a bit to delegate ints to longs here
-def hacky_delegate_to_long(space, w_intobj):
-    return space.wrap(long(w_intobj.intval))
-hacky_delegate_to_long.result_class = W_CPythonObject  # XXX
-hacky_delegate_to_long.priority = PRIORITY_CHANGE_TYPE + 0.1  # XXX too
-StdObjSpace.delegate.register(hacky_delegate_to_long, W_IntObject)
+#def hacky_delegate_to_long(space, w_intobj):
+#    return space.wrap(long(w_intobj.intval))
+#hacky_delegate_to_long.result_class = W_CPythonObject  # XXX
+#hacky_delegate_to_long.priority = PRIORITY_CHANGE_TYPE + 0.1  # XXX too
+#StdObjSpace.delegate.register(hacky_delegate_to_long, W_IntObject)
 
 
 # XXX XXX XXX
@@ -85,9 +85,13 @@
     name = exc.__name__
     if hasattr(space, 'w_' + name):
         w_exc = getattr(space, 'w_' + name)
+        w_value = space.call(w_exc,
+            space.newtuple([space.wrap(a) for a in value.args]),
+            space.newdict([]))
     else:
         w_exc = space.wrap(exc)
-    raise OperationError, OperationError(w_exc, space.wrap(value)), tb
+        w_value = space.wrap(value)
+    raise OperationError, OperationError(w_exc, w_value), tb
 
 # in-place operators
 def inplace_pow(x1, x2):

Modified: pypy/trunk/src/pypy/objspace/std/intobject.py
==============================================================================
--- pypy/trunk/src/pypy/objspace/std/intobject.py	(original)
+++ pypy/trunk/src/pypy/objspace/std/intobject.py	Thu Jun 10 14:50:51 2004
@@ -277,8 +277,7 @@
 # a derived integer object, where it should return
 # an exact one.
 def pos__Int(space, w_int1):
-    #not sure if this should be done this way:
-    if w_int1.__class__ is W_IntObject:
+    if space.is_true(space.is_(space.type(w_int1), space.w_int)):
         return w_int1
     a = w_int1.intval
     return W_IntObject(space, a)
@@ -313,6 +312,9 @@
     ## the overflow checking, using macro Py_ARITHMETIC_RIGHT_SHIFT
     ## we *assume* that the overflow checking is done correctly
     ## in the code generator, which is not trivial!
+    
+    ## XXX also note that Python 2.3 returns a long and never raises
+    ##     OverflowError.
     try:
         c = a << b
         ## the test in C code is

Added: pypy/trunk/src/pypy/objspace/std/longobject.py
==============================================================================
--- (empty file)
+++ pypy/trunk/src/pypy/objspace/std/longobject.py	Thu Jun 10 14:50:51 2004
@@ -0,0 +1,197 @@
+import sys
+from pypy.objspace.std.objspace import *
+from intobject import W_IntObject
+from floatobject import W_FloatObject
+from noneobject import W_NoneObject
+
+class W_LongObject(W_Object):
+    """This is a non-reimplementation of longs.
+    It uses real CPython longs.
+    XXX we must really choose another representation (e.g. list of ints)
+    XXX and implement it in detail.
+    """
+    from pypy.objspace.std.longtype import long_typedef as typedef
+    
+    def __init__(w_self, space, longval=0L):
+        W_Object.__init__(w_self, space)
+        w_self.longval = longval
+
+
+registerimplementation(W_LongObject)
+
+# int-to-long delegation
+def delegate__Int(space, w_intobj):
+    return W_LongObject(space, long(w_intobj.intval))
+delegate__Int.result_class = W_LongObject
+delegate__Int.priority = PRIORITY_CHANGE_TYPE
+
+
+def long__Long(space, w_value):
+    return w_value
+
+def long__Int(space, w_intobj):
+    return W_LongObject(space, long(w_intobj.intval))
+
+def int__Long(space, w_value):
+    if -sys.maxint-1 <= w_value.longval <= sys.maxint:
+        return space.newint(int(w_value.longval))
+    else:
+        return w_value   # 9999999999999L.__int__() == 9999999999999L
+
+def float__Long(space, w_longobj):
+    return space.newfloat(float(w_longobj.longval))
+
+def long__Float(space, w_floatobj):
+    return W_LongObject(space, long(w_floatobj.floatval))
+
+def unwrap__Long(space, w_long):
+    return w_long.longval
+
+def repr__Long(space, w_long):
+    return space.wrap(repr(w_long.longval))
+
+def str__Long(space, w_long):
+    return space.wrap(str(w_long.longval))
+
+def lt__Long_Long(space, w_long1, w_long2):
+    i = w_long1.longval
+    j = w_long2.longval
+    return space.newbool( i < j )
+
+def hash__Long(space,w_value):
+    ## %reimplement%
+    # real Implementation should be taken from _Py_HashDouble in object.c
+    return space.wrap(hash(w_value.longval))
+
+def add__Long_Long(space, w_long1, w_long2):
+    x = w_long1.longval
+    y = w_long2.longval
+    z = x + y
+    return W_LongObject(space, z)
+
+def sub__Long_Long(space, w_long1, w_long2):
+    x = w_long1.longval
+    y = w_long2.longval
+    z = x - y
+    return W_LongObject(space, z)
+
+def mul__Long_Long(space, w_long1, w_long2):
+    x = w_long1.longval
+    y = w_long2.longval
+    z = x * y
+    return W_LongObject(space, z)
+
+def div__Long_Long(space, w_long1, w_long2):
+    x = w_long1.longval
+    y = w_long2.longval
+    if not y:
+        raise OperationError(space.w_ZeroDivisionError,
+                             space.wrap("long division"))
+    z = x / y
+    return W_LongObject(space, z)
+
+def floordiv__Long_Long(space, w_long1, w_long2):
+    x = w_long1.longval
+    y = w_long2.longval
+    if not y:
+        raise OperationError(space.w_ZeroDivisionError,
+                             space.wrap("long division"))
+    z = x // y
+    return W_LongObject(space, z)
+
+def mod__Long_Long(space, w_long1, w_long2):
+    x = w_long1.longval
+    y = w_long2.longval
+    if not y:
+        raise OperationError(space.w_ZeroDivisionError,
+                             space.wrap("long modulo"))
+    z = x % y
+    return W_LongObject(space, z)
+
+def divmod__Long_Long(space, w_long1, w_long2):
+    x = w_long1.longval
+    y = w_long2.longval
+    if not y:
+        raise OperationError(space.w_ZeroDivisionError,
+                             space.wrap("long modulo"))
+    z1, z2 = divmod(x, y)
+    return space.newtuple([W_LongObject(space, z1),
+                           W_LongObject(space, z2)])
+
+def pow__Long_Long_None(space, w_long1, w_long2, w_none3):
+    x = w_long1.longval
+    y = w_long2.longval
+    z = x ** y
+    return W_LongObject(space, z)
+
+def pow__Long_Long_Long(space, w_long1, w_long2, w_long3):
+    x = w_long1.longval
+    y = w_long2.longval
+    z = w_long2.longval
+    t = pow(x, y, z)
+    return W_LongObject(space, t)
+
+def neg__Long(space, w_long1):
+    return W_LongObject(space, -w_long1.longval)
+
+def pos__Long(space, w_long):
+    if space.is_true(space.is_(space.type(w_long), space.w_long)):
+        return w_long
+    else:
+        return W_LongObject(space, w_long.longval)
+
+def abs__Long(space, w_long):
+    return W_LongObject(space, abs(w_long.longval))
+
+def nonzero__Long(space, w_long):
+    return space.newbool(w_long.longval != 0L)
+
+def invert__Long(space, w_long):
+    return W_LongObject(space, ~w_long.longval)
+
+def lshift__Long_Int(space, w_long1, w_int2):
+    a = w_long1.longval
+    b = w_int2.intval
+    if b < 0:
+        raise OperationError(space.w_ValueError,
+                             space.wrap("negative shift count"))
+    res = a << b
+    return W_LongObject(space, res)
+
+def rshift__Long_Int(space, w_long1, w_int2):
+    a = w_long1.longval
+    b = w_int2.intval
+    if b < 0:
+        raise OperationError(space.w_ValueError,
+                             space.wrap("negative shift count"))
+    res = a >> b
+    return W_LongObject(space, res)
+
+def and__Long_Long(space, w_long1, w_long2):
+    a = w_long1.longval
+    b = w_long2.longval
+    res = a & b
+    return W_LongObject(space, res)
+
+def xor__Long_Long(space, w_long1, w_long2):
+    a = w_long1.longval
+    b = w_long2.longval
+    res = a ^ b
+    return W_LongObject(space, res)
+
+def or__Long_Long(space, w_long1, w_long2):
+    a = w_long1.longval
+    b = w_long2.longval
+    res = a | b
+    return W_LongObject(space, res)
+
+def oct__Long(space, w_long1):
+    x = w_long1.longval
+    return space.wrap(oct(x))
+
+def hex__Long(space, w_long1):
+    x = w_long1.longval
+    return space.wrap(hex(x))
+
+
+register_all(vars())

Added: pypy/trunk/src/pypy/objspace/std/longtype.py
==============================================================================
--- (empty file)
+++ pypy/trunk/src/pypy/objspace/std/longtype.py	Thu Jun 10 14:50:51 2004
@@ -0,0 +1,17 @@
+from pypy.objspace.std.stdtypedef import *
+from pypy.objspace.std.objecttype import object_typedef
+from pypy.interpreter.error import OperationError
+
+def descr__new__(space, w_longtype, w_value=None):
+    from longobject import W_LongObject
+    if w_value is None:
+        w_obj = W_LongObject(space)
+    else:
+        w_obj = space.long(w_value)
+    return space.w_long.check_user_subclass(w_longtype, w_obj)
+
+# ____________________________________________________________
+
+long_typedef = StdTypeDef("long", [object_typedef],
+    __new__ = newmethod(descr__new__),
+    )

Modified: pypy/trunk/src/pypy/objspace/std/objspace.py
==============================================================================
--- pypy/trunk/src/pypy/objspace/std/objspace.py	(original)
+++ pypy/trunk/src/pypy/objspace/std/objspace.py	Thu Jun 10 14:50:51 2004
@@ -55,6 +55,7 @@
             from stringtype import str_typedef
             from typetype   import type_typedef
             from slicetype  import slice_typedef
+            from longtype   import long_typedef
 	return [value for key, value in result.__dict__.items()
                       if not key.startswith('_')]   # don't look
 
@@ -138,11 +139,12 @@
         import stringobject
         import typeobject
         import sliceobject
+        import longobject
 	import cpythonobject
 	# hack to avoid imports in the time-critical functions below
         global W_ObjectObject, W_BoolObject, W_IntObject, W_FloatObject
 	global W_TupleObject, W_ListObject, W_DictObject, W_StringObject
-	global W_TypeObject, W_SliceObject
+	global W_TypeObject, W_SliceObject, W_LongObject
 	global W_CPythonObject, W_BuiltinFunctionObject
 	W_ObjectObject = objectobject.W_ObjectObject
 	W_BoolObject = boolobject.W_BoolObject
@@ -154,6 +156,7 @@
 	W_StringObject = stringobject.W_StringObject
 	W_TypeObject = typeobject.W_TypeObject
 	W_SliceObject = sliceobject.W_SliceObject
+	W_LongObject = longobject.W_LongObject
 	W_CPythonObject = cpythonobject.W_CPythonObject
 	W_BuiltinFunctionObject = cpythonobject.W_BuiltinFunctionObject
         # end of hacks
@@ -226,6 +229,8 @@
         if isinstance(x, list):
             wrappeditems = [self.wrap(item) for item in x]
             return W_ListObject(self, wrappeditems)
+        if isinstance(x, long):
+            return W_LongObject(self, x)
         if isinstance(x, Wrappable):
             w_result = x.__spacebind__(self)
             #print 'wrapping', x, '->', w_result
@@ -233,7 +238,7 @@
         SlotWrapperType = type(type(None).__repr__)
         if isinstance(x, (types.FunctionType, types.BuiltinFunctionType, SlotWrapperType)):
             return W_BuiltinFunctionObject(self, x)
-        #print "cpython wrapping %r (%s)" % (x, type(x))
+        #print "cpython wrapping %r" % (x,)
         #if hasattr(x, '__bases__'): 
         #    print "cpython wrapping a class %r (%s)" % (x, type(x))
             #raise TypeError, "cannot wrap classes"



More information about the Pypy-commit mailing list