[pypy-svn] r55329 - in pypy/branch/smalltalk-shadow-changes/pypy/lang/smalltalk: . test

tverwaes at codespeak.net tverwaes at codespeak.net
Wed May 28 11:26:42 CEST 2008


Author: tverwaes
Date: Wed May 28 11:26:40 2008
New Revision: 55329

Modified:
   pypy/branch/smalltalk-shadow-changes/pypy/lang/smalltalk/constants.py
   pypy/branch/smalltalk-shadow-changes/pypy/lang/smalltalk/model.py
   pypy/branch/smalltalk-shadow-changes/pypy/lang/smalltalk/objspace.py
   pypy/branch/smalltalk-shadow-changes/pypy/lang/smalltalk/test/test_model.py
Log:
fixing at0 and atput0 for wordsobjects so they also support 4 bytes
LargePositiveIntegers 


Modified: pypy/branch/smalltalk-shadow-changes/pypy/lang/smalltalk/constants.py
==============================================================================
--- pypy/branch/smalltalk-shadow-changes/pypy/lang/smalltalk/constants.py	(original)
+++ pypy/branch/smalltalk-shadow-changes/pypy/lang/smalltalk/constants.py	Wed May 28 11:26:40 2008
@@ -109,7 +109,7 @@
     "MethodContext" : SO_METHODCONTEXT_CLASS,
     "BlockContext" : SO_BLOCKCONTEXT_CLASS,
     "Point" : SO_POINT_CLASS,
-#    "LargePositiveInteger" : SO_LARGEPOSITIVEINTEGER_CLASS,
+    "LargePositiveInteger" : SO_LARGEPOSITIVEINTEGER_CLASS,
 #    "Display" : SO_DISPLAY_CLASS,
 #    "Message" : SO_MESSAGE_CLASS,
     "CompiledMethod" : SO_COMPILEDMETHOD_CLASS,

Modified: pypy/branch/smalltalk-shadow-changes/pypy/lang/smalltalk/model.py
==============================================================================
--- pypy/branch/smalltalk-shadow-changes/pypy/lang/smalltalk/model.py	(original)
+++ pypy/branch/smalltalk-shadow-changes/pypy/lang/smalltalk/model.py	Wed May 28 11:26:40 2008
@@ -17,7 +17,7 @@
 import sys
 from pypy.rlib import rrandom, objectmodel
 from pypy.rlib.rarithmetic import intmask
-from pypy.lang.smalltalk import constants
+from pypy.lang.smalltalk import constants, error
 from pypy.tool.pairtype import extendabletype
 from pypy.rlib.objectmodel import instantiate
 from pypy.lang.smalltalk.tool.bitmanipulation import splitter
@@ -396,10 +396,28 @@
         self.words = [0] * size
         
     def at0(self, space, index0):
-        return space.wrap_int(self.getword(index0))
-       
+        val = self.getword(index0)
+        if val & (3 << 30) == 0:
+            return space.wrap_int(val)
+        else:
+            w_result = W_BytesObject(space.classtable['w_LargePositiveInteger'], 4)
+            for i in range(4):
+                w_result.setchar(i, chr((val >> i*8) & 255))
+            return w_result
+ 
     def atput0(self, space, index0, w_value):
-        self.setword(index0, space.unwrap_int(w_value))
+        if isinstance(w_value, W_BytesObject):
+            # XXX Probably we want to allow all subclasses
+            if not (w_value.getclass(self).is_same_object(
+                    space.classtable['w_LargePositiveInteger']) and
+                    w_value.size() == 4):
+                raise UnwrappingError("Failed to convert bytes to word")
+            word = 0
+            for i in range(4):
+                word += ord(w_value.getchar(i)) << 8*i
+        else:
+            word = space.unwrap_int(w_value)
+        self.setword(index0, word)
 
     def getword(self, n):
         return self.words[n]

Modified: pypy/branch/smalltalk-shadow-changes/pypy/lang/smalltalk/objspace.py
==============================================================================
--- pypy/branch/smalltalk-shadow-changes/pypy/lang/smalltalk/objspace.py	(original)
+++ pypy/branch/smalltalk-shadow-changes/pypy/lang/smalltalk/objspace.py	Wed May 28 11:26:40 2008
@@ -89,6 +89,7 @@
         define_cls("w_Number", "w_Magnitude")
         define_cls("w_Integer", "w_Number")
         define_cls("w_SmallInteger", "w_Integer")
+        define_cls("w_LargePositiveInteger", "w_Integer", format=shadow.BYTES)
         define_cls("w_Float", "w_Number", format=shadow.BYTES)
         define_cls("w_Collection", "w_Object")
         define_cls("w_SequenceableCollection", "w_Collection")

Modified: pypy/branch/smalltalk-shadow-changes/pypy/lang/smalltalk/test/test_model.py
==============================================================================
--- pypy/branch/smalltalk-shadow-changes/pypy/lang/smalltalk/test/test_model.py	(original)
+++ pypy/branch/smalltalk-shadow-changes/pypy/lang/smalltalk/test/test_model.py	Wed May 28 11:26:40 2008
@@ -199,3 +199,25 @@
     assert res
     assert w_clsa.as_class_get_shadow(space) is s_clsb
     assert w_clsb.as_class_get_shadow(space) is s_clsa
+
+def test_word_atput():
+    i = model.W_SmallInteger(100)
+    b = model.W_WordsObject(None, 1)
+    b.atput0(space, 0, i)
+    assert 100 == b.getword(0)
+    i = space.classtable['w_LargePositiveInteger'].as_class_get_shadow(space).new(4)
+    i.atput0(space, 3, space.wrap_int(192))
+    b.atput0(space, 0, i)
+    assert b.getword(0) == 3221225472
+
+def test_word_at():
+    b = model.W_WordsObject(None, 1)
+    b.setword(0, 100)
+    r = b.at0(space, 0)
+    assert isinstance(r, model.W_SmallInteger)
+    assert space.unwrap_int(r) == 100
+
+    b.setword(0, 3221225472)
+    r = b.at0(space, 0)
+    assert isinstance(r, model.W_BytesObject)
+    assert r.size() == 4



More information about the Pypy-commit mailing list