[pypy-svn] r15303 - pypy/dist/pypy/objspace/std

arigo at codespeak.net arigo at codespeak.net
Thu Jul 28 23:37:57 CEST 2005


Author: arigo
Date: Thu Jul 28 23:37:54 2005
New Revision: 15303

Modified:
   pypy/dist/pypy/objspace/std/longobject.py
   pypy/dist/pypy/objspace/std/objspace.py
Log:
Fixed space.newlong().  Required a minor refactoring in longobject.py.


Modified: pypy/dist/pypy/objspace/std/longobject.py
==============================================================================
--- pypy/dist/pypy/objspace/std/longobject.py	(original)
+++ pypy/dist/pypy/objspace/std/longobject.py	Thu Jul 28 23:37:54 2005
@@ -90,6 +90,34 @@
         w_self.sign = sign
         assert len(w_self.digits)
 
+    def fromint(space, intval):
+        if intval < 0:
+            sign = -1
+            ival = -intval
+        elif intval > 0:
+            sign = 1
+            ival = intval
+        else:
+            return W_LongObject(space, [0], 0)
+        # Count the number of Python digits.
+        # We used to pick 5 ("big enough for anything"), but that's a
+        # waste of time and space given that 5*15 = 75 bits are rarely
+        # needed.
+        t = ival
+        ndigits = 0
+        while t:
+            ndigits += 1
+            t >>= SHIFT
+        v = W_LongObject(space, [0] * ndigits, sign)
+        t = ival
+        p = 0
+        while t:
+            v.digits[p] = t & MASK
+            t >>= SHIFT
+            p += 1
+        return v
+    fromint = staticmethod(fromint)
+
     def longval(self): #YYYYYY
         l = 0
         digits = list(self.digits)
@@ -162,31 +190,7 @@
     return W_LongObject(space, digits, sign)
 
 def long__Int(space, w_intobj):
-    if w_intobj.intval < 0:
-        sign = -1
-        ival = -w_intobj.intval
-    elif w_intobj.intval > 0:
-        sign = 1
-        ival = w_intobj.intval
-    else:
-        return W_LongObject(space, [0], 0)
-    # Count the number of Python digits.
-    # We used to pick 5 ("big enough for anything"), but that's a
-    # waste of time and space given that 5*15 = 75 bits are rarely
-    # needed.
-    t = ival
-    ndigits = 0
-    while t:
-        ndigits += 1
-        t >>= SHIFT
-    v = W_LongObject(space, [0] * ndigits, sign)
-    t = ival
-    p = 0
-    while t:
-        v.digits[p] = t & MASK
-        t >>= SHIFT
-        p += 1
-    return v
+    return W_LongObject.fromint(space, w_intobj.intval)
 
 def int__Long(space, w_value):
     try:

Modified: pypy/dist/pypy/objspace/std/objspace.py
==============================================================================
--- pypy/dist/pypy/objspace/std/objspace.py	(original)
+++ pypy/dist/pypy/objspace/std/objspace.py	Thu Jul 28 23:37:54 2005
@@ -319,12 +319,8 @@
         return W_FloatObject(self, floatval)
 
     def newlong(self, val): # val is an int
-        if val == 0:
-            sign = 0
-        else:
-            sign = 1
-        return W_LongObject(self, [val], sign)
-        
+        return W_LongObject.fromint(self, val)
+
     def newtuple(self, list_w):
         assert isinstance(list_w, list)
         return W_TupleObject(self, list_w)



More information about the Pypy-commit mailing list