Skip to content

Commit

Permalink
Remove Java to Python stdout and stderr forwarding
Browse files Browse the repository at this point in the history
We noticed that running macros and scripts (e.g. ij.py.run_script())
produces a duplicate stdout and stderr. We traced the duplicating
output to the OutputListener forwarding removed in this commit. But
why were we forwarding the stdout and stderr in the first place? Looking
at commit db9bae3 we can see that we carried the OutputListener
forwarding from pyjnius to JPype. JPype already handles stdout and
stderr forwarding, thus a duplicate is generated. Apparently we
never noticed this. This commit removes the duplicate forwarding.
  • Loading branch information
elevans committed Aug 8, 2023
1 parent b6083f0 commit d99fcf0
Showing 1 changed file with 1 addition and 18 deletions.
19 changes: 1 addition & 18 deletions src/imagej/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
import numpy as np
import scyjava as sj
import xarray as xr
from jpype import JImplementationFor, JImplements, JOverride, setupGuiEnvironment
from jpype import JImplementationFor, setupGuiEnvironment
from scyjava.config import find_jars

import imagej.convert as convert
Expand Down Expand Up @@ -1278,23 +1278,6 @@ def _create_gateway():

ij = ImageJ()

# Forward stdout and stderr from Java to Python.
@JImplements("org.scijava.console.OutputListener")
class JavaOutputListener:
@JOverride
def outputOccurred(self, e):
source = e.getSource().toString
output = e.getOutput()
if source == "STDOUT":
sys.stdout.write(output)
elif source == "STDERR":
sys.stderr.write(output)
else:
sys.stderr.write(f"[{source}] {output}")

ij.py._outputMapper = JavaOutputListener()
ij.console().addOutputListener(ij.py._outputMapper)

# Register a Python-side script runner object, used by the
# org.scijava:scripting-python script language plugin.
if callable(getattr(sj, "enable_python_scripting", None)):
Expand Down

0 comments on commit d99fcf0

Please sign in to comment.