MCPcopy Index your code
hub / github.com/python/cpython / strace_python

Function strace_python

Lib/test/support/strace_helper.py:97–144  ·  view source on GitHub ↗

Run strace and return the trace. Sets strace_returncode and python_returncode to `-1` on error.

(code, strace_flags, check=True)

Source from the content-addressed store, hash-verified

95
96@support.requires_subprocess()
97def strace_python(code, strace_flags, check=True):
98 """Run strace and return the trace.
99
100 Sets strace_returncode and python_returncode to `-1` on error."""
101 res = None
102
103 def _make_error(reason, details):
104 return StraceResult(
105 strace_returncode=-1,
106 python_returncode=-1,
107 event_bytes= f"error({reason},details={details!r}) = -1".encode('utf-8'),
108 stdout=res.out if res else b"",
109 stderr=res.err if res else b"")
110
111 # Run strace, and get out the raw text
112 try:
113 res, cmd_line = run_python_until_end(
114 "-c",
115 textwrap.dedent(code),
116 __run_using_command=[_strace_binary] + strace_flags,
117 )
118 except OSError as err:
119 return _make_error("Caught OSError", err)
120
121 if check and res.rc:
122 res.fail(cmd_line)
123
124 # Get out program returncode
125 stripped = res.err.strip()
126 output = stripped.rsplit(b"\n", 1)
127 if len(output) != 2:
128 return _make_error("Expected strace events and exit code line",
129 stripped[-50:])
130
131 returncode_match = _returncode_regex.match(output[1])
132 if not returncode_match:
133 return _make_error("Expected to find returncode in last line.",
134 output[1][:50])
135
136 python_returncode = int(returncode_match["returncode"])
137 if check and python_returncode:
138 res.fail(cmd_line)
139
140 return StraceResult(strace_returncode=res.rc,
141 python_returncode=python_returncode,
142 event_bytes=output[0],
143 stdout=res.out,
144 stderr=res.err)
145
146
147def get_events(code, strace_flags, prelude, cleanup):

Callers 2

get_eventsFunction · 0.85
_can_straceFunction · 0.85

Calls 8

run_python_until_endFunction · 0.90
_make_errorFunction · 0.85
StraceResultClass · 0.85
dedentMethod · 0.45
failMethod · 0.45
stripMethod · 0.45
rsplitMethod · 0.45
matchMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…