提交 7e23a1a4 编写于 作者: F fary86

Fix issues of save_graphs_path, Type/Value error message and log file mode

上级 ae7556ff
......@@ -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;
......
......@@ -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):
......
......@@ -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";
}
......
......@@ -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)
......@@ -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)
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册