Skip to content

Commit abcce76

Browse files
Mr-Neutr0nclaude
andcommitted
Fix crash when traceback frame has tb_lineno=None
When formatting exceptions, Loguru calls linecache.getline() with the line number from traceback frames. In some edge cases on Python 3.12+, tb_lineno can be None (see CPython issues #139531, #89726), which causes a TypeError in linecache.getline(). This fix guards against None lineno values by returning an empty source string in that case, allowing the exception to be logged even if the source line cannot be retrieved. Fixes #1435 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 764cd30 commit abcce76

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

loguru/_better_exceptions.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,11 @@ def _extract_frames(self, tb, is_first, *, limit=None, from_decorator=False):
208208
def get_info(frame, lineno):
209209
filename = frame.f_code.co_filename
210210
function = frame.f_code.co_name
211-
source = linecache.getline(filename, lineno).strip()
211+
# Guard against None lineno (edge case on Python 3.12+, see CPython #139531)
212+
if lineno is None:
213+
source = ""
214+
else:
215+
source = linecache.getline(filename, lineno).strip()
212216
return filename, lineno, function, source
213217

214218
infos = []

0 commit comments

Comments
 (0)