All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
pyUtils.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_UTILS_H
8#define PXR_BASE_TF_PY_UTILS_H
9
12
13#include "pxr/pxr.h"
14
15#include "pxr/base/tf/refPtr.h"
16#include "pxr/base/tf/weakPtr.h"
18#include "pxr/base/tf/preprocessorUtilsLite.h"
21#include "pxr/base/tf/pyLock.h"
22#include "pxr/base/tf/api.h"
23
24#include <functional>
25#include <typeinfo>
26#include <string>
27#include <vector>
28
29#include "pxr/external/boost/python/dict.hpp"
30#include "pxr/external/boost/python/extract.hpp"
31#include "pxr/external/boost/python/handle.hpp"
32#include "pxr/external/boost/python/object.hpp"
33#include "pxr/external/boost/python/type_id.hpp"
34
35PXR_NAMESPACE_OPEN_SCOPE
36
42#define TF_PY_REPR_PREFIX \
43 std::string(TF_PP_STRINGIZE(MFB_PACKAGE_MODULE) ".")
44
46TF_API bool TfPyIsInitialized();
47
51TF_API void TfPyThrowIndexError(const char *msg);
52
54inline void TfPyThrowIndexError(std::string const &msg)
55{
56 TfPyThrowIndexError(msg.c_str());
57}
58
62TF_API void TfPyThrowRuntimeError(const char *msg);
63
65inline void TfPyThrowRuntimeError(std::string const &msg)
66{
67 TfPyThrowRuntimeError(msg.c_str());
68}
69
73TF_API void TfPyThrowStopIteration(const char *msg);
74
76inline void TfPyThrowStopIteration(std::string const &msg)
77{
78 TfPyThrowStopIteration(msg.c_str());
79}
80
84TF_API void TfPyThrowKeyError(const char *msg);
85
87inline void TfPyThrowKeyError(std::string const &msg)
88{
89 TfPyThrowKeyError(msg.c_str());
90}
91
95TF_API void TfPyThrowValueError(const char *msg);
96
98inline void TfPyThrowValueError(std::string const &msg)
99{
100 TfPyThrowValueError(msg.c_str());
101}
102
106TF_API void TfPyThrowTypeError(const char *msg);
107
109inline void TfPyThrowTypeError(std::string const &msg)
110{
111 TfPyThrowTypeError(msg.c_str());
112}
113
115TF_API bool TfPyIsNone(pxr_boost::python::object const &obj);
116
118TF_API bool TfPyIsNone(pxr_boost::python::handle<> const &obj);
119
120// Helper for \c TfPyObject().
121TF_API void Tf_PyObjectError(bool printError);
122
126template <typename T>
127pxr_boost::python::object TfPyObject(T const &t, bool complainOnFailure = true) {
128 // initialize python if it isn't already, so at least we can try to return
129 // an object
130 if (!TfPyIsInitialized()) {
131 TF_CODING_ERROR("Called TfPyObject without python being initialized!");
133 }
134
135 TfPyLock pyLock;
136
137 // Will only be able to return objects which have been wrapped.
138 // Returns None otherwise
139 try {
140 return pxr_boost::python::object(t);
141 } catch (pxr_boost::python::error_already_set const &) {
142 Tf_PyObjectError(complainOnFailure);
143 return pxr_boost::python::object();
144 }
145}
146
147inline
148pxr_boost::python::object TfPyObject(PyObject* t, bool complainOnFailure = true) {
149 TfPyLock pyLock;
150 return pxr_boost::python::object(pxr_boost::python::handle<>(t));
151}
152
156TF_API std::string TfPyObjectRepr(pxr_boost::python::object const &t);
157
162template <typename T>
163std::string TfPyRepr(T const &t) {
164 if (!TfPyIsInitialized())
165 return "<python not initialized>";
166 TfPyLock lock;
167 return TfPyObjectRepr(TfPyObject(t));
168}
169
171template <typename T>
172std::string TfPyRepr(const std::vector<T> &v) {
173 std::string result("[");
174 typename std::vector<T>::const_iterator i = v.begin();
175 if (i != v.end()) {
176 result += TfPyRepr(*i);
177 ++i;
178 }
179 while (i != v.end()) {
180 result += ", " + TfPyRepr(*i);
181 ++i;
182 }
183 result += "]";
184 return result;
185}
186
190TF_API
191pxr_boost::python::object
193 std::string const &expr,
194 pxr_boost::python::dict const &extraGlobals = pxr_boost::python::dict());
195
198TF_API
199int64_t
200TfPyNormalizeIndex(int64_t index, uint64_t size, bool throwError = false);
201
203TF_API std::string TfPyGetClassName(pxr_boost::python::object const &obj);
204
205
208TF_API pxr_boost::python::object
209TfPyGetClassObject(std::type_info const &type);
210
213template <typename T>
214pxr_boost::python::object
216 return TfPyGetClassObject(typeid(T));
217}
218
219TF_API
220void
221Tf_PyWrapOnceImpl(pxr_boost::python::type_info const &,
222 std::function<void()> const&,
223 bool *);
224
232template <typename T>
233void
234TfPyWrapOnce(std::function<void()> const &wrapFunc)
235{
236 // Don't try to wrap if python isn't initialized.
237 if (!TfPyIsInitialized()) {
238 return;
239 }
240
241 static bool isTypeWrapped = false;
242 if (isTypeWrapped) {
243 return;
244 }
245
246 Tf_PyWrapOnceImpl(pxr_boost::python::type_id<T>(), wrapFunc, &isTypeWrapped);
247}
248
253TF_API
254void Tf_PyLoadScriptModule(std::string const &name);
255
257template <class Map>
258pxr_boost::python::dict TfPyCopyMapToDictionary(Map const &map) {
259 TfPyLock lock;
260 pxr_boost::python::dict d;
261 for (typename Map::const_iterator i = map.begin(); i != map.end(); ++i)
262 d[i->first] = i->second;
263 return d;
264}
265
266template<class Seq>
267pxr_boost::python::list TfPyCopySequenceToList(Seq const &seq) {
268 TfPyLock lock;
269 pxr_boost::python::list l;
270 for (typename Seq::const_iterator i = seq.begin();
271 i != seq.end(); ++i)
272 l.append(*i);
273 return l;
274}
275
280template <class Seq>
281pxr_boost::python::object TfPyCopySequenceToSet(Seq const &seq) {
282 TfPyLock lock;
283 pxr_boost::python::handle<> set{pxr_boost::python::allow_null(PySet_New(nullptr))};
284 if (!set) {
285 pxr_boost::python::throw_error_already_set();
286 }
287 for (auto const& item : seq) {
288 pxr_boost::python::object obj(item);
289 if (PySet_Add(set.get(), obj.ptr()) == -1) {
290 pxr_boost::python::throw_error_already_set();
291 }
292 }
293 return pxr_boost::python::object(set);
294}
295
296template<class Seq>
297pxr_boost::python::tuple TfPyCopySequenceToTuple(Seq const &seq) {
298 return pxr_boost::python::tuple(TfPyCopySequenceToList(seq));
299}
300
307TF_API
308pxr_boost::python::object TfPyCopyBufferToByteArray(const char* buffer, size_t size);
309
314TF_API
315std::vector<std::string> TfPyGetTraceback();
316
338TF_API
339bool TfPySetenv(const std::string & name, const std::string & value);
340
363TF_API
364bool TfPyUnsetenv(const std::string & name);
365
366// Private helper method to TfPyEvaluateAndExtract.
367//
368TF_API bool Tf_PyEvaluateWithErrorCheck(
369 const std::string & expr, pxr_boost::python::object * obj);
370
374template <typename T>
375bool TfPyEvaluateAndExtract(const std::string & expr, T * t)
376{
377 if (expr.empty())
378 return false;
379
380 // Take the lock before doing anything with pxr_boost::python.
381 TfPyLock lock;
382
383 // Though TfPyEvaluate (called by Tf_PyEvaluateWithErroCheck) takes the
384 // python lock, it is important that we lock before we initialize the
385 // pxr_boost::python::object, since it will increment and decrement ref counts
386 // outside of the call to TfPyEvaluate.
387 pxr_boost::python::object obj;
388 if (!Tf_PyEvaluateWithErrorCheck(expr, &obj))
389 return false;
390
391 pxr_boost::python::extract<T> extractor(obj);
392
393 if (!extractor.check())
394 return false;
395
396 *t = extractor();
397
398 return true;
399}
400
404TF_API
406
407PXR_NAMESPACE_CLOSE_SCOPE
408
409#endif // PXR_BASE_TF_PY_UTILS_H
TF_API void TfPyPrintError()
Print a standard traceback to sys.stderr and clear the error indicator.
TF_API std::string TfPyObjectRepr(pxr_boost::python::object const &t)
Return repr(t).
TF_API void TfPyThrowIndexError(const char *msg)
Raises a Python IndexError with the given error msg and throws a pxr_boost::python::error_already_set...
TF_API void TfPyThrowRuntimeError(const char *msg)
Raises a Python RuntimeError with the given error msg and throws a pxr_boost::python::error_already_s...
TF_API void TfPyThrowStopIteration(const char *msg)
Raises a Python StopIteration with the given error msg and throws a pxr_boost::python::error_already_...
TF_API bool TfPyIsNone(pxr_boost::python::object const &obj)
Return true iff obj is None.
pxr_boost::python::object TfPyCopySequenceToSet(Seq const &seq)
Create a python set from an iterable sequence.
Definition: pyUtils.h:281
pxr_boost::python::object TfPyGetClassObject()
Return the python class object for T if T has been wrapped.
Definition: pyUtils.h:215
TF_API void TfPyThrowKeyError(const char *msg)
Raises a Python KeyError with the given error msg and throws a pxr_boost::python::error_already_set e...
TF_API pxr_boost::python::object TfPyCopyBufferToByteArray(const char *buffer, size_t size)
Create a python bytearray from an input buffer and size.
pxr_boost::python::dict TfPyCopyMapToDictionary(Map const &map)
Creates a python dictionary from a std::map.
Definition: pyUtils.h:258
TF_API bool TfPyUnsetenv(const std::string &name)
Remove an environment variable from os.environ.
TF_API std::string TfPyGetClassName(pxr_boost::python::object const &obj)
Return the name of the class of obj.
bool TfPyEvaluateAndExtract(const std::string &expr, T *t)
Safely evaluates expr and extracts the return object of type T.
Definition: pyUtils.h:375
pxr_boost::python::object TfPyObject(T const &t, bool complainOnFailure=true)
Return a python object for the given C++ object, loading the appropriate wrapper code if necessary.
Definition: pyUtils.h:127
TF_API bool TfPyIsInitialized()
Returns true if python is initialized.
TF_API void TfPyThrowTypeError(const char *msg)
Raises a Python TypeError with the given error msg and throws a pxr_boost::python::error_already_set ...
TF_API std::vector< std::string > TfPyGetTraceback()
Return a vector of strings containing the current python traceback.
TF_API bool TfPySetenv(const std::string &name, const std::string &value)
Set an environment variable in os.environ.
void TfPyWrapOnce(std::function< void()> const &wrapFunc)
Invokes wrapFunc to wrap type T if T is not already wrapped.
Definition: pyUtils.h:234
TF_API void TfPyThrowValueError(const char *msg)
Raises a Python ValueError with the given error msg and throws a pxr_boost::python::error_already_set...
TF_API int64_t TfPyNormalizeIndex(int64_t index, uint64_t size, bool throwError=false)
Return a positive index in the range [0,size).
TF_API pxr_boost::python::object TfPyEvaluate(std::string const &expr, pxr_boost::python::dict const &extraGlobals=pxr_boost::python::dict())
Evaluate python expression expr with all the known script modules imported under their standard names...
std::string TfPyRepr(T const &t)
Return repr(t).
Definition: pyUtils.h:163
Convenience class for accessing the Python Global Interpreter Lock.
Definition: pyLock.h:105
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.
Definition: diagnostic.h:68
Python runtime utilities.
TF_API void TfPyInitialize()
Starts up the python runtime.
Intended to replace a direct include of Python.h, which causes several build problems with certain co...
Reference counting.
Pointer storage with deletion detection.