24 #ifndef PXR_BASE_TF_PY_UTILS_H
25 #define PXR_BASE_TF_PY_UTILS_H
35 #include "pxr/base/tf/preprocessorUtilsLite.h"
38 #include "pxr/base/tf/pyLock.h"
39 #include "pxr/base/tf/api.h"
46 #include <boost/python/dict.hpp>
47 #include <boost/python/extract.hpp>
48 #include <boost/python/handle.hpp>
49 #include <boost/python/object.hpp>
50 #include <boost/python/type_id.hpp>
52 PXR_NAMESPACE_OPEN_SCOPE
59 #define TF_PY_REPR_PREFIX \
60 std::string(TF_PP_STRINGIZE(MFB_PACKAGE_MODULE) ".")
90 TF_API
bool TfPyIsNone(boost::python::object
const &obj);
93 TF_API
bool TfPyIsNone(boost::python::handle<>
const &obj);
96 TF_API
void Tf_PyObjectError(
bool printError);
101 template <
typename T>
102 boost::python::object
TfPyObject(T
const &t,
bool complainOnFailure =
true) {
106 TF_CODING_ERROR(
"Called TfPyObject without python being initialized!");
115 return boost::python::object(t);
116 }
catch (boost::python::error_already_set
const &) {
117 Tf_PyObjectError(complainOnFailure);
118 return boost::python::object();
123 boost::python::object
TfPyObject(PyObject* t,
bool complainOnFailure =
true) {
125 return boost::python::object(boost::python::handle<>(t));
131 TF_API std::string
TfPyObjectRepr(boost::python::object
const &t);
137 template <
typename T>
140 return "<python not initialized>";
146 template <
typename T>
148 std::string result(
"[");
149 typename std::vector<T>::const_iterator i = v.begin();
154 while (i != v.end()) {
166 boost::python::object
168 std::string
const &expr,
169 boost::python::dict
const &extraGlobals = boost::python::dict());
183 TF_API boost::python::object
188 template <
typename T>
189 boost::python::object
196 Tf_PyWrapOnceImpl(boost::python::type_info
const &,
197 std::function<
void()>
const&,
207 template <
typename T>
209 TfPyWrapOnce(std::function<
void()>
const &wrapFunc)
216 static bool isTypeWrapped =
false;
221 Tf_PyWrapOnceImpl(boost::python::type_id<T>(), wrapFunc, &isTypeWrapped);
229 void Tf_PyLoadScriptModule(std::string
const &name);
235 boost::python::dict d;
236 for (
typename Map::const_iterator i = map.begin(); i != map.end(); ++i)
237 d[i->first] = i->second;
242 boost::python::list TfPyCopySequenceToList(Seq
const &seq) {
244 boost::python::list l;
245 for (
typename Seq::const_iterator i = seq.begin();
258 boost::python::handle<> set{boost::python::allow_null(PySet_New(
nullptr))};
260 boost::python::throw_error_already_set();
262 for (
auto const& item : seq) {
263 boost::python::object obj(item);
264 if (PySet_Add(set.get(), obj.ptr()) == -1) {
265 boost::python::throw_error_already_set();
268 return boost::python::object(set);
272 boost::python::tuple TfPyCopySequenceToTuple(Seq
const &seq) {
273 return boost::python::tuple(TfPyCopySequenceToList(seq));
325 bool TfPySetenv(
const std::string & name,
const std::string & value);
354 TF_API
bool Tf_PyEvaluateWithErrorCheck(
355 const std::string & expr, boost::python::object * obj);
360 template <
typename T>
373 boost::python::object obj;
374 if (!Tf_PyEvaluateWithErrorCheck(expr, &obj))
377 boost::python::extract<T> extractor(obj);
379 if (!extractor.check())
393 PXR_NAMESPACE_CLOSE_SCOPE
395 #endif // PXR_BASE_TF_PY_UTILS_H
TF_API bool TfPyIsInitialized()
Returns true if python is initialized.
TF_API std::string TfPyGetClassName(boost::python::object const &obj)
Return the name of the class of obj.
Compatibility code for supporting python 2 and 3.
TF_API boost::python::object TfPyEvaluate(std::string const &expr, boost::python::dict const &extraGlobals=boost::python::dict())
Evaluate python expression expr with all the known script modules imported under their standard names...
#define TF_CODING_ERROR(fmt, args)
Issue an internal programming error, but continue execution.
TF_API bool TfPyUnsetenv(const std::string &name)
Remove an environment variable from os.environ.
TF_API void TfPyThrowValueError(std::string const &msg)
Raises a python ValueError and throws a C++ exception.
TF_API void TfPyThrowKeyError(std::string const &msg)
Raises a python KeyError and throws a C++ exception.
TF_API void TfPyPrintError()
Print a standard traceback to sys.stderr and clear the error indicator.
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...
Pointer storage with deletion detection.
TF_API void TfPyThrowIndexError(std::string const &msg)
Raises a python IndexError and throws a C++ exception.
std::string TfPyRepr(T const &t)
Return repr(t).
TF_API void TfPyDumpTraceback()
Print the current python traceback to stdout.
TF_API void TfPyGetStackFrames(std::vector< uintptr_t > *frames)
Populates the vector passed in with pointers to strings containing the python interpreter stack frame...
TF_API boost::python::object TfPyCopyBufferToByteArray(const char *buffer, size_t size)
Create a python bytearray from an input buffer and size.
TF_API void TfPyThrowStopIteration(std::string const &msg)
Raises a python StopIteration exception and throws a C++ exception.
TF_API bool TfPyIsNone(boost::python::object const &obj)
Return true iff obj is None.
bool TfPyEvaluateAndExtract(const std::string &expr, T *t)
Safely evaluates expr and extracts the return object of type T.
TF_API std::string TfPyObjectRepr(boost::python::object const &t)
Return repr(t).
TF_API void TfPyThrowTypeError(std::string const &msg)
Raises a python TypeError and throws a C++ exception.
TF_API boost::python::object TfPyGetClassObject(std::type_info const &type)
Return the python class object for type if type has been wrapped.
Python runtime utilities.
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 void TfPyInitialize()
Starts up the python runtime.
TF_API bool TfPySetenv(const std::string &name, const std::string &value)
Set an environment variable in os.environ.
boost::python::object TfPyCopySequenceToSet(Seq const &seq)
Create a python set from an iterable sequence.
TF_API void TfPyThrowRuntimeError(std::string const &msg)
Raises a python RuntimError and throws a C++ exception.
TF_API std::vector< std::string > TfPyGetTraceback()
Return a vector of strings containing the current python traceback.
Stripped down version of diagnostic.h that doesn't define std::string.
boost::python::dict TfPyCopyMapToDictionary(Map const &map)
Creates a python dictionary from a std::map.