Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

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

Thanks petrvanblokland for reporting and saz for the patch
  • Loading branch information...
commit ff881aef536640d712fd2bc53fa1ff98e300adad 1 parent 2c84f44
Shai Berger authored
Showing with 24 additions and 5 deletions.
  1. +4 −4 django/views/debug.py
  2. +20 −1 tests/view_tests/tests/test_debug.py
View
8 django/views/debug.py
@@ -347,7 +347,7 @@ def _get_lines_from_file(self, filename, lineno, context_lines, loader=None, mod
if source is None:
try:
with open(filename, 'rb') as fp:
- source = fp.readlines()
+ source = fp.read().splitlines()
except (OSError, IOError):
pass
if source is None:
@@ -370,9 +370,9 @@ def _get_lines_from_file(self, filename, lineno, context_lines, loader=None, mod
lower_bound = max(0, lineno - context_lines)
upper_bound = lineno + context_lines
- pre_context = [line.strip('\n') for line in source[lower_bound:lineno]]
- context_line = source[lineno].strip('\n')
- post_context = [line.strip('\n') for line in source[lineno+1:upper_bound]]
+ pre_context = source[lower_bound:lineno]
+ context_line = source[lineno]
+ post_context = source[lineno+1:upper_bound]
return lower_bound, pre_context, context_line, post_context
View
21 tests/view_tests/tests/test_debug.py
@@ -6,6 +6,7 @@
import inspect
import os
import sys
+import tempfile
from django.core import mail
from django.core.files.uploadedfile import SimpleUploadedFile
@@ -13,7 +14,7 @@
from django.test import TestCase, RequestFactory
from django.test.utils import (override_settings, setup_test_template_loader,
restore_template_loaders)
-from django.utils.encoding import force_text
+from django.utils.encoding import force_text, force_bytes
from django.views.debug import ExceptionReporter
from .. import BrokenException, except_args
@@ -122,6 +123,24 @@ def test_no_request(self):
self.assertIn('<h2>Request information</h2>', html)
self.assertIn('<p>Request data not supplied</p>', html)
+ def test_eol_support(self):
+ """Test that the ExceptionReporter supports Unix, Windows and Macintosh EOL markers"""
+ LINES = list(u'print %d' % i for i in range(1,6))
@aaugustin Owner

u'...' is a syntax error under Python 3.

@shaib Collaborator
shaib added a note

For the record, only under Python 3.[0-2]. Python 3.3 makes it kosher again.

@aaugustin Owner

Yes, but we took the pain to remove every instance from Django, I'd like to keep it free from this historical artifact in the future! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ reporter = ExceptionReporter(None, None, None, None)
+
+ for newline in ['\n','\r\n','\r']:
+ fd,filename = tempfile.mkstemp(text = False)
+ os.write(fd, force_bytes(newline.join(LINES)+newline))
+ os.close(fd)
+
+ try:
+ self.assertEqual(
+ reporter._get_lines_from_file(filename, 3, 2),
+ (1, LINES[1:3], LINES[3], LINES[4:])
+ )
+ finally:
+ os.unlink(filename)
+
def test_no_exception(self):
"An exception report can be generated for just a request"
request = self.rf.get('/test_view/')
Please sign in to comment.
Something went wrong with that request. Please try again.