diff --git a/common/lib/capa/capa_problem.py b/common/lib/capa/capa_problem.py index 4ee2a2113d..c4ed385e5e 100644 --- a/common/lib/capa/capa_problem.py +++ b/common/lib/capa/capa_problem.py @@ -286,6 +286,7 @@ class LoncapaProblem(object): context.update(global_context) # initialize context to have stuff in global_context context['__builtins__'] = globals()['__builtins__'] # put globals there also context['the_lcp'] = self # pass instance of LoncapaProblem in + context['script_code'] = '' for script in tree.findall('.//script'): stype = script.get('type') @@ -298,8 +299,9 @@ class LoncapaProblem(object): code = script.text XMLESC = {"'": "'", """: '"'} code = unescape(code, XMLESC) + context['script_code'] += code # store code source in context try: - exec code in context, context # use "context" for global context; thus defs in code are global within code + exec code in context, context # use "context" for global context; thus defs in code are global within code except Exception: log.exception("Error while execing code: " + code) return context diff --git a/common/lib/capa/responsetypes.py b/common/lib/capa/responsetypes.py index 56dfd32343..ed6ef8f7c4 100644 --- a/common/lib/capa/responsetypes.py +++ b/common/lib/capa/responsetypes.py @@ -746,12 +746,20 @@ main() xml = self.xml self.url = xml.get('url') or "http://eecs1.mit.edu:8889/pyloncapa" # FIXME - hardcoded URL - answer = xml.xpath('//*[@id=$id]//answer',id=xml.get('id'))[0] # FIXME - catch errors - answer_src = answer.get('src') - if answer_src is not None: - self.code = self.system.filesystem.open('src/'+answer_src).read() - else: - self.code = answer.text + # answer = xml.xpath('//*[@id=$id]//answer',id=xml.get('id'))[0] # FIXME - catch errors + answer = xml.find('answer') + if answer is not None: + answer_src = answer.get('src') + if answer_src is not None: + self.code = self.system.filesystem.open('src/'+answer_src).read() + else: + self.code = answer.text + else: # no stanza; get code from