SlideShare a Scribd company logo
PY-MA

    Why Extension Programmers Should Stop Worrying
   About Parsing and Start Thinking About Type Systems
                        (or automatic extension building is harder than it looks,
                          but that hasn’t stopped anyone from trying to do it)



                                      David M. Beazley
                               Department of Computer Science
                                  The University of Chicago
                                  beazley@cs.uchicago.edu

                                         November 9, 2002




LL2, November 9, 2002                                         1                     beazley@cs.uchicago.edu
Background

I work on programming tools and applications
       • SWIG project (www.swig.org), 1995-present.
       • Scientific software (molecular dynamics, climate modeling)
       • And some other projects.


I am not a programming languages researcher
       • Which is why I am here




LL2, November 9, 2002                              2                  beazley@cs.uchicago.edu
What Programmers Want
Access to the system
       • Files and I/O
       • Processes
       • Threads
       • Networking


Use of existing libraries
       • Graphics
       • Databases
       • GUI


Mixing of languages
       • C or C++ controlled by a high level language




LL2, November 9, 2002                               3   beazley@cs.uchicago.edu
Extension Programming
Most HLL languages support "extensions"
       • Foreign function interface (FFI).
       • Provides access to compiled code (C, assembler, etc.)
       • However, you usually have to write wrappers


Example: Python
       extern int gcd(int x, int y);
       ...
       /* Python wrapper for gcd() */
       PyObject *wrap_gcd(PyObject *self, PyObject *args) {
           int x, y, r;
           if (!PyArg_ParseTuple(args,"ii",&x,&y)) {
                return NULL;
           }
           r = gcd(x,y);
           return Py_BuildValue("i",r);
       }



LL2, November 9, 2002                              4             beazley@cs.uchicago.edu
Extension Tools
Nobody likes writing wrappers

This has spawned an "extension tool" industry...
       • SWIG                               • SIP
       • Boost.Python                       • Matwrap
       • CABLE                              • tolua
       • h2xs/xsubpp                        • TclObj
       • Inline                             • ModularTcl
       • Vtk                                • Itcl++
       • pyfort                             • GRAD
       • f2py                               • Babel
       • gwrap                              • iVRS
       • Weave                              • Wrappy
       • Pyrex                              • ... and many others.

More extension tools than languages
       • Or so it seems...


LL2, November 9, 2002                   5                    beazley@cs.uchicago.edu
"Mondo" Extension Building
Mixed-language programming
       • Application programmers are combining C/C++/Fortran with a HLL
       • Examples: C++ driven by Python, Scheme, Perl, etc.


The utility of HLLs
       • Control and configuration.
       • User interfaces.
       • Rapid prototyping.
       • Debugging.
       • Testing.
       • Systems integration.


Examples of "mondo"
       • 427 classes, 4351 methods, 220 templates (SWIG mailing list, Oct. 2002)
       • 280 classes, 2500 methods, 45 templates (iVRS, USENIX Freenix’02)
       • 200 classes (Vtk, USENIX 4th Tcl/Tk Workshop, 1996)
       • 300 classes, 5750 methods/functions (SIP PyQT extension).
                                                 ,
LL2, November 9, 2002                             6                  beazley@cs.uchicago.edu
Automation
The only practical way to wrap large applications
       • Hand-written wrappers impossible to maintain.
       • Limited programmer resources.


A common approach: Header file parsing
       • Header files define interfaces in C/C++ (separate compilation)
       • A natural starting point in the eyes of many application programmers.
       • Variations of header parsing used in many different tools.


Extension tools are trying to do more than you think
       • Automatic wrapping of C++ class hierarchies.
       • Overloaded methods, functions, and operators.
       • Templates, STL integration.
       • Smart pointers, memory management.
       • Exception handling
       • Mixed-language polymorphism (terminology?)


LL2, November 9, 2002                               7                  beazley@cs.uchicago.edu
A Prototypical Extension Tool


       .h                  Parsing                          Wrapper                    .c
                            Hack                 ?         Generation
                                                            (printf)
                                             semantics
                                             (optional)



Tools mostly target the common case
       • Can generate useful wrappers with only a superficial approach
       • Simple string matching, regex matching, reduced grammars
       • Some heuristics
       • And some customization features (a.k.a, "workarounds")
       • Ignore the hard corner cases.


Parsing header files doesn’t seem that hard
LL2, November 9, 2002                                8                   beazley@cs.uchicago.edu
Death by Corner Cases
Do any of these tools actually work?
       • Well, it depends on your definition of "works."
       • Tools kind of work "well enough," but they could be a lot better.
       • The problem is very difficult to generalize.
       • Wrapper generation is different than compiling.


PL: Parsing header files will never work
       • Too hard (or bogus). Don’t do it.
       • Have to reimplement the C++ compiler (or instrument an existing one)
       • Not enough information in header files.


Users: Parsing header files might work
       • It seems to work pretty well in a lot of specific applications.
       • Programmers really like it (well, when it works).
       • Have viable alternatives been offered?



LL2, November 9, 2002                                  9                     beazley@cs.uchicago.edu
Parsing is the Wrong Focus
Header file parsing is only a convenient user-interface
       • Yes, users like it.


Extension building is not a parsing problem
       • Marshalling of types (representation).
       • Creating wrappers around objects (C++ types).
       • In fact, you are gluing two type systems together (in a practical sense)
       • Wrappers and parsing are merely implementation details.


Parsing alone is not enough to understand C++
       • Classes introduce "types"
       • The members of a class determine its behavior.
       • Templates/namespaces/typedef introduce very complicated relationships.
       • (Then again, maybe C++ can’t be understood by anything)




LL2, November 9, 2002                                10                  beazley@cs.uchicago.edu
The Classic Parsing Problem
Not enough information in headers:

                        void blah(int *x, int n);


                                 What is this?

       • An input value?
       • An array?
       • An output value?
       • An input/output value?
       • Is it somehow related to n?


Common workarounds (modifiers, naming conventions)
       void blah(%out int *x, int n);
       void blah(int *out_x, int n);

This has never been a major obstacle

LL2, November 9, 2002                            11   beazley@cs.uchicago.edu
Breaking The Common Case
What is this?


       extern binop_t   add, sub, mul, div;

A declaration of 4 global variables (obviously).




LL2, November 9, 2002                   12         beazley@cs.uchicago.edu
Breaking The Common Case
What is this?

       typedef int binop_t(int, int);
       extern binop_t add, sub, mul, div;

A declaration of 4 global variables (obviously).

Well, actually, it’s a declaration of 4 functions.
       • Okay, it’s perverse.


Question: what will a tool do?
       • A lot of tools will break.


==> You can’t just look for simple text patterns.



LL2, November 9, 2002                 13             beazley@cs.uchicago.edu
Breaking Typenames
Consider:
       namespace A {
           typedef int Integer;
       }
       class B {
       public:
           typedef A::Integer type;
       };
       template<class T> struct Foo {
           typedef typename T::type Footype;
       };
       using A::Integer;

       extern Foo<B>::Footype spam(Integer x, B::type y, int z);

Question: Can a tool figure out the types?
       • Not if it looks for simple names like "int"


==> Types aren’t strings and more than a table lookup.

LL2, November 9, 2002                                  14   beazley@cs.uchicago.edu
Breaking Classes
Consider a class:
       class Foo : public Bar {
       public:
             int blah(int x);
       };


Question: Can you create a Foo?
       • Can you create a wrapper that constructs a Foo?
       • Well, that depends. What is Bar?




LL2, November 9, 2002                             15       beazley@cs.uchicago.edu
Breaking Classes
Consider a class:
       class Foo : public Bar {
       public:
             int blah(int x);
       };


Question: Can you create a Foo?
       • Can you create a wrapper that constructs a Foo?
       • Well, that depends. What is Bar?


Examples:
       class Bar {                         class Bar {
       private:                            public:
           Bar();                             virtual void spam() = 0;
       };                                  };


==> Can’t look at classes in isolation

LL2, November 9, 2002                             16          beazley@cs.uchicago.edu
Overloading
What happens here?
       void foo(char *s);               // A
       void foo(double x);              // B
       void foo(int x);                 // C


Consider a language like Tcl
       • Typeless. All objects are strings.


Yet, some tools actually seem to work
       % foo 3              # Invokes C
       % foo 3.0            # Invokes B
       % foo hello          # Invokes A

A type hierarchy imposed on a typeless target (somehow)

==> Definitely more than parsing

LL2, November 9, 2002                          17   beazley@cs.uchicago.edu
Breaking Your Head
Advanced C++ idioms. Example: smart-pointers
       struct Foo {
           int x;
           int blah(int x);
       };
       template<class T> class SmartPtr {
           ...
           T *operator->();
           ...
       };
       extern SmartPtr<Foo> create_foo();


Now, in Python:
       >>> f = create_foo()
       >>> f.x                # Implicit dereference through
       3                      # operator->()
       >>> f.blah()

==> More than parsing (and possibly a bad idea).

LL2, November 9, 2002                  18             beazley@cs.uchicago.edu
Stepping Back
Wrapping C++ is nontrivial
       • You knew this.
       • Many corner cases.
       • Dark and obscure parts of the standard.
       • Even more complicated than C wrapping (which is already hard).


Parsing it is not enough
       • This, I am absolutely sure about.


Non-trivial interaction of several components
       • C++ types, target language, semantics, user-driven customization features.


My view:
       • Tool builders have been working on the problem at the wrong level
       • Reflects my experience with the SWIG project.


LL2, November 9, 2002                              19                  beazley@cs.uchicago.edu
An Alternative View


       .h                                                    Wrapper               .c
                                        Type System         Generation
                                     C++/Target Language
                                                             (printf)
                         Parsing
                          Hack
                        (optional)



A Better Approach
       • Build extension tools around "type" systems, not parsers.


It makes sense
       • Wrapper generation is a fundamentally a problem in type systems.
       • C++ is only understood through its type system.
       • It makes sense even if you don’t parse C++.

LL2, November 9, 2002                                20              beazley@cs.uchicago.edu
A Tale of Two Tools
Two type-based extension building tools
       • Boost.Python (http://www.boost.org/libs/python/doc)
       • SWIG (http://www.swig.org)


Both of these tools
       • Support a large subset of C++
       • Support a variety of advanced features (overloading, templates, etc.)
       • Both tools are primarily built around the C++ type system.
       • Both unconventional in approach.




LL2, November 9, 2002                               21                  beazley@cs.uchicago.edu
Boost.Python
Wrapper generation using the C++ compiler itself
       struct Foo {
           double x;
           Foo(double);
           int blah(int x, int y);
       };

       BOOST_PYTHON_MODULE_INIT(foomodule) {
           class_<Foo>("Foo")
              .def(init<double>())
              .def("blah", &Foo::blah)
              .def_readwrite("x",&Foo::x);
       }
What is this doing?
       • Using C++ template machinery to automatically generate wrappers to C++.
       • Template meta-programming.
       • There is no "tool" here. The C++ compiler is the tool.


C++ type system used to wrap C++ types.
LL2, November 9, 2002                            22                 beazley@cs.uchicago.edu
Boost.Python
This is very cool
       • It is able to handle very complicated C++
       • Especially templates, namespaces, other advanced features.
       • Didn’t require an implementation of a C++ parser or a C++ type system.


Why it’s interesting
       • Very unconventional (the only tool I know that works like this)
       • Operates completely within the C++ type system domain.
       • A good example of the right problem focus.




LL2, November 9, 2002                                23                    beazley@cs.uchicago.edu
SWIG
Wrappers generated through pattern matching rules

       // Typemap patterns (not supplied by user)
       %typemap(in) int { $1 = PyInt_AsLong($input); }
       %typemap(out) int { $result = PyInt_FromLong($1); }
       ...
       %typemap(in) double { $1 = PyFloat_AsDouble($input); }
       %typemap(in) char * { $1 = PyString_AsString($input); }
       ...

       // Code to wrap (supplied by user)
       class Foo {
           double x;
           Foo(double);
           int blah(int x, int y);
       };

Patterns built into the C++ type system.
       • The patterns provide code fragments and other details.


LL2, November 9, 2002                              24             beazley@cs.uchicago.edu
SWIG - Pattern Matching
       %typemap(in) int { $1 = PyInt_AsLong($input); }
       %typemap(out) int { $result = PyInt_FromLong($1); }

       namespace A {
           typedef int Integer;
       }
       class B {
       public:
           typedef A::Integer type;
       };
       template<class T> struct Foo {
           typedef typename T::type Footype;
       };
       using A::Integer;

       extern Foo<B>::Footype spam(Integer x, A::Integer y, int z);

Pattern matching is more than string matching



LL2, November 9, 2002                  25             beazley@cs.uchicago.edu
SWIG - Pattern Matching
Patterns may incorporate more than types
       • Argument names, typedef names, argument sequences

       %typemap(in) int positive { ... }
       %typemap(in) dnonzero_t { ... }
       %typemap(in) (char *str, int len) {
            $1 = PyString_AsString($input);
            $2 = PyString_Size($input);
       }
       ...
       typedef double dnonzero_t;
       extern int factorial(int positive);
       extern double inverse(dnonzero_t);
       extern int find(char *str, int len, char *pat);

There’s more to this, but that’s a different talk
       • Patterns used for specifying behavior (in, out, arrays, checking, etc.)
       • Typemaps can be incorporated into classes, templates, etc.
       • Declaration annotation.


LL2, November 9, 2002                                26                   beazley@cs.uchicago.edu
Big Picture
By shifting the focus to "types"...
       • It solves really hard problems (templates, namespaces, overloading, etc...)
       • It eliminates vast numbers of corner cases.
       • Greater reliability and simplified maintenance
       • It allows tool builders to focus on more important problems
       • Users really like it, but they don’t quite know why (it just "works").


But this work is incomplete and ongoing....
       • No tool is perfect or free of bugs.
       • There is definitely no "silver bullet."
       • Still fighting a very complicated, possibly intractable, but practical problem.




LL2, November 9, 2002                                 27                   beazley@cs.uchicago.edu
Where do we go from here?
There are hard problems that need to be resolved
       • Extension building tools are only starting to play with type systems.
       • I don’t think that anyone has quite figured it out.


Connection to programming languages
       • This is clearly a programming language issue (at least I think so)
       • However, it’s hard for us to make a precise connection to prior work.
       • If not type systems, what is the right connection?
       • Also strong ties to software engineering.


Obstacles
       • Cultural divide. Extension tools primarily built by applications programmers.
       • Language designers have better things to worry about.
       • A lot of work in FFIs primarily focused on C, representation issues.
       • "Wasn’t this problem dismissed/solved in 1971?"



LL2, November 9, 2002                                28                  beazley@cs.uchicago.edu
In Closing
To extension tool builders...
       • Parsing is not the most important problem.
       • Think about the C/C++ type system--that’s where the real action is.
       • You will like it.


To the programming languages community...
       • A lot of people are building extension tools. Pay attention.
       • This is an important practical problem to application programmers.
       • They don’t want to reimplement existing code and libraries.
       • There is an opportunity to make connections (and have impact).
       • Metaobject protocol? AOP? FFI’s? Type systems?


Acknowledgements
       • David Abrahams
       • William Fulton, Matthias Koeppe, Jason Stewart, Lyle Johnson, Richard Palmer,
       Luigi Ballabio, Sam Liddicott, Art Yerkes, Thien-Thi Nguyen, Marcelo Matus, Loic
       Dachary, Masaki Fukushima, and others.

LL2, November 9, 2002                               29                  beazley@cs.uchicago.edu
Ad

More Related Content

What's hot (20)

Generator Tricks for Systems Programmers, v2.0
Generator Tricks for Systems Programmers, v2.0Generator Tricks for Systems Programmers, v2.0
Generator Tricks for Systems Programmers, v2.0
David Beazley (Dabeaz LLC)
 
An Introduction to Python Concurrency
An Introduction to Python ConcurrencyAn Introduction to Python Concurrency
An Introduction to Python Concurrency
David Beazley (Dabeaz LLC)
 
Mastering Python 3 I/O (Version 2)
Mastering Python 3 I/O (Version 2)Mastering Python 3 I/O (Version 2)
Mastering Python 3 I/O (Version 2)
David Beazley (Dabeaz LLC)
 
In Search of the Perfect Global Interpreter Lock
In Search of the Perfect Global Interpreter LockIn Search of the Perfect Global Interpreter Lock
In Search of the Perfect Global Interpreter Lock
David Beazley (Dabeaz LLC)
 
Python Generator Hacking
Python Generator HackingPython Generator Hacking
Python Generator Hacking
David Beazley (Dabeaz LLC)
 
Interfacing C/C++ and Python with SWIG
Interfacing C/C++ and Python with SWIGInterfacing C/C++ and Python with SWIG
Interfacing C/C++ and Python with SWIG
David Beazley (Dabeaz LLC)
 
WAD : A Module for Converting Fatal Extension Errors into Python Exceptions
WAD : A Module for Converting Fatal Extension Errors into Python ExceptionsWAD : A Module for Converting Fatal Extension Errors into Python Exceptions
WAD : A Module for Converting Fatal Extension Errors into Python Exceptions
David Beazley (Dabeaz LLC)
 
Ry pyconjp2015 karaoke
Ry pyconjp2015 karaokeRy pyconjp2015 karaoke
Ry pyconjp2015 karaoke
Renyuan Lyu
 
An Embedded Error Recovery and Debugging Mechanism for Scripting Language Ext...
An Embedded Error Recovery and Debugging Mechanism for Scripting Language Ext...An Embedded Error Recovery and Debugging Mechanism for Scripting Language Ext...
An Embedded Error Recovery and Debugging Mechanism for Scripting Language Ext...
David Beazley (Dabeaz LLC)
 
groovy DSLs from beginner to expert
groovy DSLs from beginner to expertgroovy DSLs from beginner to expert
groovy DSLs from beginner to expert
Paul King
 
Executable Bloat - How it happens and how we can fight it
Executable Bloat - How it happens and how we can fight itExecutable Bloat - How it happens and how we can fight it
Executable Bloat - How it happens and how we can fight it
Electronic Arts / DICE
 
Tales@tdc
Tales@tdcTales@tdc
Tales@tdc
Tales Andrade
 
Bar Camp Auckland - Mongo DB Presentation BCA4
Bar Camp Auckland - Mongo DB Presentation BCA4Bar Camp Auckland - Mongo DB Presentation BCA4
Bar Camp Auckland - Mongo DB Presentation BCA4
John Ballinger
 
Thinking hard about_python
Thinking hard about_pythonThinking hard about_python
Thinking hard about_python
Daniel Greenfeld
 
Testing Apache Modules with Python and Ctypes
Testing Apache Modules with Python and CtypesTesting Apache Modules with Python and Ctypes
Testing Apache Modules with Python and Ctypes
Markus Litz
 
Puppet Virtual Bolt Workshop - 23 April 2020 (Singapore)
Puppet Virtual Bolt Workshop - 23 April 2020 (Singapore)Puppet Virtual Bolt Workshop - 23 April 2020 (Singapore)
Puppet Virtual Bolt Workshop - 23 April 2020 (Singapore)
Puppet
 
Sphinx autodoc - automated API documentation (PyCon APAC 2015 in Taiwan)
Sphinx autodoc - automated API documentation (PyCon APAC 2015 in Taiwan)Sphinx autodoc - automated API documentation (PyCon APAC 2015 in Taiwan)
Sphinx autodoc - automated API documentation (PyCon APAC 2015 in Taiwan)
Takayuki Shimizukawa
 
Chapter08
Chapter08Chapter08
Chapter08
Robbie AkaChopa
 
Everything is Permitted: Extending Built-ins
Everything is Permitted: Extending Built-insEverything is Permitted: Extending Built-ins
Everything is Permitted: Extending Built-ins
Andrew Dupont
 
Dojo for programmers (TXJS 2010)
Dojo for programmers (TXJS 2010)Dojo for programmers (TXJS 2010)
Dojo for programmers (TXJS 2010)
Eugene Lazutkin
 
Generator Tricks for Systems Programmers, v2.0
Generator Tricks for Systems Programmers, v2.0Generator Tricks for Systems Programmers, v2.0
Generator Tricks for Systems Programmers, v2.0
David Beazley (Dabeaz LLC)
 
In Search of the Perfect Global Interpreter Lock
In Search of the Perfect Global Interpreter LockIn Search of the Perfect Global Interpreter Lock
In Search of the Perfect Global Interpreter Lock
David Beazley (Dabeaz LLC)
 
WAD : A Module for Converting Fatal Extension Errors into Python Exceptions
WAD : A Module for Converting Fatal Extension Errors into Python ExceptionsWAD : A Module for Converting Fatal Extension Errors into Python Exceptions
WAD : A Module for Converting Fatal Extension Errors into Python Exceptions
David Beazley (Dabeaz LLC)
 
Ry pyconjp2015 karaoke
Ry pyconjp2015 karaokeRy pyconjp2015 karaoke
Ry pyconjp2015 karaoke
Renyuan Lyu
 
An Embedded Error Recovery and Debugging Mechanism for Scripting Language Ext...
An Embedded Error Recovery and Debugging Mechanism for Scripting Language Ext...An Embedded Error Recovery and Debugging Mechanism for Scripting Language Ext...
An Embedded Error Recovery and Debugging Mechanism for Scripting Language Ext...
David Beazley (Dabeaz LLC)
 
groovy DSLs from beginner to expert
groovy DSLs from beginner to expertgroovy DSLs from beginner to expert
groovy DSLs from beginner to expert
Paul King
 
Executable Bloat - How it happens and how we can fight it
Executable Bloat - How it happens and how we can fight itExecutable Bloat - How it happens and how we can fight it
Executable Bloat - How it happens and how we can fight it
Electronic Arts / DICE
 
Bar Camp Auckland - Mongo DB Presentation BCA4
Bar Camp Auckland - Mongo DB Presentation BCA4Bar Camp Auckland - Mongo DB Presentation BCA4
Bar Camp Auckland - Mongo DB Presentation BCA4
John Ballinger
 
Thinking hard about_python
Thinking hard about_pythonThinking hard about_python
Thinking hard about_python
Daniel Greenfeld
 
Testing Apache Modules with Python and Ctypes
Testing Apache Modules with Python and CtypesTesting Apache Modules with Python and Ctypes
Testing Apache Modules with Python and Ctypes
Markus Litz
 
Puppet Virtual Bolt Workshop - 23 April 2020 (Singapore)
Puppet Virtual Bolt Workshop - 23 April 2020 (Singapore)Puppet Virtual Bolt Workshop - 23 April 2020 (Singapore)
Puppet Virtual Bolt Workshop - 23 April 2020 (Singapore)
Puppet
 
Sphinx autodoc - automated API documentation (PyCon APAC 2015 in Taiwan)
Sphinx autodoc - automated API documentation (PyCon APAC 2015 in Taiwan)Sphinx autodoc - automated API documentation (PyCon APAC 2015 in Taiwan)
Sphinx autodoc - automated API documentation (PyCon APAC 2015 in Taiwan)
Takayuki Shimizukawa
 
Everything is Permitted: Extending Built-ins
Everything is Permitted: Extending Built-insEverything is Permitted: Extending Built-ins
Everything is Permitted: Extending Built-ins
Andrew Dupont
 
Dojo for programmers (TXJS 2010)
Dojo for programmers (TXJS 2010)Dojo for programmers (TXJS 2010)
Dojo for programmers (TXJS 2010)
Eugene Lazutkin
 

Viewers also liked (6)

Generators: The Final Frontier
Generators: The Final FrontierGenerators: The Final Frontier
Generators: The Final Frontier
David Beazley (Dabeaz LLC)
 
Writing Parsers and Compilers with PLY
Writing Parsers and Compilers with PLYWriting Parsers and Compilers with PLY
Writing Parsers and Compilers with PLY
David Beazley (Dabeaz LLC)
 
Ctypes в игровых приложениях на python
Ctypes в игровых приложениях на pythonCtypes в игровых приложениях на python
Ctypes в игровых приложениях на python
PyNSK
 
A Curious Course on Coroutines and Concurrency
A Curious Course on Coroutines and ConcurrencyA Curious Course on Coroutines and Concurrency
A Curious Course on Coroutines and Concurrency
David Beazley (Dabeaz LLC)
 
Using Python3 to Build a Cloud Computing Service for my Superboard II
Using Python3 to Build a Cloud Computing Service for my Superboard IIUsing Python3 to Build a Cloud Computing Service for my Superboard II
Using Python3 to Build a Cloud Computing Service for my Superboard II
David Beazley (Dabeaz LLC)
 
Подключение внешних библиотек в python
Подключение внешних библиотек в pythonПодключение внешних библиотек в python
Подключение внешних библиотек в python
Maxim Shalamov
 
Ctypes в игровых приложениях на python
Ctypes в игровых приложениях на pythonCtypes в игровых приложениях на python
Ctypes в игровых приложениях на python
PyNSK
 
A Curious Course on Coroutines and Concurrency
A Curious Course on Coroutines and ConcurrencyA Curious Course on Coroutines and Concurrency
A Curious Course on Coroutines and Concurrency
David Beazley (Dabeaz LLC)
 
Using Python3 to Build a Cloud Computing Service for my Superboard II
Using Python3 to Build a Cloud Computing Service for my Superboard IIUsing Python3 to Build a Cloud Computing Service for my Superboard II
Using Python3 to Build a Cloud Computing Service for my Superboard II
David Beazley (Dabeaz LLC)
 
Подключение внешних библиотек в python
Подключение внешних библиотек в pythonПодключение внешних библиотек в python
Подключение внешних библиотек в python
Maxim Shalamov
 
Ad

Similar to Why Extension Programmers Should Stop Worrying About Parsing and Start Thinking about Type Systems (20)

Are High Level Programming Languages for Multicore and Safety Critical Conver...
Are High Level Programming Languages for Multicore and Safety Critical Conver...Are High Level Programming Languages for Multicore and Safety Critical Conver...
Are High Level Programming Languages for Multicore and Safety Critical Conver...
InfinIT - Innovationsnetværket for it
 
2015 bioinformatics python_introduction_wim_vancriekinge_vfinal
2015 bioinformatics python_introduction_wim_vancriekinge_vfinal2015 bioinformatics python_introduction_wim_vancriekinge_vfinal
2015 bioinformatics python_introduction_wim_vancriekinge_vfinal
Prof. Wim Van Criekinge
 
James Coplien - Trygve - October 17, 2016
James Coplien - Trygve - October 17, 2016James Coplien - Trygve - October 17, 2016
James Coplien - Trygve - October 17, 2016
Foo Café Copenhagen
 
An introduction to Julia
An introduction to JuliaAn introduction to Julia
An introduction to Julia
Jiahao Chen
 
Metamorphic Domain-Specific Languages
Metamorphic Domain-Specific LanguagesMetamorphic Domain-Specific Languages
Metamorphic Domain-Specific Languages
University of Rennes, INSA Rennes, Inria/IRISA, CNRS
 
DSL's with Groovy
DSL's with GroovyDSL's with Groovy
DSL's with Groovy
paulbowler
 
TypeScript . the JavaScript developer best friend!
TypeScript . the JavaScript developer best friend!TypeScript . the JavaScript developer best friend!
TypeScript . the JavaScript developer best friend!
Alessandro Giorgetti
 
Illustrated Code (ASE 2021)
Illustrated Code (ASE 2021)Illustrated Code (ASE 2021)
Illustrated Code (ASE 2021)
CISPA Helmholtz Center for Information Security
 
14_Ed_Symp_Open_Source
14_Ed_Symp_Open_Source14_Ed_Symp_Open_Source
14_Ed_Symp_Open_Source
Steve Arnold
 
Open Source Design at Ignite lightning talk
Open Source Design at Ignite lightning talkOpen Source Design at Ignite lightning talk
Open Source Design at Ignite lightning talk
Mushon Zer-Aviv
 
Java basics
Java basicsJava basics
Java basics
Hoang Nguyen
 
Orthogonality: A Strategy for Reusable Code
Orthogonality: A Strategy for Reusable CodeOrthogonality: A Strategy for Reusable Code
Orthogonality: A Strategy for Reusable Code
rsebbe
 
Intro to Python for C# Developers
Intro to Python for C# DevelopersIntro to Python for C# Developers
Intro to Python for C# Developers
Sarah Dutkiewicz
 
The Big Picture
The Big PictureThe Big Picture
The Big Picture
Munazza-Mah-Jabeen
 
2023-My AI Experience - Colm Dunphy.pdf
2023-My AI Experience - Colm Dunphy.pdf2023-My AI Experience - Colm Dunphy.pdf
2023-My AI Experience - Colm Dunphy.pdf
Colm Dunphy
 
Programming Languages #devcon2013
Programming Languages #devcon2013Programming Languages #devcon2013
Programming Languages #devcon2013
Iván Montes
 
Documentation An Engineering Problem Unsolved
Documentation  An Engineering Problem UnsolvedDocumentation  An Engineering Problem Unsolved
Documentation An Engineering Problem Unsolved
Schalk Cronjé
 
6_Object-oriented-using-java.pdf object oriented programming concepts
6_Object-oriented-using-java.pdf object oriented programming concepts6_Object-oriented-using-java.pdf object oriented programming concepts
6_Object-oriented-using-java.pdf object oriented programming concepts
harinipradeep15
 
C-and-Cpp-Brochure-English. .
C-and-Cpp-Brochure-English.               .C-and-Cpp-Brochure-English.               .
C-and-Cpp-Brochure-English. .
spotguys705
 
Fossetcon15
Fossetcon15Fossetcon15
Fossetcon15
Dru Lavigne
 
Are High Level Programming Languages for Multicore and Safety Critical Conver...
Are High Level Programming Languages for Multicore and Safety Critical Conver...Are High Level Programming Languages for Multicore and Safety Critical Conver...
Are High Level Programming Languages for Multicore and Safety Critical Conver...
InfinIT - Innovationsnetværket for it
 
2015 bioinformatics python_introduction_wim_vancriekinge_vfinal
2015 bioinformatics python_introduction_wim_vancriekinge_vfinal2015 bioinformatics python_introduction_wim_vancriekinge_vfinal
2015 bioinformatics python_introduction_wim_vancriekinge_vfinal
Prof. Wim Van Criekinge
 
James Coplien - Trygve - October 17, 2016
James Coplien - Trygve - October 17, 2016James Coplien - Trygve - October 17, 2016
James Coplien - Trygve - October 17, 2016
Foo Café Copenhagen
 
An introduction to Julia
An introduction to JuliaAn introduction to Julia
An introduction to Julia
Jiahao Chen
 
DSL's with Groovy
DSL's with GroovyDSL's with Groovy
DSL's with Groovy
paulbowler
 
TypeScript . the JavaScript developer best friend!
TypeScript . the JavaScript developer best friend!TypeScript . the JavaScript developer best friend!
TypeScript . the JavaScript developer best friend!
Alessandro Giorgetti
 
14_Ed_Symp_Open_Source
14_Ed_Symp_Open_Source14_Ed_Symp_Open_Source
14_Ed_Symp_Open_Source
Steve Arnold
 
Open Source Design at Ignite lightning talk
Open Source Design at Ignite lightning talkOpen Source Design at Ignite lightning talk
Open Source Design at Ignite lightning talk
Mushon Zer-Aviv
 
Orthogonality: A Strategy for Reusable Code
Orthogonality: A Strategy for Reusable CodeOrthogonality: A Strategy for Reusable Code
Orthogonality: A Strategy for Reusable Code
rsebbe
 
Intro to Python for C# Developers
Intro to Python for C# DevelopersIntro to Python for C# Developers
Intro to Python for C# Developers
Sarah Dutkiewicz
 
2023-My AI Experience - Colm Dunphy.pdf
2023-My AI Experience - Colm Dunphy.pdf2023-My AI Experience - Colm Dunphy.pdf
2023-My AI Experience - Colm Dunphy.pdf
Colm Dunphy
 
Programming Languages #devcon2013
Programming Languages #devcon2013Programming Languages #devcon2013
Programming Languages #devcon2013
Iván Montes
 
Documentation An Engineering Problem Unsolved
Documentation  An Engineering Problem UnsolvedDocumentation  An Engineering Problem Unsolved
Documentation An Engineering Problem Unsolved
Schalk Cronjé
 
6_Object-oriented-using-java.pdf object oriented programming concepts
6_Object-oriented-using-java.pdf object oriented programming concepts6_Object-oriented-using-java.pdf object oriented programming concepts
6_Object-oriented-using-java.pdf object oriented programming concepts
harinipradeep15
 
C-and-Cpp-Brochure-English. .
C-and-Cpp-Brochure-English.               .C-and-Cpp-Brochure-English.               .
C-and-Cpp-Brochure-English. .
spotguys705
 
Ad

Recently uploaded (20)

HCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-UmgebungenHCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
panagenda
 
SAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdf
SAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdfSAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdf
SAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdf
Precisely
 
AI and Data Privacy in 2025: Global Trends
AI and Data Privacy in 2025: Global TrendsAI and Data Privacy in 2025: Global Trends
AI and Data Privacy in 2025: Global Trends
InData Labs
 
Drupalcamp Finland – Measuring Front-end Energy Consumption
Drupalcamp Finland – Measuring Front-end Energy ConsumptionDrupalcamp Finland – Measuring Front-end Energy Consumption
Drupalcamp Finland – Measuring Front-end Energy Consumption
Exove
 
Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...
Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...
Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...
Noah Loul
 
AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...
AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...
AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...
SOFTTECHHUB
 
Special Meetup Edition - TDX Bengaluru Meetup #52.pptx
Special Meetup Edition - TDX Bengaluru Meetup #52.pptxSpecial Meetup Edition - TDX Bengaluru Meetup #52.pptx
Special Meetup Edition - TDX Bengaluru Meetup #52.pptx
shyamraj55
 
Semantic Cultivators : The Critical Future Role to Enable AI
Semantic Cultivators : The Critical Future Role to Enable AISemantic Cultivators : The Critical Future Role to Enable AI
Semantic Cultivators : The Critical Future Role to Enable AI
artmondano
 
Dev Dives: Automate and orchestrate your processes with UiPath Maestro
Dev Dives: Automate and orchestrate your processes with UiPath MaestroDev Dives: Automate and orchestrate your processes with UiPath Maestro
Dev Dives: Automate and orchestrate your processes with UiPath Maestro
UiPathCommunity
 
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc
 
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Impelsys Inc.
 
Into The Box Conference Keynote Day 1 (ITB2025)
Into The Box Conference Keynote Day 1 (ITB2025)Into The Box Conference Keynote Day 1 (ITB2025)
Into The Box Conference Keynote Day 1 (ITB2025)
Ortus Solutions, Corp
 
Cyber Awareness overview for 2025 month of security
Cyber Awareness overview for 2025 month of securityCyber Awareness overview for 2025 month of security
Cyber Awareness overview for 2025 month of security
riccardosl1
 
UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager APIUiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPathCommunity
 
How analogue intelligence complements AI
How analogue intelligence complements AIHow analogue intelligence complements AI
How analogue intelligence complements AI
Paul Rowe
 
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep DiveDesigning Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
ScyllaDB
 
2025-05-Q4-2024-Investor-Presentation.pptx
2025-05-Q4-2024-Investor-Presentation.pptx2025-05-Q4-2024-Investor-Presentation.pptx
2025-05-Q4-2024-Investor-Presentation.pptx
Samuele Fogagnolo
 
tecnologias de las primeras civilizaciones.pdf
tecnologias de las primeras civilizaciones.pdftecnologias de las primeras civilizaciones.pdf
tecnologias de las primeras civilizaciones.pdf
fjgm517
 
DevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptx
DevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptxDevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptx
DevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptx
Justin Reock
 
Quantum Computing Quick Research Guide by Arthur Morgan
Quantum Computing Quick Research Guide by Arthur MorganQuantum Computing Quick Research Guide by Arthur Morgan
Quantum Computing Quick Research Guide by Arthur Morgan
Arthur Morgan
 
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-UmgebungenHCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
panagenda
 
SAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdf
SAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdfSAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdf
SAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdf
Precisely
 
AI and Data Privacy in 2025: Global Trends
AI and Data Privacy in 2025: Global TrendsAI and Data Privacy in 2025: Global Trends
AI and Data Privacy in 2025: Global Trends
InData Labs
 
Drupalcamp Finland – Measuring Front-end Energy Consumption
Drupalcamp Finland – Measuring Front-end Energy ConsumptionDrupalcamp Finland – Measuring Front-end Energy Consumption
Drupalcamp Finland – Measuring Front-end Energy Consumption
Exove
 
Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...
Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...
Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...
Noah Loul
 
AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...
AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...
AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...
SOFTTECHHUB
 
Special Meetup Edition - TDX Bengaluru Meetup #52.pptx
Special Meetup Edition - TDX Bengaluru Meetup #52.pptxSpecial Meetup Edition - TDX Bengaluru Meetup #52.pptx
Special Meetup Edition - TDX Bengaluru Meetup #52.pptx
shyamraj55
 
Semantic Cultivators : The Critical Future Role to Enable AI
Semantic Cultivators : The Critical Future Role to Enable AISemantic Cultivators : The Critical Future Role to Enable AI
Semantic Cultivators : The Critical Future Role to Enable AI
artmondano
 
Dev Dives: Automate and orchestrate your processes with UiPath Maestro
Dev Dives: Automate and orchestrate your processes with UiPath MaestroDev Dives: Automate and orchestrate your processes with UiPath Maestro
Dev Dives: Automate and orchestrate your processes with UiPath Maestro
UiPathCommunity
 
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc
 
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Impelsys Inc.
 
Into The Box Conference Keynote Day 1 (ITB2025)
Into The Box Conference Keynote Day 1 (ITB2025)Into The Box Conference Keynote Day 1 (ITB2025)
Into The Box Conference Keynote Day 1 (ITB2025)
Ortus Solutions, Corp
 
Cyber Awareness overview for 2025 month of security
Cyber Awareness overview for 2025 month of securityCyber Awareness overview for 2025 month of security
Cyber Awareness overview for 2025 month of security
riccardosl1
 
UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager APIUiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPathCommunity
 
How analogue intelligence complements AI
How analogue intelligence complements AIHow analogue intelligence complements AI
How analogue intelligence complements AI
Paul Rowe
 
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep DiveDesigning Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
ScyllaDB
 
2025-05-Q4-2024-Investor-Presentation.pptx
2025-05-Q4-2024-Investor-Presentation.pptx2025-05-Q4-2024-Investor-Presentation.pptx
2025-05-Q4-2024-Investor-Presentation.pptx
Samuele Fogagnolo
 
tecnologias de las primeras civilizaciones.pdf
tecnologias de las primeras civilizaciones.pdftecnologias de las primeras civilizaciones.pdf
tecnologias de las primeras civilizaciones.pdf
fjgm517
 
DevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptx
DevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptxDevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptx
DevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptx
Justin Reock
 
Quantum Computing Quick Research Guide by Arthur Morgan
Quantum Computing Quick Research Guide by Arthur MorganQuantum Computing Quick Research Guide by Arthur Morgan
Quantum Computing Quick Research Guide by Arthur Morgan
Arthur Morgan
 

Why Extension Programmers Should Stop Worrying About Parsing and Start Thinking about Type Systems

  • 1. PY-MA Why Extension Programmers Should Stop Worrying About Parsing and Start Thinking About Type Systems (or automatic extension building is harder than it looks, but that hasn’t stopped anyone from trying to do it) David M. Beazley Department of Computer Science The University of Chicago [email protected] November 9, 2002 LL2, November 9, 2002 1 [email protected]
  • 2. Background I work on programming tools and applications • SWIG project (www.swig.org), 1995-present. • Scientific software (molecular dynamics, climate modeling) • And some other projects. I am not a programming languages researcher • Which is why I am here LL2, November 9, 2002 2 [email protected]
  • 3. What Programmers Want Access to the system • Files and I/O • Processes • Threads • Networking Use of existing libraries • Graphics • Databases • GUI Mixing of languages • C or C++ controlled by a high level language LL2, November 9, 2002 3 [email protected]
  • 4. Extension Programming Most HLL languages support "extensions" • Foreign function interface (FFI). • Provides access to compiled code (C, assembler, etc.) • However, you usually have to write wrappers Example: Python extern int gcd(int x, int y); ... /* Python wrapper for gcd() */ PyObject *wrap_gcd(PyObject *self, PyObject *args) { int x, y, r; if (!PyArg_ParseTuple(args,"ii",&x,&y)) { return NULL; } r = gcd(x,y); return Py_BuildValue("i",r); } LL2, November 9, 2002 4 [email protected]
  • 5. Extension Tools Nobody likes writing wrappers This has spawned an "extension tool" industry... • SWIG • SIP • Boost.Python • Matwrap • CABLE • tolua • h2xs/xsubpp • TclObj • Inline • ModularTcl • Vtk • Itcl++ • pyfort • GRAD • f2py • Babel • gwrap • iVRS • Weave • Wrappy • Pyrex • ... and many others. More extension tools than languages • Or so it seems... LL2, November 9, 2002 5 [email protected]
  • 6. "Mondo" Extension Building Mixed-language programming • Application programmers are combining C/C++/Fortran with a HLL • Examples: C++ driven by Python, Scheme, Perl, etc. The utility of HLLs • Control and configuration. • User interfaces. • Rapid prototyping. • Debugging. • Testing. • Systems integration. Examples of "mondo" • 427 classes, 4351 methods, 220 templates (SWIG mailing list, Oct. 2002) • 280 classes, 2500 methods, 45 templates (iVRS, USENIX Freenix’02) • 200 classes (Vtk, USENIX 4th Tcl/Tk Workshop, 1996) • 300 classes, 5750 methods/functions (SIP PyQT extension). , LL2, November 9, 2002 6 [email protected]
  • 7. Automation The only practical way to wrap large applications • Hand-written wrappers impossible to maintain. • Limited programmer resources. A common approach: Header file parsing • Header files define interfaces in C/C++ (separate compilation) • A natural starting point in the eyes of many application programmers. • Variations of header parsing used in many different tools. Extension tools are trying to do more than you think • Automatic wrapping of C++ class hierarchies. • Overloaded methods, functions, and operators. • Templates, STL integration. • Smart pointers, memory management. • Exception handling • Mixed-language polymorphism (terminology?) LL2, November 9, 2002 7 [email protected]
  • 8. A Prototypical Extension Tool .h Parsing Wrapper .c Hack ? Generation (printf) semantics (optional) Tools mostly target the common case • Can generate useful wrappers with only a superficial approach • Simple string matching, regex matching, reduced grammars • Some heuristics • And some customization features (a.k.a, "workarounds") • Ignore the hard corner cases. Parsing header files doesn’t seem that hard LL2, November 9, 2002 8 [email protected]
  • 9. Death by Corner Cases Do any of these tools actually work? • Well, it depends on your definition of "works." • Tools kind of work "well enough," but they could be a lot better. • The problem is very difficult to generalize. • Wrapper generation is different than compiling. PL: Parsing header files will never work • Too hard (or bogus). Don’t do it. • Have to reimplement the C++ compiler (or instrument an existing one) • Not enough information in header files. Users: Parsing header files might work • It seems to work pretty well in a lot of specific applications. • Programmers really like it (well, when it works). • Have viable alternatives been offered? LL2, November 9, 2002 9 [email protected]
  • 10. Parsing is the Wrong Focus Header file parsing is only a convenient user-interface • Yes, users like it. Extension building is not a parsing problem • Marshalling of types (representation). • Creating wrappers around objects (C++ types). • In fact, you are gluing two type systems together (in a practical sense) • Wrappers and parsing are merely implementation details. Parsing alone is not enough to understand C++ • Classes introduce "types" • The members of a class determine its behavior. • Templates/namespaces/typedef introduce very complicated relationships. • (Then again, maybe C++ can’t be understood by anything) LL2, November 9, 2002 10 [email protected]
  • 11. The Classic Parsing Problem Not enough information in headers: void blah(int *x, int n); What is this? • An input value? • An array? • An output value? • An input/output value? • Is it somehow related to n? Common workarounds (modifiers, naming conventions) void blah(%out int *x, int n); void blah(int *out_x, int n); This has never been a major obstacle LL2, November 9, 2002 11 [email protected]
  • 12. Breaking The Common Case What is this? extern binop_t add, sub, mul, div; A declaration of 4 global variables (obviously). LL2, November 9, 2002 12 [email protected]
  • 13. Breaking The Common Case What is this? typedef int binop_t(int, int); extern binop_t add, sub, mul, div; A declaration of 4 global variables (obviously). Well, actually, it’s a declaration of 4 functions. • Okay, it’s perverse. Question: what will a tool do? • A lot of tools will break. ==> You can’t just look for simple text patterns. LL2, November 9, 2002 13 [email protected]
  • 14. Breaking Typenames Consider: namespace A { typedef int Integer; } class B { public: typedef A::Integer type; }; template<class T> struct Foo { typedef typename T::type Footype; }; using A::Integer; extern Foo<B>::Footype spam(Integer x, B::type y, int z); Question: Can a tool figure out the types? • Not if it looks for simple names like "int" ==> Types aren’t strings and more than a table lookup. LL2, November 9, 2002 14 [email protected]
  • 15. Breaking Classes Consider a class: class Foo : public Bar { public: int blah(int x); }; Question: Can you create a Foo? • Can you create a wrapper that constructs a Foo? • Well, that depends. What is Bar? LL2, November 9, 2002 15 [email protected]
  • 16. Breaking Classes Consider a class: class Foo : public Bar { public: int blah(int x); }; Question: Can you create a Foo? • Can you create a wrapper that constructs a Foo? • Well, that depends. What is Bar? Examples: class Bar { class Bar { private: public: Bar(); virtual void spam() = 0; }; }; ==> Can’t look at classes in isolation LL2, November 9, 2002 16 [email protected]
  • 17. Overloading What happens here? void foo(char *s); // A void foo(double x); // B void foo(int x); // C Consider a language like Tcl • Typeless. All objects are strings. Yet, some tools actually seem to work % foo 3 # Invokes C % foo 3.0 # Invokes B % foo hello # Invokes A A type hierarchy imposed on a typeless target (somehow) ==> Definitely more than parsing LL2, November 9, 2002 17 [email protected]
  • 18. Breaking Your Head Advanced C++ idioms. Example: smart-pointers struct Foo { int x; int blah(int x); }; template<class T> class SmartPtr { ... T *operator->(); ... }; extern SmartPtr<Foo> create_foo(); Now, in Python: >>> f = create_foo() >>> f.x # Implicit dereference through 3 # operator->() >>> f.blah() ==> More than parsing (and possibly a bad idea). LL2, November 9, 2002 18 [email protected]
  • 19. Stepping Back Wrapping C++ is nontrivial • You knew this. • Many corner cases. • Dark and obscure parts of the standard. • Even more complicated than C wrapping (which is already hard). Parsing it is not enough • This, I am absolutely sure about. Non-trivial interaction of several components • C++ types, target language, semantics, user-driven customization features. My view: • Tool builders have been working on the problem at the wrong level • Reflects my experience with the SWIG project. LL2, November 9, 2002 19 [email protected]
  • 20. An Alternative View .h Wrapper .c Type System Generation C++/Target Language (printf) Parsing Hack (optional) A Better Approach • Build extension tools around "type" systems, not parsers. It makes sense • Wrapper generation is a fundamentally a problem in type systems. • C++ is only understood through its type system. • It makes sense even if you don’t parse C++. LL2, November 9, 2002 20 [email protected]
  • 21. A Tale of Two Tools Two type-based extension building tools • Boost.Python (http://www.boost.org/libs/python/doc) • SWIG (http://www.swig.org) Both of these tools • Support a large subset of C++ • Support a variety of advanced features (overloading, templates, etc.) • Both tools are primarily built around the C++ type system. • Both unconventional in approach. LL2, November 9, 2002 21 [email protected]
  • 22. Boost.Python Wrapper generation using the C++ compiler itself struct Foo { double x; Foo(double); int blah(int x, int y); }; BOOST_PYTHON_MODULE_INIT(foomodule) { class_<Foo>("Foo") .def(init<double>()) .def("blah", &Foo::blah) .def_readwrite("x",&Foo::x); } What is this doing? • Using C++ template machinery to automatically generate wrappers to C++. • Template meta-programming. • There is no "tool" here. The C++ compiler is the tool. C++ type system used to wrap C++ types. LL2, November 9, 2002 22 [email protected]
  • 23. Boost.Python This is very cool • It is able to handle very complicated C++ • Especially templates, namespaces, other advanced features. • Didn’t require an implementation of a C++ parser or a C++ type system. Why it’s interesting • Very unconventional (the only tool I know that works like this) • Operates completely within the C++ type system domain. • A good example of the right problem focus. LL2, November 9, 2002 23 [email protected]
  • 24. SWIG Wrappers generated through pattern matching rules // Typemap patterns (not supplied by user) %typemap(in) int { $1 = PyInt_AsLong($input); } %typemap(out) int { $result = PyInt_FromLong($1); } ... %typemap(in) double { $1 = PyFloat_AsDouble($input); } %typemap(in) char * { $1 = PyString_AsString($input); } ... // Code to wrap (supplied by user) class Foo { double x; Foo(double); int blah(int x, int y); }; Patterns built into the C++ type system. • The patterns provide code fragments and other details. LL2, November 9, 2002 24 [email protected]
  • 25. SWIG - Pattern Matching %typemap(in) int { $1 = PyInt_AsLong($input); } %typemap(out) int { $result = PyInt_FromLong($1); } namespace A { typedef int Integer; } class B { public: typedef A::Integer type; }; template<class T> struct Foo { typedef typename T::type Footype; }; using A::Integer; extern Foo<B>::Footype spam(Integer x, A::Integer y, int z); Pattern matching is more than string matching LL2, November 9, 2002 25 [email protected]
  • 26. SWIG - Pattern Matching Patterns may incorporate more than types • Argument names, typedef names, argument sequences %typemap(in) int positive { ... } %typemap(in) dnonzero_t { ... } %typemap(in) (char *str, int len) { $1 = PyString_AsString($input); $2 = PyString_Size($input); } ... typedef double dnonzero_t; extern int factorial(int positive); extern double inverse(dnonzero_t); extern int find(char *str, int len, char *pat); There’s more to this, but that’s a different talk • Patterns used for specifying behavior (in, out, arrays, checking, etc.) • Typemaps can be incorporated into classes, templates, etc. • Declaration annotation. LL2, November 9, 2002 26 [email protected]
  • 27. Big Picture By shifting the focus to "types"... • It solves really hard problems (templates, namespaces, overloading, etc...) • It eliminates vast numbers of corner cases. • Greater reliability and simplified maintenance • It allows tool builders to focus on more important problems • Users really like it, but they don’t quite know why (it just "works"). But this work is incomplete and ongoing.... • No tool is perfect or free of bugs. • There is definitely no "silver bullet." • Still fighting a very complicated, possibly intractable, but practical problem. LL2, November 9, 2002 27 [email protected]
  • 28. Where do we go from here? There are hard problems that need to be resolved • Extension building tools are only starting to play with type systems. • I don’t think that anyone has quite figured it out. Connection to programming languages • This is clearly a programming language issue (at least I think so) • However, it’s hard for us to make a precise connection to prior work. • If not type systems, what is the right connection? • Also strong ties to software engineering. Obstacles • Cultural divide. Extension tools primarily built by applications programmers. • Language designers have better things to worry about. • A lot of work in FFIs primarily focused on C, representation issues. • "Wasn’t this problem dismissed/solved in 1971?" LL2, November 9, 2002 28 [email protected]
  • 29. In Closing To extension tool builders... • Parsing is not the most important problem. • Think about the C/C++ type system--that’s where the real action is. • You will like it. To the programming languages community... • A lot of people are building extension tools. Pay attention. • This is an important practical problem to application programmers. • They don’t want to reimplement existing code and libraries. • There is an opportunity to make connections (and have impact). • Metaobject protocol? AOP? FFI’s? Type systems? Acknowledgements • David Abrahams • William Fulton, Matthias Koeppe, Jason Stewart, Lyle Johnson, Richard Palmer, Luigi Ballabio, Sam Liddicott, Art Yerkes, Thien-Thi Nguyen, Marcelo Matus, Loic Dachary, Masaki Fukushima, and others. LL2, November 9, 2002 29 [email protected]