Skip to content

Commit

Permalink
Remove threading check and add override flag
Browse files Browse the repository at this point in the history
Python will always think its in the mainthread, regardless
of if it was started from C via a pthread. You are considered
"off the main thread" when a new thread is spawned from your current
instance. Because there is no way to know whether we started python
with something like jaunch or the terminal, the only way to bypass
the macOS interactive mode block is to introduce an override flag.
  • Loading branch information
elevans committed Jul 5, 2024
1 parent ca14ef3 commit 5993e09
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions src/imagej/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1125,6 +1125,7 @@ def init(
ij_dir_or_version_or_endpoint=None,
mode: Union[Mode, str] = Mode.HEADLESS,
add_legacy=True,
override=False,
headless=None,
):
"""Initialize an ImageJ2 environment.
Expand Down Expand Up @@ -1183,6 +1184,13 @@ def init(
the JVM and Python will both terminate when ImageJ closes!
For further details, see: https://imagej.net/libs/imagej-legacy
:param override:
Whether or not to override initialization blocks. If True,
unsupported/application breaking behavior, such as interactive mode on
macOS, will be possible. If False, initialization blocks preventing
unsupported/application breaking behavior are active.
:param headless:
Deprecated. Please use the mode parameter instead.
Expand All @@ -1205,7 +1213,7 @@ def init(

macos = sys.platform == "darwin"

if macos and mode == Mode.INTERACTIVE and _is_main_thread():
if macos and mode == Mode.INTERACTIVE and is not override:
raise EnvironmentError("Sorry, the interactive mode is not available on macOS.")

if not sj.jvm_started():
Expand Down Expand Up @@ -1517,14 +1525,6 @@ def _includes_imagej_legacy(items: list):
return any(item.startswith("net.imagej:imagej-legacy") for item in items)


def _is_main_thread():
"""Detect if on main thread running on the main thread.
:return: Boolean indicating if the current thread is the main thread.
"""
return threading.current_thread() == threading.main_thread()


def _set_ij_env(ij_dir):
"""
Create a list of required jars and add to the java classpath.
Expand Down

0 comments on commit 5993e09

Please sign in to comment.