Sitemap

McMillan Enterprises, Inc.
Python Pages
  Sockets HOWTO
  Distributing Python Programs
  A Python C++ API
    SCXX API
  Embedding Python
  Stackless Python
  MkSQL
  Import Hooks
Java Samples
About ME Inc.

SCXX - A Simple Python/C++ API

What's New

[Jan 25 2002]

Classes (PWEngine, FreeThreadedBlock, PythonThreadedBlock) for dealing with Python threas. PWOModule, a simple wrapper for Python module objects.

Overview

SCXX (Simplified CXX) is a lightweight C++ wrapper for dealing with PyObjects. It is inspired by Paul Dubois' CXX, but is much simpler. It does not use templates, so almost any compiler should work. It does not try to hide things like the Python method tables, or the init function. It only covers wrapping the most common PyObjects, with a minimum of code bloat. You can write extensions in C++ that can be loaded by a Python compiled and linked with the C compiler.

It lets you write C++ that looks a lot more like Python than the C API. Reference counts are handled automatically. It has not been optimized - it generally uses the highest possible level of the Python C API.

Using SCXX

Most of the SCXX classes can be used to create new Python objects, or wrap existing ones. Wrapping an existing one forces a typecheck (except for PWOBase, which doesn't care). So:

  PWOMapping dict(d); 
will throw a PWException if d is not a Python Mapping object (i.e, a dict). More on PWException below.

Operators are overloaded so C++ looks as much like Python as possible.

          PWOMapping d;
          PWOList lst(args[0]);
          d["key1"] = lst[0];
        

Since errors are normally reported through exceptions, use code like this:

           try {
               //....
           }
           catch(PWException e) {
               return e;
           }
        
This (on an exception) will return NULL to Python with the Python exception information already set.

To signal errors in your own code, use:

           throw PWException(PyExc_xxxx, msg);
        
That is: throw a stack-based instance (not heap based), and give it the appropriate PyExc_xxx type and a string that the Python exception can show.

To return a PWOxxx wrapped (or created) instance to Python, use disOwn():

  return PWONumber(7.0).disOwn(); 
Without the disOwn(), the object would be deallocated before Python can get it.

Note that the PWOxxx classes are generally designed to be created on the stack. The corresponding PyObject is on the heap. When the PWOxxx instance goes out of scope, the PyObject is automatically decreffed (unless you've used disOwn()).

See SCXX API for more.

Instructions

Download scxx_b3.tar.gz. Point to the installation directory with a -I directive. Include PWOImp.cpp in your make.

Warning: With certain recent versions of g++, you cannot throw and catch at the same level unless the calling app (Python) is compiled and linked with g++. One easy way around this is to use a Fail(PyExc_xxx, msg) function that does nothing but throw the exception for you.

License

No restrictions on usage, modification or redistribution, as long as the copyright notice is maintained. No warranty whatsoever.

copyright 1999-2002
McMillan Enterprises, Inc.