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 <boost/python/converter/from_python.hpp>
18#include <boost/python/converter/registered.hpp>
19#include <boost/python/converter/rvalue_from_python_data.hpp>
20#include <boost/python/extract.hpp>
21#include <boost/python/handle.hpp>
22#include <boost/python/object.hpp>
24#include <boost/function.hpp>
28PXR_NAMESPACE_OPEN_SCOPE
31struct TfPyFunctionFromPython;
33template <
typename Ret,
typename... Args>
34struct TfPyFunctionFromPython<Ret (Args...)>
40 Ret operator()(Args... args) {
50 Ret operator()(Args... args) {
51 using namespace boost::python;
54 object callable(handle<>(borrowed(PyWeakref_GetObject(weak.
ptr()))));
56 TF_WARN(
"Tried to call an expired python callback");
68 Ret operator()(Args... args) {
69 using namespace boost::python;
73 PyObject *self = PyWeakref_GetObject(weakSelf.
ptr());
74 if (self == Py_None) {
75 TF_WARN(
"Tried to call a method on an expired python instance");
78 object method(handle<>(PyMethod_New(func.
ptr(), self)));
83 TfPyFunctionFromPython() {
84 RegisterFunctionType<boost::function<Ret (Args...)>>();
85 RegisterFunctionType<std::function<Ret (Args...)>>();
88 template <
typename FuncType>
90 RegisterFunctionType() {
91 using namespace boost::python;
93 insert(&convertible, &construct<FuncType>, type_id<FuncType>());
96 static void *convertible(PyObject *obj) {
97 return ((obj == Py_None) || PyCallable_Check(obj)) ? obj : 0;
100 template <
typename FuncType>
101 static void construct(PyObject *src, boost::python::converter::
102 rvalue_from_python_stage1_data *data) {
104 using namespace boost::python;
106 void *storage = ((converter::rvalue_from_python_storage<FuncType> *)
107 data)->storage.bytes;
109 if (src == Py_None) {
110 new (storage) FuncType();
132 object callable(handle<>(borrowed(src)));
133 PyObject *pyCallable = callable.ptr();
135 PyMethod_Check(pyCallable) ?
136 PyMethod_GET_SELF(pyCallable) : NULL;
141 object func(handle<>(borrowed(PyMethod_GET_FUNCTION(
143 object weakSelf(handle<>(PyWeakref_NewRef(self, NULL)));
150 }
else if (PyObject_HasAttrString(pyCallable,
"__name__") &&
151 extract<string>(callable.attr(
"__name__"))()
157 if (PyObject *weakCallable =
158 PyWeakref_NewRef(pyCallable, NULL)) {
162 object(handle<>(weakCallable)))});
171 data->convertible = storage;
175PXR_NAMESPACE_CLOSE_SCOPE
Miscellaneous Utilities for dealing with script.
TF_API bool TfPyIsNone(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.