Opened 5 years ago

Closed 2 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: saz
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 kmtracey)

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@… 3 years ago.
unittest_13958.diff (1.9 KB) - added by fredrik.kers@… 3 years ago.
unittest_13958.2.diff (1.9 KB) - added by fredrik.kers@… 3 years ago.
patch_13958.2.diff (1.0 KB) - added by riquito 3 years ago.
unittest_13958.3.diff (1.4 KB) - added by riquito 3 years ago.
13958_fixed.diff (2.5 KB) - added by saz 2 years ago.
This patch is working for the current Django master

Download all attachments as: .zip

Change History (20)

comment:1 Changed 5 years ago by kmtracey

  • Description modified (diff)

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

comment:2 Changed 4 years ago by nessita

  • Owner changed from nobody to nessita

comment:3 Changed 4 years ago by nessita

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 4 years ago by russellm

  • Resolution set to invalid
  • Status changed from new to closed

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 4 years ago by fredrik.kers@…

  • Easy pickings unset
  • Has patch set
  • Resolution invalid deleted
  • Severity set to Normal
  • Status changed from closed to reopened
  • Type set to 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 4 years ago by anonymous

  • Easy pickings set
  • Type changed from Uncategorized to Bug

comment:7 Changed 3 years ago by Alex

  • Resolution set to needsinfo
  • Status changed from reopened to closed

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 3 years ago by fredrik.kers@…

Changed 3 years ago by fredrik.kers@…

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

  • Resolution needsinfo deleted
  • Status changed from closed to reopened

Patch and unittest added

Changed 3 years ago by fredrik.kers@…

comment:9 Changed 3 years ago by aaugustin

  • Component changed from Uncategorized to Core (Other)
  • Triage Stage changed from Unreviewed to Accepted

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

utils.text.normalize_newlines might be handy here.

Changed 3 years ago by riquito

Changed 3 years ago by riquito

comment:10 Changed 3 years ago by riquito

Updated the patch and refactored the test code

comment:11 Changed 2 years ago by aaugustin

  • Status changed from reopened to new

Changed 2 years ago by saz

This patch is working for the current Django master

comment:12 Changed 2 years ago by saz

  • Cc me@… added

comment:13 Changed 2 years ago by saz

  • Owner changed from nessita to saz
  • Status changed from new to assigned

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

  • Resolution set to fixed
  • Status changed from assigned to closed

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