MCPcopy
hub / github.com/sqlalchemy/sqlalchemy / _tracer

Method _tracer

tools/trace_orm_adapter.py:62–118  ·  view source on GitHub ↗
(self, meth)

Source from the content-addressed store, hash-verified

60
61 @contextlib.contextmanager
62 def _tracer(self, meth):
63 adapter = self
64 ctx = self._orm_adapter_trace_context.get(
65 {"stack": [], "last_depth": 0, "line_no": 0}
66 )
67 self._orm_adapter_trace_context.set(ctx)
68
69 stack: List[Any] = ctx["stack"] # type: ignore
70 last_depth = len(stack)
71 line_no: int = ctx["line_no"] # type: ignore
72 ctx["last_depth"] = last_depth
73 stack.append((adapter, meth))
74 indent = " " * last_depth
75
76 if hasattr(adapter, "mapper"):
77 adapter_desc = (
78 f"{adapter.__class__.__name__}"
79 f"({adapter.role.name}, mapper={adapter.mapper})"
80 )
81 else:
82 adapter_desc = f"{adapter.__class__.__name__}({adapter.role.name})"
83
84 def tracer_fn(fn, arg):
85 nonlocal line_no
86
87 line_no += 1
88
89 print(f"{indent} {line_no} {adapter_desc}", file=REAL_STDOUT)
90 sub_indent = " " * len(f"{line_no} ")
91
92 print(
93 f"{indent}{sub_indent} -> "
94 f"{meth} {_orm_adapter_trace_print(arg)}",
95 file=REAL_STDOUT,
96 )
97 ctx["line_no"] = line_no
98 ret = fn(arg)
99
100 if DEBUG_ADAPT_STEP == line_no:
101 breakpoint()
102
103 if ret is arg:
104 print(f"{indent} {line_no} <- same object", file=REAL_STDOUT)
105 else:
106 print(
107 f"{indent} {line_no} <- {_orm_adapter_trace_print(ret)}",
108 file=REAL_STDOUT,
109 )
110
111 if last_depth == 0:
112 print("", file=REAL_STDOUT)
113 return ret
114
115 try:
116 yield tracer_fn
117 finally:
118 stack.pop(-1)
119

Callers 2

_locate_colMethod · 0.95
replaceMethod · 0.95

Calls 4

getMethod · 0.45
setMethod · 0.45
appendMethod · 0.45
popMethod · 0.45

Tested by

no test coverage detected