7#ifndef PXR_BASE_TF_PY_FUNCTION_H
8#define PXR_BASE_TF_PY_FUNCTION_H
13#include "pxr/base/tf/pyLock.h"
14#include "pxr/base/tf/pyObjWrapper.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"
26PXR_NAMESPACE_OPEN_SCOPE
29struct TfPyFunctionFromPython;
31template <
typename Ret,
typename... Args>
32struct TfPyFunctionFromPython<Ret (Args...)>
38 Ret operator()(Args... args) {
48 Ret operator()(Args... 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");
66 Ret operator()(Args... args) {
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)));
81 TfPyFunctionFromPython() {
82 RegisterFunctionType<std::function<Ret (Args...)>>();
85 template <
typename FuncType>
87 RegisterFunctionType() {
88 using namespace pxr_boost::python;
90 insert(&convertible, &construct<FuncType>, type_id<FuncType>());
93 static void *convertible(PyObject *obj) {
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) {
107 new (storage) FuncType();
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)))});
168 data->convertible = storage;
172PXR_NAMESPACE_CLOSE_SCOPE
Miscellaneous Utilities for dealing with script.
TF_API bool TfPyIsNone(pxr_boost::python::object const &obj)
Return true iff obj is None.
Convenience class for accessing the Python Global Interpreter Lock.
Boost Python object wrapper.
TF_API PyObject * ptr() const
Underlying PyObject* access.
#define TF_WARN(...)
Issue a warning, but continue execution.
Utilities for calling python callables.
Provide a way to call a Python callable.