Skip to content

Commit 0e9ca43

Browse files
authored
Merge pull request #237 from Project-MONAI/fix_inspect
Fix IPython detection routine
2 parents e7d0361 + 625dd02 commit 0e9ca43

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

monai/deploy/core/application.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ def __init__(
9595
self.path = get_class_file_path(self.__class__)
9696

9797
# Set the runtime environment
98-
if str(self.path).startswith("<ipython-"):
98+
if str(self.path) == "ipython":
9999
self.in_ipython = True
100100
else:
101101
self.in_ipython = False

monai/deploy/utils/importutil.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,11 @@ def get_application(path: Union[str, Path]) -> Optional["Application"]:
9999
def get_class_file_path(cls: Type) -> Path:
100100
"""Get the file path of a class.
101101
102-
If file path is not available, it tries to get the internal class name used in IPython(starting with <ipython-...>).
102+
If the file path is not available, it tries to see each frame information
103+
in the stack to check whether the file name ends with "interactiveshell.py"
104+
and the function name is "run_code".
105+
If so, it returns Path("ipython") to notify that the class is defined
106+
inside IPython.
103107
104108
Args:
105109
cls (Type): A class to get file path from.
@@ -114,8 +118,8 @@ def get_class_file_path(cls: Type) -> Path:
114118
# If in IPython shell, use inspect.stack() to get the caller's file path
115119
stack = inspect.stack()
116120
for frame in stack:
117-
if frame.filename.startswith("<ipython-"):
118-
return Path(frame.filename)
121+
if frame.filename.endswith("interactiveshell.py") and frame.function == "run_code":
122+
return Path("ipython")
119123
# If not in IPython shell, re-raise the error
120124
raise
121125

0 commit comments

Comments
 (0)