Author: Anton Gulenko <[email protected]>
Branch: storage-cleanups
Changeset: r1012:80fb749ef958
Date: 2014-08-05 19:39 +0200
http://bitbucket.org/pypy/lang-smalltalk/changeset/80fb749ef958/

Log:    Removed obsolete analyseimage.py Cleaned up squeakimage.py a little

diff --git a/spyvm/squeakimage.py b/spyvm/squeakimage.py
--- a/spyvm/squeakimage.py
+++ b/spyvm/squeakimage.py
@@ -1,12 +1,11 @@
-import py
-import os
-import sys
-import time
-from spyvm import constants
-from spyvm import model
+import os, sys, time
+from spyvm import constants, model
 from spyvm.tool.bitmanipulation import splitter
+from rpython.rlib import objectmodel, streamio
 
-from rpython.rlib import objectmodel
+# ____________________________________________________________
+#
+# Stream class for reading raw input data
 
 def chrs2int(b):
     assert len(b) == 4
@@ -38,26 +37,28 @@
     return (      first << 56 | ord(b[6]) << 48 | ord(b[5]) << 40 | ord(b[4]) 
<< 32
             | ord(b[3]) << 24 | ord(b[2]) << 16 | ord(b[1]) <<  8 | ord(b[0])  
    )
 
-
-# ____________________________________________________________
-#
-# Reads an image file and creates all model objects
-
 class Stream(object):
-    """ Simple input stream """
-    def __init__(self, inputfile=None, data=None):
-        if inputfile is None and data is None:
-            raise RuntimeError("need to supply either inputfile or data")
-
-        if inputfile:
+    """ Simple input stream. Constructor can raise OSError. """
+    
+    def __init__(self, filename=None, inputfile=None, data=None):
+        if filename:
+            f = streamio.open_file_as_stream(filename, mode="rb", buffering=0)
+            try:
+                self.data = f.readall()
+            finally:
+                f.close()
+        elif inputfile:
             try:
                 self.data = inputfile.read()
             finally:
                 inputfile.close()
+        elif data:
+            self.data = data
         else:
-            self.data = data
+            raise RuntimeError("need to supply either inputfile or data")
+        
         self.reset()
-
+    
     def peek(self):
         if self.pos >= len(self.data):
             raise IndexError
@@ -73,7 +74,6 @@
             else:
                 return chrs2int(data_peek)
 
-
     def next(self):
         integer = self.peek()
         self.pos += self.word_size
@@ -101,7 +101,6 @@
         self.pos += jump
         self.count += jump
 
-
     def length(self):
         return len(self.data)
 
@@ -117,14 +116,24 @@
         self.use_long_read = False
 
 
+# ____________________________________________________________
+#
+# Constants and image versions.
+
+# from the squeak source code:
+# in squeak, the compact classes array can be found at this position
+# in the special objects array
+COMPACT_CLASSES_ARRAY = 28
+
+# The image data can optionally start after this fixed offset.
+POSSIBLE_IMAGE_OFFSET = 512
+
 class CorruptImageError(Exception):
     pass
 
 class UnsupportedImageError(Exception):
     pass
 
-# ____________________________________________________________
-
 class ImageVersion(object):
 
     def __init__(self, magic, is_big_endian, is_64bit, has_closures, 
has_floats_reversed):
@@ -159,7 +168,6 @@
                         ImageVersion(68003, False, True,  True,  True ),
 })
 
-
 def version(magic):
     ver = image_versions.get(magic, None)
     if ver is None:
@@ -168,15 +176,13 @@
     #     raise UnsupportedImageError
     return ver
 
-possible_image_offset = 512
-
 def version_from_stream(stream):
     # 32 bit
     try:
         return version(stream.peek())
     except CorruptImageError as e:
-        if stream.length() > possible_image_offset + 4:
-            stream.skipbytes(possible_image_offset)
+        if stream.length() > POSSIBLE_IMAGE_OFFSET + 4:
+            stream.skipbytes(POSSIBLE_IMAGE_OFFSET)
             try:
                 return version(stream.peek())
             except CorruptImageError:
@@ -189,8 +195,8 @@
             assert v.is_64bit
             return v
         except CorruptImageError as e:
-            if stream.length() > possible_image_offset + 4:
-                stream.skipbytes(possible_image_offset)
+            if stream.length() > POSSIBLE_IMAGE_OFFSET + 4:
+                stream.skipbytes(POSSIBLE_IMAGE_OFFSET)
                 try:
                     v = version(stream.peek())
                     assert v.is_64bit
@@ -200,6 +206,9 @@
         raise
 
 
+# ____________________________________________________________
+#
+# Parser classes for Squeak image format.
 
 def reader_for_image(space, stream):
     ver = version_from_stream(stream)
@@ -207,6 +216,13 @@
         stream.swap = True
     return ImageReader(space, stream, ver)
 
+def parse_image(space, stream):
+    image_reader = reader_for_image(space, stream)
+    image_reader.read_all()
+    image = SqueakImage()
+    image.from_reader(space, image_reader)
+    return image
+
 class ImageReader(object):
 
     def __init__(self, space, stream, version):
@@ -222,8 +238,7 @@
 
         self.lastWindowSize = 0
 
-    def initialize(self):
-        # XXX should be called something like read_full_image
+    def read_all(self):
         self.read_header()
         self.read_body()
         self.init_compactclassesarray()
@@ -257,7 +272,6 @@
         self.stream.skipbytes(headersize - self.stream.pos)
 
     def read_body(self):
-        import sys
         self.stream.reset_count()
         while self.stream.count < self.endofmemory:
             chunk, pos = self.read_object()
@@ -372,7 +386,6 @@
                           "is_modern", "startup_time"]
 
     def from_reader(self, space, reader):
-        from spyvm import constants
         self.special_objects = [g_object.w_object for g_object in
                                 reader.chunks[reader.specialobjectspointer]
                                 .g_object.pointers]
@@ -415,11 +428,6 @@
     def special(self, index):
         return self.special_objects[index]
 
-# from the squeak source code:
-# in squeak, the compact classes array can be found at this position
-# in the special objects array
-COMPACT_CLASSES_ARRAY = 28
-
 # ____________________________________________________________
 
 class GenericObject(object):
diff --git a/spyvm/test/test_squeakimage.py b/spyvm/test/test_squeakimage.py
--- a/spyvm/test/test_squeakimage.py
+++ b/spyvm/test/test_squeakimage.py
@@ -26,7 +26,7 @@
 
 def imagestream_mock(string):
     f = StringIO.StringIO(string)
-    return squeakimage.Stream(f)
+    return squeakimage.Stream(inputfile=f)
 
 def imagereader_mock(string):
     stream = imagestream_mock(string)
diff --git a/spyvm/test/util.py b/spyvm/test/util.py
--- a/spyvm/test/util.py
+++ b/spyvm/test/util.py
@@ -1,4 +1,4 @@
-import sys
+import py, sys
 from spyvm import model, storage_classes, objspace, version, constants, 
squeakimage, interpreter, interpreter_bytecodes
 from rpython.rlib.objectmodel import instantiate
 
@@ -6,15 +6,16 @@
 # This way, as many tests as possible use the real, not-bootstrapped ObjSpace.
 bootstrap_by_default = False
 
+image_dir = py.path.local(__file__).dirpath().dirpath().dirpath('images')
+
 def open_reader(space, imagefilename):
-    from spyvm.tool.analyseimage import image_dir
-    imagefilename = image_dir.join(imagefilename)
-    return squeakimage.reader_for_image(space, 
squeakimage.Stream(imagefilename.open(mode="rb")))
+    stream = 
squeakimage.Stream(filename=str(image_dir.join(imagefilename).strpath))
+    return squeakimage.reader_for_image(space, stream)
 
 def read_image(image_filename, bootstrap = bootstrap_by_default):
     space = create_space(bootstrap)
     reader = open_reader(space, image_filename)
-    reader.initialize()
+    reader.read_all()
     image = squeakimage.SqueakImage()
     image.from_reader(space, reader)
     interp = TestInterpreter(space, image)
diff --git a/spyvm/tool/analyseimage.py b/spyvm/tool/analyseimage.py
deleted file mode 100644
--- a/spyvm/tool/analyseimage.py
+++ /dev/null
@@ -1,77 +0,0 @@
-import py
-from spyvm import squeakimage
-from spyvm import constants
-from spyvm import model
-from spyvm import interpreter
-import sys
-
-image_dir = py.path.local(__file__).dirpath().dirpath().dirpath('images')
-
-mini_image = image_dir.join('mini.image')
-minitest_image = image_dir.join('minitest.image')
-s45_image = image_dir.join('Squeak4.5-12568.image')
-
-def get_miniimage(space):
-    return squeakimage.reader_for_image(space, 
squeakimage.Stream(mini_image.open(mode="rb")))
-
-def get_minitestimage(space):
-    return squeakimage.reader_for_image(space, 
squeakimage.Stream(minitest_image.open(mode="rb")))
-
-def get_45image(space):
-    return squeakimage.reader_for_image(space, 
squeakimage.Stream(s45_image.open(mode="rb")))
-
-def create_image(space, image_reader):
-    image_reader.initialize()
-
-    image = squeakimage.SqueakImage()
-    image.from_reader(space, image_reader)
-    return image
-
-def create_squeakimage(space):
-    return create_image(space, get_miniimage(space))
-
-def create_testimage(space):
-    return create_image(space, get_minitestimage(space))
-
-def create_45image(space):
-    return create_image(space, get_45image(space))
-
-def printStringsInImage():
-    image = create_squeakimage()
-    for each in image.objects:
-        if isinstance(each,model.W_BytesObject):
-          print each.class_shadow()
-          print each.as_string()
-
-def tinyBenchmarks():
-    image = create_squeakimage()
-    interp = interpreter.Interpreter()
-
-    w_object = model.W_SmallInteger(0)
-
-    # Should get this from w_object
-    w_smallint_class = image.special(constants.SO_SMALLINTEGER_CLASS)
-    s_class = w_object.class_shadow()
-    #w_method = s_class.lookup("benchFib")
-    w_method = s_class.lookup("tinyBenchmarks")
-
-    assert w_method
-    w_frame = w_method.create_frame(interp.space, w_object)
-    interp.store_w_active_context(w_frame)
-
-    from spyvm.interpreter_bytecodes import BYTECODE_TABLE
-    while True:
-        try:
-            interp.step()
-        except interpreter.ReturnFromTopLevel, e:
-            print e.object
-            return
-
-def test_do():
-    #testSelector()
-    #printStringsInImage()
-    #testDoesNotUnderstand()
-    tinyBenchmarks()
-
-if __name__ == '__main__':
-    test_do()
diff --git a/targetimageloadingsmalltalk.py b/targetimageloadingsmalltalk.py
--- a/targetimageloadingsmalltalk.py
+++ b/targetimageloadingsmalltalk.py
@@ -1,10 +1,8 @@
 #! /usr/bin/env python
 import sys, time, os
 
-from rpython.rlib.streamio import open_file_as_stream
 from rpython.rlib import jit, rpath, objectmodel
 from spyvm import model, interpreter, squeakimage, objspace, wrapper, error, 
storage_logger
-from spyvm.tool.analyseimage import create_image
 
 def _usage(argv):
     print """
@@ -155,18 +153,13 @@
     
     path = rpath.rabspath(path)
     try:
-        f = open_file_as_stream(path, mode="rb", buffering=0)
-        try:
-            imagedata = f.readall()
-        finally:
-            f.close()
+        stream = squeakimage.Stream(filename=path)
     except OSError as e:
         print_error("%s -- %s (LoadError)" % (os.strerror(e.errno), path))
         return 1
     
     # Load & prepare image and environment
-    image_reader = squeakimage.reader_for_image(space, 
squeakimage.Stream(data=imagedata))
-    image = create_image(space, image_reader)
+    image = squeakimage.parse_image(space, stream)
     interp = interpreter.Interpreter(space, image,
                 trace=trace, trace_important=trace_important,
                 evented=not poll, interrupts=interrupts)
diff --git a/targettinybenchsmalltalk.py b/targettinybenchsmalltalk.py
--- a/targettinybenchsmalltalk.py
+++ b/targettinybenchsmalltalk.py
@@ -1,8 +1,7 @@
 #! /usr/bin/env python
 import sys
 from rpython.jit.codewriter.policy import JitPolicy
-from spyvm import model, interpreter
-from spyvm.tool.analyseimage import create_testimage
+from spyvm import model, objspace, interpreter, squeakimage
 
 # This loads the whole mini.image in advance.  At run-time,
 # it executes the tinyBenchmark.  In this way we get an RPython
@@ -14,10 +13,11 @@
 # compile...
 #sys.setrecursionlimit(100000)
 
+imagefile = ""
+
 def setup():
-    from spyvm import objspace
     space = objspace.ObjSpace()
-    image = create_testimage(space)
+    image = squeakimage.parse_image(space, Stream(filename=imagefile))
     interp = interpreter.Interpreter(space, image)
     w_selector = interp.perform(space.wrap_string("loopTest"), "asSymbol")
     w_object = model.W_SmallInteger(0)
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to