|
| 1 | +# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | +# This source code is licensed under the MIT license found in the |
| 3 | +# LICENSE file in the root directory of this source tree. |
| 4 | + |
| 5 | +# pyre-strict |
| 6 | + |
| 7 | +import os |
| 8 | +import tempfile |
| 9 | +import unittest |
| 10 | + |
| 11 | +import tintype |
| 12 | +from tintype._exception import _MAX_CHAIN_DEPTH |
| 13 | + |
| 14 | + |
| 15 | +class ReconstructExceptionTest(unittest.TestCase): |
| 16 | + def _capture( |
| 17 | + self, exc: BaseException |
| 18 | + ) -> tuple[tintype.SnapshotReader, tintype.Snapshot]: |
| 19 | + tmpdir = tempfile.mkdtemp() |
| 20 | + path = os.path.join(tmpdir, "test.pytb") |
| 21 | + tintype.initialize() |
| 22 | + tintype.take_snapshot(exc) |
| 23 | + tintype.finalize(path) |
| 24 | + reader = tintype.SnapshotReader(path) |
| 25 | + snap = reader.get_latest_snapshot() |
| 26 | + self.assertIsNotNone(snap) |
| 27 | + assert snap is not None |
| 28 | + return reader, snap |
| 29 | + |
| 30 | + def _find(self, snap: tintype.Snapshot, type_name: str) -> tintype.Stacktrace: |
| 31 | + for st in snap.stacktraces.values(): |
| 32 | + if st.exception_object is not None and type_name in repr( |
| 33 | + st.exception_object |
| 34 | + ): |
| 35 | + return st |
| 36 | + self.fail(f"No stacktrace found for exception type {type_name}") |
| 37 | + |
| 38 | + def test_reconstructs_message_and_traceback(self) -> None: |
| 39 | + try: |
| 40 | + raise KeyError("missing") |
| 41 | + except KeyError as e: |
| 42 | + captured = e |
| 43 | + _reader, snap = self._capture(captured) |
| 44 | + st = self._find(snap, "KeyError") |
| 45 | + |
| 46 | + exc = st.reconstruct_exception() |
| 47 | + self.assertIsNotNone(exc) |
| 48 | + assert exc is not None |
| 49 | + # Class is always Exception (original class is not recoverable), but |
| 50 | + # the message is preserved and the traceback is wired. |
| 51 | + self.assertIsInstance(exc, Exception) |
| 52 | + self.assertIn("missing", str(exc)) |
| 53 | + self.assertIsNotNone(exc.__traceback__) |
| 54 | + |
| 55 | + def test_wires_cause_chain(self) -> None: |
| 56 | + try: |
| 57 | + try: |
| 58 | + raise KeyError("inner") |
| 59 | + except KeyError as inner: |
| 60 | + raise RuntimeError("outer") from inner |
| 61 | + except RuntimeError as e: |
| 62 | + captured = e |
| 63 | + _reader, snap = self._capture(captured) |
| 64 | + st = self._find(snap, "RuntimeError") |
| 65 | + |
| 66 | + exc = st.reconstruct_exception() |
| 67 | + assert exc is not None |
| 68 | + self.assertIn("outer", str(exc)) |
| 69 | + cause = exc.__cause__ |
| 70 | + self.assertIsNotNone(cause) |
| 71 | + assert cause is not None |
| 72 | + self.assertIn("inner", str(cause)) |
| 73 | + self.assertTrue(exc.__suppress_context__) |
| 74 | + |
| 75 | + def test_wires_context_chain(self) -> None: |
| 76 | + try: |
| 77 | + try: |
| 78 | + raise ValueError("first") |
| 79 | + except ValueError: |
| 80 | + raise TypeError("second") |
| 81 | + except TypeError as e: |
| 82 | + captured = e |
| 83 | + _reader, snap = self._capture(captured) |
| 84 | + st = self._find(snap, "TypeError") |
| 85 | + |
| 86 | + exc = st.reconstruct_exception() |
| 87 | + assert exc is not None |
| 88 | + self.assertIn("second", str(exc)) |
| 89 | + context = exc.__context__ |
| 90 | + self.assertIsNotNone(context) |
| 91 | + assert context is not None |
| 92 | + self.assertIn("first", str(context)) |
| 93 | + |
| 94 | + def test_chain_depth_is_capped(self) -> None: |
| 95 | + def raise_chain(level: int) -> None: |
| 96 | + if level == 0: |
| 97 | + raise RuntimeError("level-0") |
| 98 | + try: |
| 99 | + raise_chain(level - 1) |
| 100 | + except RuntimeError as inner: |
| 101 | + raise RuntimeError(f"level-{level}") from inner |
| 102 | + |
| 103 | + # Unlike the tests above, the raise is behind a call, so the type |
| 104 | + # checker cannot prove the ``except`` branch runs — seed ``captured``. |
| 105 | + captured: RuntimeError | None = None |
| 106 | + try: |
| 107 | + raise_chain(_MAX_CHAIN_DEPTH + 3) |
| 108 | + except RuntimeError as e: |
| 109 | + captured = e |
| 110 | + assert captured is not None |
| 111 | + _reader, snap = self._capture(captured) |
| 112 | + st = snap.stacktraces[1] |
| 113 | + |
| 114 | + exc = st.reconstruct_exception() |
| 115 | + assert exc is not None |
| 116 | + depth = 0 |
| 117 | + current = exc |
| 118 | + while current.__cause__ is not None or current.__context__ is not None: |
| 119 | + current = current.__cause__ or current.__context__ |
| 120 | + assert current is not None |
| 121 | + depth += 1 |
| 122 | + self.assertEqual(depth, _MAX_CHAIN_DEPTH) |
| 123 | + |
| 124 | + def test_none_for_thread_snapshot(self) -> None: |
| 125 | + tmpdir = tempfile.mkdtemp() |
| 126 | + path = os.path.join(tmpdir, "thread.pytb") |
| 127 | + tintype.initialize() |
| 128 | + tintype.take_snapshot() |
| 129 | + tintype.finalize(path) |
| 130 | + snap = tintype.SnapshotReader(path).get_latest_snapshot() |
| 131 | + assert snap is not None |
| 132 | + # A plain (non-exception) snapshot has no exception_object, so |
| 133 | + # reconstruct_exception is None. |
| 134 | + for st in snap.stacktraces.values(): |
| 135 | + if st.exception_object is None: |
| 136 | + self.assertIsNone(st.reconstruct_exception()) |
| 137 | + return |
| 138 | + self.skipTest("no non-exception stacktrace present") |
0 commit comments