24#ifndef PXR_BASE_TF_PY_INVOKE_H
25#define PXR_BASE_TF_PY_INVOKE_H
31#include "pxr/base/tf/api.h"
34#include "pxr/base/tf/pyError.h"
36#include "pxr/base/tf/pyLock.h"
37#include "pxr/base/tf/pyObjWrapper.h"
39#include <boost/python/dict.hpp>
40#include <boost/python/extract.hpp>
41#include <boost/python/list.hpp>
42#include <boost/python/object.hpp>
49PXR_NAMESPACE_OPEN_SCOPE
58boost::python::object Tf_ArgToPy(
const T &value)
60 return boost::python::object(value);
64TF_API boost::python::object Tf_ArgToPy(
const std::nullptr_t &value);
89 TfPyKwArg(
const std::string &nameIn,
const T &valueIn)
96 value = Tf_ArgToPy(valueIn);
109TF_API
void Tf_BuildPyInvokeKwArgs(
110 boost::python::dict *kwArgsOut);
114template <
typename Arg,
typename... RestArgs>
115void Tf_BuildPyInvokeKwArgs(
116 boost::python::dict *kwArgsOut,
123 std::is_same<Arg, TfPyKwArg>::value,
124 "Non-keyword args not allowed after keyword args");
128template <
typename... RestArgs>
129void Tf_BuildPyInvokeKwArgs(
130 boost::python::dict *kwArgsOut,
135 (*kwArgsOut)[kwArg.name] = kwArg.value.
Get();
138 Tf_BuildPyInvokeKwArgs(kwArgsOut, rest...);
142TF_API
void Tf_BuildPyInvokeArgs(
143 boost::python::list *posArgsOut,
144 boost::python::dict *kwArgsOut);
147template <
typename Arg,
typename... RestArgs>
148void Tf_BuildPyInvokeArgs(
149 boost::python::list *posArgsOut,
150 boost::python::dict *kwArgsOut,
156 posArgsOut->append(Tf_ArgToPy(arg));
159 Tf_BuildPyInvokeArgs(posArgsOut, kwArgsOut, rest...);
163template <
typename... RestArgs>
164void Tf_BuildPyInvokeArgs(
165 boost::python::list *posArgsOut,
166 boost::python::dict *kwArgsOut,
173 Tf_BuildPyInvokeKwArgs(kwArgsOut, kwArg, rest...);
184TF_API
bool Tf_PyInvokeImpl(
185 const std::string &moduleName,
186 const std::string &callableExpr,
187 const boost::python::list &posArgs,
188 const boost::python::dict &kwArgs,
189 boost::python::object *resultObjOut);
192template <
typename... Args>
194 const std::string &moduleName,
195 const std::string &callableExpr,
196 boost::python::object *resultOut,
250template <
typename Result,
typename... Args>
252 const std::string &moduleName,
253 const std::string &callableExpr,
266 boost::python::object resultObj;
268 moduleName, callableExpr, &resultObj, args...)) {
273 boost::python::extract<Result> extractor(resultObj);
274 if (!extractor.check()) {
278 *resultOut = extractor();
287template <
typename... Args>
289 const std::string &moduleName,
290 const std::string &callableExpr,
291 boost::python::object *resultOut,
305 boost::python::list posArgs;
306 boost::python::dict kwArgs;
307 Tf_BuildPyInvokeArgs(&posArgs, &kwArgs, args...);
310 if (!Tf_PyInvokeImpl(
311 moduleName, callableExpr, posArgs, kwArgs, resultOut)) {
315 catch (boost::python::error_already_set
const &) {
317 TfPyConvertPythonExceptionToTfErrors();
328template <
typename... Args>
330 const std::string &moduleName,
331 const std::string &callableExpr,
338 boost::python::object ignoredResult;
340 moduleName, callableExpr, &ignoredResult, args...);
343PXR_NAMESPACE_CLOSE_SCOPE
Convenience class for accessing the Python Global Interpreter Lock.
Boost Python object wrapper.
object const & Get() const
Underlying object access.
Stripped down version of diagnostic.h that doesn't define std::string.
#define TF_CODING_ERROR(fmt, args)
Issue an internal programming error, but continue execution.
Python runtime utilities.
TF_API void TfPyInitialize()
Starts up the python runtime.
bool TfPyInvoke(const std::string &moduleName, const std::string &callableExpr, Args... args)
A version of TfPyInvokeAndExtract that ignores the Python function's return value.
bool TfPyInvokeAndExtract(const std::string &moduleName, const std::string &callableExpr, Result *resultOut, Args... args)
Call a Python function and obtain its return value.
bool TfPyInvokeAndReturn(const std::string &moduleName, const std::string &callableExpr, boost::python::object *resultOut, Args... args)
A version of TfPyInvokeAndExtract that provides the Python function's return value as a boost::python...
Wrapper object for a keyword-argument pair in a call to TfPyInvoke*.