Author: Anton Gulenko <[email protected]>
Branch: storage
Changeset: r698:83ba26abe561
Date: 2014-03-26 13:19 +0100
http://bitbucket.org/pypy/lang-smalltalk/changeset/83ba26abe561/

Log:    Added setup_module and teardown_module to every test module. All
        module-wide resources are cleaned up after the tests; added some
        util functions to make this more concise. Following this pattern
        makes the tests more safe and guards agains mixing up space/interp
        instances in unintended ways. Also, this frees a lot of memory after
        test modules that load big images etc. Unfortunately, default
        arguments to test_* methods cannot be used this way, had to be
        replaced with None guards. Still, way cleaner this way.

diff --git a/spyvm/test/test_bitblt.py b/spyvm/test/test_bitblt.py
--- a/spyvm/test/test_bitblt.py
+++ b/spyvm/test/test_bitblt.py
@@ -1,9 +1,14 @@
 from spyvm import model, shadow, constants, interpreter, objspace
 from spyvm.plugins import bitblt
-from .util import create_space
+from .util import create_space, copy_to_module, cleanup_module
 
-space = create_space()
-w = space.w
+def setup_module():
+    space = create_space()
+    w = space.w
+    copy_to_module(locals(), __name__)
+
+def teardown_module():
+    cleanup_module(__name__)
 
 def make_form(bits, width, height, depth, o_x=0, o_y=0):
     w_f = model.W_PointersObject(space, space.w_Array, 5)
diff --git a/spyvm/test/test_bootstrappedimage.py 
b/spyvm/test/test_bootstrappedimage.py
--- a/spyvm/test/test_bootstrappedimage.py
+++ b/spyvm/test/test_bootstrappedimage.py
@@ -1,14 +1,17 @@
 import py
 from spyvm import squeakimage, model, constants
 from spyvm import interpreter, shadow
-from .util import read_image
+from .util import read_image, copy_to_module, cleanup_module
 
-def setup():
-    import spyvm.test.test_bootstrappedimage as mod
-    mod.space, mod.interp, mod.image, mod.reader = 
read_image("bootstrapped.image")
-    mod.w = space.w
-    mod.perform = interp.perform
-    mod.space.initialize_class(mod.space.w_String, mod.interp)
+def setup_module():
+    space, interp, image, reader = read_image("bootstrapped.image")
+    w = space.w
+    perform = interp.perform
+    copy_to_module(locals(), __name__)
+    space.initialize_class(space.w_String, interp)
+
+def teardown_module():
+    cleanup_module(__name__)
 
 def test_symbol_asSymbol():
     w_result = perform(image.w_asSymbol, "asSymbol")
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
@@ -1,10 +1,15 @@
 import py, operator, sys
 from spyvm import model, interpreter, primitives, shadow, objspace, wrapper, 
constants
-from .util import create_space_interp
+from .util import create_space_interp, copy_to_module, cleanup_module
 from spyvm.wrapper import PointWrapper
 from spyvm.conftest import option
 
-space, interp = create_space_interp()
+def setup_module():
+    space, interp = create_space_interp()
+    copy_to_module(locals(), __name__)
+
+def teardown_module():
+    cleanup_module(__name__)
 
 def bootstrap_class(instsize, w_superclass=None, w_metaclass=None,
                     name='?', format=shadow.POINTERS, varsized=True):
@@ -101,7 +106,9 @@
     s_frame = w_method.as_compiledmethod_get_shadow(space).create_frame(space, 
receiver, ["foo", "bar"])
     return s_frame.w_self(), s_frame
 
-def new_frame(bytes, receiver=space.w_nil, space=space):
+def new_frame(bytes, receiver=None):
+    if not receiver:
+        receiver = space.w_nil
     return _new_frame(space, bytes, receiver)
     
 def test_create_frame():
@@ -697,7 +704,9 @@
 
     storeAssociation(doubleExtendedDoAnythingBytecode + chr(7<<5) + chr(0))
 
-def interpret_bc(bcodes, literals, receiver=space.w_nil):
+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)
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,14 +1,19 @@
 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
+from .util import read_image, find_symbol_in_methoddict_of, copy_to_module, 
cleanup_module
 from rpython.rlib.rarithmetic import intmask, r_uint
 
-space, interp, _, _ = read_image('bootstrapped.image')
-w = space.w
-perform = interp.perform
-interp.trace = False
-space.initialize_class(space.w_String, interp)
+def setup_module():
+    space, interp, _, _ = read_image('bootstrapped.image')
+    w = space.w
+    perform = interp.perform
+    copy_to_module(locals(), __name__)
+    interp.trace = False
+    space.initialize_class(space.w_String, interp)
+
+def teardown_module():
+    cleanup_module(__name__)
 
 def perform_primitive(rcvr, w_selector, *args):
     code = rcvr.class_shadow(space).lookup(w_selector).primitive()
diff --git a/spyvm/test/test_miniimage.py b/spyvm/test/test_miniimage.py
--- a/spyvm/test/test_miniimage.py
+++ b/spyvm/test/test_miniimage.py
@@ -1,10 +1,15 @@
 import py, math
 from spyvm import squeakimage, model, constants, interpreter, shadow, 
objspace, wrapper, primitives
-from .util import read_image, open_reader
+from .util import read_image, open_reader, copy_to_module, cleanup_module
 
-space, interp, image, reader = read_image("mini.image")
-w = space.w
-perform = interp.perform
+def setup_module():
+    space, interp, image, reader = read_image("mini.image")
+    w = space.w
+    perform = interp.perform
+    copy_to_module(locals(), __name__)
+
+def teardown_module():
+    cleanup_module(__name__)
 
 def open_miniimage():
     return open_reader(space, "mini.image")
diff --git a/spyvm/test/test_model.py b/spyvm/test/test_model.py
--- a/spyvm/test/test_model.py
+++ b/spyvm/test/test_model.py
@@ -3,12 +3,17 @@
 from spyvm.shadow import MethodNotFound, WEAK_POINTERS
 from rpython.rlib.rarithmetic import intmask, r_uint
 from rpython.rtyper.lltypesystem import lltype, rffi
-from .util import create_space
+from .util import create_space, copy_to_module, cleanup_module
 
-space = create_space()
-bootstrap_class = space.bootstrap_class
-w_foo = space.wrap_string("foo")
-w_bar = space.wrap_string("bar")
+def setup_module():
+    space = create_space()
+    bootstrap_class = space.bootstrap_class
+    w_foo = space.wrap_string("foo")
+    w_bar = space.wrap_string("bar")
+    copy_to_module(locals(), __name__)
+
+def teardown_module():
+    cleanup_module(__name__)
 
 def joinbits(values, lengths):
     result = 0
@@ -171,13 +176,19 @@
     with py.test.raises(error.PrimitiveFailedError):
         w_method.atput0(space, 9, space.wrap_int(5))
 
-def test_is_same_object(w_o1=model.W_PointersObject(space, None,0), w_o2=None):
+def test_is_same_object(w_o1=None, w_o2=None):
+    if w_o1 is None:
+        w_o1 = model.W_PointersObject(space, None, 0)
     if w_o2 is None:
         w_o2 = w_o1
     assert w_o1.is_same_object(w_o2)
     assert w_o2.is_same_object(w_o1)
 
-def test_not_is_same_object(w_o1=model.W_PointersObject(space, 
None,0),w_o2=model.W_PointersObject(space, None,0)):
+def test_not_is_same_object(w_o1=None,w_o2=None):
+    if w_o1 is None:
+        w_o1 = model.W_PointersObject(space, None, 0)
+    if w_o2 is None:
+        w_o2 = model.W_PointersObject(space, None,0)
     assert not w_o1.is_same_object(w_o2)
     assert not w_o2.is_same_object(w_o1)
     w_o2 = model.W_SmallInteger(2)
diff --git a/spyvm/test/test_objectspace.py b/spyvm/test/test_objectspace.py
--- a/spyvm/test/test_objectspace.py
+++ b/spyvm/test/test_objectspace.py
@@ -1,9 +1,14 @@
 import py, sys
 from spyvm import objspace, model
 from rpython.rlib.rarithmetic import r_uint
-from .util import create_space
+from .util import create_space, copy_to_module, cleanup_module
 
-space = create_space()
+def setup_module():
+    space = create_space()
+    copy_to_module(locals(), __name__)
+
+def teardown_module():
+    cleanup_module(__name__)
 
 def ismetaclass(w_cls):
     # Heuristic to detect if this is a metaclass. Don't use apart
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
@@ -5,12 +5,17 @@
 from rpython.rlib.rfloat import INFINITY, NAN, isinf, isnan
 from rpython.rlib.rarithmetic import intmask
 from rpython.rtyper.lltypesystem import lltype, rffi
-from .util import create_space
+from .util import create_space, copy_to_module, cleanup_module
 from .test_interpreter import _new_frame
 
-space = create_space()
-wrap = space.w
-bootstrap_class = space.bootstrap_class
+def setup_module():
+    space = create_space()
+    wrap = space.w
+    bootstrap_class = space.bootstrap_class
+    copy_to_module(locals(), __name__)
+
+def teardown_module():
+    cleanup_module(__name__)
 
 def new_frame(bytes):
     return _new_frame(space, bytes, space.w_nil)
diff --git a/spyvm/test/test_shadow.py b/spyvm/test/test_shadow.py
--- a/spyvm/test/test_shadow.py
+++ b/spyvm/test/test_shadow.py
@@ -1,14 +1,18 @@
 import random
 from spyvm import model, shadow, constants, interpreter, objspace, wrapper
-from .util import create_space
+from .util import create_space, copy_to_module, cleanup_module
 from test_model import joinbits
 
-space = create_space()
+def setup_module():
+    space = create_space()
+    w_Object     = space.classtable['w_Object']
+    w_Metaclass  = space.classtable['w_Metaclass']
+    w_MethodDict = space.classtable['w_MethodDict']
+    w_Array      = space.classtable['w_Array']
+    copy_to_module(locals(), __name__)
 
-w_Object     = space.classtable['w_Object']
-w_Metaclass  = space.classtable['w_Metaclass']
-w_MethodDict = space.classtable['w_MethodDict']
-w_Array      = space.classtable['w_Array']
+def teardown_module():
+    cleanup_module(__name__)
 
 def build_methoddict(methods):
     size = int(len(methods) * 1.5)
@@ -29,8 +33,10 @@
         w_array.store(space, pos, w_compiledmethod)
     return w_methoddict
 
-def build_smalltalk_class(name, format, w_superclass=w_Object,
+def build_smalltalk_class(name, format, w_superclass=None,
                           w_classofclass=None, methods={}):
+    if w_superclass is None:
+        w_superclass = w_Object
     if w_classofclass is None:
         w_classofclass = build_smalltalk_class(None, 0x94,
                                                w_superclass.getclass(space),
@@ -76,7 +82,7 @@
     for w_key, value in methoddict.items():
         assert methods[w_key.as_string()].as_compiledmethod_get_shadow(space) 
is value
 
-def method(tempsize=3,argsize=2, bytes="abcde"):
+def create_method(tempsize=3,argsize=2, bytes="abcde"):
     w_m = model.W_CompiledMethod(space, )
     w_m.bytes = bytes
     w_m.tempsize = tempsize
@@ -84,8 +90,12 @@
     w_m.literalsize = 2
     return w_m
 
-def methodcontext(w_sender=space.w_nil, pc=13, stackpointer=0, stacksize=5,
-                  method=method()):
+def methodcontext(w_sender=None, pc=13, stackpointer=0, stacksize=5,
+                  method=None):
+    if w_sender is None:
+        w_sender = space.w_nil
+    if method is None:
+        method = create_method()
     w_object = model.W_PointersObject(space, space.w_MethodContext, 
constants.MTHDCTX_TEMP_FRAME_START+method.tempsize+stacksize)
     w_object.store(space, constants.CTXPART_SENDER_INDEX, w_sender)
     w_object.store(space, constants.CTXPART_PC_INDEX, space.wrap_int(pc))
@@ -98,8 +108,12 @@
     w_object.store(space, constants.MTHDCTX_TEMP_FRAME_START, 
space.wrap_string('el'))
     return w_object
 
-def blockcontext(w_sender=space.w_nil, pc=13, stackpointer=1, stacksize=5,
-                  home=methodcontext()):
+def blockcontext(w_sender=None, pc=13, stackpointer=1, stacksize=5,
+                  home=None):
+    if w_sender is None:
+        w_sender = space.w_nil
+    if home is None:
+        home = methodcontext()
     w_object = model.W_PointersObject(space, space.w_MethodContext, 
constants.MTHDCTX_TEMP_FRAME_START+stacksize)
     w_object.store(space, constants.CTXPART_SENDER_INDEX, w_sender)
     w_object.store(space, constants.CTXPART_PC_INDEX, space.wrap_int(pc))
@@ -111,7 +125,7 @@
     return w_object
 
 def test_context():
-    w_m = method()
+    w_m = create_method()
     w_object = methodcontext(stackpointer=3, method=w_m)
     w_object2 = methodcontext(w_sender=w_object)
     s_object = w_object.as_methodcontext_get_shadow(space)
@@ -144,7 +158,7 @@
     assert s_object.stackdepth() == s_object.tempsize()
 
 def test_methodcontext():
-    w_m = method()
+    w_m = create_method()
                               # Point over 2 literals of size 4
     w_object = methodcontext(pc=13,method=w_m)
     s_object = w_object.as_methodcontext_get_shadow(space)
@@ -160,7 +174,7 @@
         assert space.w_nil == w_obj.fetch(space, i)
 
 def test_attach_mc():
-    w_m = method()
+    w_m = create_method()
     w_object = methodcontext(pc=13, method=w_m)
     s_object = w_object.as_methodcontext_get_shadow(space)
     assert s_object.fetch(1).value == 13
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
@@ -3,9 +3,14 @@
 from spyvm import squeakimage
 from spyvm.squeakimage import chrs2int, chrs2long, swapped_chrs2long
 from spyvm import objspace
-from .util import create_space
+from .util import create_space, copy_to_module, cleanup_module
 
-space = create_space()
+def setup_module():
+    space = create_space()
+    copy_to_module(locals(), __name__)
+
+def teardown_module():
+    cleanup_module(__name__)
 
 # ----- helpers ----------------------------------------------
 
@@ -27,7 +32,6 @@
     stream = imagestream_mock(string)
     return squeakimage.reader_for_image(space, stream)
 
-
 SIMPLE_VERSION_HEADER = pack(">i", 6502)
 SIMPLE_VERSION_HEADER_LE = pack("<i", 6502)
 
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
@@ -1,10 +1,15 @@
 import py
 from spyvm import wrapper, model, interpreter, objspace
 from spyvm.error import WrapperException, FatalError
-from .util import create_space
+from .util import create_space, copy_to_module, cleanup_module
 from spyvm.test.test_interpreter import _new_frame
 
-space = create_space()
+def setup_module():
+    space = create_space()
+    copy_to_module(locals(), __name__)
+
+def teardown_module():
+    cleanup_module(__name__)
 
 def new_frame():
     return _new_frame(space, "")[0]
@@ -67,10 +72,16 @@
     linkedlist.remove(w_last)
     assert linkedlist.last_link() is w_first
 
-def new_process(w_next=space.w_nil,
-                w_my_list=space.w_nil,
-                w_suspended_context=space.w_nil,
+def new_process(w_next=None,
+                w_my_list=None,
+                w_suspended_context=None,
                 priority=0):
+    if w_next is None:
+        w_next = space.w_nil
+    if w_my_list is None:
+        w_my_list = space.w_nil
+    if w_suspended_context is None:
+        w_suspended_context = space.w_nil
     w_priority = space.wrap_int(priority)
     w_process = model.W_PointersObject(space, None, 4)
     process = wrapper.ProcessWrapper(space, w_process)
@@ -106,7 +117,9 @@
 
     return prioritylist
 
-def new_scheduler(w_process=space.w_nil, prioritydict=None):
+def new_scheduler(w_process=None, prioritydict=None):
+    if w_process is None:
+        w_process = space.w_nil
     priority_list = new_prioritylist(prioritydict)
     w_scheduler = model.W_PointersObject(space, None, 2)
     scheduler = wrapper.SchedulerWrapper(space, w_scheduler)
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,18 +1,13 @@
 from spyvm import squeakimage, model, constants, interpreter, shadow, objspace
-from .util import read_image
+from .util import read_image, find_symbol_in_methoddict_of, copy_to_module, 
cleanup_module
 
-def setup():
-    import spyvm.test.test_zin_squeak_4_5_image as mod
-    mod.space, mod.interp, mod.image, mod.reader = 
read_image('Squeak4.5-12568.image')
-    mod.w = space.w
+def setup_module():
+    space, interp, image, reader = read_image('Squeak4.5-12568.image')
+    w = space.w
+    copy_to_module(locals(), __name__)
 
-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 teardown_module():
+    cleanup_module(__name__)
 
 def test_all_pointers_are_valid():
     from test_miniimage import _test_all_pointers_are_valid
diff --git a/spyvm/test/util.py b/spyvm/test/util.py
--- a/spyvm/test/util.py
+++ b/spyvm/test/util.py
@@ -1,3 +1,4 @@
+import sys
 from spyvm import model, shadow, objspace, version, constants, squeakimage, 
interpreter
 from rpython.rlib.objectmodel import instantiate
 
@@ -30,7 +31,22 @@
     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_ = []
+    for name, obj in locals.items():
+        setattr(mod, name, obj)
+        mod._copied_objects_.append(name)
+
+def cleanup_module(module_name):
+    mod = sys.modules[module_name]
+    if hasattr(mod, "_copied_objects_"):
+        for name in mod._copied_objects_:
+            delattr(mod, name)
+        del mod._copied_objects_
+        import gc; gc.collect()
+
 class BootstrappedObjSpace(objspace.ObjSpace):
     
     def w(self, any):
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to