returning local character string from python extension dll

Ben Held ben.held at staarinc.com
Tue Feb 13 16:45:51 CET 2001


I am building a python extension dll using C++ and SWIG.  I need to return a
character string from a class member function.  The problem is the that the
string is not a member of the class, but it is a local string:

char* CFoo::GetName() {
    char pc[] = "blah";
.........
    return pc;
}

I want to be able to call "print pFoo.GetName()" in python and have "blah"
printed.  If I define my function just as above, I get garbage printed since
the data is local and destroyed when the function exits.  I know I can
return a PyObject* using PyString_FromString, but then print on that object
doesn't show the string like I need.

Ideas?

Ben
ben.held at staarinc.com





More information about the Python-list mailing list