forked from Maelic/libqi-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpymodule.cpp
More file actions
63 lines (50 loc) · 1.36 KB
/
pymodule.cpp
File metadata and controls
63 lines (50 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
** Copyright (C) 2020 SoftBank Robotics Europe
** See COPYING for the license
*/
#include <qipython/common.hpp>
#include <qipython/pymodule.hpp>
#include <qipython/pyobject.hpp>
#include <qipython/pyguard.hpp>
#include <qi/anyobject.hpp>
#include <qi/anymodule.hpp>
#include <pybind11/pybind11.h>
namespace py = pybind11;
namespace qi
{
namespace py
{
namespace
{
::py::object call(::py::object obj, ::py::str name,
::py::args args, ::py::kwargs kwargs)
{
GILAcquire lock;
return obj.attr(name)(*args, **kwargs);
}
::py::object getModule(const std::string& name)
{
const auto mod = import(name);
const auto pyMod = toPyObject(mod);
const ::py::cpp_function callFn(&call, ::py::is_method(pyMod.get_type()),
::py::arg("name"));
const auto types = ::py::module::import("types");
::py::setattr(pyMod, "createObject", types.attr("MethodType")(callFn, pyMod));
return pyMod;
}
::py::list listModules()
{
const auto modules = invokeGuarded<GILRelease>(&qi::listModules);
return castToPyObject(AnyReference::from(modules));
}
} // namespace
void exportObjectFactory(::py::module& m)
{
using namespace ::py;
GILAcquire lock;
m.def("module", &getModule,
doc(":returns: an object that represents the requested module.\n"));
m.def("listModules", &listModules);
}
} // namespace py
} // namespace qi