aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLucas Abel <22837557+uael@users.noreply.github.com>2023-02-14 17:56:20 -0800
committerGitHub <noreply@github.com>2023-02-14 17:56:20 -0800
commitcaf37d004aea640011dc641857e52423b6c8442a (patch)
treebae15f28535eea3aa42f72bab920d63f26c7cf50
parenta8dc1c739d91adf93a83f4e4d4bb27168807df34 (diff)
downloadmobly-caf37d004aea640011dc641857e52423b6c8442a.tar.gz
Fix unhandling of possible recursion error on `ExceptionRecord.__deepcopy__` (#870)
-rw-r--r--mobly/records.py2
-rwxr-xr-xtests/mobly/records_test.py26
2 files changed, 27 insertions, 1 deletions
diff --git a/mobly/records.py b/mobly/records.py
index 38c992d..69f1f9d 100644
--- a/mobly/records.py
+++ b/mobly/records.py
@@ -277,7 +277,7 @@ class ExceptionRecord:
"""
try:
exception = copy.deepcopy(self.exception)
- except TypeError:
+ except (TypeError, RecursionError):
# If the exception object cannot be copied, use the original
# exception object.
exception = self.exception
diff --git a/tests/mobly/records_test.py b/tests/mobly/records_test.py
index 7b37a2f..ec6254d 100755
--- a/tests/mobly/records_test.py
+++ b/tests/mobly/records_test.py
@@ -36,6 +36,19 @@ class RecordTestError(Exception):
self._something = something
+class RecordTestRecursiveError(Exception):
+ """Error class with self recursion.
+
+ Used for ExceptionRecord tests.
+ """
+
+ def __init__(self):
+ super().__init__(self) # create a self recursion here.
+
+ def __str__(self):
+ return 'Oh ha!'
+
+
class RecordsTest(unittest.TestCase):
"""This test class tests the implementation of classes in mobly.records.
"""
@@ -427,6 +440,19 @@ class RecordsTest(unittest.TestCase):
self.assertDictEqual(er.to_dict(), new_er.to_dict())
self.assertEqual(er.type, 'RecordTestError')
+ def test_recursive_exception_record_deepcopy(self):
+ """Makes sure ExceptionRecord wrapper handles deep copy properly in case of
+ recursive exception.
+ """
+ try:
+ raise RecordTestRecursiveError()
+ except RecordTestRecursiveError as e:
+ er = records.ExceptionRecord(e)
+ new_er = copy.deepcopy(er)
+ self.assertIsNot(er, new_er)
+ self.assertDictEqual(er.to_dict(), new_er.to_dict())
+ self.assertEqual(er.type, 'RecordTestRecursiveError')
+
def test_add_controller_info_record(self):
tr = records.TestResult()
self.assertFalse(tr.controller_info)