|
| 1 | +from django.template import TemplateSyntaxError |
| 2 | + |
| 3 | +try: |
| 4 | + # support Django 1.9 |
| 5 | + from django.template.base import Origin |
| 6 | +except ImportError: |
| 7 | + # backward compatibility |
| 8 | + from django.template.loader import LoaderOrigin as Origin |
| 9 | + |
| 10 | + |
| 11 | +def get_template_frame_from_exception(exc_value): |
| 12 | + # As of Django 1.9 or so the new template debug thing showed up. |
| 13 | + if hasattr(exc_value, "template_debug"): |
| 14 | + return _get_template_frame_from_debug(exc_value.template_debug) |
| 15 | + |
| 16 | + # As of r16833 (Django) all exceptions may contain a |
| 17 | + # ``django_template_source`` attribute (rather than the legacy |
| 18 | + # ``TemplateSyntaxError.source`` check) |
| 19 | + if hasattr(exc_value, "django_template_source"): |
| 20 | + return _get_template_frame_from_source(exc_value.django_template_source) |
| 21 | + |
| 22 | + if isinstance(exc_value, TemplateSyntaxError) and hasattr(exc_value, "source"): |
| 23 | + source = exc_value.source |
| 24 | + if isinstance(source, (tuple, list)) and isinstance(source[0], Origin): |
| 25 | + return _get_template_frame_from_source(source) |
| 26 | + |
| 27 | + |
| 28 | +def _get_template_frame_from_debug(debug): |
| 29 | + if debug is None: |
| 30 | + return None |
| 31 | + |
| 32 | + lineno = debug["line"] |
| 33 | + filename = debug["name"] |
| 34 | + if filename is None: |
| 35 | + filename = "<django template>" |
| 36 | + |
| 37 | + pre_context = [] |
| 38 | + post_context = [] |
| 39 | + context_line = None |
| 40 | + |
| 41 | + for i, line in debug["source_lines"]: |
| 42 | + if i < lineno: |
| 43 | + pre_context.append(line) |
| 44 | + elif i > lineno: |
| 45 | + post_context.append(line) |
| 46 | + else: |
| 47 | + context_line = line |
| 48 | + |
| 49 | + return { |
| 50 | + "filename": filename, |
| 51 | + "lineno": lineno, |
| 52 | + "pre_context": pre_context[-5:], |
| 53 | + "post_context": post_context[:5], |
| 54 | + "context_line": context_line, |
| 55 | + } |
| 56 | + |
| 57 | + |
| 58 | +def _linebreak_iter(template_source): |
| 59 | + yield 0 |
| 60 | + p = template_source.find("\n") |
| 61 | + while p >= 0: |
| 62 | + yield p + 1 |
| 63 | + p = template_source.find("\n", p + 1) |
| 64 | + |
| 65 | + |
| 66 | +def _get_template_frame_from_source(source): |
| 67 | + if not source: |
| 68 | + return None |
| 69 | + |
| 70 | + origin, (start, end) = source |
| 71 | + filename = getattr(origin, "loadname", None) |
| 72 | + if filename is None: |
| 73 | + filename = "<django template>" |
| 74 | + template_source = origin.reload() |
| 75 | + lineno = None |
| 76 | + upto = 0 |
| 77 | + pre_context = [] |
| 78 | + post_context = [] |
| 79 | + context_line = None |
| 80 | + |
| 81 | + for num, next in enumerate(_linebreak_iter(template_source)): |
| 82 | + line = template_source[upto:next] |
| 83 | + if start >= upto and end <= next: |
| 84 | + lineno = num |
| 85 | + context_line = line |
| 86 | + elif lineno is None: |
| 87 | + pre_context.append(line) |
| 88 | + else: |
| 89 | + post_context.append(line) |
| 90 | + |
| 91 | + upto = next |
| 92 | + |
| 93 | + if context_line is None or lineno is None: |
| 94 | + return None |
| 95 | + |
| 96 | + return { |
| 97 | + "filename": filename, |
| 98 | + "lineno": lineno, |
| 99 | + "pre_context": pre_context[-5:], |
| 100 | + "post_context": post_context[:5], |
| 101 | + "context_line": context_line, |
| 102 | + } |
0 commit comments