Comprehensive C++ to Python binding library.
The lass::python namespace provides a comprehensive Python binding system that allows easy integration between C++ code and Python scripts. Lass Python bindings support both direct Python-aware C++ classes and shadow Python classes for existing C++ types.
Key Components:
Class Export Approaches:
Binding Features:
__add__, __str__, __eq__, etc.)To define these Python bindings, you typically use macros that define an export on a module or class. These macro names follow a consistent grammar, see Python Macro Name Conventions.
Topics | |
| Class Definitions | |
| Defining Python classes from C++ with methods, properties, operators, and nested types. | |
| Enum Definitions | |
| Defining first-class enum types in Python from C++ enums. | |
| Python Exceptions | |
| Handling Python exceptions in C++, and translating C++ exceptions to Python. | |
| PyExportTraits | |
| Traits to convert between C++ and Python types. | |
| Module Definitions | |
| Defining Python modules from C++ with classes, functions, and enums. | |
| Python Iterators | |
| Exposing iterators to Python. | |
| Python Macro Name Conventions | |
| Every export macro is composed as: | |
| Special Methods | |
| Predefined constants for Python special methods (magic methods) that can be used as method names in class export macros. | |
| Python Shadow Classes | |
| Export C++ classes that don't derive from lass::python::PyObjectPlus. | |
| Subscript Protocol | |
| Support for index- and slice-based access, assignment and deletion. | |
Macros | |
| #define | PY_MODULE_INTEGER_CONSTANTS(i_module, ...) |
| Injects a number of constants into a Python module. | |
| #define | PY_HEADER(t_parentClass) |
| Place as first line of your Pythonized class. | |
Typedefs | |
| typedef PyObjectPtr< PyObject >::Type | lass::python::TPyObjPtr |
| PyObjectPtr to a PyObject. | |
Functions | |
| template<class T> | |
| lass::util::SharedPtr< T, PyObjectStorage, PyObjectCounter > | lass::python::fromNakedToSharedPtrCast (PyObject *object) |
| fromNakedToSharedPtrCast. | |
| template<class T> | |
| PyObject * | lass::python::fromSharedPtrToNakedCast (const util::SharedPtr< T, PyObjectStorage, PyObjectCounter > &object) |
| fromSharedPtrToNakedCast. | |
| template<typename Out, typename In> | |
| Out | lass::python::staticPyCast (const In &in) |
| static_cast for python pointers | |
| template<typename Out, typename In> | |
| Out | lass::python::dynamicPyCast (const In &in) |
| dynamic_cast for python pointers | |
| TPyObjPtr | lass::python::getPyObjectByName (const std::string &iName) |
| retrieve pointer to PyObject by its name in the script. | |
| void | lass::python::putenv (const std::string &key, const std::string &value) |
| os.environ[key] = value | |
| void | lass::python::putenv (const char *key, const char *value) |
| os.environ[key] = value | |
| #define PY_MODULE_INTEGER_CONSTANTS | ( | i_module, | |
| ... ) |
Injects a number of constants into a Python module.
The number of arguments to the macro is variable but is limited to the number specified during the build of lass. The name of the identifier in Python is the same as the one in C++. This makes export of enums more efficient.
An example is: enum Foo { the = 0x1, fan = 0x2, has = 0x5, started = 0x7 };
PY_MODULE_INTEGER_CONSTANTS( theFanModule, the, fan, has, started )
| i_module | the identifier of a module declared by PY_DECLARE_MODULE |
| ... | the identifiers that will be injected |
Definition at line 72 of file bulk_add.h.
| #define PY_HEADER | ( | t_parentClass | ) |
Place as first line of your Pythonized class.
For t_parentClass use the C++ class from which you wish the python object inherits.
t_parentClass must also be a Pythonized class or use lass::python::PyObjectPlus as default.
Definition at line 57 of file pyobject_plus.h.
| typedef PyObjectPtr<PyObject>::Type lass::python::TPyObjPtr |
PyObjectPtr to a PyObject.
Due to historical reasons, the names PyObjectPtr and TPyObjPtr are a bit confusing: TPyObjPtr is a PyObjectPtr for PyObject, while PyObjectPtr is the general one.
Definition at line 260 of file pyobject_ptr.h.
| lass::util::SharedPtr< T, PyObjectStorage, PyObjectCounter > lass::python::fromNakedToSharedPtrCast | ( | PyObject * | object | ) |
fromNakedToSharedPtrCast.
Helper function casting a PyObject coming from the Python interface to a SharedPtr object for use in C++. Reference counts are taken care of (it's icremented by one).
Definition at line 271 of file pyobject_ptr.h.
Referenced by lass::python::impl::ClassDefinition::establishMagicalBackLinks, and getPyObjectByName().
| PyObject * lass::python::fromSharedPtrToNakedCast | ( | const util::SharedPtr< T, PyObjectStorage, PyObjectCounter > & | object | ) |
fromSharedPtrToNakedCast.
Helper function casting an object used in C++ for use in Python. The key operation done here is to take care of the reference counting. Failing to use this function may yield unexpected reference count.
Definition at line 288 of file pyobject_ptr.h.
Referenced by lass::python::impl::construct(), lass::python::IntEnumDefinition< EnumType >::doFreezeDefinition(), lass::python::EnumDefinition< EnumType, ValueType >::freezeEnumerators(), and lass::python::impl::handleException().
| LASS_PYTHON_DLL TPyObjPtr LASS_CALL lass::python::getPyObjectByName | ( | const std::string & | iName | ) |
retrieve pointer to PyObject by its name in the script.
Definition at line 71 of file utilities.cpp.
References fromNakedToSharedPtrCast(), and PY_ENFORCE_POINTER.