How to best return a string from C to CPython 2.5?

dylan.fun at gmail.com dylan.fun at gmail.com
Mon Oct 6 22:25:29 EDT 2008


I'm trying to return a Python string from a C++ function (actually
inside gnuradio) using CPython 2.5. My C++ function is declared like:

typedef struct {
  int size;
  unsigned char *data;
} STRING

STRINGBUF myfunc(std::string s)
{
  STRINGBUF buf;
  .
  .
  .
  buf.size = s.length();
  buf.data = s.data();
  return buf;
}

Since gnuradio uses SWIG, I found a SWIG example that seemed to fit:

%typemap(myfunc) STRINGBUF {
  $result = PyString_FromStringAndSize($1.data, $1.size);
}

STRINGBUF myfunc(std::string s);

At runtime though, I'm getting a type mismatch error from Python
(PyObject of some type found when PyString was expected - I can post
the exact message tomorrow if it'll help.)

Any ideas or suggestions appreciated!



More information about the Python-list mailing list