24 #ifndef PXR_BASE_TF_PY_FUNCTION_H
25 #define PXR_BASE_TF_PY_FUNCTION_H
34 #include <hboost/python/converter/from_python.hpp>
35 #include <hboost/python/converter/registered.hpp>
36 #include <hboost/python/converter/rvalue_from_python_data.hpp>
37 #include <hboost/python/extract.hpp>
38 #include <hboost/python/handle.hpp>
39 #include <hboost/python/object.hpp>
41 #include <hboost/function.hpp>
50 template <
typename Ret,
typename... Args>
68 using namespace hboost::python;
71 object callable(
handle<>(borrowed(PyWeakref_GetObject(weak.ptr()))));
73 TF_WARN(
"Tried to call an expired python callback");
86 using namespace hboost::python;
90 PyObject *
self = PyWeakref_GetObject(weakSelf.ptr());
91 if (
self == Py_None) {
92 TF_WARN(
"Tried to call a method on an expired python instance");
101 RegisterFunctionType<hboost::function<Ret (Args...)>>();
102 RegisterFunctionType<std::function<Ret (Args...)>>();
105 template <
typename FuncType>
108 using namespace hboost::python;
110 insert(&convertible, &construct<FuncType>, type_id<FuncType>());
114 return ((obj == Py_None) || PyCallable_Check(obj)) ? obj : 0;
117 template <
typename FuncType>
119 rvalue_from_python_stage1_data *
data) {
121 using namespace hboost::python;
123 void *
storage = ((converter::rvalue_from_python_storage<FuncType> *)
124 data)->storage.bytes;
126 if (src == Py_None) {
149 object callable(
handle<>(borrowed(src)));
150 PyObject *pyCallable = callable.ptr();
152 PyMethod_Check(pyCallable) ?
153 PyMethod_GET_SELF(pyCallable) : NULL;
160 object weakSelf(
handle<>(PyWeakref_NewRef(
self, NULL)));
167 }
else if (PyObject_HasAttrString(pyCallable,
"__name__") &&
168 extract<string>(callable.attr(
"__name__"))()
174 if (PyObject *weakCallable =
175 PyWeakref_NewRef(pyCallable, NULL)) {
194 #endif // PXR_BASE_TF_PY_FUNCTION_H
Ret operator()(Args...args)
getFileOption("OpenEXR:storage") storage
Ret operator()(Args...args)
GLsizei const GLchar *const * string
OIIO_FORCEINLINE vbool4 insert(const vbool4 &a, bool val)
Helper: substitute val for a[i].
Ret operator()(Args...args)
static void RegisterFunctionType()
static void * convertible(PyObject *obj)
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
#define PXR_NAMESPACE_CLOSE_SCOPE
**If you just want to fire and args
TF_API bool TfPyIsNone(hboost::python::object const &obj)
Return true iff obj is None.
static void construct(PyObject *src, hboost::python::converter::rvalue_from_python_stage1_data *data)