Wrapping C++ code for python.

Alex alex at somewhere.round.here
Tue May 11 18:32:48 EDT 1999


Hi.  Could someone please point me to some examples of Gnu C++ code
wrapped up for Python?  I am finding the documents rather heavy going,
because they don't give any good examples.

I started with the spammodule in the doc's, and then started adding in
the C code.  I can't show you the code I'm calling, 'cause it's not
mine, but my wrapper function is shown at the bottom of this post.  I
hope it's sufficient to diagnose the problem.

When I try to import it, I get the error message

>>> import spam
Traceback (innermost last):
  File "<stdin>", line 1, in ?
ImportError: ./spammodule.so: undefined symbol: __builtin_new

I have no idea what this means or what to do about it.  Does anyone
else?  If I get rid of the code bracketed by "// New stuff starts here."
and "// New stuff ends here." everything works painlessly, though.

Incidentally, I'm building it with the commands
g++ -g -Wall -gstabs -O0 spammodule.cc -c -o spammodule.o \
        -I/data/Dictionary/ -I /usr/include/python1.5/
ld -shared spammodule.o -o spammodule.so	

Alex.

Code:
----------------------------------------------------------------------
#include "Python.h"
//
// This is the C++ code I want to use.
#include "dictionary.h" 
#include "ivector.h"    

extern "C" { // Don't know if this is necessary...
  static PyObject* spam_system(PyObject *self, \
			       PyObject *args) {
    char *command;
    int sts = 0;
    if (!PyArg_ParseTuple(args, "s", &command)) {
      return NULL;
    }
    // sts = system(command);
    // 
    // New stuff starts here.
    //
    char* dict_name = \
      "/data/alex/dictionaries/dbEST.translated.1";
    char ErrMsg [128];
    Dictionary *dictionary;
    dictionary = new Dictionary (dict_name, ErrMsg);
    ivector<unsigned long int> segposs; 
    ivector<unsigned long int> segposp; 
    ivector<unsigned long int> seglen;  
    ivector<unsigned long int> segacc;
    dictionary->Segments (command, 4, 4, 
			  segposs, segposp, 
			  seglen, segacc);
    sts = segposs.size ();
    // 
    // New stuff ends here.
    //
    return Py_BuildValue("i", sts);
  }
}

static PyMethodDef SpamMethods[] = {
  {"system",  spam_system, METH_VARARGS},
  {NULL,      NULL}        /* Sentinel */
};

extern "C" {
  void initspam () {
    (void) Py_InitModule("spam", SpamMethods);
  }
}

#include <fstream.h>
#include "dictionary.h"
#include "ivector.h"
#include "Python.h"

extern "C" {
  static PyObject* spam_system(PyObject *self, \
			       PyObject *args) {
    char *command;
    int sts = 0;
    if (!PyArg_ParseTuple(args, "s", &command)) {
      return NULL;
    }
    char* dict_name = \
      "/data/alex/dictionaries/dbEST.translated.1";
    char ErrMsg [128];
    Dictionary *dictionary;
    dictionary = new Dictionary (dict_name, ErrMsg);
    // sts = system(command);
    ivector<unsigned long int> segposs; 
    ivector<unsigned long int> segposp; 
    ivector<unsigned long int> seglen;  
    ivector<unsigned long int> segacc;
    dictionary->Segments (command, 4, 4, 
			  segposs, segposp, 
			  seglen, segacc);
    sts = segposs.size ();
    return Py_BuildValue("i", sts);
  }
}

static PyMethodDef SpamMethods[] = {
  {"system",  spam_system, METH_VARARGS},
  {NULL,      NULL}        /* Sentinel */
};

extern "C" {
  void initspam () {
    (void) Py_InitModule("spam", SpamMethods);
  }
}

// import sys; sys.path.append ('/d/alex_c/c++/')
// import spam; spam.system ('ydvm')





More information about the Python-list mailing list