Library of Assembled Shared Sources
lass::python Namespace Reference

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 native Python-aware classes and shadow classes for existing C++ types.

Key Components:

  • ModuleDefinition: Macros to create and configure Python modules
  • ClassDefinition: Macros to export C++ classes as Python types
  • PyObjectPlus: Base class for Python-aware C++ objects
  • Shadow Classes: Wrapper system for existing non-Python-aware C++ classes
  • PyExportTraits: Type conversion system between C++ and Python objects
  • Overload Resolution: Automatic dispatcher generation for overloaded functions/methods

Class Export Approaches:

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

Binding Features:

  • Multiple constructor overloads with automatic dispatching
  • Method overloading mixing member functions and free functions
  • Property binding with getter/setter pairs or direct member access
  • Python special methods (__add__, __str__, __eq__, etc.)
  • Static methods and class constants
  • Nested classes and enumerations
  • Automatic type conversion and argument validation
  • Exception mapping between C++ and Python

Namespaces

namespace  methods
 Predefined constants for Python special methods (magic methods) that can be used as method names in class export macros.
 

Data Structures

struct  ArgumentTraits
 by copy. More...
 
struct  ArgumentTraits< const char * >
 by SharedPtr More...
 
struct  ArgumentTraits< const T & >
 by reference to const object. More...
 
struct  ArgumentTraits< const T * >
 by non-const pointer to const object. More...
 
struct  ArgumentTraits< const T >
 by const copy. More...
 
struct  ArgumentTraits< T & >
 by reference to non-const object. More...
 
struct  ArgumentTraits< T * >
 by non-const pointer to non-const object. More...
 
struct  ArgumentTraits< util::SharedPtr< T, S, C > >
 by SharedPtr More...
 
class  ContainerRangeView
 View that adapts a C++ Range (begin()/end()) to a PyIteratorRange. More...
 
class  EnumDefinition
 Definition of a general enum.Enum-derived enum type in Python. More...
 
class  EnumDefinitionBase
 Base class of all enum definitions. More...
 
class  FreeIndexedRangeView
 View that adapts index-based element access to a PyIteratorRange. More...
 
class  FreeRangeView
 View that adapts free iterator accessors to a PyIteratorRange. More...
 
class  IndexedRangeView
 View that adapts index-based element access to a PyIteratorRange. More...
 
class  IntEnumDefinition
 Definition of an enum.IntEnum-derived enum type in Python. More...
 
class  IntFlagDefinition
 Definition of an enum.IntFlag-derived enum type in Python. More...
 
struct  IsPyObject
 meta function to detect if a type is a PyObject-derived type More...
 
class  LockGIL
 acquire the GIL for the current scope. More...
 
class  MaybeNone
 Wrapper to type-hint return values in Python that maybe None but not likely. More...
 
class  MemberRangeView
 View that adapts member iterator accessors to a PyIteratorRange. More...
 
class  ModuleDefinition
 Definition of a Python module. More...
 
class  MultiCallback
 MultiCallback. More...
 
class  NoNone
 Wrapper to prevent None values being passed to and from Python. More...
 
struct  PyExportTraits
 by copy, general case assumes shadow type or PyObjectPlus based type. More...
 
struct  PyExportTraits< const char16_t * >
 UTF-16 const char16_t* string is mapped to Python str | None, as it can be null. More...
 
struct  PyExportTraits< const char32_t * >
 UTF-32 const char32_t* string is mapped to Python str | None, as it can be null. More...
 
struct  PyExportTraits< const T >
 constant objects can only be build. More...
 
struct  PyExportTraits< ContainerRangeView< SelfType, ValueType > >
 Builds a PyIteratorRange from a ContainerRangeView. More...
 
struct  PyExportTraits< double >
 double is mapped to Python float type, which is also a C double. More...
 
struct  PyExportTraits< float >
 float is mapped to Python float type, which is a C double. More...
 
struct  PyExportTraits< FreeIndexedRangeView< SelfType, ValueType, SizeType, AtFunc, SizeFunc > >
 Builds a PyIteratorRange from a FreeIndexedRangeView. More...
 
struct  PyExportTraits< FreeRangeView< SelfType, ValueType, GetIterator > >
 Builds a PyIteratorRange from a FreeRangeView. More...
 
struct  PyExportTraits< IndexedRangeView< SelfType, ValueType, SizeType, AtMethod, SizeMethod > >
 Builds a PyIteratorRange from a IndexedRangeView. More...
 
struct  PyExportTraits< long double >
 long double is mapped to Python float type, which is a C double. More...
 
struct  PyExportTraits< MaybeNone< std::optional< T > > >
 MaybeNone<std::optional<T>> type-hints a type as T | MaybeNone More...
 
struct  PyExportTraits< MaybeNone< std::shared_ptr< T > > >
 MaybeNone<std::shared_ptr<T>> type-hints a type as T | MaybeNone More...
 
struct  PyExportTraits< MaybeNone< T * > >
 MaybeNone<T*> type-hints a type as T | MaybeNone More...
 
struct  PyExportTraits< MaybeNone< util::SharedPtr< T, S, C > > >
 MaybeNone<util::SharedPtr<T>> type-hints a type as T | MaybeNone More...
 
struct  PyExportTraits< MemberRangeView< SelfType, ValueType, GetIterator > >
 Builds a PyIteratorRange from a MemberRangeView. More...
 
struct  PyExportTraits< NoNone< std::shared_ptr< T > > >
 NoNone<std::shared_ptr<T>> type-hints as T and refuses None as value. More...
 
struct  PyExportTraits< NoNone< T * > >
 NoNone<T*> type-hints as T and refuses None as value. More...
 
struct  PyExportTraits< NoNone< util::SharedPtr< T, S, C > > >
 Type-hints NoNone<util::SharedPtr<T>> as T and refuses None as value. More...
 
struct  PyExportTraits< prim::Aabb2D< T, MMP > >
 Maps prim::Aabb2D<T> to a Python tuple of two tuples of two elements of type T. More...
 
struct  PyExportTraits< prim::Aabb3D< T, MMP > >
 Maps prim::Aabb3D<T> to a Python tuple of two tuples of three elements of type T. More...
 
struct  PyExportTraits< prim::ColorRGBA >
 Maps prim::ColorRGBA to a Python tuple of four elements of type float. More...
 
struct  PyExportTraits< prim::IndexTriangle >
 Maps prim::IndexTriangle to a Python tuple of three index vertices. More...
 
struct  PyExportTraits< prim::LineSegment2D< T, PP > >
 Maps prim::LineSegment2D<T> to a Python tuple of two tuples of two elements of type T. More...
 
struct  PyExportTraits< prim::LineSegment3D< T, PP > >
 Maps prim::LineSegment3D<T> to a Python tuple of two tuples of three elements of type T. More...
 
struct  PyExportTraits< prim::Point2D< T > >
 Maps prim::Point2D<T> to a Python tuple of two elements of type T. More...
 
struct  PyExportTraits< prim::Point3D< T > >
 Maps prim::Point3D<T> to a Python tuple of three elements of type T. More...
 
struct  PyExportTraits< prim::SimplePolygon2D< T, DP > >
 Maps prim::SimplePolygon2D<T> to a Python sequence of tuples of two elements of type T. More...
 
struct  PyExportTraits< prim::SimplePolygon3D< T, DP > >
 Maps prim::SimplePolygon3D<T> to a Python sequence of tuples of three elements of type T. More...
 
struct  PyExportTraits< prim::Transformation2D< T > >
 Maps prim::Transformation2D<T> to a Python tuple of three tuples of three elements of type T. More...
 
struct  PyExportTraits< prim::Transformation3D< T > >
 Maps prim::Transformation3D<T> to a Python tuple of four tuples of four elements of type T. More...
 
struct  PyExportTraits< prim::Vector2D< T > >
 Maps prim::Vector2D<T> to a Python tuple of two elements of type T. More...
 
struct  PyExportTraits< prim::Vector3D< T > >
 Maps prim::Vector3D<T> to a Python tuple of three elements of type T. More...
 
struct  PyExportTraits< prim::Vector4D< T > >
 Maps prim::Vector4D<T> to a Python tuple of four elements of type T. More...
 
struct  PyExportTraits< prim::XY >
 Maps prim::XY to a Python string literal "x" or "y". More...
 
struct  PyExportTraits< prim::XYZ >
 Maps prim::XYZ to a Python string literal "x", "y" or "z". More...
 
struct  PyExportTraits< prim::XYZW >
 Maps prim::XYZW to a Python string literal "x", "y", "z" or "w". More...
 
struct  PyExportTraits< PyIteratorRange * >
 Passes a PyIteratorRange to Python. More...
 
struct  PyExportTraits< Self< T > >
 Self<T> type-hints as Self. More...
 
struct  PyExportTraits< signed char >
 signed char is mapped to Python int More...
 
struct  PyExportTraits< signed int >
 signed int is mapped to Python int More...
 
struct  PyExportTraits< signed long >
 signed long is mapped to Python int More...
 
struct  PyExportTraits< signed short >
 signed short is mapped to Python int More...
 
struct  PyExportTraits< std::basic_string_view< T > >
 std::basic_string_view<T> is mapped to Python str More...
 
struct  PyExportTraits< std::chrono::duration< Rep, Period > >
 std::chrono::duration is mapped on datetime.timedelta by copy. More...
 
struct  PyExportTraits< std::chrono::file_clock::time_point >
 std::chrono::file_clock::time_point is mapped on a timezone-aware datetime.datetime instance by copy, in UTC. More...
 
struct  PyExportTraits< std::chrono::gps_clock::time_point >
 std::chrono::gps_clock::time_point is mapped on a timezone-aware datetime.datetime instance by copy, in UTC. More...
 
struct  PyExportTraits< std::chrono::tai_clock::time_point >
 std::chrono::tai_clock::time_point is mapped on a timezone-aware datetime.datetime instance by copy, in UTC. More...
 
struct  PyExportTraits< std::chrono::time_point< std::chrono::system_clock > >
 std::chrono::time_point<std::chrono::system_clock> is mapped on a timezone-unaware datetime.datetime instance by copy, in local time. More...
 
struct  PyExportTraits< std::chrono::utc_clock::time_point >
 std::chrono::utc_clock::time_point is mapped on a timezone-aware datetime.datetime instance by copy, in UTC. More...
 
struct  PyExportTraits< std::chrono::year_month_day >
 std::chrono::year_month_day is mapped on a datetime.date instance by copy. More...
 
struct  PyExportTraits< std::complex< T > >
 std::complex<T> is always mapped to Python complex type. More...
 
struct  PyExportTraits< std::function< R(Args...)> >
 Bidirectional wrapper between Callable objects and std::function. More...
 
struct  PyExportTraits< std::optional< T > >
 Uses None to represent a std::optional without value. More...
 
struct  PyExportTraits< std::pair< T1, T2 > >
 std::pair translates to a tuple by copy. More...
 
struct  PyExportTraits< std::shared_ptr< T > >
 std::shared_ptr assumes shadow types. More...
 
struct  PyExportTraits< std::tuple< T... > >
 std::tuple translates to a tuple by copy. More...
 
struct  PyExportTraits< std::u16string >
 UTF-16 std::u16string is mapped to str More...
 
struct  PyExportTraits< std::u32string >
 UTF-32 std::u32string is mapped to str More...
 
struct  PyExportTraits< std::unique_ptr< T, Deleter > >
 std::unique_ptr assumes shadow types More...
 
struct  PyExportTraits< TPyObjPtr >
 A shared PyObject pointer is mapped to Any in Python. More...
 
struct  PyExportTraits< unsigned char >
 unsigned char is mapped to Python int More...
 
struct  PyExportTraits< unsigned int >
 unsigned int is mapped to Python int More...
 
struct  PyExportTraits< unsigned long >
 unsigned long is mapped to Python int More...
 
struct  PyExportTraits< unsigned short >
 unsigned short is mapped to Python int More...
 
struct  PyExportTraits< util::Callback0 >
 Bidirectional mapping between util::Callback0 and a Python callable object. More...
 
struct  PyExportTraits< util::Callback10< P1, P2, P3, P4, P5, P6, P7, P8, P9, P10 > >
 Bidirectional mapping between util::Callback10 and a Python callable object. More...
 
struct  PyExportTraits< util::Callback11< P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11 > >
 Bidirectional mapping between util::Callback11 and a Python callable object. More...
 
struct  PyExportTraits< util::Callback12< P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12 > >
 Bidirectional mapping between util::Callback12 and a Python callable object. More...
 
struct  PyExportTraits< util::Callback13< P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12, P13 > >
 Bidirectional mapping between util::Callback13 and a Python callable object. More...
 
struct  PyExportTraits< util::Callback14< P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12, P13, P14 > >
 Bidirectional mapping between util::Callback14 and a Python callable object. More...
 
struct  PyExportTraits< util::Callback15< P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12, P13, P14, P15 > >
 Bidirectional mapping between util::Callback15 and a Python callable object. More...
 
struct  PyExportTraits< util::Callback1< P1 > >
 Bidirectional mapping between util::Callback1 and a Python callable object. More...
 
struct  PyExportTraits< util::Callback2< P1, P2 > >
 Bidirectional mapping between util::Callback2 and a Python callable object. More...
 
struct  PyExportTraits< util::Callback3< P1, P2, P3 > >
 Bidirectional mapping between util::Callback3 and a Python callable object. More...
 
struct  PyExportTraits< util::Callback4< P1, P2, P3, P4 > >
 Bidirectional mapping between util::Callback4 and a Python callable object. More...
 
struct  PyExportTraits< util::Callback5< P1, P2, P3, P4, P5 > >
 Bidirectional mapping between util::Callback5 and a Python callable object. More...
 
struct  PyExportTraits< util::Callback6< P1, P2, P3, P4, P5, P6 > >
 Bidirectional mapping between util::Callback6 and a Python callable object. More...
 
struct  PyExportTraits< util::Callback7< P1, P2, P3, P4, P5, P6, P7 > >
 Bidirectional mapping between util::Callback7 and a Python callable object. More...
 
struct  PyExportTraits< util::Callback8< P1, P2, P3, P4, P5, P6, P7, P8 > >
 Bidirectional mapping between util::Callback8 and a Python callable object. More...
 
struct  PyExportTraits< util::Callback9< P1, P2, P3, P4, P5, P6, P7, P8, P9 > >
 Bidirectional mapping between util::Callback9 and a Python callable object. More...
 
struct  PyExportTraits< util::CallbackR0< R > >
 Bidirectional mapping between util::CallbackR0 and a Python callable object. More...
 
struct  PyExportTraits< util::CallbackR10< R, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10 > >
 Bidirectional mapping between util::CallbackR10 and a Python callable object. More...
 
struct  PyExportTraits< util::CallbackR11< R, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11 > >
 Bidirectional mapping between util::CallbackR11 and a Python callable object. More...
 
struct  PyExportTraits< util::CallbackR12< R, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12 > >
 Bidirectional mapping between util::CallbackR12 and a Python callable object. More...
 
struct  PyExportTraits< util::CallbackR13< R, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12, P13 > >
 Bidirectional mapping between util::CallbackR13 and a Python callable object. More...
 
struct  PyExportTraits< util::CallbackR14< R, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12, P13, P14 > >
 Bidirectional mapping between util::CallbackR14 and a Python callable object. More...
 
struct  PyExportTraits< util::CallbackR15< R, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12, P13, P14, P15 > >
 Bidirectional mapping between util::CallbackR15 and a Python callable object. More...
 
struct  PyExportTraits< util::CallbackR1< R, P1 > >
 Bidirectional mapping between util::CallbackR1 and a Python callable object. More...
 
struct  PyExportTraits< util::CallbackR2< R, P1, P2 > >
 Bidirectional mapping between util::CallbackR2 and a Python callable object. More...
 
struct  PyExportTraits< util::CallbackR3< R, P1, P2, P3 > >
 Bidirectional mapping between util::CallbackR3 and a Python callable object. More...
 
struct  PyExportTraits< util::CallbackR4< R, P1, P2, P3, P4 > >
 Bidirectional mapping between util::CallbackR4 and a Python callable object. More...
 
struct  PyExportTraits< util::CallbackR5< R, P1, P2, P3, P4, P5 > >
 Bidirectional mapping between util::CallbackR5 and a Python callable object. More...
 
struct  PyExportTraits< util::CallbackR6< R, P1, P2, P3, P4, P5, P6 > >
 Bidirectional mapping between util::CallbackR6 and a Python callable object. More...
 
struct  PyExportTraits< util::CallbackR7< R, P1, P2, P3, P4, P5, P6, P7 > >
 Bidirectional mapping between util::CallbackR7 and a Python callable object. More...
 
struct  PyExportTraits< util::CallbackR8< R, P1, P2, P3, P4, P5, P6, P7, P8 > >
 Bidirectional mapping between util::CallbackR8 and a Python callable object. More...
 
struct  PyExportTraits< util::CallbackR9< R, P1, P2, P3, P4, P5, P6, P7, P8, P9 > >
 Bidirectional mapping between util::CallbackR9 and a Python callable object. More...
 
struct  PyExportTraits< util::SharedPtr< T, S, C > >
 SharedPtr assumes shadow types or PyObjectPlus types. More...
 
struct  PyExportTraitsCallback
 Helper class to implement PyExportTraits for Callback types. More...
 
struct  PyExportTraitsEnum
 Helper to specialise PyExportTraits for enumerations. More...
 
struct  PyExportTraitsFloat
 Helper class to create PyExportTraits for floating point numbers. More...
 
struct  PyExportTraitsMaybeNone
 Helper class to create PyExportTraits for MaybeNone wrapped types. More...
 
struct  PyExportTraitsNoNone
 Helper class to create PyExportTraits for NoNone wrapped types. More...
 
struct  PyExportTraitsSigned
 Helper class to create PyExportTraits for signed integers. More...
 
struct  PyExportTraitsUnsigned
 Helper class to create PyExportTraits for unsigned integers. More...
 
class  PyIteratorRange
 Python iterator object. More...
 
class  PyObjectCounter
 The recommended counter for the pyobject pointers, implementation of CounterPolicy concept. More...
 
class  PyObjectPlus
 PyObjectPlus. More...
 
struct  PyObjectPtr
 templated "typedef" to a python shared pointer More...
 
class  PyObjectStorage
 Recommended storage policy for single PyObject objects, implementation of StoragePolicy concept. More...
 
class  PythonException
 C++ exception type that holds a Python exception. More...
 
class  Self
 Wrapper to type-hint a return value as Self. More...
 
class  StrEnumDefinition
 Definition of an enum.StrEnum-derived enum type in Python. More...
 

Typedefs

typedef PyObjectPtr< PyObject >::Type TPyObjPtr
 PyObjectPtr to a PyObject.
 
template<typename ShadoweeType>
using ShadoweePtr
 Helper to get the pointer type holding the shadowee in a shadow object.
 

Enumerations

enum class  FlagBoundary { FlagBoundary::Keep , FlagBoundary::Strict , FlagBoundary::Conform }
 Defines the boundary behavior for enum.IntFlag-derived enums. More...
 

Functions

PyObject * chainErrFormat (PyObject *exception, const char *format,...)
 Raise an explicitly chained Python exception.
 
PyObject * chainErrFormatV (PyObject *exception, const char *format, va_list vargs)
 Raise an explicitly chained Python exception.
 
template<class T>
lass::util::SharedPtr< T, PyObjectStorage, PyObjectCounterfromNakedToSharedPtrCast (PyObject *object)
 fromNakedToSharedPtrCast.
 
template<class T>
PyObject * fromSharedPtrToNakedCast (const util::SharedPtr< T, PyObjectStorage, PyObjectCounter > &object)
 fromSharedPtrToNakedCast.
 
template<typename Out, typename In>
Out staticPyCast (const In &in)
 static_cast for python pointers
 
template<typename Out, typename In>
Out dynamicPyCast (const In &in)
 dynamic_cast for python pointers
 
TPyObjPtr getPyObjectByName (const std::string &iName)
 retrieve pointer to PyObject by its name in the script.
 
void putenv (const std::string &key, const std::string &value)
 os.environ[key] = value
 
void putenv (const char *key, const char *value)
 os.environ[key] = value
 
template<typename T>
auto makeContainerRangeView (const ShadoweePtr< T > &self)
 Returns a ContainerRangeView iterating over self->begin() to self->end()
 
template<typename T, typename GetIterator>
auto makeMemberRangeView (const ShadoweePtr< T > &self, GetIterator begin, GetIterator end)
 Returns a MemberRangeView iterating over (self->*begin)() and (self->*end)()
 
template<typename T, typename GetIterator>
auto makeMemberRangeViewFactory (GetIterator begin, GetIterator end)
 Returns a callable creating a MemberRangeView iterating over (self->*begin)() to (self->*end)()
 
template<typename T, typename GetIterator>
auto makeFreeRangeView (const ShadoweePtr< T > &self, GetIterator begin, GetIterator end)
 Returns a FreeRangeView iterating over begin(*self) and end(*self)
 
template<typename T, typename GetIterator>
auto makeFreeRangeViewFactory (GetIterator begin, GetIterator end)
 Returns a callable creating a FreeRangeView iterating over begin(*self) and end(*self)
 
template<typename T, typename AtMethod, typename SizeMethod>
auto makeIndexedRangeView (const ShadoweePtr< T > &self, AtMethod atMethod, SizeMethod sizeMethod)
 Returns a IndexedRangeView iterating over (self->*atMethod)(index) for index in 0 to (self->*sizeMethod)() - 1
 
template<typename T, typename AtMethod, typename SizeMethod>
auto makeIndexedRangeViewFactory (AtMethod atMethod, SizeMethod sizeMethod)
 Returns callable that creates a IndexedRangeView iterating over (self->*atMethod)(index) for index in 0 to (self->*sizeMethod)() - 1
 
template<typename T, typename AtFunc, typename SizeFunc>
auto makeFreeIndexedRangeView (const ShadoweePtr< T > &self, AtFunc atFunc, SizeFunc sizeFunc)
 Returns a FreeIndexedRangeView iterating over atFunc(*self, index) for index in 0 to sizeFunc(*self) - 1
 
template<typename T, typename AtFunc, typename SizeFunc>
auto makeFreeIndexedRangeViewFactory (AtFunc atFunc, SizeFunc sizeFunc)
 Returns a callable that creates FreeIndexedRangeView iterating over atFunc(*self, index) for index in 0 to sizeFunc(*self) - 1