Skip to content

Fix IPython detection routine #237

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion monai/deploy/core/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def __init__(
self.path = get_class_file_path(self.__class__)

# Set the runtime environment
if str(self.path).startswith("<ipython-"):
if str(self.path) == "ipython":
self.in_ipython = True
else:
self.in_ipython = False
Expand Down
10 changes: 7 additions & 3 deletions monai/deploy/utils/importutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,11 @@ def get_application(path: Union[str, Path]) -> Optional["Application"]:
def get_class_file_path(cls: Type) -> Path:
"""Get the file path of a class.

If file path is not available, it tries to get the internal class name used in IPython(starting with <ipython-...>).
If the file path is not available, it tries to see each frame information
in the stack to check whether the file name ends with "interactiveshell.py"
and the function name is "run_code".
If so, it returns Path("ipython") to notify that the class is defined
inside IPython.

Args:
cls (Type): A class to get file path from.
Expand All @@ -114,8 +118,8 @@ def get_class_file_path(cls: Type) -> Path:
# If in IPython shell, use inspect.stack() to get the caller's file path
stack = inspect.stack()
for frame in stack:
if frame.filename.startswith("<ipython-"):
return Path(frame.filename)
if frame.filename.endswith("interactiveshell.py") and frame.function == "run_code":
return Path("ipython")
# If not in IPython shell, re-raise the error
raise

Expand Down