XDress

Derived from:

XDress - Idiomatic, Automatic Wrapper Generation

Kitware

Anthony Michael Scopatz
The University of Wisconsin-Madison, Numfocus
Clifton Park, August 20th, 2014

The Big Idea

  • Xdress is an idiomatic, automatic Python wrapping tool for C and C++ libraries.

  • At its core, xdress is a suite of code generation routines built on top of a dynamic type system.

  • The interface that is created should be beautiful, or at least not ugly.

Motivation

  • First class C++ support
  • Deep access to the type system
  • A multitude of target languages

What XDress Is

Created via this link.

What XDress Is Not

  • Parser
  • Compiler
  • Build System

Parsers

Parsing is worse than hard, it is annoying!

XDress completely outsources the task of parsing source code to other projects. It mostly outsources the syntax tree too.

Parses that XDress supports currently are:

  • pycparser, for pure C-code
  • GCC-XML, for C++ code
  • Clang 3.2+, for C and C++ code

This tactic of relying on external parsers enables flexibility w.r.t. the source language.

API Descriptions

So how do the API descriptions work?

To keep them as a portable as possible, they are nothing fancy.

In memory descriptions (desc) are just Python strings, ints, dicts, tuples, lists, and other built-in types. They can easily be translated into a JSON format.

API Description Example

Code is generated from descriptions of API elements to produce idomatic Python wrappers.

brienne_desc = {
    'name': 'Brienne',
    'parents': ['LordSelwyn'],
    'namespace': 'westeros',
    'attrs': {'moniker': 'str'},
    'methods': {
        ('Brienne',): None,
        ('Brienne', ('monkier', 'str', '"Maid of Tarth"')): None,
        ('~Brienne',): None, 
        ('attack', ('person', ('char' '*'), ('weapon_id', 'i2', 1))): 'float32',
        },
    'docstrings': {
        'class': "I am so tall!",
        'attrs': {
            'monkier': 'extra name for character',
            },
        'methods': {
            'attack': "Attack someone with a weapon and returns damage."
            },
        },
    'extra': {},
    }

They heavy lifting of the API decsriptions is done by the type system.

Code Generation

After the AST has been turned into an API description, xdress can pass this the code generator.

The primary one is called cythongen and targets Python via Cython.

Learn more!