You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

python_adapter.cc 3.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. #include "include/common/utils/python_adapter.h"
  17. #include <memory>
  18. #include <string>
  19. namespace mindspore {
  20. namespace python_adapter {
  21. // python scoped env, should only have one scoped_ instance
  22. static std::shared_ptr<py::scoped_interpreter> scoped_ = nullptr;
  23. // true: start process from python, false: start process from c++
  24. static bool python_env_ = false;
  25. static bool use_signature_in_resolve_ = true;
  26. void ResetPythonScope() { scoped_ = nullptr; }
  27. void set_use_signature_in_resolve(bool use_signature) noexcept { use_signature_in_resolve_ = use_signature; }
  28. bool UseSignatureInResolve() { return use_signature_in_resolve_; }
  29. void set_python_env_flag(bool python_env) noexcept { python_env_ = python_env; }
  30. bool IsPythonEnv() { return python_env_; }
  31. void SetPythonPath(const std::string &path) {
  32. // load the python module path
  33. (void)python_adapter::set_python_scoped();
  34. py::module sys = py::module::import("sys");
  35. py::list sys_path = sys.attr("path");
  36. // check the path is exist?
  37. bool is_exist = false;
  38. for (size_t i = 0; i < sys_path.size(); i++) {
  39. std::string path_str = py::cast<std::string>(sys_path[i]);
  40. if (path_str == path) {
  41. is_exist = true;
  42. }
  43. }
  44. if (!is_exist) {
  45. (void)sys_path.attr("append")(path.c_str());
  46. }
  47. }
  48. std::shared_ptr<py::scoped_interpreter> set_python_scoped() {
  49. // if start process from python, no need set the python scope.
  50. if (!python_env_) {
  51. if ((Py_IsInitialized() == 0) && (scoped_ == nullptr)) {
  52. scoped_ = std::make_shared<py::scoped_interpreter>();
  53. }
  54. }
  55. return scoped_;
  56. }
  57. // return the module of python
  58. py::module GetPyModule(const std::string &module) {
  59. if (!module.empty()) {
  60. return py::module::import(module.c_str());
  61. } else {
  62. return py::none();
  63. }
  64. }
  65. // Get the obj of attr
  66. py::object GetPyObjAttr(const py::object &obj, const std::string &attr) {
  67. if (!attr.empty() && !py::isinstance<py::none>(obj)) {
  68. if (py::hasattr(obj, attr.c_str())) {
  69. return obj.attr(attr.c_str());
  70. }
  71. MS_LOG(DEBUG) << "Obj have not the attr: " << attr;
  72. }
  73. return py::none();
  74. }
  75. py::object GetPyFn(const std::string &module, const std::string &name) {
  76. (void)python_adapter::set_python_scoped();
  77. if (!module.empty() && !name.empty()) {
  78. py::module mod = py::module::import(module.c_str());
  79. py::object fn = mod.attr(name.c_str());
  80. return fn;
  81. }
  82. return py::none();
  83. }
  84. } // namespace python_adapter
  85. } // namespace mindspore