473,703 Members | 2,922 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to map python's unicode stuff to a wchar_t based api?

Hi all,

besides PyUnicode_(From )|(As)WideChar I haven't found specific support
for wchar_t in the python api. Is there a default codec that produces
wchar_t* (in a platform-neutral way) or something else in
PyArg_ParseTupl e's format string that could help me? What encoding is
used for python's Py_UNICODE thing?

My specific problem is that I wrap an api where one function can have
as well an ansi as a unicode variant. I don't want to decide which
variant to use at compile time but rather at runtime. Therefore I
have a default argument useUnicode and if possible I want to get rid
of the

if (PyObject_IsTru e(useUnicode)) {
doTheUnicodeStu ff();
...
}
else {
doTheAnsiThingW hichLooksAlmost IdenticalToTheA boveButJustAlmo st();
...
}

annoyance in almost any function.
TIA,

andreas
Jul 18 '05 #1
1 2144
Ames Andreas:
Therefore I have a default argument useUnicode and
if possible I want to get rid of the

if (PyObject_IsTru e(useUnicode)) {
doTheUnicodeStu ff();
...
}
else {
doTheAnsiThingW hichLooksAlmost IdenticalToTheA boveButJustAlmo st();
...
}

annoyance in almost any function.

To support Unicode file names on Win32, the convention described in PEP
277 is to call the wide API when the argument was Unicode, otherwise call
the ANSI API. From src/Modules/posixmodule.c this looks like

#ifdef Py_WIN_WIDE_FIL ENAMES
if (unicode_file_n ames()) {
PyUnicodeObject *po;
if (PyArg_ParseTup le(args, "Ui:access" , &po, &mode)) {
Py_BEGIN_ALLOW_ THREADS
/* PyUnicode_AS_UN ICODE OK without thread lock as
it is a simple dereference. */
res = _waccess(PyUnic ode_AS_UNICODE( po), mode);
Py_END_ALLOW_TH READS
return(PyBool_F romLong(res == 0));
}
/* Drop the argument parsing error as narrow strings
are also valid. */
PyErr_Clear();
}
#endif

This code then falls through to the ANSI API.

Py_WIN_WIDE_FIL ENAMES is only defined (in src/PC/pyconfig.h) when Python
is using 2 byte wide Unicode characters (Py_UNICODE_SIZ E == 2), thus
ensuring that the result on Win32 of PyUnicode_AS_UN ICODE is equivalent to
wchar_t*.

Whether PY_UNICODE_TYPE is wchar_t depends on platform and other
definitions.

The extra code required for PEP 277 adds to size and obscures intent.
Various code reduction techniques can be alleviate this such as posix_1str
in posixmodule or using some preprocessor cleverness.

Neil

Jul 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
3246
by: Sune | last post by:
Hi! For example: 1) I want to open a file in a Chinese locale and print it. 2) The program takes the file name as a command line argument.
12
3035
by: damjan | last post by:
This may look like a silly question to someone, but the more I try to understand Unicode the more lost I feel. To say that I am not a beginner C++ programmer, only had no need to delve into character encoding intricacies before. In c/c++, the unicode characters are introduced by the means of wchar_t type. Based on the presence of _UNICODE definition C functions are macro'd to either the normal version or the one prefixed with w. Because...
4
2597
by: Rui Maciel | last post by:
I want to support Unicode on a pet project of mine (small markup language parser). I've read a bit about Unicode (didn't delved beyond the basics) and I searched for some info on how to support Unicode on C programs. Unfortunately I wasn't able to find articles that could be considered more than loose ends, small blog entries and side remarks, never delving too much into specifics. From what I gathered, the two main methods (based on...
2
2935
by: Yury | last post by:
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 */
10
8181
by: flywav | last post by:
hi all you know unicdoe is very important, under linux, i always use utf-8, but now i need save one file in unicode. my linux is centos. and i know this system support unicode. the wchar_t *p is a unicode string, i print the len it is 7. it is right,i save the file, the file length is 7. i had checked it, it is'hello??' ? = 0x3F, so what's wrong with these code? thank you. #define __STDC_ISO_10646__ 200104L
29
2110
by: Ioannis Vranos | last post by:
Hi, I am currently learning QT, a portable C++ framework which comes with both a commercial and GPL license, and which provides conversion operations to its various types to/from standard C++ types. For example its QString type provides a toWString() that returns a std::wstring with its Unicode contents. So, since wstring supports the largest character set, why do we need explicit Unicode types in C++?
0
9239
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9102
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8995
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8951
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6585
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5920
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4417
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4674
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2424
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.