From 951f5a5daa02848f4c5326c4d4e6ea8ddfc8c888 Mon Sep 17 00:00:00 2001 From: Ryan Kurtz Date: Fri, 26 Sep 2025 12:48:35 -0400 Subject: [PATCH] GP-6021: PyGhidra stdout/stderr scripting console fixes --- .../Features/PyGhidra/src/main/py/src/pyghidra/launcher.py | 5 +++-- Ghidra/Features/PyGhidra/support/pyghidra_launcher.py | 6 +++--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/Ghidra/Features/PyGhidra/src/main/py/src/pyghidra/launcher.py b/Ghidra/Features/PyGhidra/src/main/py/src/pyghidra/launcher.py index fd5bc6ce8d..385b63bfbd 100644 --- a/Ghidra/Features/PyGhidra/src/main/py/src/pyghidra/launcher.py +++ b/Ghidra/Features/PyGhidra/src/main/py/src/pyghidra/launcher.py @@ -658,6 +658,7 @@ class _PyGhidraStdOut: def __init__(self, stream): self._stream = stream + self.is_error = stream == sys.stderr def _get_current_script(self) -> "PyGhidraScript": for entry in inspect.stack(): @@ -668,7 +669,7 @@ class _PyGhidraStdOut: def flush(self): script = self._get_current_script() if script is not None: - writer = script._script.writer + writer = script._script.errorWriter if self.is_error else script._script.writer if writer is not None: writer.flush() return @@ -678,7 +679,7 @@ class _PyGhidraStdOut: def write(self, s: str) -> int: script = self._get_current_script() if script is not None: - writer = script._script.writer + writer = script._script.errorWriter if self.is_error else script._script.writer if writer is not None: writer.write(s) return len(s) diff --git a/Ghidra/Features/PyGhidra/support/pyghidra_launcher.py b/Ghidra/Features/PyGhidra/support/pyghidra_launcher.py index 8f1c302020..acb9572601 100644 --- a/Ghidra/Features/PyGhidra/support/pyghidra_launcher.py +++ b/Ghidra/Features/PyGhidra/support/pyghidra_launcher.py @@ -279,11 +279,11 @@ def main() -> None: # Launch PyGhidra save_python_cmd(install_dir, python_cmd, args.dev) - py_args: List[str] = python_cmd + ['-m', 'pyghidra.ghidra_launch', '--install-dir', str(install_dir)] + py_args: List[str] = python_cmd + ['-m'] if args.headless: - py_args += ['ghidra.app.util.headless.AnalyzeHeadless'] + py_args += ['pyghidra.ghidra_launch', '--install-dir', str(install_dir), 'ghidra.app.util.headless.AnalyzeHeadless'] else: - py_args += ['-g', 'ghidra.GhidraRun'] + py_args += ['pyghidra', '-g', '--install-dir', str(install_dir)] if args.console: subprocess.call(py_args + remaining) else: