Skip to content

Commit

Permalink
[Bug] [misc] Use sys.getfilesystemencoding() to prevent possible loca…
Browse files Browse the repository at this point in the history
…le 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 <[email protected]>
  • Loading branch information
archibate and taichi-gardener authored Aug 28, 2020
1 parent c0c5a57 commit 29e43b5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
23 changes: 17 additions & 6 deletions python/taichi/core/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)

Expand Down
10 changes: 8 additions & 2 deletions taichi/python/export_lang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 29e43b5

Please sign in to comment.