Base classes for all parameter/return type handlers, and base interfaces for wrapper generators.
Bases: object
Object to keep track of Py_BuildValue (or similar) parameters
>>> bld = BuildValueParameters()
>>> bld.add_parameter('i', [123, 456])
>>> bld.add_parameter('s', ["hello"])
>>> bld.get_parameters()
['"is"', 123, 456, 'hello']
>>> bld = BuildValueParameters()
>>> bld.add_parameter('i', [123])
>>> bld.add_parameter('s', ["hello"], prepend=True)
>>> bld.get_parameters()
['"si"', 'hello', 123]
Adds a new parameter to the Py_BuildValue (or similar) statement.
Parameters: |
|
---|
Get a list of handles to cleanup actions
returns a list of parameters to pass into a Py_BuildValue-style function call, the first paramter in the list being the template string.
Parameters: | force_tuple_creation – if True, Py_BuildValue is instructed to always create a tuple, even for zero or 1 values. |
---|
Bases: object
An intelligent code block that keeps track of cleanup actions. This object is to be used by TypeHandlers when generating code.
CodeBlock constructor
>>> block = CodeBlock("return NULL;", DeclarationsScope())
>>> block.write_code("foo();")
>>> cleanup1 = block.add_cleanup_code("clean1();")
>>> cleanup2 = block.add_cleanup_code("clean2();")
>>> cleanup3 = block.add_cleanup_code("clean3();")
>>> cleanup2.cancel()
>>> block.write_error_check("error()", "error_clean()")
>>> block.write_code("bar();")
>>> block.write_cleanup()
>>> print block.sink.flush().rstrip()
foo();
if (error()) {
error_clean()
clean3();
clean1();
return NULL;
}
bar();
clean3();
clean1();
Parameters: |
|
---|
Bases: object
Handle for some cleanup code
Create a handle given code_block and position
Cancel the cleanup code
returns the cleanup code relative position
Add a chunk of code used to cleanup previously allocated resources
Returns a handle used to cancel the cleanup code
Calls declare_variable() on the associated DeclarationsScope object.
return a new list with all cleanup actions, including the ones from predecessor code blocks; Note: cleanup actions are executed in reverse order than when they were added.
Add a certain ammount of indentation to all lines written from now on and until unindent() is called
Remove cleanup code previously added with add_cleanup_code()
Revert indentation level to the value before last indent() call
Write the current cleanup code.
Write out some simple code
Add a chunk of code that checks for a possible error
Parameters: |
|
---|
Add a chunk of code that cleans up and returns an error.
Bases: pybindgen.typehandlers.base.CodegenErrorBase
Exception that is raised when wrapper generation fails for some reason.
Bases: exceptions.Exception
Bases: object
Manages variable declarations in a given scope.
Constructor
>>> scope = DeclarationsScope()
>>> scope.declare_variable('int', 'foo')
'foo'
>>> scope.declare_variable('char*', 'bar')
'bar'
>>> scope.declare_variable('int', 'foo')
'foo2'
>>> scope.declare_variable('int', 'foo', '1')
'foo3'
>>> scope.declare_variable('const char *', 'kwargs', '{"hello", NULL}', '[]')
'kwargs'
>>> print scope.get_code_sink().flush().rstrip()
int foo;
char *bar;
int foo2;
int foo3 = 1;
const char *kwargs[] = {"hello", NULL};
Parameters: | parent_scope – optional ‘parent scope’; if given, declarations in this scope will avoid clashing with names in the parent scope, and vice versa. |
---|
Add code to declare a variable. Returns the actual variable name used (uses ‘name’ as base, with a number in case of conflict.)
Parameters: |
|
---|
Returns the internal MemoryCodeSink that holds all declararions.
Reserve a variable name, to be used later.
Parameters: | name – base name of the variable; actual name used can be slightly different in case of name conflict. |
---|
Bases: object
Generic base for all forward wrapper generators.
Forward wrappers all have the following general structure in common:
- ‘declarations’ – variable declarations; for compatibility
with older C compilers it is very important that all declarations come before any simple statement. Declarations can be added with the add_declaration() method on the ‘declarations’ attribute. Two standard declarations are always predeclared: ‘<return-type> retval’, unless return-type is void, and ‘PyObject *py_retval’;
- ‘code before parse’ – code before the
PyArg_ParseTupleAndKeywords call; code can be freely added to it by accessing the ‘before_parse’ (a CodeBlock instance) attribute;
- A PyArg_ParseTupleAndKeywords call; uses items from the
parse_params object;
- ‘code before call’ – this is a code block dedicated to contain
all code that is needed before calling the C function; code can be freely added to it by accessing the ‘before_call’ (a CodeBlock instance) attribute;
- ‘call into C’ – this is realized by a C/C++ call; the list of
parameters that should be used is in the ‘call_params’ wrapper attribute;
- ‘code after call’ – this is a code block dedicated to contain
all code that must come after calling into Python; code can be freely added to it by accessing the ‘after_call’ (a CodeBlock instance) attribute;
A py_retval = Py_BuildValue(...) call; this call can be customized, so that out/inout parameters can add additional return values, by accessing the ‘build_params’ (a BuildValueParameters instance) attribute;
Cleanup and return.
Object constructors cannot return values, and so the step 7 is to be omitted for them.
Base constructor
Parameters: |
|
---|
Generate the wrapper function body code_sink – a CodeSink object that will receive the code
Generates the code (into self.before_call) to call into Python, storing the result in the variable ‘py_retval’; should also check for call error.
Get a list of PyMethodDef flags that should be used for this wrapper.
Bases: pybindgen.typehandlers.base.CodegenErrorBase
Exception that is raised when declaring an interface configuration that is not supported or not implemented.
Bases: object
Null type transformation, returns everything unchanged.
identity transformation
identity transformation
identity transformation
identity transformation
Bases: pybindgen.typehandlers.base.TypeHandler
Abstract base class for all classes dedicated to handle specific parameter types
Creates a parameter object
Parameters: |
|
---|
Write some code before calling the Python method.
Write some code before calling the C method.
>>> import inttype
>>> isinstance(Parameter.new('int', 'name'), inttype.IntParam)
True
Bases: object
Object to keep track of PyArg_ParseTuple (or similar) parameters
>>> tuple_params = ParseTupleParameters()
>>> tuple_params.add_parameter('i', ['&foo'], 'foo')
1
>>> tuple_params.add_parameter('s', ['&bar'], 'bar', optional=True)
2
>>> tuple_params.get_parameters()
['"i|s"', '&foo', '&bar']
>>> tuple_params.get_keywords()
['foo', 'bar']
>>> tuple_params = ParseTupleParameters()
>>> tuple_params.add_parameter('i', ['&foo'], 'foo')
1
>>> tuple_params.add_parameter('s', ['&bar'], 'bar', prepend=True)
2
>>> tuple_params.get_parameters()
['"si"', '&bar', '&foo']
>>> tuple_params.get_keywords()
['bar', 'foo']
>>> tuple_params = ParseTupleParameters()
>>> tuple_params.add_parameter('i', ['&foo'])
1
>>> print tuple_params.get_keywords()
None
Adds a new parameter specification
Parameters: |
|
---|
returns list of keywords (parameter names), or None if none of the parameters had a name; should only be called if names were given for all parameters or none of them.
returns a list of parameters to pass into a PyArg_ParseTuple-style function call, the first paramter in the list being the template string.
Bases: pybindgen.typehandlers.base.Parameter
Base class for all pointer-to-something handlers
Bases: pybindgen.typehandlers.base.ReturnValue
Base class for all pointer-to-something handlers
Bases: pybindgen.typehandlers.base.TypeHandler
Abstract base class for all classes dedicated to handle specific return value types
Creates a return value object
Keywork Arguments:
Parameters: | ctype – actual C/C++ type being used |
---|
Writes code to convert the C return value the Python return.
Writes code to convert the Python return value into the C “retval” variable.
Return a “return <value>” code string, for use in case of error
>>> import inttype
>>> isinstance(ReturnValue.new('int'), inttype.IntReturn)
True
Bases: object
Generic base for all reverse wrapper generators.
Reverse wrappers all have the following general structure in common:
- ‘declarations’ – variable declarations; for compatibility with older C compilers it is very important that all declarations come before any simple statement. Declarations can be added with the add_declaration() method on the ‘declarations’ attribute. Two standard declarations are always predeclared: ‘<return-type> retval’, unless return-type is void, and ‘PyObject *py_retval’;
- ‘code before call’ – this is a code block dedicated to contain all code that is needed before calling into Python; code can be freely added to it by accessing the ‘before_call’ (a CodeBlock instance) attribute;
- ‘call into python’ – this is realized by a PyObject_CallMethod(...) or similar Python API call; the list of parameters used in this call can be customized by accessing the ‘build_params’ (a BuildValueParameters instance) attribute;
- ‘code after call’ – this is a code block dedicated to contain all code that must come after calling into Python; code can be freely added to it by accessing the ‘after_call’ (a CodeBlock instance) attribute;
- A ‘return retval’ statement (or just ‘return’ if return_value is void)
Base constructor
Parameters: |
|
---|
Generate the wrapper
Parameters: |
|
---|
Generates the code (into self.before_call) to call into Python, storing the result in the variable ‘py_retval’; should also check for call error.
Bases: pybindgen.typehandlers.base.CodegenErrorBase
Exception that is raised when a type handler does not find some information it needs, such as owernship transfer semantics.
Bases: object
Set the type transformation to use in this type handler
Bases: pybindgen.typehandlers.base.CodegenErrorBase
Exception that is raised when lookup of a type handler fails
Bases: object
Type matcher object: maps C type names to classes that handle those types.
Constructor
Returns an iterator over all registered items
Parameters: | name – C type name, possibly transformed (e.g. MySmartPointer<Foo> looks up Foo*) |
---|---|
Returns: | a handler with the given ctype name, or raises KeyError. |
Supports type transformations.
Register a new handler class for a given C type
Parameters: |
|
---|
Register a type transformation object
Bases: object
Type transformations are used to register handling of special types that are simple transformation over another type that is already registered. This way, only the original type is registered, and the type transformation only does the necessary adjustments over the original type handler to make it handle the transformed type as well.
This is typically used to get smart pointer templated types working.
Given a type_handler class, create an instance with proper customization.
Parameters: |
|
---|
Given a transformed named, get the original C type name. E.g., given a smart pointer transformation, MySmartPointer:
get_untransformed_name('MySmartPointer<Foo>') -> 'Foo\*'
Transforms a value expression of the original type to an equivalent value expression in the transformed type.
Transforms a value expression of the transformed type to an equivalent value expression in the original type.
Utility method that joins a C type and a variable name into a single string
>>> join_ctype_and_name('void*', 'foo')
'void *foo'
>>> join_ctype_and_name('void *', 'foo')
'void *foo'
>>> join_ctype_and_name("void**", "foo")
'void **foo'
>>> join_ctype_and_name("void **", "foo")
'void **foo'
>>> join_ctype_and_name('C*', 'foo')
'C *foo'