[Patches] Discuss: Unicode patch for open() builtin.

Mark Hammond mhammond@skippinet.com.au
Wed, 29 Mar 2000 09:24:13 +1000


Not a patch yet, but Im working towards one :)

Im thinking about how to change open() WRT unicode.

First thought was that the change would be to a PyArg_ParseTuple()
call, but inspection shows a PyFile_FromString(char *name, ...).

So it makes sense to create a new API - PyFile_FromWCHAR()?

On Windows, Im thinking that rather than performing a conversion to
MBCS, this could perform a runtime check for WCHAR CRTL support -
ie, basically "am I running NT", and call fopenW().  This could be a
global static, so the first check would be "expensive", but after
that we have a single "if" test.

The bltinmodule call to this function can simply check the type of
the arg and make the appropriate call (fopen() or fopenW()).  (Note
that on Windows 95/98 the W calls exist, they just dont work!  This
is by design, and designed for exactly this case)

Something like:

#ifdef HAVE_WCHAR_H

PyObject PyFile_FromWCHAR(WCHAR *fname, ...)
{
  ...
#ifdef MS_WIN32
   static int have_wchar_crt = -1;
   if (have_wchar_crt==-1)
     have_wchar_crt = AmIRunningNT() ? 1 : 0;
   if (have_wchar_crt)
     fp = fopenW(fname, ...);
   else {
     /* encode as MBCS */
     fp = fopen(mbcs_name, ...);
   }
  ...
#endif

}
#endif

Sound reasonable?

Mark.