Run strace and return the trace. Sets strace_returncode and python_returncode to `-1` on error.
(code, strace_flags, check=True)
| 95 | |
| 96 | @support.requires_subprocess() |
| 97 | def 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 | |
| 147 | def get_events(code, strace_flags, prelude, cleanup): |
no test coverage detected
searching dependent graphs…