Author: Anton Gulenko <[email protected]>
Branch: storage
Changeset: r961:95be1db81391
Date: 2014-07-27 12:16 +0200
http://bitbucket.org/pypy/lang-smalltalk/changeset/95be1db81391/

Log:    Cleaned up tests a little by extracting helper methods.

diff --git a/spyvm/test/test_interpreter.py b/spyvm/test/test_interpreter.py
--- a/spyvm/test/test_interpreter.py
+++ b/spyvm/test/test_interpreter.py
@@ -9,16 +9,22 @@
 def setup_module():
     space, interp = create_space_interp(bootstrap = True)
     w = space.w
+    interpret_bc = interp.interpret_bc
     copy_to_module(locals(), __name__)
 
 def teardown_module():
     cleanup_module(__name__)
 
+# ======= Helper methods =======
+
 def bootstrap_class(instsize, w_superclass=None, w_metaclass=None,
                     name='?', format=shadow.POINTERS, varsized=True):
     return space.bootstrap_class(instsize, w_superclass, w_metaclass,
                     name, format, varsized)
 
+def new_frame(bytes, receiver=None):
+    return space.make_frame(bytes, receiver=receiver, args=[w("foo"), 
w("bar")])
+
 def step_in_interp(ctxt): # due to missing resets in between tests
     interp._loop = False
     try:
@@ -89,22 +95,8 @@
         return lit
     return [fakeliteral(lit) for lit in literals]
 
-def _new_frame(space, bytes, receiver=None):
-    assert isinstance(bytes, str)
-    w_method = model.W_CompiledMethod(space, len(bytes))
-    w_method.islarge = 1
-    w_method.bytes = bytes
-    w_method.argsize=2
-    w_method._tempsize=8
-    w_method.setliterals([model.W_PointersObject(space, None, 2)])
-    if receiver is None:
-        receiver = space.w_nil
-    s_frame = w_method.create_frame(space, receiver, [space.w("foo"), 
space.w("bar")])
-    return s_frame.w_self(), s_frame
+# ======= Test methods =======
 
-def new_frame(bytes, receiver=None):
-    return _new_frame(space, bytes, receiver)
-    
 def test_create_frame():
     w_method = model.W_CompiledMethod(space, len("hello"))
     w_method.bytes="hello"
@@ -695,14 +687,6 @@
 
     storeAssociation(doubleExtendedDoAnythingBytecode + chr(7<<5) + chr(0))
 
-def interpret_bc(bcodes, literals, receiver=None):
-    if not receiver:
-        receiver = space.w_nil
-    bcode = "".join([chr(x) for x in bcodes])
-    w_frame, s_frame = new_frame(bcode, receiver=receiver)
-    s_frame.w_method().setliterals(literals)
-    return interp.interpret_toplevel(w_frame)
-
 # tests: bytecodePrimValue & bytecodePrimValueWithArg
 def test_bc_3_plus_4():
     # value0
diff --git a/spyvm/test/test_largeinteger.py b/spyvm/test/test_largeinteger.py
--- a/spyvm/test/test_largeinteger.py
+++ b/spyvm/test/test_largeinteger.py
@@ -1,7 +1,7 @@
 import py, operator
 from spyvm import squeakimage, model, constants, error, interpreter, shadow, 
primitives
 from spyvm.test.test_primitives import MockFrame
-from .util import read_image, find_symbol_in_methoddict_of, copy_to_module, 
cleanup_module
+from .util import read_image, copy_to_module, cleanup_module
 from rpython.rlib.rarithmetic import intmask, r_uint
 
 def setup_module():
@@ -36,7 +36,7 @@
     try:
         w_selector = space.get_special_selector(selector)
     except Exception:
-        w_selector = find_symbol_in_methoddict_of(selector, 
w(intmask(candidates[0])).getclass(space).shadow)
+        w_selector = space.find_symbol_in_methoddict(selector, 
w(intmask(candidates[0])).getclass(space))
     
     interp.trace = trace
     for i, v in enumerate(candidates):
diff --git a/spyvm/test/test_primitives.py b/spyvm/test/test_primitives.py
--- a/spyvm/test/test_primitives.py
+++ b/spyvm/test/test_primitives.py
@@ -6,20 +6,17 @@
 from rpython.rlib.rarithmetic import intmask
 from rpython.rtyper.lltypesystem import lltype, rffi
 from .util import create_space, copy_to_module, cleanup_module, TestInterpreter
-from .test_interpreter import _new_frame
 
 def setup_module():
     space = create_space(bootstrap = True)
     wrap = space.w
     bootstrap_class = space.bootstrap_class
+    new_frame = space.make_frame
     copy_to_module(locals(), __name__)
 
 def teardown_module():
     cleanup_module(__name__)
 
-def new_frame(bytes):
-    return _new_frame(space, bytes, space.w_nil)
-
 class MockFrame(model.W_PointersObject):
     def __init__(self, space, stack):
         size = 6 + len(stack) + 6
diff --git a/spyvm/test/test_wrapper.py b/spyvm/test/test_wrapper.py
--- a/spyvm/test/test_wrapper.py
+++ b/spyvm/test/test_wrapper.py
@@ -2,18 +2,15 @@
 from spyvm import wrapper, model, interpreter, objspace
 from spyvm.error import WrapperException, FatalError
 from .util import create_space, copy_to_module, cleanup_module
-from spyvm.test.test_interpreter import _new_frame
 
 def setup_module():
     space = create_space(bootstrap = True)
+    new_frame = lambda: space.make_frame("")[1]
     copy_to_module(locals(), __name__)
 
 def teardown_module():
     cleanup_module(__name__)
 
-def new_frame():
-    return _new_frame(space, "")[0].as_context_get_shadow(space)
-
 def test_simpleread():
     w_o = model.W_PointersObject(space, None, 2)
     w = wrapper.Wrapper(space, w_o)
diff --git a/spyvm/test/test_zin_squeak_4_5_image.py 
b/spyvm/test/test_zin_squeak_4_5_image.py
--- a/spyvm/test/test_zin_squeak_4_5_image.py
+++ b/spyvm/test/test_zin_squeak_4_5_image.py
@@ -1,11 +1,12 @@
 from spyvm import squeakimage, model, constants, interpreter, shadow, objspace
-from .util import read_image, find_symbol_in_methoddict_of, copy_to_module, 
cleanup_module
+from .util import read_image, copy_to_module, cleanup_module
 
 import operator
 
 def setup_module():
     space, interp, image, reader = read_image('Squeak4.5-12568.image')
     w = space.w
+    find_symbol = space.find_symbol_in_methoddict
     copy_to_module(locals(), __name__)
 
 def teardown_module():
@@ -16,60 +17,33 @@
     from test_miniimage import _test_lookup_abs_in_integer
     _test_all_pointers_are_valid(reader)
     _test_lookup_abs_in_integer(interp)
-
-def create_method(bytes, literals=[], islarge=0, argsize=0, tempsize=0):
-    w_method = model.W_CompiledMethod(space, len(bytes))
-    w_method.bytes = bytes
-    w_method.islarge = islarge
-    w_method.argsize = argsize
-    w_method._tempsize = tempsize
-
-    w_method.setliterals(literals)
-    return w_method
-
-def find_symbol(w_class, symbolname):
-    s_class = w_class.as_class_get_shadow(space)
-    symbol = find_symbol_in_methoddict_of(symbolname, s_class)
-    assert symbol is not None, 'Using image without %s method.' % symbolname
-    return symbol
-    
-def execute_frame(w_method):
-    # create a frame for our newly crafted method with a valid sender (to 
avoid raising returnFromTop to early)
-    s_initial_frame = create_method(chr(0x7c)).create_frame(space, w(0), [])
-    s_frame = w_method.create_frame(space, w(0))
-    s_frame.store_s_sender(s_initial_frame)
-    
-    try:
-        interp.loop(s_frame.w_self())
-    except interpreter.ReturnFromTopLevel, e:
-        return e.object
     
 def test_ensure():
     #ensure
     #    [^'b1'] ensure: [^'b2']
     
-    ensure_ = find_symbol(space.w_BlockClosure, "ensure:")
-    bytes = reduce(operator.add, map(chr, [0x8F, 0, 0, 2, 0x21, 0x7c,
-                                           0x8F, 0, 0, 2, 0x22, 0x7c,
-                                           0xe0, 0x87, 0x78]))
+    ensure_ = find_symbol("ensure:", space.w_BlockClosure)
+    bytes = [0x8F, 0, 0, 2, 0x21, 0x7c,
+               0x8F, 0, 0, 2, 0x22, 0x7c,
+               0xe0, 0x87, 0x78]
     
-    w_method = create_method(bytes, [ensure_, w('b1'), w('b2'),
+    w_method = space.make_method(bytes, [ensure_, w('b1'), w('b2'),
                                             w('ensure'), space.w_BlockClosure])
-    result = execute_frame(w_method)
+    result = interp.execute_method(w_method)
     assert result.as_string() == 'b2'
 
 def test_ensure_save_original_nlr():
     #ensure
     #    [^'b1'] ensure: ['b2']
     
-    ensure_ = find_symbol(space.w_BlockClosure, "ensure:")
-    bytes = reduce(operator.add, map(chr, [0x8F, 0, 0, 2, 0x21, 0x7c,
-                                           0x8F, 0, 0, 2, 0x22, 0x7d,
-                                           0xe0, 0x87, 0x78]))
+    ensure_ = find_symbol("ensure:", space.w_BlockClosure)
+    bytes = [0x8F, 0, 0, 2, 0x21, 0x7c,
+               0x8F, 0, 0, 2, 0x22, 0x7d,
+               0xe0, 0x87, 0x78]
 
-    w_method = create_method(bytes, [ensure_, w('b1'), w('b2'),
+    w_method = space.make_method(bytes, [ensure_, w('b1'), w('b2'),
                                             w('ensure'), space.w_BlockClosure])
-    result = execute_frame(w_method)
+    result = interp.execute_method(w_method)
     assert result.as_string() == 'b1'
 
 def test_ContextPart_jump():
@@ -82,24 +56,24 @@
     ^ a
     """
     ContextPart = 
space.w_MethodContext.as_class_get_shadow(space).s_superclass().w_self()
-    push = find_symbol(ContextPart, "push:")
-    sender = find_symbol(ContextPart, "sender")
-    jump = find_symbol(ContextPart, "jump")
+    push = find_symbol("push:", ContextPart)
+    sender = find_symbol("sender", ContextPart)
+    jump = find_symbol("jump", ContextPart)
     
-    bytes = reduce(operator.add, map(chr, [0x21, 0x82, 0xc0, # Set a
-                                           0x8f, 0x00, 0x00, 0x0b, # Push block
-                                                0x89, 0xd3, # Send sender
-                                                0x77, 0xe2, # Send push
-                                                0x87, 0x89, 0xd3, 0xd4, # Send 
jump
-                                                0x87, 0x25, 0x7d, # Block rest 
(not executed)
-                                           0xc9, 0x82, 0xc0, 0x40, 0x7c])) # 
Send value and return
+    bytes = [0x21, 0x82, 0xc0, # Set a
+           0x8f, 0x00, 0x00, 0x0b, # Push block
+                0x89, 0xd3, # Send sender
+                0x77, 0xe2, # Send push
+                0x87, 0x89, 0xd3, 0xd4, # Send jump
+                0x87, 0x25, 0x7d, # Block rest (not executed)
+           0xc9, 0x82, 0xc0, 0x40, 0x7c] # Send value and return
     
     Association = space.classtable["w_Point"] # Wrong class, doesn't matter.
     assoc = model.W_PointersObject(space, Association, 2)
     assoc.store(space, 0, w('a'))
     assoc.store(space, 1, w(3))
-    w_method = create_method(bytes, [assoc, w(5), push, sender, jump, w(10)])
-    result = execute_frame(w_method)
+    w_method = space.make_method(bytes, [assoc, w(5), push, sender, jump, 
w(10)])
+    result = interp.execute_method(w_method)
     assert isinstance(result, model.W_SmallInteger)
     assert result.value == 2
 
@@ -113,20 +87,20 @@
     ^ a
     """
     ContextPart = 
space.w_MethodContext.as_class_get_shadow(space).s_superclass().w_self()
-    push = find_symbol(ContextPart, "push:")
-    jump = find_symbol(ContextPart, "jump")
+    push = find_symbol("push:", ContextPart)
+    jump = find_symbol("jump", ContextPart)
     
-    bytes = reduce(operator.add, map(chr, [0x21, 0x82, 0xc0, # Set a
-                                           0x89, 0x82, 0xc2, # Set outer
-                                           0x8f, 0x00, 0x00, 0x15, # Push block
-                                                0x8f, 0x00, 0x00, 0x0f, # Push 
block
-                                                    0x8f, 0x00, 0x00, 0x09, # 
Push block
-                                                        0x42, 0x77, 0xe3, # 
Push 2
-                                                        0x87, 0x42, 0xd4, # 
Send jump
-                                                        0x87, 0x25, 0x7d, # 
Block rest (not executed)
-                                                    0xc9, 0x7d, # Send value 
and return
-                                                0xc9, 0x7d, # Send value and 
return
-                                           0xc9, 0x82, 0xc0, 0x40, 0x7c])) # 
Send value and return
+    bytes = [0x21, 0x82, 0xc0, # Set a
+               0x89, 0x82, 0xc2, # Set outer
+               0x8f, 0x00, 0x00, 0x15, # Push block
+                    0x8f, 0x00, 0x00, 0x0f, # Push block
+                        0x8f, 0x00, 0x00, 0x09, # Push block
+                            0x42, 0x77, 0xe3, # Push 2
+                            0x87, 0x42, 0xd4, # Send jump
+                            0x87, 0x25, 0x7d, # Block rest (not executed)
+                        0xc9, 0x7d, # Send value and return
+                    0xc9, 0x7d, # Send value and return
+               0xc9, 0x82, 0xc0, 0x40, 0x7c] # Send value and return
     
     Association = space.classtable["w_Point"] # Wrong class, doesn't matter.
     assoc = model.W_PointersObject(space, Association, 2)
@@ -135,8 +109,8 @@
     assoc2 = model.W_PointersObject(space, Association, 2)
     assoc2.store(space, 0, w('outer'))
     assoc2.store(space, 1, space.w_nil)
-    w_method = create_method(bytes, [assoc, w(5), assoc2, push, jump, w(10)])
-    result = execute_frame(w_method)
+    w_method = space.make_method(bytes, [assoc, w(5), assoc2, push, jump, 
w(10)])
+    result = interp.execute_method(w_method)
     assert isinstance(result, model.W_SmallInteger)
     assert result.value == 2
 
@@ -150,14 +124,14 @@
     """
     ContextPart = 
space.w_MethodContext.as_class_get_shadow(space).s_superclass().w_self()
     ContextPartClass = 
ContextPart.getclass(space).as_class_get_shadow(space).w_self()
-    contextOnDo = find_symbol(ContextPartClass, "contextOn:do:")
+    contextOnDo = find_symbol("contextOn:do:", ContextPartClass)
     
-    bytes = reduce(operator.add, map(chr, [
+    bytes = [
         0x42, 0x43, # Push the classes
         0x8f, 0x00, 0x00, 0x02, # Push block,
             0x24, 0x7d, # in the block
         0xf1, 0x81, 0xc0, 0x7c # Send contextOn:do:
-    ]))
+    ]
     
     Association = space.classtable["w_Point"] # Wrong class, doesn't matter.
     ctxAssoc = model.W_PointersObject(space, Association, 2)
@@ -169,11 +143,9 @@
     errorAssoc = model.W_PointersObject(space, Association, 2)
     errorAssoc.store(space, 0, w('Point'))
     errorAssoc.store(space, 1, Association)
-    w_method = create_method(bytes, [ctxAssoc, contextOnDo, contextPartAssoc, 
errorAssoc, w('nothing')])
+    w_method = space.make_method(bytes, [ctxAssoc, contextOnDo, 
contextPartAssoc, errorAssoc, w('nothing')])
     
-    interp.trace = True
-    
-    result = execute_frame(w_method)
+    result = interp.execute_method(w_method)
     assert isinstance(result, model.W_PointersObject)
     s = result.as_context_get_shadow(space)
     assert s.w_method().lookup_selector == "on:do:"
diff --git a/spyvm/test/util.py b/spyvm/test/util.py
--- a/spyvm/test/util.py
+++ b/spyvm/test/util.py
@@ -31,14 +31,6 @@
     interp = TestInterpreter(space)
     return space, interp
 
-def find_symbol_in_methoddict_of(string, s_class):
-    s_methoddict = s_class.s_methoddict()
-    s_methoddict.sync_method_cache()
-    methoddict_w = s_methoddict.methoddict
-    for each in methoddict_w.keys():
-        if each.as_string() == string:
-            return each
-
 def copy_to_module(locals, module_name):
     mod = sys.modules[module_name]
     mod._copied_objects_ = []
@@ -88,6 +80,22 @@
             s_new_frame.store_s_sender(s_sender)
             return s_new_frame
         return interpreter.Interpreter.stack_frame(self, s_new_frame, 
s_sender, may_context_switch)
+    
+    # ============ Helpers for executing ============
+    
+    def interpret_bc(self, bcodes, literals=None, receiver=None):
+        w_frame, s_frame = self.space.make_frame(bcodes, literals=literals, 
receiver=receiver)
+        self.space.wrap_frame(s_frame)
+        return self.interpret_toplevel(w_frame)
+        
+    def execute_method(self, w_method):
+        s_frame = w_method.create_frame(self.space, self.space.w(0))
+        self.space.wrap_frame(s_frame)
+        try:
+            self.loop(s_frame.w_self())
+        except interpreter.ReturnFromTopLevel, e:
+            return e.object
+        assert False, "Frame did not return correctly."
 
 class BootstrappedObjSpace(objspace.ObjSpace):
     
@@ -264,7 +272,45 @@
         raise Exception("Cannot wrap %r" % any)
     
     def initialize_class(self, w_class, interp):
-        initialize_symbol = find_symbol_in_methoddict_of("initialize", 
+        initialize_symbol = self.find_symbol_in_methoddict("initialize", 
                             w_class.class_shadow(self))
         interp.perform(w_class, w_selector=initialize_symbol)
+    
+    def find_symbol_in_methoddict(self, string, cls):
+        if isinstance(cls, model.W_PointersObject):
+            cls = cls.as_class_get_shadow(self)
+        s_methoddict = cls.s_methoddict()
+        s_methoddict.sync_method_cache()
+        methoddict_w = s_methoddict.methoddict
+        for each in methoddict_w.keys():
+            if each.as_string() == string:
+                return each
+        assert False, 'Using image without %s method in class %s.' % (string, 
cls.name)
+
+    # ============ Helpers for executing ============
+    
+    def wrap_frame(self, s_frame):
+        # Add a toplevel frame around s_frame to properly return.
+        toplevel_frame = self.make_method([0x7c]).create_frame(self, 
self.w(0), [])
+        s_frame.store_s_sender(toplevel_frame)
+        
+    def make_method(self, bytes, literals=None, numargs=0):
+        if not isinstance(bytes, str):
+            bytes = "".join([chr(x) for x in bytes])
+        w_method = model.W_CompiledMethod(self, len(bytes))
+        w_method.islarge = 1
+        w_method.bytes = bytes
+        w_method.argsize=numargs
+        w_method._tempsize=8
+        if literals is None:
+            literals = [model.W_PointersObject(self, None, 2)]
+        w_method.setliterals(literals)
+        return w_method
+    
+    def make_frame(self, bytes, literals=None, receiver=None, args=[]):
+        w_method = self.make_method(bytes, literals, len(args))
+        if receiver is None:
+            receiver = self.w_nil
+        s_frame = w_method.create_frame(self, receiver, args)
+        return s_frame.w_self(), s_frame
         
\ No newline at end of file
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to