Directory: | ./ |
---|---|
File: | include/pinocchio/bindings/python/utils/namespace.hpp |
Date: | 2025-02-12 21:03:38 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 11 | 11 | 100.0% |
Branches: | 14 | 28 | 50.0% |
Line | Branch | Exec | Source |
---|---|---|---|
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 | #include <string> | ||
10 | |||
11 | namespace pinocchio | ||
12 | { | ||
13 | namespace python | ||
14 | { | ||
15 | |||
16 | /// | ||
17 | /// \returns the name of the current Python scope. | ||
18 | /// | ||
19 | 2510 | inline std::string getCurrentScopeName() | |
20 | { | ||
21 | namespace bp = boost::python; | ||
22 |
1/2✓ Branch 1 taken 2510 times.
✗ Branch 2 not taken.
|
2510 | bp::scope current_scope; |
23 | |||
24 |
5/10✓ Branch 2 taken 2510 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 2510 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 2510 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 2510 times.
✗ Branch 12 not taken.
✓ Branch 14 taken 2510 times.
✗ Branch 15 not taken.
|
7530 | return std::string(bp::extract<const char *>(current_scope.attr("__name__"))); |
25 | 2510 | } | |
26 | |||
27 | /// | ||
28 | /// \brief Helper to create or simply return an existing namespace in Python | ||
29 | /// | ||
30 | /// \param[in] submodule_name name of the submodule | ||
31 | /// | ||
32 | /// \returns The submodule related to the namespace name. | ||
33 | /// | ||
34 | 2510 | inline boost::python::object getOrCreatePythonNamespace(const std::string & submodule_name) | |
35 | { | ||
36 | namespace bp = boost::python; | ||
37 | |||
38 |
3/6✓ Branch 1 taken 2510 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 2510 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 2510 times.
✗ Branch 8 not taken.
|
5020 | const std::string complete_submodule_name = getCurrentScopeName() + "." + submodule_name; |
39 | |||
40 |
2/4✓ Branch 2 taken 2510 times.
✗ Branch 3 not taken.
✓ Branch 6 taken 2510 times.
✗ Branch 7 not taken.
|
2510 | bp::object submodule(bp::borrowed(PyImport_AddModule(complete_submodule_name.c_str()))); |
41 |
1/2✓ Branch 1 taken 2510 times.
✗ Branch 2 not taken.
|
2510 | bp::scope current_scope; |
42 |
2/4✓ Branch 2 taken 2510 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 2510 times.
✗ Branch 6 not taken.
|
2510 | current_scope.attr(submodule_name.c_str()) = submodule; |
43 | |||
44 | 5020 | return submodule; | |
45 | 2510 | } | |
46 | } // namespace python | ||
47 | } // namespace pinocchio | ||
48 | |||
49 | #endif // ifndef __pinocchio_python_utils_namespace_hpp__ | ||
50 |