SWIG and XML and Python

David Abrahams dave at boost-consulting.com
Fri Oct 11 18:23:38 EDT 2002


Dave Kuhlman <dkuhlman at rexx.com> writes:

> David Abrahams wrote:
> 
> > Dave Kuhlman <dkuhlman at rexx.com> writes:
> > 
> >> SWIG 1.3 can now generate XML documents.  These XML documents describe
> >> the interface fed into SWIG.
> > 
> > Interesting. How does this compare with projects like GCC_XML and
> 
> I am not well-educated on GCC_XML, however from a quick read at the GCC_XML 
> Web site (http://www.gccxml.org/HTML/Index.html), GCC_XML produces XML that 
> describes your *code*.  

That includes the interface, of course...

> SWIG 1.3 with the "-xml" option produces XML that 
> describes the *interface* to your code.   This is a bit over-simplified, 
> but you can use SWIG 1.3 with "-xml" to produce an XML description of your 
> C/C++ .h header files, whereas you can use GCC_XML to produce an XML 
> description of your .c and .cpp/.cxx source code files.

...which might be just what I need. How much of C++ can it handle?
Every time I try to throw advanced C++ code at one of these tools, it
chokes and dies.

> Or, for those of you who want information that you can use, GCC_XML could be 
> used as a front-end to a compiler, whereas SWIG 1.3 with "-xml" could be 
> used as a front-end to a tool that makes interfaces (code libraries, etc) 
> and descriptions of them available to programming tools.
> 
> And for those of you who are not familiar with SWIG, this XML stuff is not 
> its primary use.  SWIG is normally used to produce scripting language 
> extensions from C/C++ code.  For example, and importantly, you can 
> (sometimes) feed a C or C++ header file to SWIG, and it will produce 
> wrappers that enable you to use the functions and classes described in that 
> header from Python.  It's in the same ecological niche as Boost.  

Just in the same niche roughly as the Boost Python library (though I
tend to think of SWIG as being in a different broad class of tools
which parse C++ code for purposes other than generating object code).
Boost as a whole is a much broader community and collection of C++
libraries and has nothing particularly to do with Python.

> Yes?  No?  

Maybe?

> David, I see "Boost" in your Sig.  Could you give us a quick
> comparison of SWIG and Boost, please.

Last time I did that I ticked off David Beazley (the lead SWIG developer) ;-)

OK, here goes an attempt at an unbiased description. I've never used
SWIG so what I'm writing here is based on documentation and hearsay
(what's that Mark Twain said? "There's lies, damn lies, and
documentation?"):

      SWIG is a multi-language interoperability tool. It attempts to
      parse the declarative parts of C and C++ (and Fortran?) code and
      automatically generate wrappers for the functions and classes
      appropriate for a variety of scripting languages, including
      Python, Perl, and Tcl. For really simple jobs SWIG may be able
      to read your header file and generate wrappers with no extra
      help. For real-world jobs you usually end up writing SWIG-ized
      versions of your header files which include some extra
      directives aimed at helping SWIG with the wrapping job.

      Boost.Python is a C++ library which allows you to write some
      simple C++ expressions which end up building an extension module
      for you. This ends up looking like a kind of IDL (Interface
      Definition Language). So, given:

        struct World
        {
            void set(std::string msg) { this->msg = msg; }
            std::string greet() { return msg; }
            std::string msg;
        };

     You can generate a corresponding extension module with:

        #include <boost/python.hpp>
        using namespace boost::python;

        BOOST_PYTHON_MODULE(hello)
        {
            class_<World>("World")
                .def("greet", &World::greet)
                .def("set", &World::set)
                ;
        }

     That's the capsule summary, but there's a lot more to it. See
     http://www.boost.org/libs/python for more details.

> > Synopsis?
> > 
> 
> Don't know about this one.  "Synopsis" is too common a term, so a Google 
> search did not give me anything useful.  Do you have a link?  Now, you have 
> *me* interested.

http://synopsis.sf.net

It uses a "real" C++ front-end to parse the input and generates a
Python AST.

-- 
           David Abrahams * Boost Consulting
dave at boost-consulting.com * http://www.boost-consulting.com



More information about the Python-list mailing list