diff --git a/mindspore/ccsrc/utils/log_adapter.cc b/mindspore/ccsrc/utils/log_adapter.cc index 704ab24d52260db82088c66cb0333ddb26534d5c..4c197a0bdf9c19f8af6053628e0ce8940f330b06 100644 --- a/mindspore/ccsrc/utils/log_adapter.cc +++ b/mindspore/ccsrc/utils/log_adapter.cc @@ -179,7 +179,7 @@ void LogWriter::operator^(const LogStream &stream) const { std::ostringstream oss; oss << location_.file_ << ":" << location_.line_ << " " << location_.func_ << "] "; - if (exception_type_ != NoExceptionType) { + if (exception_type_ != NoExceptionType && exception_type_ != TypeError && exception_type_ != ValueError) { oss << ExceptionTypeToString(exception_type_) << " "; } oss << msg.str(); @@ -242,6 +242,10 @@ void mindspore_log_init(void) { if (mindspore::GetEnv("GLOG_v").empty()) { FLAGS_v = mindspore::WARNING; } + // set default log file mode to 0640 + if (mindspore::GetEnv("GLOG_logfile_mode").empty()) { + FLAGS_logfile_mode = 0640; + } // default print log to screen if (mindspore::GetEnv("GLOG_logtostderr").empty()) { FLAGS_logtostderr = true; diff --git a/mindspore/context.py b/mindspore/context.py index 2938b871195b17717136f04b97faf8a8d78eb0ec..fae7f7f762699256f75ae10b45e237662f2f23dc 100644 --- a/mindspore/context.py +++ b/mindspore/context.py @@ -16,6 +16,7 @@ The context of mindspore, used to configure the current execution environment, including execution mode, execution backend and other feature switchs. """ +import os import threading from collections import namedtuple from types import FunctionType @@ -33,6 +34,31 @@ GRAPH_MODE = 0 PYNATIVE_MODE = 1 +def _make_directory(path: str): + """Make directory.""" + real_path = None + if path is None or not isinstance(path, str) or path.strip() == "": + raise ValueError(f"Input path `{path}` is invaild type") + + # convert the relative paths + path = os.path.realpath(path) + logger.debug("The absolute path is %r", path) + + # check whether the path is already existed and has written permissions + if os.path.exists(path): + real_path = path + else: + # All exceptions need to be caught because create directory maybe have some limit(permissions) + logger.debug("The directory(%s) doesn't exist, will create it", path) + try: + os.makedirs(path) + real_path = path + except PermissionError as e: + logger.error(f"No write permission on the directory `{path}, error = {e}") + raise ValueError(f"No write permission on the directory `{path}`.") + return real_path + + class _ThreadLocalInfo(threading.local): """ Thread local Info used for store thread local attributes. @@ -173,7 +199,7 @@ class _Context: @save_graphs_path.setter def save_graphs_path(self, save_graphs_path): - self._context_handle.set_save_graphs_path(save_graphs_path) + self._context_handle.set_save_graphs_path(_make_directory(save_graphs_path)) @property def device_target(self): diff --git a/tests/ut/cpp/operator/composite_test.cc b/tests/ut/cpp/operator/composite_test.cc index d9dd9e5e990e27999bdaa52b3592e922d799d298..2c4b9b71468475e23fe2e66e5606b282d88185d7 100644 --- a/tests/ut/cpp/operator/composite_test.cc +++ b/tests/ut/cpp/operator/composite_test.cc @@ -128,8 +128,8 @@ TEST_F(TestComposite, test_TupleSlice_arg_one_number) { trace::ClearTraceStack(); engine_->Run(tupleSliceGraphPtr, args_spec_list); FAIL() << "Excepted exception :Args type is wrong"; - } catch (std::runtime_error const &err) { - ASSERT_TRUE(std::string(err.what()).find("TypeError") != std::string::npos); + } catch (pybind11::type_error const &err) { + ASSERT_TRUE(true); } catch (...) { FAIL() << "Excepted exception :Args type is wrong"; } diff --git a/tests/ut/python/pynative_mode/test_backend.py b/tests/ut/python/pynative_mode/test_backend.py index 937f7b24ff7870372c276573769bd3f86f7f57ec..7258b69486677c3576299945ed82710c2fdab0a0 100644 --- a/tests/ut/python/pynative_mode/test_backend.py +++ b/tests/ut/python/pynative_mode/test_backend.py @@ -13,6 +13,7 @@ # limitations under the License. # ============================================================================ """ test_backend """ +import os import numpy as np import pytest from mindspore.ops import operations as P @@ -51,10 +52,11 @@ def test_vm_backend(): def test_vm_set_context(): """ test_vm_set_context """ - context.set_context(save_graphs=True, save_graphs_path="/home/mindspore", mode=context.GRAPH_MODE) + context.set_context(save_graphs=True, save_graphs_path="mindspore_ir_path", mode=context.GRAPH_MODE) assert context.get_context("save_graphs") assert context.get_context("mode") == context.GRAPH_MODE - assert context.get_context("save_graphs_path") == "/home/mindspore" + assert os.path.exists("mindspore_ir_path") + assert context.get_context("save_graphs_path").find("mindspore_ir_path") > 0 context.set_context(mode=context.PYNATIVE_MODE) @args_type_check(v_str=str, v_int=int, v_tuple=tuple) @@ -74,3 +76,15 @@ def test_args_type_check(): with pytest.raises(TypeError): check_input("name", 100, "age") check_input("name", 100, (10, 10)) + + +def teardown_module(): + dirs = ['mindspore_ir_path'] + for item in dirs: + item_name = './' + item + if not os.path.exists(item_name): + continue + if os.path.isdir(item_name): + os.rmdir(item_name) + elif os.path.isfile(item_name): + os.remove(item_name) diff --git a/tests/ut/python/pynative_mode/test_context.py b/tests/ut/python/pynative_mode/test_context.py index 450bf60b90965117f7113690a321bbc5cfa3558a..2425b53f42f077460fb6c65e204a6292e7eac9c9 100644 --- a/tests/ut/python/pynative_mode/test_context.py +++ b/tests/ut/python/pynative_mode/test_context.py @@ -13,6 +13,7 @@ # limitations under the License. # ============================================================================ """ test_context """ +import os import pytest from mindspore import context # pylint: disable=W0212 @@ -74,11 +75,12 @@ def test_dump_target(): def test_set_context(): """ test_set_context """ context.set_context(mode=context.GRAPH_MODE, device_target="Ascend", - device_id=0, save_graphs=True, save_graphs_path="/mindspore") + device_id=0, save_graphs=True, save_graphs_path="mindspore_ir_path") assert context.get_context("device_id") == 0 assert context.get_context("device_target") == "Ascend" assert context.get_context("save_graphs") - assert context.get_context("save_graphs_path") == "/mindspore" + assert os.path.exists("mindspore_ir_path") + assert context.get_context("save_graphs_path").find("mindspore_ir_path") > 0 assert context.get_context("mode") == context.GRAPH_MODE context.set_context(mode=context.PYNATIVE_MODE) @@ -87,3 +89,16 @@ def test_set_context(): with pytest.raises(ValueError): context.set_context(modex="ge") + + +def teardown_module(): + dirs = ['mindspore_ir_path'] + for item in dirs: + item_name = './' + item + if not os.path.exists(item_name): + continue + if os.path.isdir(item_name): + os.rmdir(item_name) + elif os.path.isfile(item_name): + os.remove(item_name) +