From 8eea8e99258f073374912985acc191295536deeb Mon Sep 17 00:00:00 2001 From: ankitjavalkar Date: Thu, 9 Feb 2017 18:30:00 +0530 Subject: Format the assertion and various error exceptions for Python Assertion evaluator --- yaksh/python_assertion_evaluator.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/yaksh/python_assertion_evaluator.py b/yaksh/python_assertion_evaluator.py index 11fa101..9e1a6ac 100644 --- a/yaksh/python_assertion_evaluator.py +++ b/yaksh/python_assertion_evaluator.py @@ -2,6 +2,7 @@ import sys import traceback import os +import re from os.path import join import importlib @@ -84,9 +85,17 @@ class PythonAssertionEvaluator(BaseEvaluator): ) except TimeoutException: raise - except Exception: + except RecursionError: msg = traceback.format_exc(limit=0) - err = "Error in Test case: {0}".format(msg) + err = "Error Traceback: {0}".format(msg) + except Exception: + err_tb_lines = traceback.format_exc().splitlines() + stripped_tb_lines = [] + for line in err_tb_lines: + if '.py' not in line: + stripped_tb_lines.append(line) + stripped_tb = '\n'.join(stripped_tb_lines) + err = "Error Traceback:\n{0}".format(stripped_tb) else: success = True err = None -- cgit From 85412e8f77a268590cf8ca6f6cd6facd8fdc3c2c Mon Sep 17 00:00:00 2001 From: ankitjavalkar Date: Mon, 13 Feb 2017 15:21:34 +0530 Subject: Merge all error handling into single code block and fix relevant test cases for Python assertion --- yaksh/evaluator_tests/test_python_evaluation.py | 13 +++++------ yaksh/python_assertion_evaluator.py | 31 ++++++++++++------------- 2 files changed, 21 insertions(+), 23 deletions(-) diff --git a/yaksh/evaluator_tests/test_python_evaluation.py b/yaksh/evaluator_tests/test_python_evaluation.py index 6346941..b908ff7 100644 --- a/yaksh/evaluator_tests/test_python_evaluation.py +++ b/yaksh/evaluator_tests/test_python_evaluation.py @@ -235,7 +235,7 @@ class PythonAssertionEvaluationTestCases(EvaluatorBaseTest): # Then self.assertFalse(result.get("success")) - self.assertEqual(6, len(err)) + self.assertEqual(22, len(err)) for msg in name_error_msg: self.assert_correct_output(msg, result.get("error")) @@ -245,8 +245,7 @@ class PythonAssertionEvaluationTestCases(EvaluatorBaseTest): def add(a, b): return add(3, 3) """) - recursion_error_msg = ["Traceback", - "call", + recursion_error_msg = ["Traceback", "maximum recursion depth exceeded" ] @@ -301,7 +300,7 @@ class PythonAssertionEvaluationTestCases(EvaluatorBaseTest): # Then self.assertFalse(result.get("success")) - self.assertEqual(6, len(err)) + self.assertEqual(22, len(err)) for msg in type_error_msg: self.assert_correct_output(msg, result.get("error")) @@ -337,7 +336,7 @@ class PythonAssertionEvaluationTestCases(EvaluatorBaseTest): # Then self.assertFalse(result.get("success")) - self.assertEqual(6, len(err)) + self.assertEqual(25, len(err)) for msg in value_error_msg: self.assert_correct_output(msg, result.get("error")) @@ -404,7 +403,7 @@ class PythonAssertionEvaluationTestCases(EvaluatorBaseTest): # Then self.assertFalse(result.get("success")) - self.assertEqual(5, len(err)) + self.assertEqual(12, len(err)) for msg in syntax_error_msg: self.assert_correct_output(msg, result.get("error")) @@ -446,7 +445,7 @@ class PythonAssertionEvaluationTestCases(EvaluatorBaseTest): # Then self.assertFalse(result.get("success")) - self.assertEqual(2, len(err)) + self.assertEqual(10, len(err)) for msg in name_error_msg: self.assert_correct_output(msg, result.get("error")) diff --git a/yaksh/python_assertion_evaluator.py b/yaksh/python_assertion_evaluator.py index 9e1a6ac..77efbc1 100644 --- a/yaksh/python_assertion_evaluator.py +++ b/yaksh/python_assertion_evaluator.py @@ -71,31 +71,30 @@ class PythonAssertionEvaluator(BaseEvaluator): tb = None _tests = compile(self.test_case, '', mode='exec') exec(_tests, self.exec_scope) - except AssertionError: + except TimeoutException: + raise + except Exception: type, value, tb = sys.exc_info() info = traceback.extract_tb(tb) fname, lineno, func, text = info[-1] text = str(self.test_case) - err = "Expected Test Case:\n{0}\n" \ - "Error - {1} {2} in:\n {3}\n".format( - self.test_case, - type.__name__, - str(value), - text - ) - except TimeoutException: - raise - except RecursionError: - msg = traceback.format_exc(limit=0) - err = "Error Traceback: {0}".format(msg) - except Exception: + + # Get truncated traceback err_tb_lines = traceback.format_exc().splitlines() stripped_tb_lines = [] for line in err_tb_lines: if '.py' not in line: stripped_tb_lines.append(line) - stripped_tb = '\n'.join(stripped_tb_lines) - err = "Error Traceback:\n{0}".format(stripped_tb) + stripped_tb = '\n'.join(stripped_tb_lines[-10::]) + + err = "Expected Test Case:\n{0}\n" \ + "Error Traceback- {1} {2} in:\n {3}\n{4}".format( + self.test_case, + type.__name__, + str(value), + text, + stripped_tb + ) else: success = True err = None -- cgit From 9bedcd25c823561f9a499f47fabd5f7c436be6e6 Mon Sep 17 00:00:00 2001 From: ankitjavalkar Date: Fri, 17 Feb 2017 17:33:25 +0530 Subject: Remove filename strings from error traceback in python assertion --- yaksh/python_assertion_evaluator.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/yaksh/python_assertion_evaluator.py b/yaksh/python_assertion_evaluator.py index 77efbc1..c8f2dd0 100644 --- a/yaksh/python_assertion_evaluator.py +++ b/yaksh/python_assertion_evaluator.py @@ -83,12 +83,15 @@ class PythonAssertionEvaluator(BaseEvaluator): err_tb_lines = traceback.format_exc().splitlines() stripped_tb_lines = [] for line in err_tb_lines: - if '.py' not in line: - stripped_tb_lines.append(line) + line = re.sub(r'File\s+".*?",\s+line', + 'File , line', + line + ) + stripped_tb_lines.append(line) stripped_tb = '\n'.join(stripped_tb_lines[-10::]) err = "Expected Test Case:\n{0}\n" \ - "Error Traceback- {1} {2} in:\n {3}\n{4}".format( + "Error Traceback - {1} {2} in:\n {3}\n{4}".format( self.test_case, type.__name__, str(value), -- cgit From b3c1ea3bd82f03599393916138e19dc258e2e380 Mon Sep 17 00:00:00 2001 From: ankitjavalkar Date: Mon, 20 Feb 2017 11:46:06 +0530 Subject: Fix test cases --- yaksh/evaluator_tests/test_python_evaluation.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/yaksh/evaluator_tests/test_python_evaluation.py b/yaksh/evaluator_tests/test_python_evaluation.py index b908ff7..51f9bea 100644 --- a/yaksh/evaluator_tests/test_python_evaluation.py +++ b/yaksh/evaluator_tests/test_python_evaluation.py @@ -235,7 +235,7 @@ class PythonAssertionEvaluationTestCases(EvaluatorBaseTest): # Then self.assertFalse(result.get("success")) - self.assertEqual(22, len(err)) + self.assertEqual(25, len(err)) for msg in name_error_msg: self.assert_correct_output(msg, result.get("error")) @@ -300,7 +300,7 @@ class PythonAssertionEvaluationTestCases(EvaluatorBaseTest): # Then self.assertFalse(result.get("success")) - self.assertEqual(22, len(err)) + self.assertEqual(25, len(err)) for msg in type_error_msg: self.assert_correct_output(msg, result.get("error")) @@ -336,7 +336,7 @@ class PythonAssertionEvaluationTestCases(EvaluatorBaseTest): # Then self.assertFalse(result.get("success")) - self.assertEqual(25, len(err)) + self.assertEqual(28, len(err)) for msg in value_error_msg: self.assert_correct_output(msg, result.get("error")) @@ -403,7 +403,7 @@ class PythonAssertionEvaluationTestCases(EvaluatorBaseTest): # Then self.assertFalse(result.get("success")) - self.assertEqual(12, len(err)) + self.assertEqual(13, len(err)) for msg in syntax_error_msg: self.assert_correct_output(msg, result.get("error")) @@ -445,7 +445,7 @@ class PythonAssertionEvaluationTestCases(EvaluatorBaseTest): # Then self.assertFalse(result.get("success")) - self.assertEqual(10, len(err)) + self.assertEqual(11, len(err)) for msg in name_error_msg: self.assert_correct_output(msg, result.get("error")) -- cgit