Loading...
Searching...
No Matches
pyCall.h
Go to the documentation of this file.
1//
2// Copyright 2016 Pixar
3//
4// Licensed under the Apache License, Version 2.0 (the "Apache License")
5// with the following modification; you may not use this file except in
6// compliance with the Apache License and the following modification to it:
7// Section 6. Trademarks. is deleted and replaced with:
8//
9// 6. Trademarks. This License does not grant permission to use the trade
10// names, trademarks, service marks, or product names of the Licensor
11// and its affiliates, except as required to comply with Section 4(c) of
12// the License and to reproduce the content of the NOTICE file.
13//
14// You may obtain a copy of the Apache License at
15//
16// http://www.apache.org/licenses/LICENSE-2.0
17//
18// Unless required by applicable law or agreed to in writing, software
19// distributed under the Apache License with the above modification is
20// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21// KIND, either express or implied. See the Apache License for the specific
22// language governing permissions and limitations under the Apache License.
23//
24#ifndef PXR_BASE_TF_PY_CALL_H
25#define PXR_BASE_TF_PY_CALL_H
26
32
33#include "pxr/pxr.h"
34
35#include "pxr/base/tf/pyError.h"
36#include "pxr/base/tf/pyLock.h"
37#include "pxr/base/tf/pyObjWrapper.h"
38
39#include <boost/python/call.hpp>
40
41PXR_NAMESPACE_OPEN_SCOPE
42
56template <typename Return>
57struct TfPyCall {
61 explicit TfPyCall(TfPyObjWrapper const &c) : _callable(c) {}
62
63 template <typename... Args>
64 Return operator()(Args... args);
65
66private:
67 TfPyObjWrapper _callable;
68};
69
70template <typename Return>
71template <typename... Args>
72inline Return
74{
75 TfPyLock pyLock;
76 // Do *not* call through if there's an active python exception.
77 if (!PyErr_Occurred()) {
78 try {
79 return boost::python::call<Return>
80 (_callable.ptr(), args...);
81 } catch (boost::python::error_already_set const &) {
82 // Convert any exception to TF_ERRORs.
83 TfPyConvertPythonExceptionToTfErrors();
84 PyErr_Clear();
85 }
86 }
87 return Return();
88}
89
90PXR_NAMESPACE_CLOSE_SCOPE
91
92#endif
Convenience class for accessing the Python Global Interpreter Lock.
Definition: pyLock.h:122
Boost Python object wrapper.
Definition: pyObjWrapper.h:97
Provide a way to call a Python callable.
Definition: pyCall.h:57
TfPyCall(TfPyObjWrapper const &c)
Construct with callable c.
Definition: pyCall.h:61