Objects that represent – and generate code for – C/C++ Python extension modules.
A L{Module} object takes care of generating the code for a Python module. The way a Python module is organized is as follows. There is one “root” L{Module} object. There can be any number of L{SubModule}s. Sub-modules themselves can have additional sub-modules. Calling L{Module.generate} on the root module will trigger code generation for the whole module, not only functions and types, but also all its sub-modules.
In Python, a sub-module will appear as a I{built-in} Python module that is available as an attribute of its parent module. For instance, a module I{foo} having a sub-module I{xpto} appears like this:
|>>> import foo
|>>> foo.xpto
|<module 'foo.xpto' (built-in)>
Modules can be associated with specific C++ namespaces. This means, for instance, that any C++ class wrapped inside that module must belong to that C++ namespace. Example:
|>>> from cppclass import *
|>>> mod = Module("foo", cpp_namespace="::foo")
|>>> mod.add_class("Bar")
|<pybindgen.CppClass 'foo::Bar'>
When we have a toplevel C++ namespace which contains another nested namespace, we want to wrap the nested namespace as a Python sub-module. The method L{ModuleBase.add_cpp_namespace} makes it easy to create sub-modules for wrapping nested namespaces. For instance:
|>>> from cppclass import *
|>>> mod = Module("foo", cpp_namespace="::foo")
|>>> submod = mod.add_cpp_namespace('xpto')
|>>> submod.add_class("Bar")
|<pybindgen.CppClass 'foo::xpto::Bar'>
Bases: pybindgen.module.ModuleBase
Parameters: |
|
---|
Generates the module
Parameters: | module_file_base_name – base name of the module file. |
---|
This is useful when we want to produce a _foo module that will be imported into a foo module, to avoid making all types docstrings contain _foo.Xpto instead of foo.Xpto.
Generates a c-to-python converter function for a given type and returns the name of the generated function. If called multiple times with the same name only the first time is the converter function generated.
Use: this method is to be considered pybindgen internal, used by code generation modules.
Returns: | name of the converter function |
---|
Generates a python-to-c converter function for a given type and returns the name of the generated function. If called multiple times with the same name only the first time is the converter function generated.
Use: this method is to be considered pybindgen internal, used by code generation modules.
Returns: | name of the converter function |
---|
Internal API, do not use.
Internal API, do not use.
Bases: dict
ModuleBase objects can be indexed dictionary style to access contained types. Example:
>>> from enum import Enum
>>> from cppclass import CppClass
>>> m = Module("foo", cpp_namespace="foo")
>>> subm = m.add_cpp_namespace("subm")
>>> c1 = m.add_class("Bar")
>>> c2 = subm.add_class("Zbr")
>>> e1 = m.add_enum("En1", ["XX"])
>>> e2 = subm.add_enum("En2", ["XX"])
>>> m["Bar"] is c1
True
>>> m["foo::Bar"] is c1
True
>>> m["En1"] is e1
True
>>> m["foo::En1"] is e1
True
>>> m["badname"]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'badname'
>>> m["foo::subm::Zbr"] is c2
True
>>> m["foo::subm::En2"] is e2
True
Note: this is an abstract base class, see L{Module}
Parameters: |
|
---|---|
Returns: | a new module object |
Add a class to the module. See the documentation for L{CppClass.__init__} for information on accepted parameters.
Add a container to the module. See the documentation for L{Container.__init__} for information on accepted parameters.
Add a nested module namespace corresponding to a C++ namespace. If the requested namespace was already added, the existing module is returned instead of creating a new one.
Parameters: | name – name of C++ namespace (just the last component, |
---|
not full scoped name); this also becomes the name of the submodule.
Returns: | a L{SubModule} object that maps to this namespace. |
---|
Add a function, using custom wrapper code, to the module/namespace. See the documentation for pybindgen.function.CustomFunctionWrapper for information on accepted parameters.
Add an enumeration to the module. See the documentation for L{Enum.__init__} for information on accepted parameters.
Add a C++ exception to the module. See the documentation for L{CppException.__init__} for information on accepted parameters.
Add a function to the module/namespace. See the documentation for Function.__init__() for information on accepted parameters.
Adds an additional include directive, needed to compile this python module
Parameters: | include – the name of the header file to include, including surrounding “” or <>. |
---|
Add a struct to the module.
In addition to the parameters accepted by L{CppClass.__init__}, this method accepts the following keyword parameters:
- no_constructor (bool): if True, the structure will not have a constructor by default (if omitted, it will be considered to have a trivial constructor).
- no_copy (bool): if True, the structure will not have a copy constructor by default (if omitted, it will be considered to have a simple copy constructor).
Parameters: |
|
---|
@note: only typedefs for CppClass objects have been implemented so far; others will be implemented in the future.
Declare that types and functions registered with the module in the future belong to the section given by that section_name parameter, until a matching end_section() is called.
Note
begin_section()/end_section() are silently ignored unless a MultiSectionFactory object is used as code generation output.
Internal helper method for code geneneration to coordinate generation of code that can only be defined once per compilation unit
(note: assuming here one-to-one mapping between ‘module’ and ‘compilation unit’).
Parameters: | definition_name – a string that uniquely identifies the code |
---|
definition that will be added. If the given definition was already declared KeyError is raised.
>>> module = Module('foo')
>>> module.declare_one_time_definition("zbr")
>>> module.declare_one_time_definition("zbr")
Traceback (most recent call last):
...
KeyError: 'zbr'
>>> module.declare_one_time_definition("bar")
(internal) Generates the module.
Declare the end of a section, i.e. further types and functions will belong to the main module.
Parameters: | section_name – name of section; must match the one in the previous begin_section() call. |
---|
(internal) generate forward declarations for types
Get the full [module, submodule, submodule,...] path
Get the full [root_namespace, namespace, namespace,...] path (C++)
get a submodule by its name
Register a type wrapper with the module, for easy access in the future. Normally should not be called by the programmer, as it is meant for internal pybindgen use and called automatically.
Parameters: |
|
---|
Sets the function to be used when transforming a C function name into the python function name; the given given function is called like this:
python_name = transformer(c_name)
Sets the prefix string to be used when transforming a C function name into the python function name; the given prefix string is removed from the C function name.
Bases: object
Abstract base class for objects providing support for multi-section code generation, i.e., splitting the generated C/C++ code into multiple files. The generated code will generally have the following structure:
- For each section there is one source file specific to that section;
2. There is a I{main} source file, e.g. C{foomodule.cc}. Code that does not belong to any section will be included in this main file;
3. Finally, there is a common header file, (e.g. foomodule.h), which is included by the main file and section files alike. Typically this header file contains function prototypes and type definitions.
@see: L{Module.generate}
Create and/or return a code sink for the common header.
Return the argument for an #include directive to include the common header.
Returns: | a string with the header name, including surrounding |
---|
“” or <>. For example, ‘“foomodule.h”’.
Create and/or return a code sink for the main file.
Create and/or return a code sink for a given section.
Parameters: | section_name – name of the section |
---|---|
Returns: | a L{CodeSink} object that will receive generated code belonging to the section C{section_name} |
Bases: pybindgen.module.ModuleBase
Parameters: |
|
---|