|
2 | 2 |
|
3 | 3 | import inspect
|
4 | 4 | import os.path
|
5 |
| -import django |
| 5 | +import re |
6 | 6 | import sys
|
7 | 7 |
|
| 8 | +import django |
8 | 9 | from django.core.exceptions import ImproperlyConfigured
|
9 | 10 | from django.utils.encoding import force_text
|
10 | 11 | from django.utils.html import escape
|
@@ -160,14 +161,27 @@ def getframeinfo(frame, context=1):
|
160 | 161 | try:
|
161 | 162 | lines, lnum = inspect.findsource(frame)
|
162 | 163 | except Exception: # findsource raises platform-dependant exceptions
|
163 |
| - lines = index = None |
| 164 | + first_lines = lines = index = None |
164 | 165 | else:
|
165 | 166 | start = max(start, 1)
|
166 | 167 | start = max(0, min(start, len(lines) - context))
|
| 168 | + first_lines = lines[:2] |
167 | 169 | lines = lines[start:(start + context)]
|
168 | 170 | index = lineno - 1 - start
|
169 | 171 | else:
|
170 |
| - lines = index = None |
| 172 | + first_lines = lines = index = None |
| 173 | + |
| 174 | + # Code taken from Django's ExceptionReporter._get_lines_from_file |
| 175 | + if first_lines and isinstance(first_lines[0], bytes): |
| 176 | + encoding = 'ascii' |
| 177 | + for line in first_lines[:2]: |
| 178 | + # File coding may be specified. Match pattern from PEP-263 |
| 179 | + # (http://www.python.org/dev/peps/pep-0263/) |
| 180 | + match = re.search(br'coding[:=]\s*([-\w.]+)', line) |
| 181 | + if match: |
| 182 | + encoding = match.group(1).decode('ascii') |
| 183 | + break |
| 184 | + lines = [line.decode(encoding, 'replace') for line in lines] |
171 | 185 |
|
172 | 186 | if hasattr(inspect, 'Traceback'):
|
173 | 187 | return inspect.Traceback(filename, lineno, frame.f_code.co_name, lines, index)
|
|
0 commit comments