[Python-checkins] r83253 - python/branches/import_unicode/Python/import.c

victor.stinner python-checkins at python.org
Fri Jul 30 05:51:47 CEST 2010


Author: victor.stinner
Date: Fri Jul 30 05:51:46 2010
New Revision: 83253

Log:
use _wfopen() and _wstat() in _Py_fopen() and _Py_stat()

Modified:
   python/branches/import_unicode/Python/import.c

Modified: python/branches/import_unicode/Python/import.c
==============================================================================
--- python/branches/import_unicode/Python/import.c	(original)
+++ python/branches/import_unicode/Python/import.c	Fri Jul 30 05:51:46 2010
@@ -2130,7 +2130,22 @@
 FILE*
 _Py_fopen(PyObject *unicode, const char *mode)
 {
-    /* FIXME: use _wfopen() on Windows */
+#ifdef MS_WINDOWS
+    wchar_t path[MAXPATHLEN+1];
+    wchar_t wmode[10];
+    Py_ssize_t len;
+    int usize;
+
+    len = PyUnicode_AsWideChar((PyUnicodeObject*)unicode, path, sizeof(path));
+    if (len == -1)
+        return NULL;
+
+    usize = MultiByteToWideChar(CP_ACP, 0, mode, -1, wmode, sizeof(wmode));
+    if (usize == 0)
+        return NULL;
+
+    return _wfopen(path, wmode);
+#else
     FILE *f;
     PyObject *bytes = PyUnicode_EncodeFSDefault(unicode);
     if (bytes == NULL) {
@@ -2141,6 +2156,7 @@
     f = fopen(PyBytes_AS_STRING(bytes), mode);
     Py_DECREF(bytes);
     return f;
+#endif
 }
 
 
@@ -2148,6 +2164,22 @@
 int
 _Py_stat(PyObject *unicode, struct stat *statbuf)
 {
+#ifdef MS_WINDOWS
+    wchar_t path[MAXPATHLEN+1];
+    Py_ssize_t len;
+    int err;
+    struct _stat wstatbuf;
+
+    len = PyUnicode_AsWideChar((PyUnicodeObject*)unicode, path, sizeof(path));
+    if (len == -1)
+        return -1;
+    err = _wstat(path, &wstatbuf);
+    if (!err) {
+        /* FIXME: copy more attributes? */
+        statbuf->st_mode = wstatbuf.st_mode;
+    }
+    return err;
+#else
     int ret;
     PyObject *bytes = PyUnicode_EncodeFSDefault(unicode);
     if (bytes == NULL) {
@@ -2158,6 +2190,7 @@
     ret = stat(PyBytes_AS_STRING(bytes), statbuf);
     Py_DECREF(bytes);
     return ret;
+#endif
 }
 
 /* Helper to look for __init__.py or __init__.py[co] in potential package */


More information about the Python-checkins mailing list