1.1 --- a/micropython/__init__.py Sun May 24 20:28:56 2009 +0200 1.2 +++ b/micropython/__init__.py Mon May 25 00:45:53 2009 +0200 1.3 @@ -47,6 +47,7 @@ 1.4 import micropython.inspect 1.5 import micropython.table 1.6 import os 1.7 + 1.8 try: 1.9 set 1.10 except NameError: 1.11 @@ -448,6 +449,12 @@ 1.12 1.13 return self.modules.values() 1.14 1.15 + def get_module(self, name): 1.16 + 1.17 + "Return the module with the given 'name'." 1.18 + 1.19 + return self.modules[name] 1.20 + 1.21 # General maintenance. 1.22 1.23 def vacuum(self): 1.24 @@ -736,4 +743,23 @@ 1.25 continue 1.26 module.set_module(submodule, self.add_module(module.name + "." + submodule)) 1.27 1.28 +# Convenience functions. 1.29 + 1.30 +def program(filename, path, requested_optimisations, verbose=0): 1.31 + 1.32 + """ 1.33 + Return the program object for the program specified by the given 'filename', 1.34 + module search 'path' and 'requested_optimisations'. 1.35 + """ 1.36 + 1.37 + i = micropython.Importer(path, verbose, requested_optimisations) 1.38 + p = micropython.Program(i, requested_optimisations) 1.39 + 1.40 + i.load_from_file("lib/builtins.py", "__builtins__") 1.41 + 1.42 + if filename is not None: 1.43 + i.load_from_file(filename) 1.44 + 1.45 + return p 1.46 + 1.47 # vim: tabstop=4 expandtab shiftwidth=4
2.1 --- a/rsvp.py Sun May 24 20:28:56 2009 +0200 2.2 +++ b/rsvp.py Mon May 25 00:45:53 2009 +0200 2.3 @@ -248,11 +248,15 @@ 2.4 2.5 "Execute code in the memory, starting from the current PC address." 2.6 2.7 + breakpoint = 0 2.8 + 2.9 try: 2.10 while 1: 2.11 self.execute() 2.12 except EmptyPCStack: 2.13 pass 2.14 + except BreakpointReached: 2.15 + breakpoint = 1 2.16 2.17 print "Execution terminated", 2.18 if self.exception is not None: 2.19 @@ -260,9 +264,38 @@ 2.20 addr = self.load(ref + 1) 2.21 print "with exception:", self.load(ref) 2.22 print "At address %d: %r" % (addr, self.load(addr)) 2.23 + elif breakpoint: 2.24 + print "with breakpoint." 2.25 + print "At address", self.pc 2.26 else: 2.27 print "successfully." 2.28 2.29 + def test(self, module): 2.30 + 2.31 + """ 2.32 + Test the code in the memory by running the code and investigating the 2.33 + contents of variables. Use 'module' to identify result variables. 2.34 + """ 2.35 + 2.36 + self.run() 2.37 + success = 1 2.38 + 2.39 + for name in module.keys(): 2.40 + if name.startswith("result"): 2.41 + label, expected = name.split("_") 2.42 + attr = module[name] 2.43 + 2.44 + # NOTE: Assumptions about headers and content made. 2.45 + 2.46 + attr_location = module.location + 1 + attr.position 2.47 + context, ref = self.load(attr_location) 2.48 + content = self.load(ref + 1) 2.49 + print label, expected, content 2.50 + 2.51 + success = success and (int(expected) == content) 2.52 + 2.53 + return success 2.54 + 2.55 def execute(self): 2.56 2.57 "Execute code in the memory at the current PC address." 2.58 @@ -837,4 +870,24 @@ 2.59 "__builtins__.object.__init__" : builtins_object_init, 2.60 } 2.61 2.62 +# Convenience functions. 2.63 + 2.64 +def machine(program, with_builtins=0, debug=0): 2.65 + print "Making the image..." 2.66 + code = program.get_image(with_builtins) 2.67 + print "Getting raw structures..." 2.68 + ot = program.get_object_table() 2.69 + pt = program.get_parameter_table() 2.70 + objlist = ot.as_list() 2.71 + paramlist = pt.as_list() 2.72 + print "Getting raw image..." 2.73 + rc = program.get_raw_image() 2.74 + print "Initialising the machine..." 2.75 + importer = program.get_importer() 2.76 + true_constant = importer.get_constant(True).location 2.77 + false_constant = importer.get_constant(False).location 2.78 + rm = RSVPMachine(rc, objlist, paramlist, true_constant, false_constant, debug=debug) 2.79 + rm.pc = program.code_location 2.80 + return rm 2.81 + 2.82 # vim: tabstop=4 expandtab shiftwidth=4
3.1 --- a/test.py Sun May 24 20:28:56 2009 +0200 3.2 +++ b/test.py Mon May 25 00:45:53 2009 +0200 3.3 @@ -6,19 +6,6 @@ 3.4 import sys 3.5 import os 3.6 3.7 -code = None 3.8 - 3.9 -def show(program, with_builtins=0): 3.10 - make(program, with_builtins) 3.11 - show_code(code) 3.12 - 3.13 -def make(program, with_builtins=0): 3.14 - global code 3.15 - code = program.get_image(with_builtins) 3.16 - 3.17 -def raw(program, with_builtins=0): 3.18 - return program.get_raw_image() 3.19 - 3.20 def show_code(code): 3.21 for i, x in enumerate(code): 3.22 print i, x 3.23 @@ -29,37 +16,15 @@ 3.24 print "%6d" % (len(table_slice) - table_slice.count(None)), \ 3.25 "".join(entry and "#" or "_" for entry in table_slice) 3.26 3.27 -def machine(program, with_builtins=0, debug=0): 3.28 - print "Making the image..." 3.29 - make(program, with_builtins) 3.30 - print "Getting raw structures..." 3.31 - ot = program.get_object_table() 3.32 - pt = program.get_parameter_table() 3.33 - objlist = ot.as_list() 3.34 - paramlist = pt.as_list() 3.35 - print "Getting raw image..." 3.36 - rc = program.get_raw_image() 3.37 - print "Initialising the machine..." 3.38 - importer = program.get_importer() 3.39 - true_constant = importer.get_constant(True).location 3.40 - false_constant = importer.get_constant(False).location 3.41 - rm = rsvp.RSVPMachine(rc, objlist, paramlist, true_constant, false_constant, debug=debug) 3.42 - rm.pc = program.code_location 3.43 - return rm 3.44 - 3.45 def attrs(obj): 3.46 for name, attr in obj.items(): 3.47 print name, attr 3.48 3.49 -if __name__ == "__main__": 3.50 - args = sys.argv[2:] 3.51 - path = sys.path[:] 3.52 +# Command option processing. 3.53 3.54 - if len(sys.argv) > 1: 3.55 - program = os.path.abspath(sys.argv[1]) 3.56 - path.append(os.path.split(program)[0]) 3.57 - else: 3.58 - program = None 3.59 +def parse_optimisations(args): 3.60 + 3.61 + "Parse 'args' for optimisation flags." 3.62 3.63 if "-omax" in args: 3.64 requested_optimisations = micropython.Program.supported_optimisations 3.65 @@ -70,26 +35,49 @@ 3.66 for arg_part in arg[2:].split(","): 3.67 requested_optimisations.append(arg_part) 3.68 3.69 - i = micropython.Importer(path, "-v" in args, requested_optimisations) 3.70 - p = micropython.Program(i, requested_optimisations) 3.71 + return requested_optimisations 3.72 + 3.73 +# Main program. 3.74 + 3.75 +if __name__ == "__main__": 3.76 + args = sys.argv[2:] 3.77 + path = sys.path[:] 3.78 + 3.79 + if len(sys.argv) > 1: 3.80 + filename = os.path.abspath(sys.argv[1]) 3.81 + path.append(os.path.split(filename)[0]) 3.82 + else: 3.83 + filename = None 3.84 + 3.85 + requested_optimisations = parse_optimisations(args) 3.86 + 3.87 + # Make the program. 3.88 3.89 try: 3.90 - builtins = i.load_from_file("lib/builtins.py", "__builtins__") 3.91 - if program is None: 3.92 + p = micropython.program(filename, path, requested_optimisations, "-v" in args) 3.93 + if filename is None: 3.94 print "Loading module micropython ..." 3.95 - m = i.load("micropython") 3.96 + m = p.get_importer().load("micropython") 3.97 else: 3.98 - print "Loading program", program, "..." 3.99 - m = i.load_from_file(program) 3.100 + m = p.get_importer().get_module("__main__") 3.101 + 3.102 + # Report any errors. 3.103 + 3.104 except micropython.ProcessingError, exc: 3.105 print repr(exc) 3.106 if "-tb" in args: 3.107 raise 3.108 + 3.109 + # Provide information to any interactive session. 3.110 + 3.111 else: 3.112 ot = p.get_object_table() 3.113 pt = p.get_parameter_table() 3.114 3.115 - if "-m" in args: 3.116 - rm = machine(p) 3.117 + if "-m" in args or "-t" in args: 3.118 + rm = rsvp.machine(p) 3.119 + if "-t" in args: 3.120 + success = rm.test(m) 3.121 + print "Test successful?", success 3.122 3.123 # vim: tabstop=4 expandtab shiftwidth=4
4.1 --- a/tests/attributes4.py Sun May 24 20:28:56 2009 +0200 4.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 4.3 @@ -1,40 +0,0 @@ 4.4 -#!/usr/bin/env python 4.5 - 4.6 -class C: 4.7 - def h(self): 4.8 - self.m = self.f 4.9 - return self.m # not known 4.10 - def f(self): pass 4.11 - 4.12 - def test(self): 4.13 - self.f() 4.14 - self.h() 4.15 - 4.16 -class D: 4.17 - f = C.f 4.18 - def h(self): 4.19 - self.m = self.f 4.20 - return self.m # not known 4.21 - 4.22 - def test(self): 4.23 - self.f(2) 4.24 - self.h() 4.25 - 4.26 -class E(C): 4.27 - def h(self): 4.28 - self.m = self.f 4.29 - return self.m # not known 4.30 - 4.31 - def test(self): 4.32 - self.f() 4.33 - self.h() 4.34 - 4.35 -c = C() 4.36 -d = D() 4.37 -e = E() 4.38 - 4.39 -x = c.f # bound C.f == c.h() 4.40 -y = d.f # unbound C.f == d.h() 4.41 -z = e.f # bound E.f == e.h() 4.42 - 4.43 -# vim: tabstop=4 expandtab shiftwidth=4
5.1 --- a/tests/attributes_class.py Sun May 24 20:28:56 2009 +0200 5.2 +++ b/tests/attributes_class.py Mon May 25 00:45:53 2009 +0200 5.3 @@ -4,7 +4,7 @@ 5.4 clsattr = 123 5.5 clsattr2 = 456 5.6 5.7 -a = C.clsattr 5.8 -b = C.clsattr2 5.9 +result_123 = C.clsattr 5.10 +result_456 = C.clsattr2 5.11 5.12 # vim: tabstop=4 expandtab shiftwidth=4
6.1 --- a/tests/attributes_class_bind_function.py Sun May 24 20:28:56 2009 +0200 6.2 +++ b/tests/attributes_class_bind_function.py Mon May 25 00:45:53 2009 +0200 6.3 @@ -8,6 +8,6 @@ 6.4 6.5 c = C() 6.6 p = c.e # bound C.e 6.7 -result = p(321) 6.8 +result_321 = p(321) 6.9 6.10 # vim: tabstop=4 expandtab shiftwidth=4
7.1 --- a/tests/attributes_class_bind_function_inherited.py Sun May 24 20:28:56 2009 +0200 7.2 +++ b/tests/attributes_class_bind_function_inherited.py Mon May 25 00:45:53 2009 +0200 7.3 @@ -12,6 +12,6 @@ 7.4 7.5 e = E() 7.6 r = e.e # bound E.e 7.7 -result = r(321) 7.8 +result_321 = r(321) 7.9 7.10 # vim: tabstop=4 expandtab shiftwidth=4
8.1 --- a/tests/attributes_class_bind_function_inherited_internal.py Sun May 24 20:28:56 2009 +0200 8.2 +++ b/tests/attributes_class_bind_function_inherited_internal.py Mon May 25 00:45:53 2009 +0200 8.3 @@ -13,6 +13,6 @@ 8.4 return self.e(x) 8.5 8.6 e = E() 8.7 -result = e.e(321) 8.8 +result_321 = e.e(321) 8.9 8.10 # vim: tabstop=4 expandtab shiftwidth=4
9.1 --- a/tests/attributes_class_bind_function_inherited_via_self.py Sun May 24 20:28:56 2009 +0200 9.2 +++ b/tests/attributes_class_bind_function_inherited_via_self.py Mon May 25 00:45:53 2009 +0200 9.3 @@ -14,6 +14,6 @@ 9.4 9.5 e = E() 9.6 r = e.f() # bound E.e 9.7 -result = r(321) 9.8 +result_321 = r(321) 9.9 9.10 # vim: tabstop=4 expandtab shiftwidth=4
10.1 --- a/tests/attributes_class_bind_function_internal.py Sun May 24 20:28:56 2009 +0200 10.2 +++ b/tests/attributes_class_bind_function_internal.py Mon May 25 00:45:53 2009 +0200 10.3 @@ -10,6 +10,6 @@ 10.4 return self.e(x) 10.5 10.6 c = C() 10.7 -result = c.test(321) 10.8 +result_321 = c.test(321) 10.9 10.10 # vim: tabstop=4 expandtab shiftwidth=4
11.1 --- a/tests/attributes_class_bind_function_unbound.py Sun May 24 20:28:56 2009 +0200 11.2 +++ b/tests/attributes_class_bind_function_unbound.py Mon May 25 00:45:53 2009 +0200 11.3 @@ -11,6 +11,6 @@ 11.4 11.5 d = D() 11.6 q = d.e # unbound C.e 11.7 -result = q(321) 11.8 +result_321 = q(321) 11.9 11.10 # vim: tabstop=4 expandtab shiftwidth=4
12.1 --- a/tests/attributes_class_bind_function_unbound_fake_self.py Sun May 24 20:28:56 2009 +0200 12.2 +++ b/tests/attributes_class_bind_function_unbound_fake_self.py Mon May 25 00:45:53 2009 +0200 12.3 @@ -11,6 +11,6 @@ 12.4 12.5 d = D() 12.6 q = d.e # unbound C.e 12.7 -result = q(123, 321) 12.8 +result_321 = q(123, 321) 12.9 12.10 # vim: tabstop=4 expandtab shiftwidth=4
13.1 --- a/tests/attributes_class_bind_function_via_self.py Sun May 24 20:28:56 2009 +0200 13.2 +++ b/tests/attributes_class_bind_function_via_self.py Mon May 25 00:45:53 2009 +0200 13.3 @@ -11,6 +11,6 @@ 13.4 13.5 c = C() 13.6 p = c.f() # bound C.e 13.7 -result = p(321) 13.8 +result_321 = p(321) 13.9 13.10 # vim: tabstop=4 expandtab shiftwidth=4
14.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 14.2 +++ b/tests/attributes_class_bind_function_via_self_uncertain.py Mon May 25 00:45:53 2009 +0200 14.3 @@ -0,0 +1,17 @@ 14.4 +#!/usr/bin/env python 14.5 + 14.6 +def e(self, x): 14.7 + return x 14.8 + 14.9 +class C: 14.10 + e = e 14.11 + 14.12 + def f(self): 14.13 + self.m = self.e 14.14 + return self.m # uncertain 14.15 + 14.16 +c = C() 14.17 +p = c.f() # bound C.e 14.18 +result_321 = p(321) 14.19 + 14.20 +# vim: tabstop=4 expandtab shiftwidth=4
15.1 --- a/tests/attributes_class_copy.py Sun May 24 20:28:56 2009 +0200 15.2 +++ b/tests/attributes_class_copy.py Mon May 25 00:45:53 2009 +0200 15.3 @@ -6,7 +6,7 @@ 15.4 15.5 x = 0 # prevent optimisation of assignments 15.6 x = C # prevent optimisation of assignments 15.7 -a = x.clsattr 15.8 -b = x.clsattr2 15.9 +result_123 = x.clsattr 15.10 +result2_123 = x.clsattr2 15.11 15.12 # vim: tabstop=4 expandtab shiftwidth=4
16.1 --- a/tests/attributes_class_in_method.py Sun May 24 20:28:56 2009 +0200 16.2 +++ b/tests/attributes_class_in_method.py Mon May 25 00:45:53 2009 +0200 16.3 @@ -11,7 +11,7 @@ 16.4 return self.clsattr2 16.5 16.6 c = C() 16.7 -a = c.f() 16.8 -b = c.g() 16.9 +result_123 = c.f() 16.10 +result_456 = c.g() 16.11 16.12 # vim: tabstop=4 expandtab shiftwidth=4
17.1 --- a/tests/attributes_class_multiple.py Sun May 24 20:28:56 2009 +0200 17.2 +++ b/tests/attributes_class_multiple.py Mon May 25 00:45:53 2009 +0200 17.3 @@ -5,6 +5,6 @@ 17.4 clsattr = 456 17.5 17.6 a = C 17.7 -b = C.clsattr 17.8 +result_456 = C.clsattr 17.9 17.10 # vim: tabstop=4 expandtab shiftwidth=4
18.1 --- a/tests/attributes_instance.py Sun May 24 20:28:56 2009 +0200 18.2 +++ b/tests/attributes_instance.py Mon May 25 00:45:53 2009 +0200 18.3 @@ -9,7 +9,7 @@ 18.4 18.5 c = C(789) 18.6 c.update(987) 18.7 -a = c.instattr 18.8 -b = c.attr 18.9 +result_789 = c.instattr 18.10 +result_987 = c.attr 18.11 18.12 # vim: tabstop=4 expandtab shiftwidth=4
19.1 --- a/tests/attributes_shadowing.py Sun May 24 20:28:56 2009 +0200 19.2 +++ b/tests/attributes_shadowing.py Mon May 25 00:45:53 2009 +0200 19.3 @@ -8,5 +8,7 @@ 19.4 19.5 c1 = C(789) 19.6 c2 = C() 19.7 +result_789 = c1.instattr 19.8 +result_123 = c2.instattr 19.9 19.10 # vim: tabstop=4 expandtab shiftwidth=4