Opened 6 years ago

Closed 3 years ago

#13958 closed Bug (fixed)

There is an error, when a source file with backslash-r returns is interpreted by the Python error report page.

Reported by: petrvanblokland Owned by: Steffen Zieger
Component: Core (Other) Version: 1.2
Severity: Normal Keywords:
Cc: me@… Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description (last modified by Karen Tracey)

There is an error, when a source file with backslash-r returns is interpreted by the Python error report page.

Due to a hard split on backslah-n, the lineno is out of range.
Also it is not clear what the name of the file is where this goes wrong.
Adding source = source.replace('\r', '\n') solves the problem, but maybe not structural enough.

Kind regards,
Petr van Blokland
buro@…

 Traceback (most recent call last):

  File "/Library/Python/2.5/site-packages/django/core/servers/basehttp.py", line 279, in run
    self.result = application(self.environ, self.start_response)

  File "/Library/Python/2.5/site-packages/django/core/servers/basehttp.py", line 651, in __call__
    return self.application(environ, start_response)

  File "/Library/Python/2.5/site-packages/django/core/handlers/wsgi.py", line 241, in __call__
    response = self.get_response(request)

  File "/Library/Python/2.5/site-packages/django/core/handlers/base.py", line 137, in get_response
    return self.handle_uncaught_exception(request, resolver, exc_info)

  File "/Library/Python/2.5/site-packages/django/core/handlers/base.py", line 157, in handle_uncaught_exception
    return debug.technical_500_response(request, *exc_info)

  File "/Library/Python/2.5/site-packages/django/views/debug.py", line 58, in technical_500_response
    html = reporter.get_traceback_html()

  File "/Library/Python/2.5/site-packages/django/views/debug.py", line 109, in get_traceback_html
    frames = self.get_traceback_frames()

  File "/Library/Python/2.5/site-packages/django/views/debug.py", line 228, in get_traceback_frames
    pre_context_lineno, pre_context, context_line, post_context = self._get_lines_from_file(filename, lineno, 7, loader, module_name)

  File "/Library/Python/2.5/site-packages/django/views/debug.py", line 209, in _get_lines_from_file
    context_line = source[lineno].strip('\n')

IndexError: list index out of range

Attachments (6)

patch_13958.diff (1023 bytes) - added by fredrik.kers@… 5 years ago.
unittest_13958.diff (1.9 KB) - added by fredrik.kers@… 5 years ago.
unittest_13958.2.diff (1.9 KB) - added by fredrik.kers@… 5 years ago.
patch_13958.2.diff (1.0 KB) - added by Riccardo Attilio Galli 4 years ago.
unittest_13958.3.diff (1.4 KB) - added by Riccardo Attilio Galli 4 years ago.
13958_fixed.diff (2.5 KB) - added by Steffen Zieger 3 years ago.
This patch is working for the current Django master

Download all attachments as: .zip

Change History (20)

comment:1 Changed 6 years ago by Karen Tracey

Description: modified (diff)

Fixed formatting. Please use WikiFormatting and the preview button before submitting.

comment:2 Changed 6 years ago by Natalia Bidart

Owner: changed from nobody to Natalia Bidart

comment:3 Changed 6 years ago by Natalia Bidart

Hello!

I'm tying to reproduce this issue but I can't. Can you please add detailed instructions so we can reproduce?

I've confirmed that when opening a file with open(), python will correctly handle the different line terminators according to the platform, so after calling readlines() on a file descriptor returned with open() (using mode 'r'), the lines will only have the '\n' at the end, not the '\r\n'.

So maybe, this bug is caused by the call to 'loader.get_source' (django/views/debug.py(180)), but I can't confirm it without some more context.

comment:4 Changed 6 years ago by Russell Keith-Magee

Resolution: invalid
Status: newclosed

Closing invalid. Reporter hasn't responded to a request for more info. Please reopen if you can provide a test case as requested.

comment:5 Changed 5 years ago by fredrik.kers@…

Easy pickings: unset
Has patch: set
Resolution: invalid
Severity: Normal
Status: closedreopened
Type: Uncategorized
UI/UX: unset

django.views.debug._get_lines_from_file uses readlines to split the lines of a file. This functions does not support the old Macintosh line break (\r) which is a valid line break in python code (http://docs.python.org/reference/lexical_analysis.html#physical-lines). So when an exception occurs in a file with Macintosh line break, the get_lines_from_file method will get an exception on the line

context_line = source[lineno].strip('\n')

since the source list will only contain one line with alot of '\r'.

This can be fixed by replacing the line

source = f.readlines()

with

source = f.read().splitlines()

comment:6 Changed 5 years ago by anonymous

Easy pickings: set
Type: UncategorizedBug

comment:7 Changed 5 years ago by Alex Gaynor

Resolution: needsinfo
Status: reopenedclosed

Closing as needsinfo. In order to accept this bug we need to know how to actually reproduce it, not just a statement of what looks wrong in the code and what to change.

Changed 5 years ago by fredrik.kers@…

Attachment: patch_13958.diff added

Changed 5 years ago by fredrik.kers@…

Attachment: unittest_13958.diff added

comment:8 Changed 5 years ago by fredrik.kers@…

Resolution: needsinfo
Status: closedreopened

Patch and unittest added

Changed 5 years ago by fredrik.kers@…

Attachment: unittest_13958.2.diff added

comment:9 Changed 5 years ago by Aymeric Augustin

Component: UncategorizedCore (Other)
Triage Stage: UnreviewedAccepted

The test case looks valid (even though the style could be improved).

utils.text.normalize_newlines might be handy here.

Changed 4 years ago by Riccardo Attilio Galli

Attachment: patch_13958.2.diff added

Changed 4 years ago by Riccardo Attilio Galli

Attachment: unittest_13958.3.diff added

comment:10 Changed 4 years ago by Riccardo Attilio Galli

Updated the patch and refactored the test code

comment:11 Changed 4 years ago by Aymeric Augustin

Status: reopenednew

Changed 3 years ago by Steffen Zieger

Attachment: 13958_fixed.diff added

This patch is working for the current Django master

comment:12 Changed 3 years ago by Steffen Zieger

Cc: me@… added

comment:13 Changed 3 years ago by Steffen Zieger

Owner: changed from Natalia Bidart to Steffen Zieger
Status: newassigned

comment:14 Changed 3 years ago by Shai Berger <shai.berger@…>

Resolution: fixed
Status: assignedclosed

In ff881aef536640d712fd2bc53fa1ff98e300adad:

Fixed #13958 -- problem reporting exception from \r-line-ended file

Thanks petrvanblokland for reporting and saz for the patch

Note: See TracTickets for help on using tickets.
Back to Top