HDK
 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 PXR_BASE_TF_PY_CALL_H
25 #define PXR_BASE_TF_PY_CALL_H
26 
27 /// \file tf/pyCall.h
28 /// Utilities for calling python callables.
29 ///
30 /// These functions handle trapping python errors and converting them to \a
31 /// TfErrors.
32 
33 #include "pxr/pxr.h"
34 
35 #include "pxr/base/tf/pyError.h"
36 #include "pxr/base/tf/pyLock.h"
38 
39 #include <hboost/python/call.hpp>
40 
42 
43 /// \class TfPyCall
44 ///
45 /// Provide a way to call a Python callable.
46 ///
47 /// Usage is as follows:
48 /// \code
49 /// return TfPyCall<RetType>(callable)(arg1, arg2, ... argN);
50 /// \endcode
51 /// Generally speaking, TfPyCall instances may be copied, assigned, destroyed,
52 /// and invoked without the client holding the GIL. However, if the \a Return
53 /// template parameter is a \a hboost::python::object (or a derived class, such
54 /// as list or tuple) then the client must hold the GIL in order to invoke the
55 /// call operator.
56 template <typename Return>
57 struct TfPyCall {
58  /// Construct with callable \a c. Constructing with a \c
59  /// hboost::python::object works, since those implicitly convert to \c
60  /// TfPyObjWrapper, however in that case the GIL must be held by the caller.
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
74 {
75  TfPyLock pyLock;
76  // Do *not* call through if there's an active python exception.
77  if (!PyErr_Occurred()) {
78  try {
79  return hboost::python::call<Return>
80  (_callable.ptr(), args...);
81  } catch (hboost::python::error_already_set const &) {
82  // Convert any exception to TF_ERRORs.
84  PyErr_Clear();
85  }
86  }
87  return Return();
88 }
89 
91 
92 #endif
Return operator()(Args...args)
Definition: pyCall.h:73
TfPyCall(TfPyObjWrapper const &c)
Definition: pyCall.h:61
TF_API void TfPyConvertPythonExceptionToTfErrors()
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1441
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
**If you just want to fire and args
Definition: thread.h:609