1 |
|
|
// |
2 |
|
|
// Copyright (c) 2019-2021 INRIA |
3 |
|
|
// |
4 |
|
|
|
5 |
|
|
#ifndef __pinocchio_python_utils_namespace_hpp__ |
6 |
|
|
#define __pinocchio_python_utils_namespace_hpp__ |
7 |
|
|
|
8 |
|
|
#include <boost/python.hpp> |
9 |
|
|
|
10 |
|
|
namespace pinocchio |
11 |
|
|
{ |
12 |
|
|
namespace python |
13 |
|
|
{ |
14 |
|
|
|
15 |
|
|
/// |
16 |
|
|
/// \brief Helper to create or simply return an existing namespace in Python |
17 |
|
|
/// |
18 |
|
|
/// \param[in] submodule_name name of the submodule |
19 |
|
|
/// |
20 |
|
|
/// \returns The submodule related to the namespace name. |
21 |
|
|
/// |
22 |
|
608 |
inline boost::python::object getOrCreatePythonNamespace(const std::string & submodule_name) |
23 |
|
|
{ |
24 |
|
|
namespace bp = boost::python; |
25 |
|
|
|
26 |
✓✗ |
1216 |
bp::scope current_scope; |
27 |
✓✗✓✗ ✓✗✓✗ ✓✗ |
1216 |
std::string current_scope_name(bp::extract<const char*>(current_scope.attr("__name__"))); |
28 |
✓✗✓✗
|
1216 |
std::string complete_submodule_name = current_scope_name + "." + submodule_name; |
29 |
|
|
|
30 |
✓✗✓✗
|
608 |
bp::object submodule(bp::borrowed(PyImport_AddModule(complete_submodule_name.c_str()))); |
31 |
✓✗✓✗
|
608 |
current_scope.attr(submodule_name.c_str()) = submodule; |
32 |
|
|
|
33 |
|
1216 |
return submodule; |
34 |
|
|
} |
35 |
|
|
} // namespace python |
36 |
|
|
} // namespace pinocchio |
37 |
|
|
|
38 |
|
|
#endif // ifndef __pinocchio_python_utils_namespace_hpp__ |