[C++-sig] Creating a unicode instrance from utf-8 encoded string

2011-02-21 Thread Wichert Akkerman
I'm trying to do something which should be very simple, but I'm not 
having much luck figuring out how from the existing documentation.


For a python 2 project I am trying to return a gettext-translated string 
as a unicode instance to python. The return value for gettext() is a 
UTF-8 encoded char*, which should be pretty simple to convert to a 
python unicode instrance using PyUnicode_FromString. I have a feeling 
this is trivial to do, but I can't seem to figure out how.


Wichert.

___
Cplusplus-sig mailing list
[email protected]
http://mail.python.org/mailman/listinfo/cplusplus-sig


Re: [C++-sig] Creating a unicode instrance from utf-8 encoded string

2011-02-21 Thread Hans Meine
Am Montag, 21. Februar 2011, um 11:36:52 schrieb Wichert Akkerman:
> I'm trying to do something which should be very simple, but I'm not
> having much luck figuring out how from the existing documentation.
> 
> For a python 2 project I am trying to return a gettext-translated string
> as a unicode instance to python. The return value for gettext() is a
> UTF-8 encoded char*, which should be pretty simple to convert to a
> python unicode instrance using PyUnicode_FromString. I have a feeling
> this is trivial to do, but I can't seem to figure out how.

According to http://docs.python.org/c-api/unicode.html , this is exactly what 
PyUnicode_FromString is supposed to do.

What's the problem?  Do you get an error?  Some code might help.

HTH,
  Hans
___
Cplusplus-sig mailing list
[email protected]
http://mail.python.org/mailman/listinfo/cplusplus-sig


Re: [C++-sig] Creating a unicode instrance from utf-8 encoded string

2011-02-21 Thread Wichert Akkerman

On 2/21/11 14:33 , Hans Meine wrote:

Am Montag, 21. Februar 2011, um 11:36:52 schrieb Wichert Akkerman:

I'm trying to do something which should be very simple, but I'm not
having much luck figuring out how from the existing documentation.

For a python 2 project I am trying to return a gettext-translated string
as a unicode instance to python. The return value for gettext() is a
UTF-8 encoded char*, which should be pretty simple to convert to a
python unicode instrance using PyUnicode_FromString. I have a feeling
this is trivial to do, but I can't seem to figure out how.


According to http://docs.python.org/c-api/unicode.html , this is exactly what
PyUnicode_FromString is supposed to do.

What's the problem?  Do you get an error?  Some code might help.


For a single unicode instance this is indeed working correctly. 
Unfortunately it breaks down when I need to return a list of unicode 
instances. My simple approach looks like this:


boost::python::list PyMyFunc() {
std::vector raw_strings = BaseClass::MyFunc();
std::vector::const_iterator i;
boost::python::list result;

for (i=raw_strings.begin(); i!=raw_strings.end(); i++)
result.append(PyUnicode_FromString(i->c_str()));
return result;
}

but that results in a lot of compile errors suggesting that bp::list 
does not know how to handle PyObject* values.


Wichert.
___
Cplusplus-sig mailing list
[email protected]
http://mail.python.org/mailman/listinfo/cplusplus-sig


Re: [C++-sig] Creating a unicode instrance from utf-8 encoded string

2011-02-21 Thread Ralf W. Grosse-Kunstleve
>  result.append(PyUnicode_FromString(i->c_str()));


try

result.append(object(handle<>(PyUnicode_FromString(i->c_str();

The handle<> is a way to tell Boost.Python if you own the reference or if it is 
borrowed (among other things). The
suggested code assumes you own the reference.

Ralf
___
Cplusplus-sig mailing list
[email protected]
http://mail.python.org/mailman/listinfo/cplusplus-sig