ctree package

Submodules

ctree.analyses module

validation of ast trees

exception ctree.analyses.AstValidationError[source]

Bases: exceptions.Exception

Exception for non C nodes in an AST

class ctree.analyses.DeclFinder[source]

Bases: ast.NodeVisitor

Returns the first use of a particular symbol.

find(node)[source]

look for a declaration of a symbol ref

class ctree.analyses.VerifyOnlyCtreeNodes[source]

Bases: ast.NodeVisitor

Checks that every node in the tree is an instance of ctree.nodes.common.CtreeNode. Raises an exception if a bad node is found.

visit(node)[source]

ctree.codegen module

base class for generating code appropriate to the selected backend

class ctree.codegen.CodeGenVisitor(indent=0)[source]

Bases: ast.NodeVisitor

Return a string containing the program text.

ctree.dotgen module

class ctree.dotgen.DotGenLabeller[source]

Bases: ast.NodeVisitor

generic_visit(node)[source]
class ctree.dotgen.DotGenVisitor[source]

Bases: ast.NodeVisitor

Generates a representation of the AST in the DOT graph language. See http://en.wikipedia.org/wiki/DOT_(graph_description_language)

generic_visit(node)[source]
label(node)[source]

A string to provide useful information for visualization, debugging, etc.

ctree.dotgen.label_for_py_ast_nodes(self)[source]
ctree.dotgen.to_dot_inner_for_py_ast_nodes(self)[source]
ctree.dotgen.to_dot_outer_for_py_ast_nodes(self)[source]

ctree.frontend module

helper for getting properly formatted ast from arbitrary python object

ctree.frontend.get_ast(obj)[source]

Return the Python ast for the given object, which may be anything that inspect.getsource accepts (incl. a module, class, method, function, traceback, frame, or code object).

ctree.jit module

Just-in-time compilation support.

class ctree.jit.ConcreteSpecializedFunction[source]

Bases: object

A function backed by generated code.

class ctree.jit.JitModule[source]

Bases: object

Manages compilation of multiple ASTs.

get_callable(entry_point_name, entry_point_typesig)[source]

Returns a python callable that dispatches to the requested C function.

class ctree.jit.LazySpecializedFunction(py_ast)[source]

Bases: object

A callable object that will produce executable code just-in-time.

args_to_subconfig(args)[source]

Extract features from the arguments to define uniqueness of this particular invocation. The return value must be a hashable object, or a dictionary of hashable objects.

finalize(tree, program_config)[source]

This function will be passed the result of transform. The specializer should return an ConcreteSpecializedFunction.

get_tuning_driver()[source]

Define the space of possible implementations.

report(*args, **kwargs)[source]

Records the performance of the most recent configuration.

transform(tree, program_config)[source]

Convert the AST ‘tree’ into a C AST, optionally taking advantage of the actual runtime arguments.

ctree.precedence module

Utilities for determining precedence in C, with the goal of minimizing the number of parentheses in the generated code.

ctree.precedence.get_precedence(node)[source]
ctree.precedence.is_left_associative(node)[source]

ctree.transformations module

A set of basic transformers for python asts

class ctree.transformations.Lifter(lift_params=True, lift_includes=True)[source]

Bases: ast.NodeTransformer

To aid in adding new includes or parameters during tree traversals, users can store them with arbitrary child nodes and call this transformation to move them to the correct position.

visit_CFile(node)[source]
visit_FunctionDecl(node)[source]
class ctree.transformations.PyBasicConversions(names_dict={}, constants_dict={})[source]

Bases: ast.NodeTransformer

Convert constructs with obvious C analogues.

PY_OP_TO_CTREE_OP = {<class '_ast.Is'>: <class 'ctree.c.nodes.Eq'>, <class '_ast.BitXor'>: <class 'ctree.c.nodes.BitXor'>, <class '_ast.GtE'>: <class 'ctree.c.nodes.GtE'>, <class '_ast.LShift'>: <class 'ctree.c.nodes.BitShL'>, <class '_ast.Gt'>: <class 'ctree.c.nodes.Gt'>, <class '_ast.BitAnd'>: <class 'ctree.c.nodes.BitAnd'>, <class '_ast.LtE'>: <class 'ctree.c.nodes.LtE'>, <class '_ast.Mod'>: <class 'ctree.c.nodes.Mod'>, <class '_ast.RShift'>: <class 'ctree.c.nodes.BitShR'>, <class '_ast.Lt'>: <class 'ctree.c.nodes.Lt'>, <class '_ast.BitOr'>: <class 'ctree.c.nodes.BitOr'>, <class '_ast.Div'>: <class 'ctree.c.nodes.Div'>, <class '_ast.NotEq'>: <class 'ctree.c.nodes.NotEq'>, <class '_ast.Or'>: <class 'ctree.c.nodes.Or'>, <class '_ast.Mult'>: <class 'ctree.c.nodes.Mul'>, <class '_ast.Not'>: <class 'ctree.c.nodes.Not'>, <class '_ast.Eq'>: <class 'ctree.c.nodes.Eq'>, <class '_ast.And'>: <class 'ctree.c.nodes.And'>, <class '_ast.Sub'>: <class 'ctree.c.nodes.Sub'>, <class '_ast.IsNot'>: <class 'ctree.c.nodes.NotEq'>, <class '_ast.Add'>: <class 'ctree.c.nodes.Add'>}
PY_UOP_TO_CTREE_UOP = {'Not': <class 'ctree.c.nodes.Not'>, 'UAdd': <class 'ctree.c.nodes.Add'>, 'USub': <class 'ctree.c.nodes.Sub'>, 'Invert': <class 'ctree.c.nodes.BitNot'>}
visit_Assign(node)[source]
visit_AugAssign(node)[source]
visit_BinOp(node)[source]
visit_Call(node)[source]
visit_Compare(node)[source]
visit_For(node)[source]

restricted, for now, to range as iterator with long-type args

visit_FunctionDef(node)[source]
visit_If(node)[source]
visit_IfExp(node)[source]
visit_Module(node)[source]
visit_Name(node)[source]
visit_Num(node)[source]
visit_Return(node)[source]
visit_Str(node)[source]
visit_Subscript(node)[source]
visit_UnaryOp(node)[source]
visit_arg(node)[source]
class ctree.transformations.PyCtxScrubber[source]

Bases: ast.NodeTransformer

Removes pesky ctx attributes from Python ast.Name nodes, yielding much cleaner python asts.

visit_Name(node)[source]
class ctree.transformations.ResolveGeneratedPathRefs(compilation_dir)[source]

Bases: ast.NodeTransformer

Converts any instances of ctree.nodes.GeneratedPathRef into strings containing the absolute path of the target file.

visit_GeneratedPathRef(node)[source]

ctree.types module

ctree.types.codegen_type(ctype)[source]

Unparses the given ctype.

Parameters:ctype – A ctype type instance to be unparsed.
ctree.types.get_ctype(py_obj)[source]

Given a python object, this routine tries to return the closest ctype type instance corresponding to that object.

Parameters:py_obj – A python object.
ctree.types.register_type_codegenerators(codegen_dict)[source]

Registers routines for generating code for types.

Parameters:codegen_dict – Maps type classes to functions that

take an instance of that class and return the corresponding string.

ctree.types.register_type_recognizers(typerec_dict)[source]

Registers routines for getting ctypes objects from Python objects.

Parameters:typerec_dict – Maps Python classes to functions that

take an instance of that class and return the corresponding ctypes object.

ctree.util module

class ctree.util.Timer[source]
ctree.util.enumerate_flatten(obj_or_list)[source]

Iterator for all objects arbitrarily nested in lists.

ctree.util.flatten(obj)[source]

Iterator for all objects arbitrarily nested in lists.

ctree.util.highlight(code, language='c')[source]

Syntax-highlight code using pygments, if installed.

ctree.util.lower_case_underscore_to_camel_case(string)[source]

Convert string or unicode from lower-case underscore to camel-case

ctree.util.singleton(cls)[source]
ctree.util.truncate(text)[source]

ctree.visitors module

For now, just use visitors defined by Python’s AST library.

Module contents

The ctree package

class ctree.Counter[source]

Bases: object

Tracks events, reports counts upon garbage collections.

log(event_str)[source]

record a single named event

report()[source]

send a counter report of all named events to the log

ctree.browser_show_ast(tree, file_name)[source]

convenience method to display an AST in ipython converts tree in place to a dot format then renders that into a png file

ctree.get_ast(func)[source]

convenience method for displaying a callable objects ast

ctree.ipython_show_ast(tree)[source]

convenience method to display an AST in ipython converts tree in place to a dot format then renders that into a png file