!24146 Optimize error log about inspect.getsourcelines fail if run source code in terminal

Merge pull request !24146 from huanghui/optimize-inspect-getsourcelines-fail-log
This commit is contained in:
i-robot 2021-09-29 09:01:27 +00:00 committed by Gitee
commit d7a08c3e2d
1 changed files with 7 additions and 1 deletions

View File

@ -555,7 +555,13 @@ class Parser:
"""Parse the function or method.""" """Parse the function or method."""
logger.debug("fn = %r", self.fn) logger.debug("fn = %r", self.fn)
if isinstance(self.fn, (types.FunctionType, types.MethodType)): if isinstance(self.fn, (types.FunctionType, types.MethodType)):
lines, self.line_offset = inspect.getsourcelines(self.fn) try:
lines, self.line_offset = inspect.getsourcelines(self.fn)
except OSError as e:
if e.__str__() == "could not get source code":
raise OSError(f"Mindspore can not compile temporary source code in terminal. "
f"Please write source code to a python file and run the file.")
raise e
original_src = ''.join(lines) original_src = ''.join(lines)
hexstr = hashlib.sha256(original_src.encode()).hexdigest() hexstr = hashlib.sha256(original_src.encode()).hexdigest()
ast_tokens = Parser.ast_cache.get(hexstr) ast_tokens = Parser.ast_cache.get(hexstr)