Boost logo

Boost :

From: rwgk_at_[hidden]
Date: 2001-04-04 05:58:19


The following is already in the CVS tree:

PyObject* to_python(const std::string& s)
{
    return PyString_FromStringAndSize(s.data(), s.size());
}

std::string from_python(PyObject* p, boost::python::type<std::string>)
{
    return std::string(PyString_AsString(p), PyString_Size(p));
}

Looking at this again, it seems to me that the from_python()
converter needs some work. Either something like
    const char* s = PyString_AsString(p);
    if (!s)
        throw boost::python::argument_error();
or maybe
    if (! PyString_Check(p)) ...

I believe that PyString_AsStringAndSize is not available in
Python 1.5.2 and should therefore not be used.

Ralf

--- In boost_at_y..., "Rainer Deyke" <root_at_r...> wrote:
> Both Python strings and 'std::string' support embedded '\0'
characters.
> However, 'boost::python::from_python(PyObject *,
> boost::python::type<std::string>' fails to correctly convert Python
strings
> with embedded '\0' to 'std::string'. I propose the following
alternate
> implementation:
>
> std::string from_python(PyObject *p,
boost::python::type<std::string>)
> {
> int length;
> char *s;
> if (PyString_AsStringAndSize(p, &s, &length))
> throw boost::python::argument_error();
> return std::string(s, length);
> }
>
> There is a similar problem in 'boost::python::to_python(const
> std::string&)'. I propose the following alternate implementation:
>
> PyObject *to_python(const std::string& s)
> {
> return PyString_FromStringAndSize(s.data(), s.size());
> }
>
>
>
> --
> Rainer Deyke (root_at_r...)
> Shareware computer games -
http://rainerdeyke.com
> "In ihren Reihen zu stehen heisst unter Feinden zu kaempfen" -
Abigor


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk