7#ifndef PXR_BASE_TF_PY_UTILS_H
8#define PXR_BASE_TF_PY_UTILS_H
18#include "pxr/base/tf/preprocessorUtilsLite.h"
21#include "pxr/base/tf/pyLock.h"
22#include "pxr/base/tf/api.h"
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"
35PXR_NAMESPACE_OPEN_SCOPE
42#define TF_PY_REPR_PREFIX \
43 std::string(TF_PP_STRINGIZE(MFB_PACKAGE_MODULE) ".")
118TF_API
bool TfPyIsNone(pxr_boost::python::handle<>
const &obj);
121TF_API
void Tf_PyObjectError(
bool printError);
127pxr_boost::python::object
TfPyObject(T
const &t,
bool complainOnFailure =
true) {
131 TF_CODING_ERROR(
"Called TfPyObject without python being initialized!");
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();
148pxr_boost::python::object
TfPyObject(PyObject* t,
bool complainOnFailure =
true) {
150 return pxr_boost::python::object(pxr_boost::python::handle<>(t));
165 return "<python not initialized>";
173 std::string result(
"[");
174 typename std::vector<T>::const_iterator i = v.begin();
179 while (i != v.end()) {
191pxr_boost::python::object
193 std::string
const &expr,
194 pxr_boost::python::dict
const &extraGlobals = pxr_boost::python::dict());
208TF_API pxr_boost::python::object
214pxr_boost::python::object
221Tf_PyWrapOnceImpl(pxr_boost::python::type_info
const &,
222 std::function<
void()>
const&,
241 static bool isTypeWrapped =
false;
246 Tf_PyWrapOnceImpl(pxr_boost::python::type_id<T>(), wrapFunc, &isTypeWrapped);
254void Tf_PyLoadScriptModule(std::string
const &name);
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;
267pxr_boost::python::list TfPyCopySequenceToList(Seq
const &seq) {
269 pxr_boost::python::list l;
270 for (
typename Seq::const_iterator i = seq.begin();
283 pxr_boost::python::handle<> set{pxr_boost::python::allow_null(PySet_New(
nullptr))};
285 pxr_boost::python::throw_error_already_set();
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();
293 return pxr_boost::python::object(set);
297pxr_boost::python::tuple TfPyCopySequenceToTuple(Seq
const &seq) {
298 return pxr_boost::python::tuple(TfPyCopySequenceToList(seq));
339bool TfPySetenv(
const std::string & name,
const std::string & value);
368TF_API
bool Tf_PyEvaluateWithErrorCheck(
369 const std::string & expr, pxr_boost::python::object * obj);
387 pxr_boost::python::object obj;
388 if (!Tf_PyEvaluateWithErrorCheck(expr, &obj))
391 pxr_boost::python::extract<T> extractor(obj);
393 if (!extractor.check())
407PXR_NAMESPACE_CLOSE_SCOPE
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.
pxr_boost::python::object TfPyGetClassObject()
Return the python class object for T if T has been wrapped.
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.
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.
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.
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.
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).
Convenience class for accessing the Python Global Interpreter Lock.
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.
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...
Pointer storage with deletion detection.