7#ifndef PXR_BASE_TF_PY_INVOKE_H
8#define PXR_BASE_TF_PY_INVOKE_H
14#include "pxr/base/tf/api.h"
17#include "pxr/base/tf/pyError.h"
19#include "pxr/base/tf/pyLock.h"
20#include "pxr/base/tf/pyObjWrapper.h"
22#include "pxr/external/boost/python/dict.hpp"
23#include "pxr/external/boost/python/extract.hpp"
24#include "pxr/external/boost/python/list.hpp"
25#include "pxr/external/boost/python/object.hpp"
32PXR_NAMESPACE_OPEN_SCOPE
41pxr_boost::python::object Tf_ArgToPy(
const T &value)
43 return pxr_boost::python::object(value);
47TF_API pxr_boost::python::object Tf_ArgToPy(
const std::nullptr_t &value);
72 TfPyKwArg(
const std::string &nameIn,
const T &valueIn)
79 value = Tf_ArgToPy(valueIn);
92TF_API
void Tf_BuildPyInvokeKwArgs(
93 pxr_boost::python::dict *kwArgsOut);
97template <
typename Arg,
typename... RestArgs>
98void Tf_BuildPyInvokeKwArgs(
99 pxr_boost::python::dict *kwArgsOut,
106 std::is_same<Arg, TfPyKwArg>::value,
107 "Non-keyword args not allowed after keyword args");
111template <
typename... RestArgs>
112void Tf_BuildPyInvokeKwArgs(
113 pxr_boost::python::dict *kwArgsOut,
118 (*kwArgsOut)[kwArg.name] = kwArg.value.
Get();
121 Tf_BuildPyInvokeKwArgs(kwArgsOut, rest...);
125TF_API
void Tf_BuildPyInvokeArgs(
126 pxr_boost::python::list *posArgsOut,
127 pxr_boost::python::dict *kwArgsOut);
130template <
typename Arg,
typename... RestArgs>
131void Tf_BuildPyInvokeArgs(
132 pxr_boost::python::list *posArgsOut,
133 pxr_boost::python::dict *kwArgsOut,
139 posArgsOut->append(Tf_ArgToPy(arg));
142 Tf_BuildPyInvokeArgs(posArgsOut, kwArgsOut, rest...);
146template <
typename... RestArgs>
147void Tf_BuildPyInvokeArgs(
148 pxr_boost::python::list *posArgsOut,
149 pxr_boost::python::dict *kwArgsOut,
156 Tf_BuildPyInvokeKwArgs(kwArgsOut, kwArg, rest...);
167TF_API
bool Tf_PyInvokeImpl(
168 const std::string &moduleName,
169 const std::string &callableExpr,
170 const pxr_boost::python::list &posArgs,
171 const pxr_boost::python::dict &kwArgs,
172 pxr_boost::python::object *resultObjOut);
175template <
typename... Args>
177 const std::string &moduleName,
178 const std::string &callableExpr,
179 pxr_boost::python::object *resultOut,
233template <
typename Result,
typename... Args>
235 const std::string &moduleName,
236 const std::string &callableExpr,
249 pxr_boost::python::object resultObj;
251 moduleName, callableExpr, &resultObj, args...)) {
256 pxr_boost::python::extract<Result> extractor(resultObj);
257 if (!extractor.check()) {
261 *resultOut = extractor();
270template <
typename... Args>
272 const std::string &moduleName,
273 const std::string &callableExpr,
274 pxr_boost::python::object *resultOut,
288 pxr_boost::python::list posArgs;
289 pxr_boost::python::dict kwArgs;
290 Tf_BuildPyInvokeArgs(&posArgs, &kwArgs, args...);
293 if (!Tf_PyInvokeImpl(
294 moduleName, callableExpr, posArgs, kwArgs, resultOut)) {
298 catch (pxr_boost::python::error_already_set
const &) {
300 TfPyConvertPythonExceptionToTfErrors();
311template <
typename... Args>
313 const std::string &moduleName,
314 const std::string &callableExpr,
321 pxr_boost::python::object ignoredResult;
323 moduleName, callableExpr, &ignoredResult, args...);
326PXR_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 TfPyInvokeAndReturn(const std::string &moduleName, const std::string &callableExpr, pxr_boost::python::object *resultOut, Args... args)
A version of TfPyInvokeAndExtract that provides the Python function's return value as a pxr_boost::py...
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.
Wrapper object for a keyword-argument pair in a call to TfPyInvoke*.