Opened 20 minutes ago

Last modified 20 minutes ago

#37548 new Bug

Technical 500 page shows wrong source lines when a module contains form feeds or Unicode line separators

Reported by: Maddipatla Chatan Owned by:
Component: Error reporting Version: dev
Severity: Normal Keywords:
Cc: Maddipatla Chatan Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

ExceptionReporter._get_source() splits the source returned by a module loader with str.splitlines(). That also splits on form feeds (\x0c), \x1c-\x1e, \x85, \u2028 and \u2029, but Python only counts \n, \r\n and \r when numbering source lines. So after any of those characters, the context_line and the surrounding context shown on the technical 500 page are shifted.

The file fallback in the same method reads bytes and uses bytes.splitlines(), which only splits on \n, \r\n and \r, so it doesn't have this problem.

To reproduce, put this in a module (the third line is a single form feed character, which PEP 8 allows as a page break) and raise from it with DEBUG=True:

def one():
    pass
^L
def boom():
    x = 1
    raise ValueError("here")  # line 6

The frame reports lineno 6, but the highlighted context line is "x = 1" (line 5). A \u2028 inside a string literal shifts the lines the same way.

Splitting the loader source only on \n, \r\n and \r, like the file fallback does, fixes it.

Related: #13958, where splitlines() was introduced for \r line endings.

Change History (1)

comment:1 by Maddipatla Chatan, 20 minutes ago

Cc: Maddipatla Chatan added
Note: See TracTickets for help on using tickets.
Back to Top