From 29e43b5799a190f771e9e7532ba667f446480a35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BD=AD=E4=BA=8E=E6=96=8C?= <1931127624@qq.com> Date: Fri, 28 Aug 2020 11:09:18 +0800 Subject: [PATCH] [Bug] [misc] Use sys.getfilesystemencoding() to prevent possible locale errors (#1775) * fix locale: use sys.getfilesystemencoding() instead of locale.getdefaultlocale()[1] * nit import: better error message * [skip ci] update readme: TaichiMD * [skip ci] enforce code format * [skip ci] revert readme: taichimd and taichiglsl * :tnp * [skip ci] make the stupid locale happy * [skip ci] enforce code format Co-authored-by: Taichi Gardener --- python/taichi/core/util.py | 23 +++++++++++++++++------ taichi/python/export_lang.cpp | 10 ++++++++-- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/python/taichi/core/util.py b/python/taichi/core/util.py index 37b285f76e734..1361a87435ac1 100644 --- a/python/taichi/core/util.py +++ b/python/taichi/core/util.py @@ -49,12 +49,19 @@ def import_ti_core(tmp_dir=None): core.set_tmp_dir(locale_encode(tmp_dir)) -def locale_encode(s): +def locale_encode(path): try: import locale - return s.encode(locale.getdefaultlocale()[1]) - except TypeError: - return s.encode('utf8') + return path.encode(locale.getdefaultlocale()[1]) + except: + try: + import sys + return path.encode(sys.getfilesystemencoding()) + except: + try: + return path.encode() + except: + return path def is_ci(): @@ -159,8 +166,8 @@ def get_unique_task_id(): if get_os_name() != 'win': dll = ctypes.CDLL(get_core_shared_object(), mode=ctypes.RTLD_LOCAL) # The C backend needs a temporary directory for the generated .c and compiled .so files: - ti_core.set_tmp_dir(prepare_sandbox( - )) # TODO: always allocate a tmp_dir for all situations + ti_core.set_tmp_dir(locale_encode(prepare_sandbox( + ))) # TODO: always allocate a tmp_dir for all situations ti_core.set_python_package_dir(package_root()) os.makedirs(ti_core.get_repo_dir(), exist_ok=True) @@ -200,6 +207,10 @@ def get_unique_task_id(): from colorama import Fore, Back, Style print_red_bold("Taichi core import failed: ", end='') print(e) + print( + Fore.YELLOW + "check this page for possible solutions:\n" + "https://taichi.readthedocs.io/en/stable/install.html#troubleshooting" + + Fore.RESET) exit(-1) os.chdir(tmp_cwd) diff --git a/taichi/python/export_lang.cpp b/taichi/python/export_lang.cpp index 5a883ad56baa0..20deadcc5a7a7 100644 --- a/taichi/python/export_lang.cpp +++ b/taichi/python/export_lang.cpp @@ -574,8 +574,14 @@ void export_lang(py::module &m) { m.def("host_arch", host_arch); - m.def("set_lib_dir", [&](const std::string &dir) { compiled_lib_dir = dir; }); - m.def("set_tmp_dir", [&](const std::string &dir) { runtime_tmp_dir = dir; }); + m.def("set_lib_dir", [&](const std::string &dir) { + TI_INFO("set_lib_dir: [{}]", dir); + compiled_lib_dir = dir; + }); + m.def("set_tmp_dir", [&](const std::string &dir) { + TI_INFO("set_tmp_dir: [{}]", dir); + runtime_tmp_dir = dir; + }); m.def("get_runtime_dir", get_runtime_dir); m.def("get_commit_hash", get_commit_hash);