fix bug for upgrading python3.8 of parsing

This commit is contained in:
simson 2020-12-01 19:20:36 +08:00 committed by wudengsong
parent e4167da1b1
commit 9ab81181aa
2 changed files with 33 additions and 0 deletions

View File

@ -109,6 +109,7 @@ void Parser::BuildMethodMap() {
expr_method_map_["Name"] = &Parser::ParseName;
expr_method_map_["Num"] = &Parser::ParseNum;
expr_method_map_["Str"] = &Parser::ParseStr;
expr_method_map_["Constant"] = &Parser::ParseConstant;
expr_method_map_["NameConstant"] = &Parser::ParseNameConstant;
expr_method_map_["Call"] = &Parser::ParseCall;
expr_method_map_["IfExp"] = &Parser::ParseIfExp;
@ -557,6 +558,36 @@ AnfNodePtr Parser::ParseStr(const FunctionBlockPtr &, const py::object &node) {
return NewValueNode(str_s);
}
AnfNodePtr Parser::ParseConstant(const FunctionBlockPtr &, const py::object &node) {
MS_LOG(DEBUG) << "Process ast Constant";
py::object obj = python_adapter::GetPyObjAttr(node, "value");
TraceGuard trace_guard(GetLocation(node));
if (py::isinstance<py::bool_>(obj)) {
MS_LOG(INFO) << "The Constant is bool:" << (std::string)py::str(obj);
auto data = py::cast<bool>(obj);
return NewValueNode(data);
} else if (py::isinstance<py::int_>(obj)) {
MS_LOG(INFO) << "The Constant is int64_t:" << (std::string)py::str(obj);
auto data = py::cast<int64_t>(obj);
return NewValueNode(data);
} else if (py::isinstance<py::float_>(obj)) {
MS_LOG(INFO) << "The Constant is float:" << (std::string)py::str(obj);
auto data = py::cast<float>(obj);
return NewValueNode(data);
} else if (py::isinstance<py::str>(obj)) {
MS_LOG(INFO) << "The Constant is string:" << (std::string)py::str(obj);
auto data = py::cast<std::string>(obj);
return NewValueNode(data);
} else if (py::isinstance<py::none>(obj)) {
MS_LOG(INFO) << "The Constant is none:" << (std::string)py::str(obj);
return NewValueNode(kNone);
} else {
// no else actually
MS_EXCEPTION(TypeError) << "Unsupported Constant type : " << (std::string)py::str(obj)
<< GetLocation(node)->ToString();
}
}
AnfNodePtr Parser::ParseNameConstant(const FunctionBlockPtr &, const py::object &node) {
MS_LOG(DEBUG) << "Process ast NameConstant";
py::object obj = python_adapter::GetPyObjAttr(node, "value");

View File

@ -143,6 +143,8 @@ class Parser {
AnfNodePtr ParseNum(const FunctionBlockPtr &block, const py::object &node);
// process a string variable
AnfNodePtr ParseStr(const FunctionBlockPtr &block, const py::object &node);
// process a Constant
AnfNodePtr ParseConstant(const FunctionBlockPtr &block, const py::object &node);
// process a name
AnfNodePtr ParseNameConstant(const FunctionBlockPtr &block, const py::object &node);
// process a function call