[C++-sig] Re: Custom by value type to python type conversion
Niall Douglas
s_sourceforge at nedprod.com
Fri Nov 7 01:04:31 CET 2003
On 5 Nov 2003 at 15:00, David Abrahams wrote:
> > ... and then it goes to NOT call the
> > FXString_to_python_str::convert() anyway (again, according to
> > breakpoint). What am I missing? From the examples and list archives,
> > I would have appeared to have done everything!
> >
> > Looking at the internal builtin_converters.hpp, it would seem that
> > there's more that needs to be done, but since that's internal there
> > must be another way?
>
> I'm guessing this may be a BPL bug, but without a small reproducible
> example it's hard to tell. You know the drill by now...
I've attached it below. I can tell you that it only occurs when the
type to be converted is a data member of some class, so obviously
it's failing to invoke the converter in this situation.
I should mention that my BPL has its invoke.hpp heavily altered which
while it shouldn't be incompatible, might be. Put it this way:
everything compiles and works fine here, possibly excepting this.
Cheers,
Niall
python bit:
from TnFOX2 import *
a=GetData()
print a.name
<error>
C++ bit:
#include <string.h>
namespace FX {
// Very simple FXString implementation
class FXString2
{
char data[256];
public:
FXString2(const char *str=0) { if(str) strcpy(data, str); else
data[0]=0; }
FXString2(const char *str, int length) { memcpy(data, str, length);
data[length]=0; }
const char *text() const { return data; }
int length() const { return strlen(data); }
};
struct MappedData
{
FXString2 name;
};
MappedData GetData()
{
MappedData ret;
ret.name=FXString2("Niall is a teapot");
return ret;
}
}
#include <boost/python.hpp>
using namespace boost::python;
struct FXString_to_python_str : public
to_python_converter<FX::FXString2, FXString_to_python_str>
{
static PyObject *convert(const FX::FXString2 &str)
{
return PyString_FromStringAndSize(str.text(), str.length());
//return incref(boost::python::object(str).ptr());
}
};
BOOST_PYTHON_MODULE(TnFOX2)
{
FXString_to_python_str();
class_< FX::MappedData >("FXMappedData")
.def_readwrite("name", &FX::MappedData::name);
def("GetData", &FX::GetData);
}
More information about the Cplusplus-sig
mailing list