All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
pyUtils.h File Reference

Miscellaneous Utilities for dealing with script. More...

+ Include dependency graph for pyUtils.h:

Go to the source code of this file.

Macros

#define TF_PY_REPR_PREFIX
 A macro which expands to the proper repr prefix for a library. More...
 

Functions

TF_API bool TfPyIsInitialized ()
 Returns true if python is initialized. More...
 
TF_API void TfPyThrowIndexError (std::string const &msg)
 Raises a python IndexError and throws a C++ exception. More...
 
TF_API void TfPyThrowRuntimeError (std::string const &msg)
 Raises a python RuntimError and throws a C++ exception. More...
 
TF_API void TfPyThrowStopIteration (std::string const &msg)
 Raises a python StopIteration exception and throws a C++ exception. More...
 
TF_API void TfPyThrowKeyError (std::string const &msg)
 Raises a python KeyError and throws a C++ exception. More...
 
TF_API void TfPyThrowValueError (std::string const &msg)
 Raises a python ValueError and throws a C++ exception. More...
 
TF_API void TfPyThrowTypeError (std::string const &msg)
 Raises a python TypeError and throws a C++ exception. More...
 
TF_API bool TfPyIsNone (boost::python::object const &obj)
 Return true iff obj is None. More...
 
TF_API bool TfPyIsNone (boost::python::handle<> const &obj)
 Return true iff obj is None. More...
 
template<typename T >
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. More...
 
boost::python::object TfPyObject (PyObject *t, bool complainOnFailure=true)
 
TF_API std::string TfPyObjectRepr (boost::python::object const &t)
 Return repr(t). More...
 
template<typename T >
std::string TfPyRepr (T const &t)
 Return repr(t). More...
 
template<typename T >
std::string TfPyRepr (const std::vector< T > &v)
 Return repr(t) for a vector as a python list. More...
 
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. More...
 
TF_API int64_t TfPyNormalizeIndex (int64_t index, uint64_t size, bool throwError=false)
 Return a positive index in the range [0,size). More...
 
TF_API std::string TfPyGetClassName (boost::python::object const &obj)
 Return the name of the class of obj. More...
 
TF_API boost::python::object TfPyGetClassObject (std::type_info const &type)
 Return the python class object for type if type has been wrapped. More...
 
template<typename T >
boost::python::object TfPyGetClassObject ()
 Return the python class object for T if T has been wrapped. More...
 
template<typename T >
void if (!TfPyIsInitialized())
 Invokes wrapFunc to wrap type T if T is not already wrapped. More...
 
 if (isTypeWrapped)
 
template<class Map >
boost::python::dict TfPyCopyMapToDictionary (Map const &map)
 Creates a python dictionary from a std::map. More...
 
template<class Seq >
boost::python::list TfPyCopySequenceToList (Seq const &seq)
 
template<class Seq >
boost::python::object TfPyCopySequenceToSet (Seq const &seq)
 Create a python set from an iterable sequence. More...
 
template<class Seq >
boost::python::tuple TfPyCopySequenceToTuple (Seq const &seq)
 
TF_API std::vector< std::string > TfPyGetTraceback ()
 Return a vector of strings containing the current python traceback. More...
 
TF_API void TfPyGetStackFrames (std::vector< uintptr_t > *frames)
 Populates the vector passed in with pointers to strings containing the python interpreter stack frames. More...
 
TF_API void TfPyDumpTraceback ()
 Print the current python traceback to stdout. More...
 
TF_API bool TfPySetenv (const std::string &name, const std::string &value)
 Set an environment variable in os.environ. More...
 
TF_API bool TfPyUnsetenv (const std::string &name)
 Remove an environment variable from os.environ. More...
 
template<typename T >
bool TfPyEvaluateAndExtract (const std::string &expr, T *t)
 Safely evaluates expr and extracts the return object of type T. More...
 
TF_API void TfPyPrintError ()
 Print a standard traceback to sys.stderr and clear the error indicator. More...
 

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 57 of file pyUtils.h.

Function Documentation

void if ( TfPyIsInitialized())

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

Executing wrapFunc must register T with boost 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 210 of file pyUtils.h.

boost::python::dict TfPyCopyMapToDictionary ( Map const &  map)

Creates a python dictionary from a std::map.

Definition at line 231 of file pyUtils.h.

boost::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 boost::python::error_already_set.

Definition at line 254 of file pyUtils.h.

TF_API void TfPyDumpTraceback ( )

Print the current python traceback to stdout.

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.

Additional globals may be provided in the extraGlobals dictionary.

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 350 of file pyUtils.h.

TF_API std::string TfPyGetClassName ( boost::python::object const &  obj)

Return the name of the class of obj.

TF_API boost::python::object TfPyGetClassObject ( std::type_info const &  type)

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

Otherwise return None.

boost::python::object TfPyGetClassObject ( )

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

Otherwise return None.

Definition at line 188 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 ( boost::python::object const &  obj)

Return true iff obj is None.

TF_API bool TfPyIsNone ( boost::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.

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.

Spams users if complainOnFailure is true and conversion fails.

Definition at line 100 of file pyUtils.h.

TF_API std::string TfPyObjectRepr ( boost::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.

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 136 of file pyUtils.h.

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

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

Definition at line 145 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 ( std::string const &  msg)

Raises a python IndexError and throws a C++ exception.

Intended to be used in wrapper code.

TF_API void TfPyThrowKeyError ( std::string const &  msg)

Raises a python KeyError and throws a C++ exception.

Intended to be used in wrapper code.

TF_API void TfPyThrowRuntimeError ( std::string const &  msg)

Raises a python RuntimError and throws a C++ exception.

Intended to be used in wrapper code.

TF_API void TfPyThrowStopIteration ( std::string const &  msg)

Raises a python StopIteration exception and throws a C++ exception.

Intended to be used in wrapper code.

TF_API void TfPyThrowTypeError ( std::string const &  msg)

Raises a python TypeError and throws a C++ exception.

Intended to be used in wrapper code.

TF_API void TfPyThrowValueError ( std::string const &  msg)

Raises a python ValueError and throws a C++ exception.

Intended to be used in wrapper code.

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.