[C++-sig] Re: Automatic PyUnicode to 'const char*'
by David Abrahams other posts by this author
Jul 31 2003 4:18PM messages near this date
[C++-sig] Automatic PyUnicode to 'const char*'
|
[C++-sig] Re: Automatic PyUnicode to 'const char*'
Stefan Seefeld <seefeld@[...].ca> writes:
> David Abrahams wrote:
> > "Lijun Qin" <qinlj@[...].com> writes:
> >
> >>Hi all,
> >>
> >>I'm using boost.python to wrap the WTL (Windows Template Libaray), using VC
> >>7.1 and porting some old code previously use win32ui.pyd.
> >>Basically it is easy to do, though gccxml failed to parse the ATL/WTL code
> >>so I can not use Pyste.
> >>But there is a trouble, does anybody know how to automaticly convert
> >>PyUnicode to 'const char *'? Without this, I have to change lot of code
> >>lines to explictly use str() function.
> > I'm not an expert in it, but I thought Unicode used 16- or 32- byte
> > wchar_t characters. How would you convert it to char const*?
>
> unicode allows different encodings, some (such as utf-8) with variably
> sized character representations. This means that conversions usually
> can't be done on-the-fly without helper constructs, i.e. some memory
> management is needed.
>
> As an example, I'm doing such conversions in a xml library. The C
> implementation uses 'xmlChar *', which is just an alias for 'char *',
> but really holds utf-8 encoded text.
> I convert it to various string types (the public API is parametrized
> for the string type):
To do that kind of narrowing, at the moment, the only way would be to
write a thin wrapper function:
void f(char const*);
void f_thin_wrapper(object unicode)
{
str narrowed(unicode);
f(extract<char const*> (str));
}
we may have some support for doing this automatically in the version
of Boost.Python which integrates with Luabind, but that's a ways off
yet.
--
Dave Abrahams
Boost Consulting
www.boost-consulting.com
_______________________________________________
C++-sig mailing list
C++-sig@[...].org
http://mail.python.org/mailman/listinfo/c++-sig
Thread:
Lijun Qin
David Abrahams
David Abrahams
Stefan Seefeld
|