[pypy-svn] r47935 - in pypy/dist/pypy/lang/smalltalk: . test

akuhn at codespeak.net akuhn at codespeak.net
Thu Oct 25 16:22:11 CEST 2007


Author: akuhn
Date: Thu Oct 25 16:22:10 2007
New Revision: 47935

Modified:
   pypy/dist/pypy/lang/smalltalk/squeakimage.py
   pypy/dist/pypy/lang/smalltalk/test/test_squeakimage.py
Log:
(akuhn, tverwaes, arigo)
replaced use of struct with RPython code 


Modified: pypy/dist/pypy/lang/smalltalk/squeakimage.py
==============================================================================
--- pypy/dist/pypy/lang/smalltalk/squeakimage.py	(original)
+++ pypy/dist/pypy/lang/smalltalk/squeakimage.py	Thu Oct 25 16:22:10 2007
@@ -9,6 +9,21 @@
             chr((integer >> 8) & 0xff) + 
             chr((integer >> 0) & 0xff))
 
+def chrs2int(b):
+    assert len(b) == 4
+    first = ord(b[0]) # big endian
+    if first & 0x80 != 0:
+        first = first - 0x100
+    return first << 24 | ord(b[1]) << 16 | ord(b[2]) << 8 | ord(b[3])
+
+def swapped_chrs2int(b):
+    assert len(b) == 4
+    first = ord(b[3]) # little endian
+    if first & 0x80 != 0:
+        first = first - 0x100
+    return first << 24 | ord(b[2]) << 16 | ord(b[1]) << 8 | ord(b[0])
+            
+
 def splitbits(integer, lengths):
     assert sum(lengths) <= 32
     result = []
@@ -35,15 +50,12 @@
         self.count = 0
 
     def peek(self):
-        import struct
         if self.pos >= len(self.data): 
             raise IndexError
         if self.swap:
-            format = "<i"
-        else: 
-            format = ">i"                
-        integer, = struct.unpack(format, self.data[self.pos:self.pos+4])
-        return integer 
+            return swapped_chrs2int( self.data[self.pos:self.pos+4] )
+        else:                 
+            return chrs2int( self.data[self.pos:self.pos+4] )
         
     def next(self):
         integer = self.peek()

Modified: pypy/dist/pypy/lang/smalltalk/test/test_squeakimage.py
==============================================================================
--- pypy/dist/pypy/lang/smalltalk/test/test_squeakimage.py	(original)
+++ pypy/dist/pypy/lang/smalltalk/test/test_squeakimage.py	Thu Oct 25 16:22:10 2007
@@ -1,5 +1,6 @@
 import py
 from pypy.lang.smalltalk import squeakimage
+from pypy.lang.smalltalk.squeakimage import chrs2int
 
 # ----- helpers ----------------------------------------------
 
@@ -23,6 +24,10 @@
 
 # ----- tests ------------------------------------------------
 
+def test_chrs2int():
+    assert 1 == chrs2int('\x00\x00\x00\x01')
+    assert -1 == chrs2int('\xFF\xFF\xFF\xFF')
+
 def test_stream():
     stream = imagereader_mock('\x00\x00\x19\x66').stream
     n = stream.peek()


More information about the pypy-svn mailing list