7 #ifndef PXR_BASE_TF_PY_FUNCTION_H
8 #define PXR_BASE_TF_PY_FUNCTION_H
17 #include "pxr/external/boost/python/converter/from_python.hpp"
18 #include "pxr/external/boost/python/converter/registered.hpp"
19 #include "pxr/external/boost/python/converter/rvalue_from_python_data.hpp"
20 #include "pxr/external/boost/python/extract.hpp"
21 #include "pxr/external/boost/python/handle.hpp"
22 #include "pxr/external/boost/python/object.hpp"
31 template <
typename Ret,
typename... Args>
49 using namespace pxr_boost::python;
52 object callable(handle<>(borrowed(PyWeakref_GetObject(weak.ptr()))));
54 TF_WARN(
"Tried to call an expired python callback");
67 using namespace pxr_boost::python;
71 PyObject *
self = PyWeakref_GetObject(weakSelf.ptr());
72 if (
self == Py_None) {
73 TF_WARN(
"Tried to call a method on an expired python instance");
76 object method(handle<>(PyMethod_New(
func.ptr(),
self)));
82 RegisterFunctionType<std::function<Ret (Args...)>>();
85 template <
typename FuncType>
88 using namespace pxr_boost::python;
90 insert(&convertible, &construct<FuncType>, type_id<FuncType>());
94 return ((obj == Py_None) || PyCallable_Check(obj)) ? obj : 0;
97 template <
typename FuncType>
98 static void construct(PyObject *
src, pxr_boost::python::converter::
99 rvalue_from_python_stage1_data *
data) {
101 using namespace pxr_boost::python;
103 void *
storage = ((converter::rvalue_from_python_storage<FuncType> *)
104 data)->storage.bytes;
106 if (src == Py_None) {
129 object callable(handle<>(borrowed(src)));
130 PyObject *pyCallable = callable.ptr();
132 PyMethod_Check(pyCallable) ?
133 PyMethod_GET_SELF(pyCallable) : NULL;
138 object func(handle<>(borrowed(PyMethod_GET_FUNCTION(
140 object weakSelf(handle<>(PyWeakref_NewRef(
self, NULL)));
147 }
else if (PyObject_HasAttrString(pyCallable,
"__name__") &&
148 extract<string>(callable.attr(
"__name__"))()
154 if (PyObject *weakCallable =
155 PyWeakref_NewRef(pyCallable, NULL)) {
159 object(handle<>(weakCallable)))});
174 #endif // PXR_BASE_TF_PY_FUNCTION_H
Ret operator()(Args...args)
getFileOption("OpenEXR:storage") storage
Ret operator()(Args...args)
OIIO_FORCEINLINE vbool4 insert(const vbool4 &a, bool val)
Helper: substitute val for a[i].
Ret operator()(Args...args)
static void RegisterFunctionType()
TF_API bool TfPyIsNone(pxr_boost::python::object const &obj)
Return true iff obj is None.
static void * convertible(PyObject *obj)
static void construct(PyObject *src, pxr_boost::python::converter::rvalue_from_python_stage1_data *data)
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
#define PXR_NAMESPACE_CLOSE_SCOPE
**If you just want to fire and args