diff --git a/backtrace.py b/backtrace.py index a45fe5d..316e959 100644 --- a/backtrace.py +++ b/backtrace.py @@ -218,27 +218,32 @@ def _extract_traceback(text): if text.count(TRACEBACK_IDENTIFIER) == 2: ignore_trace = True - for index, line in enumerate(text): + for line in text: if TRACEBACK_IDENTIFIER in line: if ignore_trace: ignore_trace = False continue capture = True + location_info = True # We're not capturing and making sure we only read lines # with spaces since, after the initial identifier, all traceback lines # contain a prefix spacing. elif capture and line.startswith(' '): - if index % 2 == 0: + if location_info: # Line containing a file, line and module. - line = line.strip().strip('\n') - next_line = text[index + 1].strip('\n') - entries.append(line + ', ' + next_line) + line = line.strip() + entries.append(line) + else: + # The corresponding line of source code. + line = line.rstrip("\n") + entries[-1] += ', ' + line + location_info = not location_info elif capture: # Line containing the module call. entries.append(line) break else: - # Add everything else after the traceback. + # Add everything else before the traceback. all_else.append(line) traceback_entries = [] @@ -246,7 +251,7 @@ def _extract_traceback(text): # Build the traceback structure later passed for formatting. for index, line in enumerate(entries[:-2]): # TODO: This should be done in a _parse_entry function - element = line.split(',') + element = line.split(',', 3) element[0] = element[0].strip().lstrip('File').strip(' "') element[1] = element[1].strip().lstrip('line').strip() element[2] = element[2].strip().lstrip('in').strip() @@ -264,7 +269,11 @@ def _stdin_hook(args): tb, all_else = _extract_traceback(output) sys.stdout.write(''.join(all_else)) - tpe, value = output[-1].strip('\n').split(': ', 1) + try: + tpe, value = output[-1].strip('\n').split(': ', 1) + except ValueError: + tpe = output[-1].strip('\n') + value = "" hook( reverse=args.reverse, align=args.align,