| 1 |
import contextlib |
| 2 |
import inspect |
| 3 |
import io |
| 4 |
import sys |
| 5 |
import traceback |
| 6 |
import types |
| 7 |
from itertools import dropwhile |
| 8 |
from typing import Generic, List, Optional, Tuple, Type |
| 9 |
|
| 10 |
from stack_data import BlankLines, Options |
| 11 |
|
| 12 |
from . import debug_helper |
| 13 |
from .frame_info import FrameInfo |
| 14 |
from .ft_gettext import current_lang |
| 15 |
from .path_info import is_excluded_file |
| 16 |
from .source_cache import cache |
| 17 |
from .syntax_errors import source_info |
| 18 |
from .typing_info import _E |
| 19 |
|
| 20 |
STR_FAILED = "<exception str() failed>" |
| 21 |
_ = current_lang.translate |
| 22 |
|
| 23 |
|
| 24 |
def convert_value_to_message(value: BaseException) -> str: |
| 25 |
"""This converts the 'value' of an exception into a string, while |
| 26 |
being safe to use for custom exceptions which have been incorrectly |
| 27 |
defined. See https://github.com/aroberge/friendly/issues/181 for an example. |
| 28 |
""" |
| 29 |
try: |
| 30 |
message = str(value) |
| 31 |
except Exception: |
| 32 |
message = STR_FAILED |
| 33 |
return message |
| 34 |
|
| 35 |
|
| 36 |
def retrieve_message(etype: Type[_E], value: _E, tb: types.TracebackType) -> str: |
| 37 |
"""Safely retrieves the message, including any additional hint from Python.""" |
| 38 |
message = convert_value_to_message(value) |
| 39 |
if ( |
| 40 |
message == STR_FAILED |
| 41 |
or sys.version_info < (3, 10) |
| 42 |
or etype not in (AttributeError, NameError) |
| 43 |
): |
| 44 |
return message |
| 45 |
|
| 46 |
err = io.StringIO() |
| 47 |
with contextlib.redirect_stderr(err): |
| 48 |
sys.__excepthook__(etype, value, tb) |
| 49 |
full_message = err.getvalue().split("\n")[-2] |
| 50 |
return full_message.split(":", 1)[1].strip() |
| 51 |
|
| 52 |
|
| 53 |
class TracebackData(Generic[_E]): |
| 54 |
"""Raw traceback info obtained from Python. |
| 55 |
|
| 56 |
Instances of this class are intended to include all the relevant |
| 57 |
information about an exception so that a FriendlyTraceback object |
| 58 |
can be created. |
| 59 |
""" |
| 60 |
|
| 61 |
def __init__(self, etype: Type[_E], value: _E, tb: types.TracebackType) -> None: |
| 62 |
"""This object is initialized with the standard values for a |
| 63 |
traceback:: |
| 64 |
|
| 65 |
etype, value, tb = sys.exc_info() |
| 66 |
""" |
| 67 |
cache.remove("<fstring>") |
| 68 |
self.exception_type = etype |
| 69 |
self.exception_name = etype.__name__ |
| 70 |
self.value = value |
| 71 |
self.message = str(value) |
| 72 |
self.full_message = retrieve_message(etype, value, tb) |
| 73 |
self.formatted_tb = traceback.format_exception(etype, value, tb) |
| 74 |
self.records = self.get_records(tb) |
| 75 |
self.python_records = self.get_records(tb, python_excluded=False) |
| 76 |
|
| 77 |
self.simulated_python_traceback: Optional[str] = None |
| 78 |
|
| 79 |
|
| 80 |
self.bad_line = "\n" |
| 81 |
self.original_bad_line = "\n" |
| 82 |
self.filename = "" |
| 83 |
self.exception_frame: Optional[types.FrameType] = None |
| 84 |
self.exception_instance: Optional[_E] = None |
| 85 |
self.program_stopped_frame: Optional[types.FrameType] = None |
| 86 |
self.program_stopped_bad_line = "\n" |
| 87 |
self.get_source_info() |
| 88 |
|
| 89 |
|
| 90 |
self.node = None |
| 91 |
self.node_text = "" |
| 92 |
self.node_range: Optional[Tuple[int, int]] = None |
| 93 |
self.program_stopped_node_range = None |
| 94 |
|
| 95 |
if issubclass(etype, SyntaxError): |
| 96 |
self.statement: Optional[source_info.Statement] = source_info.Statement( |
| 97 |
self.value, self.bad_line, self.original_bad_line |
| 98 |
) |
| 99 |
|
| 100 |
|
| 101 |
def remove_space(text: str) -> str: |
| 102 |
if text.rstrip(): |
| 103 |
return ( |
| 104 |
text.rstrip() + "\n" if text.endswith("\n") else text.rstrip() |
| 105 |
) |
| 106 |
return text |
| 107 |
|
| 108 |
self.statement.entire_statement = remove_space( |
| 109 |
self.statement.entire_statement |
| 110 |
) |
| 111 |
self.statement.bad_line = remove_space(self.statement.bad_line) |
| 112 |
else: |
| 113 |
self.statement = None |
| 114 |
self.locate_error() |
| 115 |
|
| 116 |
def get_records( |
| 117 |
self, tb: types.TracebackType, python_excluded: bool = True |
| 118 |
) -> List[FrameInfo]: |
| 119 |
"""Get the traceback frame history, excluding those originating |
| 120 |
from our own code that are included either at the beginning or |
| 121 |
at the end of the traceback. |
| 122 |
""" |
| 123 |
|
| 124 |
def trim_records(complete_records): |
| 125 |
partial_records = list( |
| 126 |
dropwhile( |
| 127 |
lambda record: is_excluded_file( |
| 128 |
record.filename, python_excluded=python_excluded |
| 129 |
), |
| 130 |
complete_records, |
| 131 |
) |
| 132 |
) |
| 133 |
partial_records.reverse() |
| 134 |
partial_records = list( |
| 135 |
dropwhile( |
| 136 |
lambda record: is_excluded_file( |
| 137 |
record.filename, python_excluded=python_excluded |
| 138 |
), |
| 139 |
partial_records, |
| 140 |
) |
| 141 |
) |
| 142 |
partial_records.reverse() |
| 143 |
return partial_records |
| 144 |
|
| 145 |
try: |
| 146 |
all_records = list( |
| 147 |
FrameInfo.stack_data( |
| 148 |
tb, |
| 149 |
Options(blank_lines=BlankLines.SINGLE), |
| 150 |
collapse_repeated_frames=False, |
| 151 |
) |
| 152 |
) |
| 153 |
records = trim_records(all_records) |
| 154 |
if records or issubclass(self.exception_type, (SyntaxError, MemoryError)): |
| 155 |
return records |
| 156 |
except AssertionError: |
| 157 |
|
| 158 |
|
| 159 |
all_records = inspect.getinnerframes(tb, cache.context) |
| 160 |
records = trim_records(all_records) |
| 161 |
if records or issubclass(self.exception_type, (SyntaxError, MemoryError)): |
| 162 |
return records |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
return all_records |
| 168 |
|
| 169 |
def get_source_info(self) -> None: |
| 170 |
"""Retrieves the file name and the line of code where the exception |
| 171 |
was raised. |
| 172 |
""" |
| 173 |
if issubclass(self.exception_type, SyntaxError): |
| 174 |
self.filename = self.value.filename |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
if not hasattr(self.value, "end_offset"): |
| 180 |
self.value.end_offset = ( |
| 181 |
self.value.offset + 1 if self.value.offset else 0 |
| 182 |
) |
| 183 |
self.value.end_lineno = self.value.lineno |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
if ( |
| 191 |
self.value.end_lineno == self.value.lineno |
| 192 |
and self.value.end_offset == self.value.offset |
| 193 |
): |
| 194 |
self.value.end_offset += 1 |
| 195 |
|
| 196 |
if self.value.text is not None: |
| 197 |
self.bad_line = self.value.text |
| 198 |
self.original_bad_line = self.bad_line |
| 199 |
return |
| 200 |
|
| 201 |
|
| 202 |
try: |
| 203 |
self.bad_line = cache.get_source_lines(self.filename)[ |
| 204 |
self.value.lineno - 1 |
| 205 |
] |
| 206 |
except Exception: |
| 207 |
self.bad_line = "\n" |
| 208 |
return |
| 209 |
|
| 210 |
if self.records: |
| 211 |
record = self.records[-1] |
| 212 |
self.exception_frame = record.frame |
| 213 |
self.filename = record.filename |
| 214 |
line = record.problem_line() |
| 215 |
self.original_bad_line = line |
| 216 |
self.bad_line = line |
| 217 |
|
| 218 |
if not self.bad_line: |
| 219 |
try: |
| 220 |
lines = cache.get_source_lines(record.filename) |
| 221 |
self.bad_line = lines[record.lineno - 1] |
| 222 |
except Exception: |
| 223 |
debug_helper.log("Could not get bad_line") |
| 224 |
|
| 225 |
if len(self.records) > 1: |
| 226 |
record = self.records[0] |
| 227 |
line = record.problem_line() |
| 228 |
self.program_stopped_frame = record.frame |
| 229 |
self.program_stopped_bad_line = line.rstrip() |
| 230 |
else: |
| 231 |
self.program_stopped_bad_line = self.bad_line |
| 232 |
self.program_stopped_frame = self.exception_frame |
| 233 |
return |
| 234 |
|
| 235 |
if issubclass(self.exception_type, MemoryError): |
| 236 |
self.bad_line = "<not available>" |
| 237 |
return |
| 238 |
|
| 239 |
|
| 240 |
def _log_error() -> None: |
| 241 |
debug_helper.log("Internal error in TracebackData.get_source_info.") |
| 242 |
debug_helper.log("No records found.") |
| 243 |
debug_helper.log(f"self.exception_type:{str(self.exception_type)}") |
| 244 |
debug_helper.log(f"self.value:{str(self.value)}") |
| 245 |
debug_helper.log_error() |
| 246 |
|
| 247 |
_log_error() |
| 248 |
|
| 249 |
def locate_error(self) -> None: |
| 250 |
"""Attempts to narrow down the location of the error so that, |
| 251 |
if possible, the problem code is highlighted with ^^^^.""" |
| 252 |
if not self.records: |
| 253 |
if issubclass(self.exception_type, MemoryError): |
| 254 |
return |
| 255 |
debug_helper.log("No records in locate_error().") |
| 256 |
return |
| 257 |
|
| 258 |
node_info = self.records[-1].node_info |
| 259 |
if node_info: |
| 260 |
self.node, _ignore, self.node_text = node_info |
| 261 |
if self.node_text.strip(): |
| 262 |
|
| 263 |
|
| 264 |
self.bad_line = self.node_text.strip() |
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
if self.records[0].node_info and self.records[0].node_info != node_info: |
| 270 |
node, _ignore, node_text = self.records[0].node_info |
| 271 |
if node_text.strip(): |
| 272 |
self.program_stopped_bad_line = node_text |