1 /**
2 * Copyright 2019 Huawei Technologies Co., Ltd
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "include/common/utils/python_adapter.h"
18 #include <memory>
19 #include <string>
20
21 namespace mindspore {
22 namespace python_adapter {
23 // Python scoped env, should only have one scoped_ instance
24 static std::shared_ptr<py::scoped_interpreter> scoped_ = nullptr;
25 // Python env: true: start process from python, false: start process from c++
26 static bool python_env_ = false;
27 static bool use_signature_in_resolve_ = true;
ResetPythonScope()28 void ResetPythonScope() { scoped_ = nullptr; }
set_use_signature_in_resolve(bool use_signature)29 void set_use_signature_in_resolve(bool use_signature) noexcept { use_signature_in_resolve_ = use_signature; }
UseSignatureInResolve()30 bool UseSignatureInResolve() { return use_signature_in_resolve_; }
set_python_env_flag(bool python_env)31 void set_python_env_flag(bool python_env) noexcept { python_env_ = python_env; }
IsPythonEnv()32 bool IsPythonEnv() { return python_env_; }
SetPythonPath(const std::string & path)33 void SetPythonPath(const std::string &path) {
34 // Load the python module path
35 (void)python_adapter::set_python_scoped();
36 py::module sys = py::module::import("sys");
37 py::list sys_path = sys.attr("path");
38
39 // Check the path is exist?
40 bool is_exist = false;
41 for (size_t i = 0; i < sys_path.size(); i++) {
42 if (!py::isinstance<py::str>(sys_path[i])) {
43 continue;
44 }
45 std::string path_str = py::cast<std::string>(sys_path[i]);
46 if (path_str == path) {
47 is_exist = true;
48 }
49 }
50 if (!is_exist) {
51 (void)sys_path.attr("append")(path.c_str());
52 }
53 }
54
set_python_scoped()55 std::shared_ptr<py::scoped_interpreter> set_python_scoped() {
56 // If start process from python, no need set the python scope.
57 if (!python_env_) {
58 if ((Py_IsInitialized() == 0) && (scoped_ == nullptr)) {
59 scoped_ = std::make_shared<py::scoped_interpreter>();
60 }
61 }
62 return scoped_;
63 }
64
65 // Return the module of python
GetPyModule(const std::string & module)66 py::module GetPyModule(const std::string &module) {
67 if (!module.empty()) {
68 return py::module::import(module.c_str());
69 } else {
70 return py::none();
71 }
72 }
73
74 // Get the obj of attr
GetPyObjAttr(const py::object & obj,const std::string & attr)75 py::object GetPyObjAttr(const py::object &obj, const std::string &attr) {
76 if (!attr.empty() && !py::isinstance<py::none>(obj)) {
77 if (py::hasattr(obj, attr.c_str())) {
78 return obj.attr(attr.c_str());
79 }
80 MS_LOG(DEBUG) << "Obj have not the attr: " << attr;
81 }
82 return py::none();
83 }
84
GetPyFn(const std::string & module,const std::string & name)85 py::object GetPyFn(const std::string &module, const std::string &name) {
86 (void)python_adapter::set_python_scoped();
87 if (!module.empty() && !name.empty()) {
88 py::module mod = py::module::import(module.c_str());
89 py::object fn = mod.attr(name.c_str());
90 return fn;
91 }
92 return py::none();
93 }
94 } // namespace python_adapter
95 } // namespace mindspore
96