|
21 | 21 | import sys |
22 | 22 | from inspect import ismodule |
23 | 23 | from tempfile import NamedTemporaryFile |
| 24 | +from types import CodeType |
24 | 25 |
|
25 | 26 | import pyficache |
26 | 27 |
|
27 | 28 | from trepan.lib.stack import check_path_with_frame, frame2file |
28 | 29 | from trepan.processor import cmdfns |
29 | 30 | from trepan.processor.cmdfns import deparse_fn |
30 | 31 |
|
| 32 | +from trepan.lib.format import ( # Opcode, |
| 33 | + Filename, |
| 34 | + Hex, |
| 35 | + LineNumber, |
| 36 | + Symbol, |
| 37 | + format_token, |
| 38 | +) |
| 39 | + |
31 | 40 | try: |
32 | 41 | from trepan.lib.deparse import deparse_and_cache |
33 | 42 |
|
|
39 | 48 | warned_file_mismatches = set() |
40 | 49 |
|
41 | 50 |
|
| 51 | +def format_code(code_object: CodeType, style) -> str: |
| 52 | + """ |
| 53 | + Format according to "style" a Python code object. The |
| 54 | + formatted string is returned. |
| 55 | + """ |
| 56 | + formatted_line = format_token(LineNumber, str(code_object.co_firstlineno), style=style) |
| 57 | + formatted_id = format_token(Hex, hex(id(code_object)), style=style) |
| 58 | + formatted_name = format_token(Symbol, code_object.co_name, style=style) |
| 59 | + formatted_filename = format_token(Filename, code_object.co_filename, style=style) |
| 60 | + return ( |
| 61 | + f"<code object {formatted_name} at {formatted_id} " |
| 62 | + f"file {formatted_filename}, line {formatted_line}" |
| 63 | + ) |
| 64 | + |
| 65 | + |
| 66 | +def format_frame(frame_object, style) -> str: |
| 67 | + """ |
| 68 | + Format according to "style" a Python frame object. The |
| 69 | + formatted string is returned. |
| 70 | + """ |
| 71 | + formatted_line = format_token(LineNumber, str(frame_object.f_lineno), style=style) |
| 72 | + formatted_id = format_token(Hex, hex(id(frame_object)), style=style) |
| 73 | + formatted_filename = format_token(Filename, frame_object.f_code.co_filename, style=style) |
| 74 | + return ( |
| 75 | + f"<frame at {formatted_id} " |
| 76 | + f"file {formatted_filename}, line {formatted_line}" |
| 77 | + ) |
| 78 | + |
| 79 | + |
42 | 80 | def print_source_line(msg, lineno, line, event_str=None): |
43 | 81 | """Print out a source line of text , e.g. the second |
44 | 82 | line in: |
|
0 commit comments