forked from aldebaran/libqi-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpystrand.cpp
More file actions
226 lines (191 loc) · 6.14 KB
/
pystrand.cpp
File metadata and controls
226 lines (191 loc) · 6.14 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*
** Copyright (C) 2020 SoftBank Robotics Europe
** See COPYING for the license
*/
#include <qipython/pystrand.hpp>
#include <qipython/common.hpp>
#include <qipython/pyguard.hpp>
#include <pybind11/pybind11.h>
qiLogCategory("qi.python.strand");
namespace py = pybind11;
namespace qi
{
namespace py
{
namespace
{
const char* const objectAttributeStrandName = "__qi_strand__";
const char* const objectAttributeThreadingName = "__qi_threading__";
const char* const objectAttributeThreadingValueMulti = "multi";
// Returns true if `func` is an unbound method of `object`, by checking if one
// of the attribute of `object` is `func`.
//
// @pre `func` and `object` are not null objects.
bool isUnboundMethod(const ::py::function& func, const ::py::object& object)
{
QI_ASSERT_TRUE(func);
QI_ASSERT_TRUE(object);
GILAcquire lock;
const auto dir = ::py::reinterpret_steal<::py::list>(PyObject_Dir(object.ptr()));
for (const auto& attrName : dir)
{
const auto attr = object.attr(attrName);
QI_ASSERT_TRUE(attr);
if (!PyMethod_Check(attr.ptr()))
continue;
const auto unboundMethod = ::py::reinterpret_borrow<::py::object>(PyMethod_GET_FUNCTION(attr.ptr()));
if (func.is(unboundMethod))
return true;
}
return false;
}
// Returns the instance associated with a partial application of a method, if
// passed.
//
// If no instance is associated, or if the object is not a partial application,
// returns `None`.
//
// See https://docs.python.org/3/library/functools.html for more details about
// partial objects.
//
// They have three read-only attributes:
// - partial.func
// A callable object or function. Calls to the partial object will be
// forwarded to func with new arguments and keywords.
//
// - partial.args
// The leftmost positional arguments that will be prepended to the
// positional arguments provided to a partial object call.
//
// - partial.keywords
// The keyword arguments that will be supplied when the partial object is
// called.
//
// @pre `obj` is not a null object.
// @post the returned value is not a null object (it evaluates to true).
::py::object getPartialSelf(const ::py::function& partialFunc)
{
QI_ASSERT_TRUE(partialFunc);
GILAcquire lock;
constexpr const char* argsAttr = "args";
constexpr const char* funcAttr = "func";
if (!::py::hasattr(partialFunc, argsAttr) || !::py::hasattr(partialFunc, funcAttr))
// It's not a partial func, return immediately.
return ::py::none();
try
{
auto func = partialFunc.attr(funcAttr).cast<::py::function>();
QI_ASSERT_TRUE(func);
// The function might be a (bound) method.
const auto self = getMethodSelf(func);
if (!self.is_none())
return self;
// The function might be a `partial`.
const auto subpartialSelf = getPartialSelf(func);
if (!subpartialSelf.is_none())
return subpartialSelf;
// The function might be an unbound method.
//
// Unlike in Python 2, there is no known way to differentiate between an
// unbound method and a free function in Python 3. So to maintain
// retrocompatibility with older versions of libqi-python, we still decide
// to assume that the first argument is an instance (`self`), even though
// the function might be a free function, and not an unbound function. Then
// we check the function is a reference to one of the function attribute of
// that first argument.
//
// We can also check if the function is a not static method (which won't
// have a self parameter).
const bool isStaticMethod = ::py::isinstance<::py::staticmethod>(func);
if (!isStaticMethod)
{
::py::tuple args = partialFunc.attr(argsAttr);
if (args.empty())
return ::py::none();
const ::py::object selfCandidate = args[0];
if (isUnboundMethod(func, selfCandidate))
return selfCandidate;
}
// Fallback to just returning the function.
return std::move(func);
}
catch (const std::exception& ex)
{
qiLogVerbose()
<< "An exception occurred when extracting a partial function: "
<< ex.what();
return ::py::none();
}
}
// Returns the instance of the object on which the function is called (the
// `self` parameter).
//
// This function tries to detect partial application of functions and deduce
// the instance from the arguments, if passed.
//
// @pre `func` is not a null object.
// @post the returned value is not a null object (it evaluates to true).
::py::object getSelf(const ::py::function& func)
{
QI_ASSERT_TRUE(func);
GILAcquire lock;
const auto self = getMethodSelf(func);
if (!self.is_none())
return self;
return getPartialSelf(func);
}
} // namespace
StrandPtr strandOfFunction(const ::py::function& func)
{
GILAcquire lock;
return strandOf(getSelf(func));
}
StrandPtr strandOf(const ::py::object& obj)
{
QI_ASSERT_TRUE(obj);
GILAcquire lock;
if (obj.is_none())
return {};
if (isMultithreaded(obj))
return {};
auto strandObj = ::py::getattr(obj, objectAttributeStrandName, ::py::none());
if (strandObj.is_none())
{
try
{
strandObj = ::py::cast(StrandPtr(new Strand, DeleteOutsideGIL()));
::py::setattr(obj, objectAttributeStrandName, strandObj);
}
catch (const ::py::error_already_set& ex)
{
// If setting the attribute fails with an AttributeError, it may mean that
// we cannot set attributes on this object, therefore it cannot have an
// associated strand.
if (ex.matches(PyExc_AttributeError)) return {};
throw;
}
}
if (strandObj.is_none() || !::py::isinstance<Strand>(strandObj))
return {};
return strandObj.cast<StrandPtr>();
}
bool isMultithreaded(const ::py::object& obj)
{
QI_ASSERT_TRUE(obj);
GILAcquire lock;
const auto pyqisig = ::py::getattr(obj, objectAttributeThreadingName, ::py::none());
if (pyqisig.is_none())
return false;
return pyqisig.cast<std::string>() == objectAttributeThreadingValueMulti;
}
void exportStrand(::py::module& m)
{
using namespace ::py;
GILAcquire lock;
class_<Strand, StrandPtr>(m, "Strand")
.def(init([] {
return StrandPtr(new Strand, DeleteOutsideGIL());
}));
}
} // namespace py
} // namespace qi