Unverified Commit 4c6346cc authored by Yih-Dar's avatar Yih-Dar Committed by GitHub
Browse files

Fix `get_class_in_module` (#21709)



Fix get_class_in_module
Co-authored-by: default avatarydshieh <ydshieh@users.noreply.github.com>
parent ed6ceb76
......@@ -154,7 +154,17 @@ def get_class_in_module(class_name, module_path):
shutil.copy(f"{module_dir}/{module_file_name}", tmp_dir)
# On Windows, we need this character `r` before the path argument of `os.remove`
cmd = f'import os; os.remove(r"{module_dir}{os.path.sep}{module_file_name}")'
subprocess.run(["python", "-c", cmd])
# We don't know which python binary file exists in an environment. For example, if `python3` exists but not
# `python`, the call `subprocess.run(["python", ...])` gives `FileNotFoundError` (about python binary). Notice
# that, if the file to be removed is not found, we also have `FileNotFoundError`, but it is not raised to the
# caller's process.
try:
subprocess.run(["python", "-c", cmd])
except FileNotFoundError:
try:
subprocess.run(["python3", "-c", cmd])
except FileNotFoundError:
pass
# copy back the file that we want to import
shutil.copyfile(f"{tmp_dir}/{module_file_name}", f"{module_dir}/{module_file_name}")
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment