Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Writing python module in C: wchar_t or Py_UNICODE?

Reply
Thread Tools

Writing python module in C: wchar_t or Py_UNICODE?

 
 
Yury
Guest
Posts: n/a
 
      03-16-2007
I am new to python and programming generally, but someday it is time
to start
I am writing a python module in C and have a question about multibyte
character strings in python<=>C.
I want a C function which takes a string as argument from python
script:

static PyObject *
connect_to_server(PyObject *self, PyObject * authinfo){
wchar_t * login; /* Must support unicode */
char * serveraddr;
int * port;

if(!PyArgsParseTuple(authinfo, "sdu", &serveraddr, &port, &login))
return NULL;

....

Will that code work?
Or i should use Py_UNICODE * data type? Will it be compatible with
standard C string comparison/concantenation functions?

 
Reply With Quote
 
 
 
 
Carsten Haese
Guest
Posts: n/a
 
      03-16-2007
On Fri, 2007-03-16 at 04:04 -0700, Yury wrote:
> I am new to python and programming generally, but someday it is time
> to start
> I am writing a python module in C and have a question about multibyte
> character strings in python<=>C.
> I want a C function which takes a string as argument from python
> script:
>
> static PyObject *
> connect_to_server(PyObject *self, PyObject * authinfo){
> wchar_t * login; /* Must support unicode */
> char * serveraddr;
> int * port;
>
> if(!PyArgsParseTuple(authinfo, "sdu", &serveraddr, &port, &login))
> return NULL;
>
> ...
>
> Will that code work?
> Or i should use Py_UNICODE * data type? Will it be compatible with
> standard C string comparison/concantenation functions?


You should familiarize yourself with the Python/C API documentation. It
contains the answers to all the above questions.

http://docs.python.org/api/arg-parsing.html says this about the "u"
format character: "a pointer to the existing Unicode data is stored into
the Py_UNICODE pointer variable whose address you pass."

http://docs.python.org/api/unicodeObjects.html says this about
Py_UNICODE: "On platforms where wchar_t is available and compatible with
the chosen Python Unicode build variant, Py_UNICODE is a typedef alias
for wchar_t to enhance native platform compatibility."

The first quote says that, to be strictly correct, "login" should be a
"Py_UNICODE*", but the second quote says that under the right
circumstances, Py_UNICODE is the same as wchar_t. It's up to you to
determine if your platform provides the right circumstances for this to
be the case.

Hope this helps,

Carsten.


 
Reply With Quote
 
 
 
 
Yury
Guest
Posts: n/a
 
      03-17-2007

Carsten Haese wrote:
>
> You should familiarize yourself with the Python/C API documentation. It
> contains the answers to all the above questions.
>
> http://docs.python.org/api/arg-parsing.html says this about the "u"
> format character: "a pointer to the existing Unicode data is stored into
> the Py_UNICODE pointer variable whose address you pass."
>
> http://docs.python.org/api/unicodeObjects.html says this about
> Py_UNICODE: "On platforms where wchar_t is available and compatible with
> the chosen Python Unicode build variant, Py_UNICODE is a typedef alias
> for wchar_t to enhance native platform compatibility."
>
> The first quote says that, to be strictly correct, "login" should be a
> "Py_UNICODE*", but the second quote says that under the right
> circumstances, Py_UNICODE is the same as wchar_t. It's up to you to
> determine if your platform provides the right circumstances for this to
> be the case.
>
> Hope this helps,
>
> Carsten.


Thanks for reply,
sorry for asking questions while not checked the manual.
Also sorry for my wierd english

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
converting from windows wchar_t to linux wchar_t yakir22@gmail.com C++ 3 08-15-2008 09:04 AM
Re: module docstring, documentation,anything? please note is the module type/object NOT some module Maric Michaud Python 0 06-24-2006 12:42 PM
How to map python's unicode stuff to a wchar_t based api? Ames Andreas (MPA/DF) Python 1 04-27-2004 12:15 PM
string class that can convert between wchar_t and char Bren C++ 4 10-07-2003 03:24 PM
wchar_t wstring char string transformations Adrian Cornish C++ 2 07-12-2003 08:05 PM



Advertisments