Wrappy -- a Python Wrapper Generator for C++ Classes

Gregory S. Couch

University of California
Computer Graphics Laboratory
San Francisco, CA 94143-0446
(415) 476-9292, fax: (415) 502-1755
email: gregc@cgl.ucsf.edu

Abstract

Wrappy generates a Python extension module from annotated C++ header files. Wrappy's power comes from how it maps between C++ and Python features. Wrappy understands ISO C++ container classes, C++ classes are converted to Python types (or a Python-subclassable extension class [Ful98]), public member data and get/set member functions are mapped to attributes, C++ exceptions are mapped to Python exceptions, overloaded functions are supported, function argument names are used for Python keyword arguments, and more. Annotating C++ header files is necessary to designate output arguments in functions and to choose how C++ classes are wrapped. Some C++ source code modifications are necessary to harmonize C++ and Python object semantics and to work around limitations in wrappy.

The impetus behind wrappy was to generate wrapper code for our (growing) C++ molecular graphics library with 500 member functions in 30 classes. We didn't want to wrap all of those functions and classes by hand, both because the likelihood of making errors due to the repetitive nature of the task, and because the initial versions of the wrapper code would have systemic errors in them. Existing wrapper generators didn't suport the C++ features we wanted to use. Wrappy generates code that is similar to hand-crafted wrapper code, but more robust. The C++ header files generate about 30 times their ``weight'' in Python interface code (in our case, from 850 lines of C++ header files to over 25000 lines of Python interface code).

Biographical Information

Greg Couch received a Bachelor of Science in E.E.C.S. from U. C. Berkeley in 1983 and a Masters in Computer Science from U. C. Berkeley in 1994. He has worked since 1983 at the University of California at San Francisco's Computer Graphics Laboratory, a NIH Biotechnology Research Center (NIH P41-RR01081). He is a member of the ACM, a member of the IEEE Computer Society, and a member of the San Francisco SIGGRAPH chapter. He currently has 3 exhibits at the Exploratorium, an interactive science museum in San Francisco, California. He also plays soccer and is a member of the National Ski Patrol.

References

[Ful98]
J. Fulton, Extension Classes, Python Extension Types Become Classes, Digital Creations, Inc., 1998. http://www.digicool.com/releases/ExtensionClass/