All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
pyCall.h
Go to the documentation of this file.
1//
2// Copyright 2016 Pixar
3//
4// Licensed under the terms set forth in the LICENSE.txt file available at
5// https://openusd.org/license.
6//
7#ifndef PXR_BASE_TF_PY_CALL_H
8#define PXR_BASE_TF_PY_CALL_H
9
15
16#include "pxr/pxr.h"
17
18#include "pxr/base/tf/pyError.h"
19#include "pxr/base/tf/pyLock.h"
20#include "pxr/base/tf/pyObjWrapper.h"
21
22#include "pxr/external/boost/python/call.hpp"
23
24PXR_NAMESPACE_OPEN_SCOPE
25
39template <typename Return>
40struct TfPyCall {
44 explicit TfPyCall(TfPyObjWrapper const &c) : _callable(c) {}
45
46 template <typename... Args>
47 Return operator()(Args... args);
48
49private:
50 TfPyObjWrapper _callable;
51};
52
53template <typename Return>
54template <typename... Args>
55inline Return
57{
58 TfPyLock pyLock;
59 // Do *not* call through if there's an active python exception.
60 if (!PyErr_Occurred()) {
61 try {
62 return pxr_boost::python::call<Return>
63 (_callable.ptr(), args...);
64 } catch (pxr_boost::python::error_already_set const &) {
65 // Convert any exception to TF_ERRORs.
66 TfPyConvertPythonExceptionToTfErrors();
67 PyErr_Clear();
68 }
69 }
70 return Return();
71}
72
73PXR_NAMESPACE_CLOSE_SCOPE
74
75#endif
Convenience class for accessing the Python Global Interpreter Lock.
Definition: pyLock.h:105
Boost Python object wrapper.
Definition: pyObjWrapper.h:79
Provide a way to call a Python callable.
Definition: pyCall.h:40
TfPyCall(TfPyObjWrapper const &c)
Construct with callable c.
Definition: pyCall.h:44