path.cpp
Go to the documentation of this file.
1 //
2 // Copyright (c) 2024 CNRS
3 //
4 
6 
7 namespace pinocchio
8 {
9  namespace python
10  {
11  namespace bp = boost::python;
12 
13  std::string path(const bp::object & path)
14  {
15 
16  auto str_path = path;
17  const bp::object Path = bp::import("pathlib").attr("Path");
18 
19  if (PyObject_IsInstance(str_path.ptr(), Path.ptr()))
20  str_path = str_path.attr("__str__")();
21 
22  if (!PyObject_IsInstance(str_path.ptr(), reinterpret_cast<PyObject *>(&PyUnicode_Type)))
23  {
24  std::string what = bp::extract<std::string>(str_path.attr("__str__")())();
25  throw std::invalid_argument(what + " is neither a Path nor a str.");
26  }
27 
28  return bp::extract<std::string>(str_path);
29  };
30 
31  std::vector<std::string> pathList(const bp::object & path_list)
32  {
33  if (!PyList_Check(path_list.ptr()))
34  {
35  std::string what = bp::extract<std::string>(path_list.attr("__str__")())();
36  throw std::invalid_argument(what + " is not a list.");
37  }
38 
39  // Retrieve the underlying list
40  bp::object bp_obj(bp::handle<>(bp::borrowed(path_list.ptr())));
41  bp::list bp_list(bp_obj);
42  bp::ssize_t list_size = bp::len(bp_list);
43 
44  std::vector<std::string> path_vec;
45  path_vec.reserve(list_size);
46  // Check if all the elements contained in the current vector is of type T
47  for (bp::ssize_t k = 0; k < list_size; ++k)
48  {
49  path_vec.push_back(path(bp_list[k]));
50  }
51  return path_vec;
52  };
53  } // namespace python
54 } // namespace pinocchio
pinocchio::python::path
std::string path(const bp::object &path)
python pathlib.Path | str -> C++ std::string
Definition: path.cpp:13
boost::python
path.hpp
python
pinocchio::python::pathList
std::vector< std::string > pathList(const bp::object &path_list)
python typing.List[pathlib.Path] | typing.List[str] -> C++ std::vector<std::string>
Definition: path.cpp:31
pinocchio
Main pinocchio namespace.
Definition: timings.cpp:27


pinocchio
Author(s):
autogenerated on Thu Dec 19 2024 03:41:32