From 1d5f32d065b959622d1bc5bb4066791810156433 Mon Sep 17 00:00:00 2001 From: Ned Batchelder Date: Wed, 29 May 2013 14:24:43 -0400 Subject: [PATCH 1/3] Give all response types an id attribute. CustomResponse had one, with a silly name. Use it for all ResponseTypes. --- common/lib/capa/capa/responsetypes.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/common/lib/capa/capa/responsetypes.py b/common/lib/capa/capa/responsetypes.py index a166438f17..79b049eba6 100644 --- a/common/lib/capa/capa/responsetypes.py +++ b/common/lib/capa/capa/responsetypes.py @@ -140,6 +140,8 @@ class LoncapaResponse(object): self.context = context self.system = system + self.id = xml.get('id') + for abox in inputfields: if abox.tag not in self.allowed_inputfields: msg = "%s: cannot have input field %s" % ( @@ -935,7 +937,6 @@ class CustomResponse(LoncapaResponse): # if has an "expect" (or "answer") attribute then save # that self.expect = xml.get('expect') or xml.get('answer') - self.myid = xml.get('id') log.debug('answer_ids=%s' % self.answer_ids) @@ -981,7 +982,7 @@ class CustomResponse(LoncapaResponse): if not self.code: if answer is None: log.error("[courseware.capa.responsetypes.customresponse] missing" - " code checking script! id=%s" % self.myid) + " code checking script! id=%s" % self.id) self.code = '' else: answer_src = answer.get('src') @@ -1034,7 +1035,7 @@ class CustomResponse(LoncapaResponse): # note that this doesn't help the "cfn" version - only the exec version self.context.update({ # my ID - 'response_id': self.myid, + 'response_id': self.id, # expected answer (if given as attribute) 'expect': self.expect, From 34911f68a7c69dd3a0046f2b1507f16af9f47c8b Mon Sep 17 00:00:00 2001 From: Ned Batchelder Date: Wed, 29 May 2013 14:25:24 -0400 Subject: [PATCH 2/3] Pass safe_exec a slug, the problem or response id; Use the latest CodeJail, with slug support. --- common/lib/capa/capa/capa_problem.py | 1 + common/lib/capa/capa/responsetypes.py | 8 ++++---- common/lib/capa/capa/safe_exec/safe_exec.py | 7 +++++-- requirements/edx/github.txt | 2 +- 4 files changed, 11 insertions(+), 7 deletions(-) diff --git a/common/lib/capa/capa/capa_problem.py b/common/lib/capa/capa/capa_problem.py index 8543e9e3e1..150b3b3c9b 100644 --- a/common/lib/capa/capa/capa_problem.py +++ b/common/lib/capa/capa/capa_problem.py @@ -469,6 +469,7 @@ class LoncapaProblem(object): random_seed=self.seed, python_path=python_path, cache=self.system.cache, + slug=self.problem_id, ) except Exception as err: log.exception("Error while execing script code: " + all_code) diff --git a/common/lib/capa/capa/responsetypes.py b/common/lib/capa/capa/responsetypes.py index 79b049eba6..0fa50079de 100644 --- a/common/lib/capa/capa/responsetypes.py +++ b/common/lib/capa/capa/responsetypes.py @@ -288,7 +288,7 @@ class LoncapaResponse(object): } try: - safe_exec.safe_exec(code, globals_dict, python_path=self.context['python_path']) + safe_exec.safe_exec(code, globals_dict, python_path=self.context['python_path'], slug=self.id) except Exception as err: msg = 'Error %s in evaluating hint function %s' % (err, hintfn) msg += "\nSee XML source line %s" % getattr( @@ -973,7 +973,7 @@ class CustomResponse(LoncapaResponse): 'ans': ans, } globals_dict.update(kwargs) - safe_exec.safe_exec(code, globals_dict, python_path=self.context['python_path']) + safe_exec.safe_exec(code, globals_dict, python_path=self.context['python_path'], slug=self.id) return globals_dict['cfn_return'] return check_function @@ -1090,7 +1090,7 @@ class CustomResponse(LoncapaResponse): # exec the check function if isinstance(self.code, basestring): try: - safe_exec.safe_exec(self.code, self.context, cache=self.system.cache) + safe_exec.safe_exec(self.code, self.context, cache=self.system.cache, slug=self.id) except Exception as err: self._handle_exec_exception(err) @@ -1814,7 +1814,7 @@ class SchematicResponse(LoncapaResponse): ] self.context.update({'submission': submission}) try: - safe_exec.safe_exec(self.code, self.context, cache=self.system.cache) + safe_exec.safe_exec(self.code, self.context, cache=self.system.cache, slug=self.id) except Exception as err: msg = 'Error %s in evaluating SchematicResponse' % err raise ResponseError(msg) diff --git a/common/lib/capa/capa/safe_exec/safe_exec.py b/common/lib/capa/capa/safe_exec/safe_exec.py index b9cdf236bd..67e93be46f 100644 --- a/common/lib/capa/capa/safe_exec/safe_exec.py +++ b/common/lib/capa/capa/safe_exec/safe_exec.py @@ -71,7 +71,7 @@ def update_hash(hasher, obj): @statsd.timed('capa.safe_exec.time') -def safe_exec(code, globals_dict, random_seed=None, python_path=None, cache=None): +def safe_exec(code, globals_dict, random_seed=None, python_path=None, cache=None, slug=None): """ Execute python code safely. @@ -87,6 +87,9 @@ def safe_exec(code, globals_dict, random_seed=None, python_path=None, cache=None to cache the execution, taking into account the code, the values of the globals, and the random seed. + `slug` is an arbitrary string, a description that's meaningful to the + caller, that will be used in log messages. + """ # Check the cache for a previous result. if cache: @@ -112,7 +115,7 @@ def safe_exec(code, globals_dict, random_seed=None, python_path=None, cache=None try: codejail_safe_exec( code_prolog + LAZY_IMPORTS + code, globals_dict, - python_path=python_path, + python_path=python_path, slug=slug, ) except SafeExecException as e: emsg = e.message diff --git a/requirements/edx/github.txt b/requirements/edx/github.txt index 092ec997b7..88610bc94c 100644 --- a/requirements/edx/github.txt +++ b/requirements/edx/github.txt @@ -9,4 +9,4 @@ # Our libraries: -e git+https://github.com/edx/XBlock.git@2144a25d#egg=XBlock --e git+https://github.com/edx/codejail.git@874361f#egg=codejail +-e git+https://github.com/edx/codejail.git@cf83d6a#egg=codejail From 3a030df5a74f2099f1e134bed52affbd6deb64cf Mon Sep 17 00:00:00 2001 From: Ned Batchelder Date: Wed, 29 May 2013 14:38:37 -0400 Subject: [PATCH 3/3] One more tweak to CodeJail --- requirements/edx/github.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements/edx/github.txt b/requirements/edx/github.txt index 88610bc94c..b1aef0a108 100644 --- a/requirements/edx/github.txt +++ b/requirements/edx/github.txt @@ -9,4 +9,4 @@ # Our libraries: -e git+https://github.com/edx/XBlock.git@2144a25d#egg=XBlock --e git+https://github.com/edx/codejail.git@cf83d6a#egg=codejail +-e git+https://github.com/edx/codejail.git@5fb5fa0#egg=codejail