-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
pyFunction.h
177 lines (151 loc) · 6.18 KB
/
pyFunction.h
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
//
// Copyright 2016 Pixar
//
// Licensed under the terms set forth in the LICENSE.txt file available at
// https://openusd.org/license.
//
#ifndef PXR_BASE_TF_PY_FUNCTION_H
#define PXR_BASE_TF_PY_FUNCTION_H
#include "pxr/pxr.h"
#include "pxr/base/tf/pyCall.h"
#include "pxr/base/tf/pyLock.h"
#include "pxr/base/tf/pyObjWrapper.h"
#include "pxr/base/tf/pyUtils.h"
#include <boost/python/converter/from_python.hpp>
#include <boost/python/converter/registered.hpp>
#include <boost/python/converter/rvalue_from_python_data.hpp>
#include <boost/python/extract.hpp>
#include <boost/python/handle.hpp>
#include <boost/python/object.hpp>
#include <boost/function.hpp>
#include <functional>
PXR_NAMESPACE_OPEN_SCOPE
template <typename T>
struct TfPyFunctionFromPython;
template <typename Ret, typename... Args>
struct TfPyFunctionFromPython<Ret (Args...)>
{
struct Call
{
TfPyObjWrapper callable;
Ret operator()(Args... args) {
TfPyLock lock;
return TfPyCall<Ret>(callable)(args...);
}
};
struct CallWeak
{
TfPyObjWrapper weak;
Ret operator()(Args... args) {
using namespace boost::python;
// Attempt to get the referenced callable object.
TfPyLock lock;
object callable(handle<>(borrowed(PyWeakref_GetObject(weak.ptr()))));
if (TfPyIsNone(callable)) {
TF_WARN("Tried to call an expired python callback");
return Ret();
}
return TfPyCall<Ret>(callable)(args...);
}
};
struct CallMethod
{
TfPyObjWrapper func;
TfPyObjWrapper weakSelf;
Ret operator()(Args... args) {
using namespace boost::python;
// Attempt to get the referenced self parameter, then build a new
// instance method and call it.
TfPyLock lock;
PyObject *self = PyWeakref_GetObject(weakSelf.ptr());
if (self == Py_None) {
TF_WARN("Tried to call a method on an expired python instance");
return Ret();
}
object method(handle<>(PyMethod_New(func.ptr(), self)));
return TfPyCall<Ret>(method)(args...);
}
};
TfPyFunctionFromPython() {
RegisterFunctionType<boost::function<Ret (Args...)>>();
RegisterFunctionType<std::function<Ret (Args...)>>();
}
template <typename FuncType>
static void
RegisterFunctionType() {
using namespace boost::python;
converter::registry::
insert(&convertible, &construct<FuncType>, type_id<FuncType>());
}
static void *convertible(PyObject *obj) {
return ((obj == Py_None) || PyCallable_Check(obj)) ? obj : 0;
}
template <typename FuncType>
static void construct(PyObject *src, boost::python::converter::
rvalue_from_python_stage1_data *data) {
using std::string;
using namespace boost::python;
void *storage = ((converter::rvalue_from_python_storage<FuncType> *)
data)->storage.bytes;
if (src == Py_None) {
new (storage) FuncType();
} else {
// In the case of instance methods, holding a strong reference will
// keep the bound 'self' argument alive indefinitely, which is
// undesirable. Unfortunately, we can't just keep a weak reference to
// the instance method, because python synthesizes these on-the-fly.
// Instead we do something like what PyQt's SIP does, and break the
// method into three parts: the class, the function, and the self
// parameter. We keep strong references to the class and the
// function, but a weak reference to 'self'. Then at call-time, if
// self has not expired, we build a new instancemethod and call it.
//
// Otherwise if the callable is a lambda (checked in a hacky way, but
// mirroring SIP), we take a strong reference.
//
// For all other callables, we attempt to take weak references to
// them. If that fails, we take a strong reference.
//
// This is all sort of contrived, but seems to have the right behavior
// for most usage patterns.
object callable(handle<>(borrowed(src)));
PyObject *pyCallable = callable.ptr();
PyObject *self =
PyMethod_Check(pyCallable) ?
PyMethod_GET_SELF(pyCallable) : NULL;
if (self) {
// Deconstruct the method and attempt to get a weak reference to
// the self instance.
object func(handle<>(borrowed(PyMethod_GET_FUNCTION(
pyCallable))));
object weakSelf(handle<>(PyWeakref_NewRef(self, NULL)));
new (storage)
FuncType(CallMethod{
TfPyObjWrapper(func),
TfPyObjWrapper(weakSelf)
});
} else if (PyObject_HasAttrString(pyCallable, "__name__") &&
extract<string>(callable.attr("__name__"))()
== "<lambda>") {
// Explicitly hold on to strong references to lambdas.
new (storage) FuncType(Call{TfPyObjWrapper(callable)});
} else {
// Attempt to get a weak reference to the callable.
if (PyObject *weakCallable =
PyWeakref_NewRef(pyCallable, NULL)) {
new (storage)
FuncType(
CallWeak{TfPyObjWrapper(
object(handle<>(weakCallable)))});
} else {
// Fall back to taking a strong reference.
PyErr_Clear();
new (storage) FuncType(Call{TfPyObjWrapper(callable)});
}
}
}
data->convertible = storage;
}
};
PXR_NAMESPACE_CLOSE_SCOPE
#endif // PXR_BASE_TF_PY_FUNCTION_H