Skip to content

Fix crash when traceback frame has tb_lineno=None#1436

Open
Mr-Neutr0n wants to merge 1 commit intoDelgan:masterfrom
Mr-Neutr0n:fix-tb-lineno-none
Open

Fix crash when traceback frame has tb_lineno=None#1436
Mr-Neutr0n wants to merge 1 commit intoDelgan:masterfrom
Mr-Neutr0n:fix-tb-lineno-none

Conversation

@Mr-Neutr0n
Copy link

Summary

  • Fixes TypeError crash when formatting exceptions where a traceback frame has tb_lineno=None
  • Guards against None lineno values by returning an empty source string instead of calling linecache.getline()

Details

When formatting exceptions, Loguru calls linecache.getline(filename, lineno) with the line number from traceback frames. In some edge cases on Python 3.12+, tb_lineno can be None (see CPython issues python/cpython#139531 and python/cpython#89726), which causes a TypeError:

TypeError: '<=' not supported between instances of 'int' and 'NoneType'

This happens because linecache.getline() compares lineno with integers.

Fix

The fix guards against None lineno values in the get_info() function:

if lineno is None:
    source = ""
else:
    source = linecache.getline(filename, lineno).strip()

This allows exceptions to be logged even when the source line cannot be retrieved for frames with None lineno.

Test plan

  • Manually verified the fix handles None lineno gracefully
  • The fix is minimal and targeted

Fixes #1435

🤖 Generated with Claude Code

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 Delgan#1435

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@Mr-Neutr0n
Copy link
Author

Friendly follow-up - is there anything I can improve in this PR? Happy to address any feedback. Thanks!

@Mr-Neutr0n
Copy link
Author

Friendly bump! Let me know if there's anything I should update or improve to help move this forward.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Crash while formatting exceptions when a traceback frame has tb_lineno=None (TypeError in linecache.getline())

1 participant