468,107 Members | 1,337 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,107 developers. It's quick & easy.

Writing python module in C: wchar_t or Py_UNICODE?

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?

Mar 16 '07 #1
2 2743
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.
Mar 16 '07 #2

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 :)

Mar 17 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

18 posts views Thread by Tad Marko | last post: by
1 post views Thread by Ames Andreas (MPA/DF) | last post: by
2 posts views Thread by Kurt B. Kaiser | last post: by
1 post views Thread by bmcnally | last post: by
17 posts views Thread by nnorwitz | last post: by
17 posts views Thread by Adam Olsen | last post: by
9 posts views Thread by Randy.Galbraith | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.