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) ".") 132 TF_API
bool TfPyIsNone(boost::python::object
const &obj);
135 TF_API
bool TfPyIsNone(boost::python::handle<>
const &obj);
138 TF_API
void Tf_PyObjectError(
bool printError);
143 template <
typename T>
144 boost::python::object
TfPyObject(T
const &t,
bool complainOnFailure =
true) {
148 TF_CODING_ERROR(
"Called TfPyObject without python being initialized!");
157 return boost::python::object(t);
158 }
catch (boost::python::error_already_set
const &) {
159 Tf_PyObjectError(complainOnFailure);
160 return boost::python::object();
165 boost::python::object
TfPyObject(PyObject* t,
bool complainOnFailure =
true) {
167 return boost::python::object(boost::python::handle<>(t));
173 TF_API std::string
TfPyObjectRepr(boost::python::object
const &t);
179 template <
typename T>
182 return "<python not initialized>";
188 template <
typename T>
190 std::string result(
"[");
191 typename std::vector<T>::const_iterator i = v.begin();
196 while (i != v.end()) {
208 boost::python::object
210 std::string
const &expr,
211 boost::python::dict
const &extraGlobals = boost::python::dict());
225 TF_API boost::python::object
230 template <
typename T>
231 boost::python::object
238 Tf_PyWrapOnceImpl(boost::python::type_info
const &,
239 std::function<
void()>
const&,
249 template <
typename T>
258 static bool isTypeWrapped =
false;
263 Tf_PyWrapOnceImpl(boost::python::type_id<T>(), wrapFunc, &isTypeWrapped);
271 void Tf_PyLoadScriptModule(std::string
const &name);
277 boost::python::dict d;
278 for (
typename Map::const_iterator i = map.begin(); i != map.end(); ++i)
279 d[i->first] = i->second;
284 boost::python::list TfPyCopySequenceToList(Seq
const &seq) {
286 boost::python::list l;
287 for (
typename Seq::const_iterator i = seq.begin();
300 boost::python::handle<> set{boost::python::allow_null(PySet_New(
nullptr))};
302 boost::python::throw_error_already_set();
304 for (
auto const& item : seq) {
305 boost::python::object obj(item);
306 if (PySet_Add(set.get(), obj.ptr()) == -1) {
307 boost::python::throw_error_already_set();
310 return boost::python::object(set);
314 boost::python::tuple TfPyCopySequenceToTuple(Seq
const &seq) {
315 return boost::python::tuple(TfPyCopySequenceToList(seq));
367 bool TfPySetenv(
const std::string & name,
const std::string & value);
396 TF_API
bool Tf_PyEvaluateWithErrorCheck(
397 const std::string & expr, boost::python::object * obj);
402 template <
typename T>
415 boost::python::object obj;
416 if (!Tf_PyEvaluateWithErrorCheck(expr, &obj))
419 boost::python::extract<T> extractor(obj);
421 if (!extractor.check())
435 PXR_NAMESPACE_CLOSE_SCOPE
437 #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 TfPyPrintError()
Print a standard traceback to sys.stderr and clear the error indicator.
TF_API void TfPyThrowStopIteration(const char *msg)
Raises a Python StopIteration with the given error msg and throws a boost::python::error_already_set ...
TF_API void TfPyThrowValueError(const char *msg)
Raises a Python ValueError with the given error msg and throws a boost::python::error_already_set exc...
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.
std::string TfPyRepr(T const &t)
Return repr(t).
Convenience class for accessing the Python Global Interpreter Lock.
TF_API void TfPyDumpTraceback()
Print the current python traceback to stdout.
TF_API void TfPyThrowTypeError(const char *msg)
Raises a Python TypeError with the given error msg and throws a boost::python::error_already_set exce...
TF_API void TfPyGetStackFrames(std::vector< uintptr_t > *frames)
Populates the vector passed in with pointers to strings containing the python interpreter stack frame...
void TfPyWrapOnce(std::function< void()> const &wrapFunc)
Invokes wrapFunc to wrap type T if T is not already wrapped.
TF_API boost::python::object TfPyCopyBufferToByteArray(const char *buffer, size_t size)
Create a python bytearray from an input buffer and size.
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 boost::python::object TfPyGetClassObject(std::type_info const &type)
Return the python class object for type if type has been wrapped.
TF_API void TfPyThrowKeyError(const char *msg)
Raises a Python KeyError with the given error msg and throws a boost::python::error_already_set excep...
Python runtime utilities.
TF_API void TfPyThrowIndexError(const char *msg)
Raises a Python IndexError with the given error msg and throws a boost::python::error_already_set exc...
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(const char *msg)
Raises a Python RuntimeError with the given error msg and throws a boost::python::error_already_set e...
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.