|
2 | 2 | # |
3 | 3 | # Exception formatter that captures frame details in string format. |
4 | 4 | # |
5 | | -# :copyright: 2019 Sonu Kumar |
| 5 | +# :copyright: 2020 Sonu Kumar |
6 | 6 | # :license: BSD-3-Clause |
7 | 7 | # |
8 | 8 |
|
|
11 | 11 | import sys |
12 | 12 | import traceback |
13 | 13 |
|
14 | | -from django.http import QueryDict |
15 | | - |
16 | 14 | try: |
17 | 15 | import builtins |
18 | 16 | except ImportError: |
|
24 | 22 |
|
25 | 23 | import re |
26 | 24 | import types |
27 | | -from werkzeug.datastructures import ImmutableMultiDict |
| 25 | + |
| 26 | + |
| 27 | +def convert_if_possible(x): |
| 28 | + try: |
| 29 | + from werkzeug.datastructures import ImmutableMultiDict |
| 30 | + if type(x) == ImmutableMultiDict: |
| 31 | + return "ImmutableMultiDict({%s})", x.to_dict(flat=False) |
| 32 | + except ImportError: |
| 33 | + pass |
| 34 | + try: |
| 35 | + from django.http import QueryDict |
| 36 | + if type(x) == QueryDict: |
| 37 | + return "QueryDict({%s})", x.dict() |
| 38 | + except ImportError: |
| 39 | + pass |
| 40 | + return None, None |
28 | 41 |
|
29 | 42 |
|
30 | 43 | def format_frame(x, max_elements, max_string, max_recursion, masking=None): |
@@ -71,38 +84,34 @@ def _it_to_string(fmt, it, per_element=_per_element): |
71 | 84 |
|
72 | 85 | if x is builtins.__dict__: |
73 | 86 | return "<builtins>" |
74 | | - elif type(x) == dict: |
| 87 | + if type(x) == dict: |
75 | 88 | return _it_to_string("{%s}", sorted(x.items()), per_element=_per_dict_element) |
76 | | - elif type(x) == list: |
| 89 | + if type(x) == list: |
77 | 90 | return _it_to_string("[%s]", x) |
78 | | - elif type(x) == tuple: |
| 91 | + if type(x) == tuple: |
79 | 92 | return _it_to_string("(%s)" if len(x) != 1 |
80 | 93 | or max_recursion <= 0 |
81 | 94 | or max_elements <= 0 |
82 | 95 | else "(%s,)", x) |
83 | | - elif type(x) == set: |
| 96 | + if type(x) == set: |
84 | 97 | return _it_to_string("set([%s])", sorted(x)) |
85 | | - elif type(x) == frozenset: |
| 98 | + if type(x) == frozenset: |
86 | 99 | return _it_to_string("frozenset([%s])", sorted(x)) |
87 | | - elif isinstance(x, six.string_types) and max_string < len(x): |
| 100 | + if isinstance(x, six.string_types) and max_string < len(x): |
88 | 101 | return repr(x[:max_string] + "...") |
89 | | - elif type(x) == ImmutableMultiDict: |
90 | | - x = x.to_dict(flat=False) |
91 | | - return _it_to_string("ImmutableMultiDict({%s})", sorted(x.items()), |
92 | | - per_element=_per_dict_element) |
93 | | - elif type(x) == QueryDict: |
94 | | - x = x.dict() |
95 | | - return _it_to_string("QueryDict({%s})", sorted(x.items()), |
| 102 | + |
| 103 | + converted, x = convert_if_possible(x) |
| 104 | + if converted is not None: |
| 105 | + return _it_to_string(converted, sorted(x.items()), |
96 | 106 | per_element=_per_dict_element) |
97 | | - else: |
98 | | - try: |
99 | | - if issubclass(x, dict): |
100 | | - x = dict(x) |
101 | | - return _it_to_string("Dict({%s})", sorted(x.items()), |
102 | | - per_element=_per_dict_element) |
103 | | - except TypeError: |
104 | | - pass |
105 | | - return repr(x) |
| 107 | + try: |
| 108 | + if issubclass(x, dict): |
| 109 | + x = dict(x) |
| 110 | + return _it_to_string("Dict({%s})", sorted(x.items()), |
| 111 | + per_element=_per_dict_element) |
| 112 | + except TypeError: |
| 113 | + pass |
| 114 | + return repr(x) |
106 | 115 |
|
107 | 116 |
|
108 | 117 | def can_be_skipped(key, value): |
|
0 commit comments