HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
pyUtils.h File Reference
#include "pxr/pxr.h"
#include "pxr/base/tf/refPtr.h"
#include "pxr/base/tf/weakPtr.h"
#include "pxr/base/tf/diagnosticLite.h"
#include "pxr/base/tf/preprocessorUtilsLite.h"
#include "pxr/base/tf/pySafePython.h"
#include "pxr/base/tf/pyInterpreter.h"
#include "pxr/base/tf/pyLock.h"
#include "pxr/base/tf/api.h"
#include <functional>
#include <typeinfo>
#include <string>
#include <vector>
#include <hboost/python/dict.hpp>
#include <hboost/python/extract.hpp>
#include <hboost/python/handle.hpp>
#include <hboost/python/object.hpp>
#include <hboost/python/type_id.hpp>
+ Include dependency graph for pyUtils.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define TF_PY_REPR_PREFIX
 

Functions

TF_API bool TfPyIsInitialized ()
 Returns true if python is initialized. More...
 
TF_API void TfPyThrowIndexError (const char *msg)
 
void TfPyThrowIndexError (std::string const &msg)
 
TF_API void TfPyThrowRuntimeError (const char *msg)
 
void TfPyThrowRuntimeError (std::string const &msg)
 
TF_API void TfPyThrowStopIteration (const char *msg)
 
void TfPyThrowStopIteration (std::string const &msg)
 
TF_API void TfPyThrowKeyError (const char *msg)
 
void TfPyThrowKeyError (std::string const &msg)
 
TF_API void TfPyThrowValueError (const char *msg)
 
void TfPyThrowValueError (std::string const &msg)
 
TF_API void TfPyThrowTypeError (const char *msg)
 
void TfPyThrowTypeError (std::string const &msg)
 
TF_API bool TfPyIsNone (hboost::python::object const &obj)
 Return true iff obj is None. More...
 
TF_API bool TfPyIsNone (hboost::python::handle<> const &obj)
 Return true iff obj is None. More...
 
TF_API void Tf_PyObjectError (bool printError)
 
template<typename T >
hboost::python::object TfPyObject (T const &t, bool complainOnFailure=true)
 
hboost::python::object TfPyObject (PyObject *t, bool complainOnFailure=true)
 
TF_API std::string TfPyObjectRepr (hboost::python::object const &t)
 
template<typename T >
std::string TfPyRepr (T const &t)
 
template<typename T >
std::string TfPyRepr (const std::vector< T > &v)
 Return repr(t) for a vector as a python list. More...
 
TF_API hboost::python::object TfPyEvaluate (std::string const &expr, hboost::python::dict const &extraGlobals=hboost::python::dict())
 
TF_API int64_t TfPyNormalizeIndex (int64_t index, uint64_t size, bool throwError=false)
 
TF_API std::string TfPyGetClassName (hboost::python::object const &obj)
 Return the name of the class of obj. More...
 
TF_API hboost::python::object TfPyGetClassObject (std::type_info const &type)
 
template<typename T >
hboost::python::object TfPyGetClassObject ()
 
TF_API void Tf_PyWrapOnceImpl (hboost::python::type_info const &, std::function< void()> const &, bool *)
 
template<typename T >
void TfPyWrapOnce (std::function< void()> const &wrapFunc)
 
TF_API void Tf_PyLoadScriptModule (std::string const &name)
 
template<class Map >
hboost::python::dict TfPyCopyMapToDictionary (Map const &map)
 Creates a python dictionary from a std::map. More...
 
template<class Seq >
hboost::python::list TfPyCopySequenceToList (Seq const &seq)
 
template<class Seq >
hboost::python::object TfPyCopySequenceToSet (Seq const &seq)
 
template<class Seq >
hboost::python::tuple TfPyCopySequenceToTuple (Seq const &seq)
 
TF_API hboost::python::object TfPyCopyBufferToByteArray (const char *buffer, size_t size)
 
TF_API std::vector< std::stringTfPyGetTraceback ()
 
TF_API void TfPyGetStackFrames (std::vector< uintptr_t > *frames)
 
TF_API void TfPyDumpTraceback ()
 Print the current python traceback to stdout. More...
 
TF_API bool TfPySetenv (const std::string &name, const std::string &value)
 
TF_API bool TfPyUnsetenv (const std::string &name)
 
TF_API bool Tf_PyEvaluateWithErrorCheck (const std::string &expr, hboost::python::object *obj)
 
template<typename T >
bool TfPyEvaluateAndExtract (const std::string &expr, T *t)
 
TF_API void TfPyPrintError ()
 

Detailed Description

Miscellaneous Utilities for dealing with script.

Definition in file pyUtils.h.

Macro Definition Documentation

#define TF_PY_REPR_PREFIX

A macro which expands to the proper repr prefix for a library. This is the "canonical" name of the module that the system uses to identify it followed by a '.'. This can be used in the implementation of repr

Definition at line 59 of file pyUtils.h.

Function Documentation

TF_API bool Tf_PyEvaluateWithErrorCheck ( const std::string expr,
hboost::python::object *  obj 
)
TF_API void Tf_PyLoadScriptModule ( std::string const name)

Load the python module moduleName. This is used by some low-level infrastructure code to load python wrapper modules corresponding to C++ shared libraries when they are needed. It should generally not need to be called from normal user code.

TF_API void Tf_PyObjectError ( bool  printError)
TF_API void Tf_PyWrapOnceImpl ( hboost::python::type_info const ,
std::function< void()> const ,
bool *   
)
TF_API hboost::python::object TfPyCopyBufferToByteArray ( const char *  buffer,
size_t  size 
)

Create a python bytearray from an input buffer and size.

If a size of zero is passed in this function will return a valid python bytearray of size zero.

An invalid object handle is returned on failure.

template<class Map >
hboost::python::dict TfPyCopyMapToDictionary ( Map const map)

Creates a python dictionary from a std::map.

Definition at line 275 of file pyUtils.h.

template<class Seq >
hboost::python::list TfPyCopySequenceToList ( Seq const seq)

Definition at line 284 of file pyUtils.h.

template<class Seq >
hboost::python::object TfPyCopySequenceToSet ( Seq const seq)

Create a python set from an iterable sequence.

If Seq::value_type is not hashable, TypeError is raised via throwing hboost::python::error_already_set.

Definition at line 298 of file pyUtils.h.

template<class Seq >
hboost::python::tuple TfPyCopySequenceToTuple ( Seq const seq)

Definition at line 314 of file pyUtils.h.

TF_API void TfPyDumpTraceback ( )

Print the current python traceback to stdout.

TF_API hboost::python::object TfPyEvaluate ( std::string const expr,
hboost::python::dict const extraGlobals = hboost::python::dict() 
)

Evaluate python expression expr with all the known script modules imported under their standard names. Additional globals may be provided in the extraGlobals dictionary.

template<typename T >
bool TfPyEvaluateAndExtract ( const std::string expr,
T *  t 
)

Safely evaluates expr and extracts the return object of type T. If successful, returns true and sets *t to the return value, otherwise returns false.

Definition at line 403 of file pyUtils.h.

TF_API std::string TfPyGetClassName ( hboost::python::object const obj)

Return the name of the class of obj.

TF_API hboost::python::object TfPyGetClassObject ( std::type_info const type)

Return the python class object for type if type has been wrapped. Otherwise return None.

template<typename T >
hboost::python::object TfPyGetClassObject ( )

Return the python class object for T if T has been wrapped. Otherwise return None.

Definition at line 232 of file pyUtils.h.

TF_API void TfPyGetStackFrames ( std::vector< uintptr_t > *  frames)

Populates the vector passed in with pointers to strings containing the python interpreter stack frames. Note that TfPyGetStackFrames allocates these strings on the heap and its the caller's responsibility to free them.

TF_API std::vector<std::string> TfPyGetTraceback ( )

Return a vector of strings containing the current python traceback.

The vector contains the same strings that python's traceback.format_stack() returns.

TF_API bool TfPyIsInitialized ( )

Returns true if python is initialized.

TF_API bool TfPyIsNone ( hboost::python::object const obj)

Return true iff obj is None.

TF_API bool TfPyIsNone ( hboost::python::handle<> const obj)

Return true iff obj is None.

TF_API int64_t TfPyNormalizeIndex ( int64_t  index,
uint64_t  size,
bool  throwError = false 
)

Return a positive index in the range [0,size). If throwError is true, this will throw an index error if the resulting index is out of range.

template<typename T >
hboost::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. Spams users if complainOnFailure is true and conversion fails.

Definition at line 144 of file pyUtils.h.

hboost::python::object TfPyObject ( PyObject *  t,
bool  complainOnFailure = true 
)
inline

Definition at line 165 of file pyUtils.h.

TF_API std::string TfPyObjectRepr ( hboost::python::object const t)

Return repr(t).

Calls PyObject_Repr on the given python object.

TF_API void TfPyPrintError ( )

Print a standard traceback to sys.stderr and clear the error indicator. If the error is a KeyboardInterrupt then this does nothing. Call this function only when the error indicator is set.

template<typename T >
std::string TfPyRepr ( T const t)

Return repr(t).

Converts t to its equivalent python object and then calls PyObject_Repr on that.

Definition at line 180 of file pyUtils.h.

template<typename T >
std::string TfPyRepr ( const std::vector< T > &  v)

Return repr(t) for a vector as a python list.

Definition at line 189 of file pyUtils.h.

TF_API bool TfPySetenv ( const std::string name,
const std::string value 
)

Set an environment variable in os.environ.

This function is equivalent to

def PySetenv(name, value):
try:
import os
os.environ[name] = value
return True
except:
return False

Calling this function without first initializing Python is an error and returns false.

Note that this function will import the os module, causing os.environ to be poputated. All modifications to the environment after os has been imported must be made with this function or TfSetenv if it important that they appear in os.environ.

TF_API void TfPyThrowIndexError ( const char *  msg)

Raises a Python IndexError with the given error msg and throws a hboost::python::error_already_set exception. Callers must hold the GIL before calling this function.

void TfPyThrowIndexError ( std::string const msg)
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 71 of file pyUtils.h.

TF_API void TfPyThrowKeyError ( const char *  msg)

Raises a Python KeyError with the given error msg and throws a hboost::python::error_already_set exception. Callers must hold the GIL before calling this function.

void TfPyThrowKeyError ( std::string const msg)
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 104 of file pyUtils.h.

TF_API void TfPyThrowRuntimeError ( const char *  msg)

Raises a Python RuntimeError with the given error msg and throws a hboost::python::error_already_set exception. Callers must hold the GIL before calling this function.

void TfPyThrowRuntimeError ( std::string const msg)
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 82 of file pyUtils.h.

TF_API void TfPyThrowStopIteration ( const char *  msg)

Raises a Python StopIteration with the given error msg and throws a hboost::python::error_already_set exception. Callers must hold the GIL before calling this function.

void TfPyThrowStopIteration ( std::string const msg)
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 93 of file pyUtils.h.

TF_API void TfPyThrowTypeError ( const char *  msg)

Raises a Python TypeError with the given error msg and throws a hboost::python::error_already_set exception. Callers must hold the GIL before calling this function.

void TfPyThrowTypeError ( std::string const msg)
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 126 of file pyUtils.h.

TF_API void TfPyThrowValueError ( const char *  msg)

Raises a Python ValueError with the given error msg and throws a hboost::python::error_already_set exception. Callers must hold the GIL before calling this function.

void TfPyThrowValueError ( std::string const msg)
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 115 of file pyUtils.h.

TF_API bool TfPyUnsetenv ( const std::string name)

Remove an environment variable from os.environ.

This function is equivalent to

def PyUnsetenv(name):
try:
import os
if name in os.environ:
del os.environ[name]
return True
except:
return False

Calling this function without first initializing Python is an error and returns false.

Note that this function will import the os module, causing os.environ to be poputated. All modifications to the environment after os has been imported must be made with this function or TfUnsetenv if it important that they appear in os.environ.

template<typename T >
void TfPyWrapOnce ( std::function< void()> const wrapFunc)

Invokes wrapFunc to wrap type T if T is not already wrapped.

Executing wrapFunc must register T with hboost python. Otherwise, wrapFunc may be executed more than once.

TfPyWrapOnce will acquire the GIL prior to invoking wrapFunc. Does not invoke wrapFunc if Python has not been initialized.

Definition at line 251 of file pyUtils.h.