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

D

dylan.fun

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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top