Library of Assembled Shared Sources
 
Loading...
Searching...
No Matches
Python bindings

Detailed Description

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:

  1. Direct Python Classes: C++ classes that inherit from PyObjectPlus and are designed to be Python-compatible from the start
  2. Shadow Python Classes: Wrapper classes for existing C++ types using PY_SHADOW_CLASS macros - the shadow inherits from PyObjectPlus and wraps the original type

Binding Features:

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.
 

Classes

struct  lass::python::impl::ArgumentTraitsPyObject< util::SharedPtr< T, PyObjectStorage, PyObjectCounter > >
 by PyObjectPtr. More...
 
struct  lass::python::impl::ArgumentTraitsShadowee< util::SharedPtr< T, S, C > >
 by SharedPtr (T may be const). More...
 
struct  lass::python::ArgumentTraits< T >
 by copy. More...
 
struct  lass::python::ArgumentTraits< const T >
 by const copy. More...
 
struct  lass::python::ArgumentTraits< T * >
 by non-const pointer to non-const object. More...
 
struct  lass::python::ArgumentTraits< const T * >
 by non-const pointer to const object. More...
 
struct  lass::python::ArgumentTraits< T & >
 by reference to non-const object. More...
 
struct  lass::python::ArgumentTraits< const T & >
 by reference to const object. More...
 
struct  lass::python::ArgumentTraits< util::SharedPtr< T, S, C > >
 by SharedPtr More...
 
struct  lass::python::ArgumentTraits< const char * >
 by SharedPtr More...
 
class  lass::python::MaybeNone< T >
 Wrapper to type-hint return values in Python that maybe None but not likely. More...
 
class  lass::python::NoNone< T >
 Wrapper to prevent None values being passed to and from Python. More...
 
class  lass::python::PyObjectPlus
 PyObjectPlus. More...
 
struct  lass::python::IsPyObject< T >
 meta function to detect if a type is a PyObject-derived type More...
 
class  lass::python::PyObjectStorage< T, Cascade >
 Recommended storage policy for single PyObject objects, implementation of StoragePolicy concept. More...
 
class  lass::python::PyObjectCounter
 The recommended counter for the pyobject pointers, implementation of CounterPolicy concept. More...
 
struct  lass::python::PyObjectPtr< T >
 templated "typedef" to a python shared pointer More...
 
class  lass::python::Self< T >
 Wrapper to type-hint a return value as Self. More...
 

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
 

Macro Definition Documentation

◆ PY_MODULE_INTEGER_CONSTANTS

#define PY_MODULE_INTEGER_CONSTANTS ( i_module,
... )
Value:
LASS_EXECUTE_BEFORE_MAIN_EX\
( LASS_CONCATENATE( lassExecutePyModuleIntegerConstants_, i_module),\
lass::python::impl::addIntegerConstantsToModule( i_module, #__VA_ARGS__, __VA_ARGS__); )

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 )

Parameters
i_modulethe identifier of a module declared by PY_DECLARE_MODULE
...the identifiers that will be injected

Definition at line 72 of file bulk_add.h.

◆ PY_HEADER

#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.

Remarks
Any declarations coming after this macro are private!

Definition at line 57 of file pyobject_plus.h.

Typedef Documentation

◆ TPyObjPtr

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.

Function Documentation

◆ fromNakedToSharedPtrCast()

template<class T>
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().

◆ fromSharedPtrToNakedCast()

template<class T>
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().

◆ getPyObjectByName()

LASS_PYTHON_DLL TPyObjPtr LASS_CALL lass::python::getPyObjectByName ( const std::string & iName)

retrieve pointer to PyObject by its name in the script.

Returns
new reference to PyObject iName or NULL if iName does not exist (without setting an exception!)

Definition at line 71 of file utilities.cpp.

References fromNakedToSharedPtrCast(), and PY_ENFORCE_POINTER.