All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 TF_PY_CALL_H
25 #define 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 
41 PXR_NAMESPACE_OPEN_SCOPE
42 
56 template <typename Return>
57 struct TfPyCall {
61  explicit TfPyCall(TfPyObjWrapper const &c) : _callable(c) {}
62 
63  template <typename... Args>
64  Return operator()(Args... args);
65 
66 private:
67  TfPyObjWrapper _callable;
68 };
69 
70 template <typename Return>
71 template <typename... Args>
72 inline Return
73 TfPyCall<Return>::operator()(Args... args)
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 
90 PXR_NAMESPACE_CLOSE_SCOPE
91 
92 #endif
TfPyCall(TfPyObjWrapper const &c)
Construct with callable c.
Definition: pyCall.h:61
Provide a way to call a Python callable.
Definition: pyCall.h:57
Boost Python object wrapper.
Definition: pyObjWrapper.h:66