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);
138Tf_PyWeakrefGetRef(PyObject *ref, PyObject **pobj)
140#if PY_VERSION_HEX >= 0x030D0000
141 return PyWeakref_GetRef(ref, pobj);
143 PyObject *obj = PyWeakref_GetObject(ref);
150 if (obj == Py_None) {
163TF_API
void Tf_PyObjectError(
bool printError);
169pxr_boost::python::object
TfPyObject(T
const &t,
bool complainOnFailure =
true) {
173 TF_CODING_ERROR(
"Called TfPyObject without python being initialized!");
182 return pxr_boost::python::object(t);
183 }
catch (pxr_boost::python::error_already_set
const &) {
184 Tf_PyObjectError(complainOnFailure);
185 return pxr_boost::python::object();
190pxr_boost::python::object
TfPyObject(PyObject* t,
bool complainOnFailure =
true) {
192 return pxr_boost::python::object(pxr_boost::python::handle<>(t));
207 return "<python not initialized>";
215 std::string result(
"[");
216 typename std::vector<T>::const_iterator i = v.begin();
221 while (i != v.end()) {
233pxr_boost::python::object
235 std::string
const &expr,
236 pxr_boost::python::dict
const &extraGlobals = pxr_boost::python::dict());
250TF_API pxr_boost::python::object
256pxr_boost::python::object
263Tf_PyWrapOnceImpl(pxr_boost::python::type_info
const &,
264 std::function<
void()>
const&,
283 static bool isTypeWrapped =
false;
288 Tf_PyWrapOnceImpl(pxr_boost::python::type_id<T>(), wrapFunc, &isTypeWrapped);
296void Tf_PyLoadScriptModule(std::string
const &name);
302 pxr_boost::python::dict d;
303 for (
typename Map::const_iterator i = map.begin(); i != map.end(); ++i)
304 d[i->first] = i->second;
309pxr_boost::python::list TfPyCopySequenceToList(Seq
const &seq) {
311 pxr_boost::python::list l;
312 for (
typename Seq::const_iterator i = seq.begin();
325 pxr_boost::python::handle<> set{pxr_boost::python::allow_null(PySet_New(
nullptr))};
327 pxr_boost::python::throw_error_already_set();
329 for (
auto const& item : seq) {
330 pxr_boost::python::object obj(item);
331 if (PySet_Add(set.get(), obj.ptr()) == -1) {
332 pxr_boost::python::throw_error_already_set();
335 return pxr_boost::python::object(set);
339pxr_boost::python::tuple TfPyCopySequenceToTuple(Seq
const &seq) {
340 return pxr_boost::python::tuple(TfPyCopySequenceToList(seq));
381bool TfPySetenv(
const std::string & name,
const std::string & value);
410TF_API
bool Tf_PyEvaluateWithErrorCheck(
411 const std::string & expr, pxr_boost::python::object * obj);
429 pxr_boost::python::object obj;
430 if (!Tf_PyEvaluateWithErrorCheck(expr, &obj))
433 pxr_boost::python::extract<T> extractor(obj);
435 if (!extractor.check())
449PXR_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.