|
| 1 | +#!/usr/bin/python2.5 |
| 2 | + |
| 3 | +# Online Python Tutor |
| 4 | +# Copyright (C) 2010 Philip J. Guo (philip@pgbovine.net) |
| 5 | +# https://github.com/pgbovine/OnlinePythonTutor/ |
| 6 | +# |
| 7 | +# This program is free software: you can redistribute it and/or modify |
| 8 | +# it under the terms of the GNU General Public License as published by |
| 9 | +# the Free Software Foundation, either version 3 of the License, or |
| 10 | +# (at your option) any later version. |
| 11 | +# |
| 12 | +# This program is distributed in the hope that it will be useful, |
| 13 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | +# GNU General Public License for more details. |
| 16 | +# |
| 17 | +# You should have received a copy of the GNU General Public License |
| 18 | +# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | + |
| 20 | + |
| 21 | +# Runs both 'user_script' and 'expect_script' and returns whether the |
| 22 | +# test has passed or failed, along with the FULL trace if the test has |
| 23 | +# failed (so that the user can debug it) |
| 24 | + |
| 25 | + |
| 26 | +import cgi |
| 27 | +import pg_logger |
| 28 | + |
| 29 | +# Python 2.5 doesn't have a built-in json module, so I'm using a |
| 30 | +# 3rd-party module. I think you can do 'import json' in Python >= 2.6 |
| 31 | +import demjson |
| 32 | + |
| 33 | +user_trace = None # the FULL user trace (without any IDs, though) |
| 34 | +expect_trace_final_entry = None |
| 35 | + |
| 36 | +def user_script_finalizer(output_lst): |
| 37 | + # very important! |
| 38 | + global user_trace |
| 39 | + global expect_trace_final_entry |
| 40 | + |
| 41 | + user_trace = output_lst |
| 42 | + |
| 43 | + # dunno which order these events come in ... |
| 44 | + if user_trace and expect_trace_final_entry: |
| 45 | + really_finalize() |
| 46 | + |
| 47 | + |
| 48 | +def expect_script_finalizer(output_lst): |
| 49 | + # very important! |
| 50 | + global user_trace |
| 51 | + global expect_trace_final_entry |
| 52 | + |
| 53 | + expect_trace_final_entry = output_lst[-1] |
| 54 | + |
| 55 | + # if there's an error here, bail NOW and return an error |
| 56 | + if (expect_trace_final_entry['event'] != 'return' or \ |
| 57 | + expect_trace_final_entry['func_name'] != '<module>'): |
| 58 | + ret = {} |
| 59 | + ret['status'] = 'error' |
| 60 | + ret['error_msg'] = "Fatal error: expected output is malformed!" |
| 61 | + |
| 62 | + # Crucial first line to make sure that Apache serves this data |
| 63 | + # correctly - DON'T FORGET THE EXTRA NEWLINES!!!: |
| 64 | + print "Content-type: text/plain; charset=iso-8859-1\n\n" |
| 65 | + output_json = demjson.encode(ret, compactly=True) |
| 66 | + print output_json |
| 67 | + |
| 68 | + else: |
| 69 | + # dunno which order these events come in ... |
| 70 | + if user_trace and expect_trace_final_entry: |
| 71 | + really_finalize() |
| 72 | + |
| 73 | + |
| 74 | +def really_finalize(): |
| 75 | + # Procedure for grading testResults vs. expectResults: |
| 76 | + # - The final line in expectResults should be a 'return' from |
| 77 | + # '<module>' that contains only ONE global variable. THAT'S |
| 78 | + # the variable that we're gonna compare against testResults. |
| 79 | + |
| 80 | + vars_to_compare = expect_trace_final_entry['globals'].keys() |
| 81 | + if len(vars_to_compare) != 1: |
| 82 | + ret['status'] = 'error' |
| 83 | + ret['error_msg'] = "Fatal error: expected output has more than one global var!" |
| 84 | + else: |
| 85 | + single_var_to_compare = vars_to_compare[0] |
| 86 | + |
| 87 | + user_trace_final_entry = user_trace[-1] |
| 88 | + |
| 89 | + ret = {} |
| 90 | + ret['status'] = 'ok' |
| 91 | + ret['passed_test'] = False |
| 92 | + |
| 93 | + assert single_var_to_compare |
| 94 | + ret['var_to_compare'] = single_var_to_compare |
| 95 | + |
| 96 | + if user_trace_final_entry['event'] == 'return': # regular termination |
| 97 | + if single_var_to_compare not in user_trace_final_entry['globals']: |
| 98 | + ret['status'] = 'error' |
| 99 | + ret['error_msg'] = "Error: output has no global var named '%s'" % (single_var_to_compare,) |
| 100 | + else: |
| 101 | + expect_val = expect_trace_final_entry['globals'][single_var_to_compare] |
| 102 | + test_val = user_trace_final_entry['globals'][single_var_to_compare] |
| 103 | + |
| 104 | + ret['test_val'] = expect_val |
| 105 | + ret['expect_val'] = expect_val |
| 106 | + |
| 107 | + # do the actual comparison here! |
| 108 | + if expect_val == test_val: |
| 109 | + ret['passed_test'] = True |
| 110 | + |
| 111 | + # find the INITIAL value of single_var_to_compare by finding its |
| 112 | + # FIRST occurrence in user_trace |
| 113 | + # |
| 114 | + # NB: This means that the initialization code for the input can |
| 115 | + # only span ONE instruction. i.e., it must be a primitive |
| 116 | + # literal assignment and not a data structure that's built up |
| 117 | + # incrementally over several steps. |
| 118 | + for e in user_trace: |
| 119 | + if 'globals' in e and single_var_to_compare in e['globals']: |
| 120 | + ret['input_val'] = e['globals'][single_var_to_compare] |
| 121 | + break # BREAK AFTER FIRST MATCH!!! |
| 122 | + |
| 123 | + else: |
| 124 | + ret['status'] = 'error' |
| 125 | + ret['error_msg'] = user_trace_final_entry['exception_msg'] |
| 126 | + |
| 127 | + |
| 128 | + # Crucial first line to make sure that Apache serves this data |
| 129 | + # correctly - DON'T FORGET THE EXTRA NEWLINES!!!: |
| 130 | + print "Content-type: text/plain; charset=iso-8859-1\n\n" |
| 131 | + output_json = demjson.encode(ret, compactly=True) |
| 132 | + print output_json |
| 133 | + |
| 134 | + |
| 135 | +form = cgi.FieldStorage() |
| 136 | +user_script = form['user_script'].value |
| 137 | +expect_script = form['expect_script'].value |
| 138 | + |
| 139 | + |
| 140 | +# WEIRD: always run the expect_script FIRST since it's less likely to have |
| 141 | +# errors. for some mysterious reason, if there's an error in user_script, |
| 142 | +# then it will never run expect_script |
| 143 | +# |
| 144 | +# also make sure to ignore IDs so that we can do direct object comparisons! |
| 145 | +pg_logger.exec_script_str(expect_script, expect_script_finalizer, ignore_id=True) |
| 146 | +pg_logger.exec_script_str(user_script, user_script_finalizer, ignore_id=True) |
| 147 | + |
0 commit comments