Skip to content

Commit 39f44d8

Browse files
author
Filip Schouwenaars
authored
Merge pull request #170 from datacamp/fix-func-of-func
Fix func of func
2 parents 8689abe + 819df39 commit 39f44d8

4 files changed

Lines changed: 50 additions & 4 deletions

File tree

pythonwhat/check_funcs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ def call(args,
370370
from pythonwhat.tasks import ReprFail, UndefinedValue
371371
from pythonwhat import utils
372372
def has_expr(incorrect_msg="FMT:Unexpected expression {test}: expected `{sol_eval}`, got `{stu_eval}` with values{extra_env}.",
373-
error_msg="Running an expression in the student process caused an issue",
373+
error_msg="Running an expression in the student process caused an issue.",
374374
undefined_msg="FMT:Have you defined `{name}` without errors?",
375375
extra_env=None,
376376
context_vals=None,

pythonwhat/tasks.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ def getRepresentation(name, process):
259259
try:
260260
stream = getStreamPickle(name, process)
261261
if not errored(stream): return pickle.loads(stream)
262-
except PicklingError:
262+
except:
263263
pass
264264

265265
# if it failed, try to dill
@@ -268,7 +268,10 @@ def getRepresentation(name, process):
268268
if not errored(stream): return dill.loads(stream)
269269
return ReprFail("dilling inside process failed for %s - write manual converter" % obj_class)
270270
except PicklingError:
271-
return ReprFail("undilling of bytestream failed - write manual converter")
271+
return ReprFail("undilling of bytestream failed with PicklingError - write manual converter")
272+
except Exception as e:
273+
return ReprFail("undilling of bytestream failed for class %s - write manual converter."
274+
"Error: %s - %s" % (obj_class, type(e), e))
272275

273276
def errored(el):
274277
return el is None or (isinstance(el, list) and 'backend-error' in str(el))

pythonwhat/test_funcs/test_function.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ def build_test(stud, sol, state, do_eval, eq_fun, feedback_msg, add_more, highli
454454
if do_eval:
455455

456456
eval_solution, str_solution = getResultInProcess(tree = sol, process = state.solution_process)
457-
if str_solution is None:
457+
if isinstance(str_solution, Exception):
458458
raise ValueError("Running an argument in the solution environment raised an error")
459459
if isinstance(eval_solution, ReprFail):
460460
raise ValueError("Couldn't figure out the argument: " + eval_solution.info)

tests/test_test_function.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,49 @@ def test_nohighlight_too_few_calls(self):
660660
self.assertEqual(sct_payload.get('line_start'), None)
661661

662662

663+
class TestFunctionComplexArgs(unittest.TestCase):
664+
def setUp(self):
665+
self.data = {
666+
"DC_SOLUTION": """
667+
def sum2(arr): return sum(arr)
668+
669+
def apply(f, arr): return f(arr)
670+
671+
apply(sum2, [1,2,3])
672+
""",
673+
"DC_SCT": """
674+
test_function('apply')
675+
"""
676+
}
677+
self.data["DC_CODE"] = self.data["DC_SOLUTION"]
678+
679+
def test_function_with_funcarg_fails(self):
680+
# because functions are shipped across student and submission processes
681+
# they are always "unequal".
682+
sct_payload = helper.run(self.data)
683+
self.assertFalse(sct_payload['correct'])
684+
685+
def test_pass_with_no_eval(self):
686+
self.data["DC_SCT"] = """test_function_v2('apply', params=['f', 'arr'], do_eval=[False, True])"""
687+
sct_payload = helper.run(self.data)
688+
self.assertTrue(sct_payload['correct'])
689+
690+
def test_fail_undillable_args(self):
691+
self.data = {
692+
"DC_PEC": """
693+
import pickle; from io import BytesIO
694+
695+
file = BytesIO(pickle.dumps('abc'))
696+
""",
697+
"DC_SOLUTION": "d = pickle.load(file); print(d)",
698+
"DC_CODE": "print(file)",
699+
"DC_SCT": """test_function("print", index=1)"""
700+
}
701+
sct_payload = helper.run(self.data)
702+
self.assertFalse(sct_payload['correct'])
703+
704+
705+
663706

664707
if __name__ == "__main__":
665708
unittest.main()

0 commit comments

Comments
 (0)