LSST Applications
21.0.0-142-gef555c1e+42c9bccae2,22.0.0+052faf71bd,22.0.0+1c4650f311,22.0.0+40ce427c77,22.0.0+5b6c068b1a,22.0.0+7589c3a021,22.0.0+81ed51be6d,22.0.1-1-g7d6de66+6cae67f2c6,22.0.1-1-g87000a6+314cd8b7ea,22.0.1-1-g8760c09+052faf71bd,22.0.1-1-g8e32f31+5b6c068b1a,22.0.1-10-g779eefa+a163f08322,22.0.1-12-g3bd7ecb+bbeacc25a9,22.0.1-15-g63cc0c1+2a7037787d,22.0.1-17-ge5a99e88+3d2c1afe2e,22.0.1-19-g88addfe+6cae67f2c6,22.0.1-2-g1cb3e5b+84de06d286,22.0.1-2-g8ef0a89+6cae67f2c6,22.0.1-2-g92698f7+1c4650f311,22.0.1-2-ga9b0f51+052faf71bd,22.0.1-2-gb66926d+5b6c068b1a,22.0.1-2-gcb770ba+0723a13595,22.0.1-2-ge470956+ff9f1dc8d5,22.0.1-22-g608e23ac+2ac85e833c,22.0.1-29-g184b6e44e+8b185d4e2d,22.0.1-3-g59f966b+11ba4df19d,22.0.1-3-g8c1d971+f90df4c6d0,22.0.1-3-g997b569+d69a7aa2f8,22.0.1-3-gaaec9c0+4d194bf81c,22.0.1-4-g1930a60+283d9d2f1a,22.0.1-4-g5b7b756+c1283a92b8,22.0.1-4-g8623105+6cae67f2c6,22.0.1-7-gba73697+283d9d2f1a,22.0.1-8-g47d23f5+43acea82f3,master-g5f2689bdc5+40ce427c77,w.2021.38
LSST Data Management Base Package
|
A helper class for subdividing pybind11 module across multiple translation units (i.e. More...
#include <python.h>
Public Types | |
using | WrapperCallback = std::function< void(pybind11::module &)> |
Function handle type used to hold deferred wrapper declaration functions. More... | |
Public Member Functions | |
WrapperCollection (pybind11::module module_, std::string const &package) | |
Construct a new WrapperCollection. More... | |
WrapperCollection (WrapperCollection &&other) noexcept | |
WrapperCollection (WrapperCollection const &)=delete | |
WrapperCollection & | operator= (WrapperCollection const &)=delete |
WrapperCollection & | operator= (WrapperCollection &&)=delete |
~WrapperCollection () noexcept | |
WrapperCollection | makeSubmodule (std::string const &name) |
Create a WrapperCollection for a submodule defined in the same binary. More... | |
void | collectSubmodule (WrapperCollection &&submodule) |
Merge deferred definitions in the given submodule into the parent WrapperCollection. More... | |
void | addInheritanceDependency (std::string const &name) |
Indicate an external module that provides a base class for a subsequent addType call. More... | |
void | addSignatureDependency (std::string const &name) |
Indicate an external module that provides a type used in function/method signatures. More... | |
void | wrap (WrapperCallback function) |
Add a set of wrappers without defining a class. More... | |
template<typename PyType , typename ClassWrapperCallback > | |
PyType | wrapType (PyType cls, ClassWrapperCallback function, bool setModuleName=true) |
Add a type (class or enum) wrapper, deferring method and other attribute definitions until finish() is called. More... | |
template<typename CxxException , typename CxxBase > | |
auto | wrapException (std::string const &pyName, std::string const &pyBase, bool setModuleName=true) |
Wrap a C++ exception as a Python exception. More... | |
void | finish () |
Invoke all deferred wrapper-declaring callables. More... | |
Public Attributes | |
pybind11::module | module |
The module object passed to the PYBIND11_MODULE block that contains this WrapperCollection. More... | |
A helper class for subdividing pybind11 module across multiple translation units (i.e.
source files).
Merging wrappers for different classes into a single compiled module can dramatically decrease the total size of the binary, but putting the source for multiple wrappers into a single file slows down incremental rebuilds and makes editing unwieldy. The right approach is to define wrappers in different source files and link them into a single module at build time. In simple cases, that's quite straightforward: pybind11 declarations are just regular C++ statements, and you can factor them out into different functions in different source files.
That approach doesn't work so well when the classes being wrapped are interdependent, because bindings are only guaranteed to work when all types used in a wrapped method signature have been declared to pybind11 before the method using them is itself declared. Naively, then, each source file would thus have to have multiple wrapper-declaring functions, so all type-wrapping functions could be executed before any method-wrapping functions. Of course, each type-wrapping function would also have to pass its type object to at least one method-wrapping function (to wrap the types own methods), and the result is a tangled mess of wrapper-declaring functions that obfuscate the code with a lot of boilerplate.
WrapperCollection provides a way out of that by allowing type wrappers and their associated methods to be declared at a single point, but the method wrappers wrapped in a lambda to defer their execution. A single WrapperCollection instance is typically constructed at the beginning of a PYBIND11_MODULE block, then passed by reference to wrapper-declaring functions defined in other source files. As type and method wrappers are added to the WrapperCollection by those functions, the types are registered immediately, and the method-wrapping lambdas are collected. After all wrapper-declaring functions have been called, finish() is called at the end of the PYBIND11_MODULE block to execute the collecting method-wrapping lambdas.
Typical usage:
Note that we recommend the use of universal lambdas (i.e. auto &
parameters) to reduce verbosity.
using lsst::utils::python::WrapperCollection::WrapperCallback = std::function<void(pybind11::module &)> |
|
inlineexplicit |
Construct a new WrapperCollection.
A WrapperCollection should be constructed at or near the top of a PYBIND11_MODULE
block.
[in] | module_ | Module instance passed to the PYBIND11_MODULE macro. |
[in] | package | String name of the package all wrapped classes should appear to be from (by resetting their __module__ attribute). Note that this can lead to problems if classes are not also lifted into the package namespace in its __init__.py (in addition to confusing users, this will prevent unpickling from working). |
|
inlinenoexcept |
|
delete |
|
inlinenoexcept |
|
inline |
Indicate an external module that provides a base class for a subsequent addType call.
Dependencies that provide base classes cannot be deferred until after types are declared, and are always imported immediately.
[in] | name | Name of the module to import (absolute). |
Definition at line 343 of file python.h.
|
inline |
Indicate an external module that provides a type used in function/method signatures.
Dependencies that provide classes are imported after types in the current module are declared and before any methods and free functions in the current module are declared.
[in] | name | Name of the module to import (absolute). |
|
inline |
Merge deferred definitions in the given submodule into the parent WrapperCollection.
submodule | A WrapperCollection created by makeSubmodule. Will be consumed (and must be an rvalue). |
Definition at line 329 of file python.h.
|
inline |
Invoke all deferred wrapper-declaring callables.
finish()
should be called exactly once, at or near the end of a PYBIND11_MODULE
block.
|
inline |
Create a WrapperCollection for a submodule defined in the same binary.
WrapperCollections created with makeSubmodule should generally be destroyed by moving them into a call to collectSubmodule; this will cause all deferred definitions to be executed when the parent WrapperCollection's finish() method is called.
name | Relative name of the submodule. |
Attributes added to the returned WrapperCollection will actually be put in a submodule that adds an underscore prefix to name
, with __module__
set with the expectation that they will be lifted into a package without that leading underscore by a line in __init__.py
like:
from ._package import _submodule as submodule
This is necessary to make importing _package
possible when submodule
already exists as a normal (i.e. directory-based) package. Of course, in that case, you'd instead use a submodule/__init__.py
with a line like:
from .._package._submodule import *
__module__
of any classes added to it to {package}.{name}
. Definition at line 318 of file python.h.
|
delete |
|
delete |
|
inline |
|
inline |
Wrap a C++ exception as a Python exception.
CxxException | C++ Exception type to wrap. |
CxxBase | Base class of CxxException. |
[in] | pyName | Python name of the new exception. |
[in] | pyBase | Python name of the pex::exceptions Exception type the new exception inherits from. |
[in] | setModuleName | If true (default), set cls.__module__ to the package string this WrapperCollection was initialized with. |
pybind11::class_
instance (template parameters unspecified) representing the Python type of the new exception. Definition at line 421 of file python.h.
|
inline |
Add a type (class or enum) wrapper, deferring method and other attribute definitions until finish() is called.
[in] | cls | A pybind11::class_ or enum_ instance. |
[in] | function | A callable object that takes a pybind11::module argument and a pybind11::class_ (or enum_ ) argument (both by reference) and defines wrappers for the class's methods and other attributes. Will be called with this->module and cls by finish() . |
[in] | setModuleName | If true (default), set cls.__module__ to the package string this WrapperCollection was initialized with. |
cls
argument for convenience Definition at line 391 of file python.h.
pybind11::module lsst::utils::python::WrapperCollection::module |
The module object passed to the PYBIND11_MODULE
block that contains this WrapperCollection.