diff options
-rw-r--r-- | Lib/test/test_traceback.py | 4 | ||||
-rw-r--r-- | Lib/traceback.py | 7 | ||||
-rw-r--r-- | Misc/NEWS.d/next/Library/2021-06-17-22-39-34.bpo-44446.qwdRic.rst | 1 |
3 files changed, 9 insertions, 3 deletions
diff --git a/Lib/test/test_traceback.py b/Lib/test/test_traceback.py index dd94590..61d86a1 100644 --- a/Lib/test/test_traceback.py +++ b/Lib/test/test_traceback.py @@ -1001,6 +1001,10 @@ class TestFrame(unittest.TestCase): '"""Test cases for traceback module"""', f.line) + def test_no_line(self): + f = traceback.FrameSummary("f", None, "dummy") + self.assertEqual(f.line, None) + def test_explicit_line(self): f = traceback.FrameSummary("f", 1, "dummy", line="line") self.assertEqual("line", f.line) diff --git a/Lib/traceback.py b/Lib/traceback.py index 7a7cca1..463cb22 100644 --- a/Lib/traceback.py +++ b/Lib/traceback.py @@ -301,9 +301,10 @@ class FrameSummary: @property def line(self): if self._line is None: - self._line = linecache.getline(self.filename, self.lineno).strip() - return self._line - + if self.lineno is None: + return None + self._line = linecache.getline(self.filename, self.lineno) + return self._line.strip() def walk_stack(f): """Walk a stack yielding the frame and line number for each frame. diff --git a/Misc/NEWS.d/next/Library/2021-06-17-22-39-34.bpo-44446.qwdRic.rst b/Misc/NEWS.d/next/Library/2021-06-17-22-39-34.bpo-44446.qwdRic.rst new file mode 100644 index 0000000..6d9758f --- /dev/null +++ b/Misc/NEWS.d/next/Library/2021-06-17-22-39-34.bpo-44446.qwdRic.rst @@ -0,0 +1 @@ +Take into account that ``lineno`` might be ``None`` in :class:`traceback.FrameSummary`. |