Skip to content

Commit a6a5ea9

Browse files
committed
fail gracefully when dill has error, issues #155, #169
1 parent 01c7a08 commit a6a5ea9

2 files changed

Lines changed: 26 additions & 8 deletions

File tree

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))

tests/test_test_function.py

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

662662

663-
class TestFunctionWithFunctionArg(unittest.TestCase):
663+
class TestFunctionComplexArgs(unittest.TestCase):
664664
def setUp(self):
665665
self.data = {
666666
"DC_SOLUTION": """
@@ -671,22 +671,37 @@ def apply(f, arr): return f(arr)
671671
apply(sum2, [1,2,3])
672672
""",
673673
"DC_SCT": """
674-
def sum2(arr): return sum(arr)
675674
test_function('apply')
676675
"""
677676
}
678677
self.data["DC_CODE"] = self.data["DC_SOLUTION"]
679678

680-
@unittest.skip("Can't unpickle user def funcs, but doesn't throw PicklingError (see issue #155)")
681-
def test_pass(self):
679+
def test_function_with_funcarg_fails(self):
680+
# because functions are shipped across student and submission processes
681+
# they are always "unequal".
682682
sct_payload = helper.run(self.data)
683-
self.assertTrue(sct_payload['correct'])
683+
self.assertFalse(sct_payload['correct'])
684684

685-
def test_pass_v2(self):
685+
def test_pass_with_no_eval(self):
686686
self.data["DC_SCT"] = """test_function_v2('apply', params=['f', 'arr'], do_eval=[False, True])"""
687687
sct_payload = helper.run(self.data)
688688
self.assertTrue(sct_payload['correct'])
689689

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+
690705

691706

692707
if __name__ == "__main__":

0 commit comments

Comments
 (0)