Commit a6677aa0 by Ned Batchelder

Computed answers are run through safe_exec.

parent 37ca6bf7
...@@ -1087,8 +1087,7 @@ def sympy_check2(): ...@@ -1087,8 +1087,7 @@ def sympy_check2():
# exec the check function # exec the check function
if isinstance(self.code, basestring): if isinstance(self.code, basestring):
try: try:
raise Exception("exec 1") safe_exec.safe_exec(self.code, self.context)
exec self.code in self.context['global_context'], self.context
correct = self.context['correct'] correct = self.context['correct']
messages = self.context['messages'] messages = self.context['messages']
overall_message = self.context['overall_message'] overall_message = self.context['overall_message']
......
...@@ -92,6 +92,20 @@ print(num) ...@@ -92,6 +92,20 @@ print(num)
</text> </text>
</problem> </problem>
<problem url_name="computed_answer">
<customresponse>
<textline size="5" correct_answer="Xyzzy"/>
<answer type="loncapa/python">
if submission[0] == "Xyzzy":
correct = ['correct']
else:
correct = ['incorrect']
</answer>
</customresponse>
</problem>
</vertical> </vertical>
</chapter> </chapter>
</course> </course>
...@@ -1023,3 +1023,8 @@ class TestSchematicResponse(TestSubmittingProblems): ...@@ -1023,3 +1023,8 @@ class TestSchematicResponse(TestSubmittingProblems):
resp = self.submit_question_answer('cfn_problem', {'2_1': "xyzzy!"}) resp = self.submit_question_answer('cfn_problem', {'2_1': "xyzzy!"})
respdata = json.loads(resp.content) respdata = json.loads(resp.content)
self.assertEqual(respdata['success'], 'incorrect') self.assertEqual(respdata['success'], 'incorrect')
def test_computed_answer(self):
resp = self.submit_question_answer('computed_answer', {'2_1': "Xyzzy"})
respdata = json.loads(resp.content)
self.assertEqual(respdata['success'], 'correct')
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment