99
1010Output:
1111
12- - a JSON representation of the result of running those tests
12+ - a JSON representation of the result of running the given example
1313
1414'''
1515
1616'''
1717TODOs
18- - return success/failure/errors/etc.
19-
20- - because doctests are based on stdout and not semantic value checking ...
21- - there is an ambiguity between 'return-ing' stuff and printing stuff to
22- the terminal. right now if students try to print, it will return None
23- and print an extra None, which could be confusing
24- - when students are trying to do printf-debugging inside of their
25- functions, they will *always* get test failures since doctests are based
26- on prints, not actual return values. hmmm, how do we fix this?!?
18+
19+ - are people going to brush up against the 1000-instruction execution
20+ limit? maybe raise that limit more?
21+
2722'''
2823
2924import cPickle
3025import imp
3126import json
3227import sys
3328import pg_logger
29+ from doctest_reader import encode_doctest
30+
3431import pprint
32+ pp = pprint .PrettyPrinter ()
3533
36- def opt_run_doctest (doctest , example_number , student_code ):
37- module_name = doctest .name .split ('.' )[0 ] # grab the module part out of the test's name
3834
39- # import student_code as a module
40- # http://code.activestate.com/recipes/82234-importing-a-dynamically-generated-module/
41- #student_module = imp.new_module(module_name)
42- #exec student_code in student_module.__dict__
43- #student_globals = student_module.__dict__
35+ def opt_run_doctest (doctest , example_number , student_code ):
36+ def my_finalizer (input_code , output_trace ):
37+ ret = dict (doctest = encode_doctest (doctest ),
38+ example_number = example_number ,
39+ student_code = student_code ,
40+ opt_trace = output_trace )
41+ json_output = json .dumps (ret , indent = None ) # use indent=None, indent=2 for debugging
42+ #print(json_output)
43+ pp .pprint (output_trace )
44+
45+
46+ def opt_doctest_exec_script_str (script_str , custom_modules ):
47+ logger = pg_logger .PGLogger (False , False , False , my_finalizer ,
48+ disable_security_checks = False ,
49+ custom_modules = custom_modules ,
50+ separate_stdout_by_module = True )
51+ try :
52+ logger ._runscript (script_str )
53+ except bdb .BdbQuit :
54+ pass
55+ finally :
56+ return logger .finalize ()
4457
58+ module_name = doctest .name .split ('.' )[0 ] # grab the module part out of the test's name
4559 if example_number != 'all' :
46- assert 0 <= example_number < len (t .examples )
60+ assert 0 <= example_number < len (doctest .examples )
4761 # run a single example
48- example_to_run = t .examples [example_number ]
49- #print example_to_run.source
50-
51- #exec compile(example_to_run.source, module_name, "single") in student_globals
62+ example_to_run = doctest .examples [example_number ]
5263 opt_doctest_exec_script_str (example_to_run .source ,
53- my_finalizer ,
5464 custom_modules = {module_name : student_code })
5565 else :
5666 raise NotImplementedError # not implemented yet!
57- #for e in t.examples:
58- # opt_run_doctest_example(t, e, student_code)
59- pass
60-
61-
62- def my_finalizer (input_code , output_trace ):
63- #ret = dict(code=input_code, trace=output_trace)
64- #json_output = json.dumps(output_trace, indent=2) # use indent=None for most compact repr
65- #print(json_output)
66- pp = pprint .PrettyPrinter ()
67- pp .pprint (output_trace )
68-
69- def opt_doctest_exec_script_str (script_str , finalizer_func , custom_modules ):
70- logger = pg_logger .PGLogger (False , False , False , finalizer_func ,
71- disable_security_checks = False ,
72- custom_modules = custom_modules )
73- try :
74- logger ._runscript (script_str )
75- except bdb .BdbQuit :
76- pass
77- finally :
78- return logger .finalize ()
7967
8068
8169if __name__ == "__main__" :
@@ -94,4 +82,3 @@ def opt_doctest_exec_script_str(script_str, finalizer_func, custom_modules):
9482 if t .name == test_name :
9583 opt_run_doctest (t , example_number , student_code )
9684 break
97-
0 commit comments