Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Show frame.f_lineno as None, rather than -1, if there is no line number.
  • Loading branch information
markshannon committed Apr 29, 2021
commit fd6c7d5307923f22d75417928c26a2f7948908d8
11 changes: 10 additions & 1 deletion Lib/test/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2081,7 +2081,10 @@ def lineno_after_raise(self, f, line):
while t.tb_next:
t = t.tb_next
frame = t.tb_frame
self.assertEqual(frame.f_lineno-frame.f_code.co_firstlineno, line)
if line is None:
self.assertEqual(frame.f_lineno, line)
else:
self.assertEqual(frame.f_lineno-frame.f_code.co_firstlineno, line)

def test_lineno_after_raise_simple(self):
def simple():
Expand Down Expand Up @@ -2153,6 +2156,12 @@ def after_with():
pass
self.lineno_after_raise(after_with, 2)

def test_missing_lineno_shows_as_none(self):
def f():
1/0
self.lineno_after_raise(f, 1)
f.__code__ = f.__code__.replace(co_linetable=b'\x04\x80\xff\x80')
self.lineno_after_raise(f, None)

if __name__ == '__main__':
unittest.main()
8 changes: 7 additions & 1 deletion Objects/frameobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,13 @@ PyFrame_GetLineNumber(PyFrameObject *f)
static PyObject *
frame_getlineno(PyFrameObject *f, void *closure)
{
return PyLong_FromLong(PyFrame_GetLineNumber(f));
int lineno = PyFrame_GetLineNumber(f);
if (lineno < 0) {
Py_RETURN_NONE;
}
else {
return PyLong_FromLong(PyFrame_GetLineNumber(f));
}
}

static PyObject *
Expand Down