Skip to content

Commit deb6ed5

Browse files
committed
Other solution
1 parent 3a9c9d2 commit deb6ed5

2 files changed

Lines changed: 33 additions & 43 deletions

File tree

prospector/run.py

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,11 @@ def execute(self) -> None:
8686

8787
running_tools[toolname] = tool
8888

89-
try:
90-
# Tools can output to stdout/stderr in unexpected places, for example,
91-
# pydocstyle emits warnings about __all__ and as pyroma exec's the setup.py
92-
# file, it will execute any print statements in that, etc etc...
93-
with CaptureOutput(hide=not self.config.direct_tool_stdout) as capture:
89+
# Tools can output to stdout/stderr in unexpected places, for example,
90+
# pydocstyle emits warnings about __all__ and as pyroma exec's the setup.py
91+
# file, it will execute any print statements in that, etc etc...
92+
with CaptureOutput(hide=not self.config.direct_tool_stdout) as capture:
93+
try:
9494
messages += tool.run(found_files)
9595

9696
if self.config.include_tool_stdout:
@@ -103,25 +103,33 @@ def execute(self) -> None:
103103
msg = f"stdout from {toolname}:\n{capture.get_hidden_stdout()}"
104104
messages.append(Message(toolname, "hidden-output", loc, message=msg))
105105

106-
except FatalProspectorException as fatal:
107-
sys.stderr.write(f"FatalProspectorException: {str(fatal)}")
108-
sys.exit(2)
109-
110-
except (SystemExit, Exception) as ex: # pylint:disable=broad-except
111-
if self.config.die_on_tool_error:
112-
raise FatalProspectorException(f"Tool {toolname} failed to run.") from ex
113-
loc = Location(self.config.workdir, None, None, None, None)
114-
msg = (
115-
f"Tool {toolname} failed to run "
116-
f"(exception was raised, re-run prospector with -X to see the stacktrace)"
117-
)
118-
message = Message(
119-
toolname,
120-
"failure",
121-
loc,
122-
message=msg,
123-
)
124-
messages.append(message)
106+
except FatalProspectorException as fatal:
107+
sys.stderr.write(f"FatalProspectorException: {str(fatal)}")
108+
sys.exit(2)
109+
110+
except (SystemExit, Exception) as ex: # pylint:disable=broad-except
111+
stderr = capture.get_hidden_stderr().strip()
112+
if self.config.die_on_tool_error:
113+
msg = f"Tool {toolname} failed to run"
114+
if stderr:
115+
msg += ", with stderr:\n" + stderr
116+
else:
117+
msg += "."
118+
raise FatalProspectorException() from ex
119+
loc = Location(self.config.workdir, None, None, None, None)
120+
121+
msg = f"Tool {toolname} failed to run "
122+
if stderr:
123+
msg += f", with stderr:\n{stderr}\n"
124+
msg += "(exception was raised, re-run prospector with -X to see the stacktrace)\n"
125+
126+
message = Message(
127+
toolname,
128+
"failure",
129+
loc,
130+
message=msg,
131+
)
132+
messages.append(message)
125133

126134
messages = self.process_messages(found_files, messages, running_tools)
127135

prospector/tools/mypy/__init__.py

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -126,25 +126,7 @@ def run(self, found_files: FileFinder) -> list[Message]:
126126

127127
def _run_std(self, args: list[str]) -> list[Message]:
128128
messages = []
129-
try:
130-
sources, options = mypy.main.process_options(args, fscache=self.fscache)
131-
except (SystemExit, Exception) as e:
132-
message = "The error(s) will be displayed before the messages" if isinstance(e, SystemExit) else str(e)
133-
messages.append(
134-
Message(
135-
"mypy",
136-
code="fatal-options-error",
137-
message=message,
138-
location=Location(
139-
path="",
140-
module=None,
141-
function=None,
142-
line=0,
143-
character=0,
144-
),
145-
)
146-
)
147-
return messages
129+
sources, options = mypy.main.process_options(args, fscache=self.fscache)
148130
options.output = "json"
149131
try:
150132
res = mypy.build.build(sources, options, fscache=self.fscache)

0 commit comments

Comments
 (0)