[Python-checkins] r85094 - python/branches/py3k/Python/import.c

victor.stinner python-checkins at python.org
Wed Sep 29 12:28:52 CEST 2010


Author: victor.stinner
Date: Wed Sep 29 12:28:51 2010
New Revision: 85094

Log:
Issue #9979: Use PyUnicode_AsWideCharString() in import.c

Don't truncate path if it is too long anymore, and allocate fewer memory (but
allocate it on the heap, not on the stack).


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

Modified: python/branches/py3k/Python/import.c
==============================================================================
--- python/branches/py3k/Python/import.c	(original)
+++ python/branches/py3k/Python/import.c	Wed Sep 29 12:28:51 2010
@@ -1961,21 +1961,21 @@
 _Py_fopen(PyObject *unicode, const char *mode)
 {
 #ifdef MS_WINDOWS
-    wchar_t path[MAXPATHLEN+1];
+    wchar_t *path;
     wchar_t wmode[10];
-    Py_ssize_t len;
     int usize;
-
-    len = PyUnicode_AsWideChar((PyUnicodeObject*)unicode, path, MAXPATHLEN);
-    if (len == -1)
-        return NULL;
-    path[len] = L'\0';
+    FILE *f;
 
     usize = MultiByteToWideChar(CP_ACP, 0, mode, -1, wmode, sizeof(wmode));
     if (usize == 0)
         return NULL;
 
-    return _wfopen(path, wmode);
+    path = PyUnicode_AsWideCharString((PyUnicodeObject*)unicode, NULL);
+    if (path == NULL)
+        return NULL;
+    f = _wfopen(path, wmode);
+    PyMem_Free(path);
+    return f;
 #else
     FILE *f;
     PyObject *bytes = PyUnicode_EncodeFSDefault(unicode);
@@ -1997,17 +1997,15 @@
 _Py_stat(PyObject *unicode, struct stat *statbuf)
 {
 #ifdef MS_WINDOWS
-    wchar_t path[MAXPATHLEN+1];
-    Py_ssize_t len;
+    wchar_t *path;
     int err;
     struct _stat wstatbuf;
 
-    len = PyUnicode_AsWideChar((PyUnicodeObject*)unicode, path, MAXPATHLEN);
-    if (len == -1)
+    path = PyUnicode_AsWideCharString((PyUnicodeObject*)unicode, NULL);
+    if (path == NULL)
         return -1;
-    path[len] = L'\0';
-
     err = _wstat(path, &wstatbuf);
+    PyMem_Free(path);
     if (!err)
         statbuf->st_mode = wstatbuf.st_mode;
     return err;
@@ -3724,7 +3722,7 @@
 #else /* MS_WINDOWS */
     PyObject *pathobj;
     DWORD rv;
-    wchar_t path[MAXPATHLEN+1];
+    wchar_t *path;
     Py_ssize_t len;
 
     if (!_PyArg_NoKeywords("NullImporter()", kwds))
@@ -3739,15 +3737,15 @@
         return -1;
     }
 
-    len = PyUnicode_AsWideChar((PyUnicodeObject*)pathobj,
-                               path, sizeof(path) / sizeof(path[0]));
-    if (len == -1)
+    path = PyUnicode_AsWideCharString((PyUnicodeObject*)pathobj, NULL);
+    if (path == NULL)
         return -1;
     /* see issue1293 and issue3677:
      * stat() on Windows doesn't recognise paths like
      * "e:\\shared\\" and "\\\\whiterab-c2znlh\\shared" as dirs.
      */
     rv = GetFileAttributesW(path);
+    PyMem_Free(path);
     if (rv != INVALID_FILE_ATTRIBUTES) {
         /* it exists */
         if (rv & FILE_ATTRIBUTE_DIRECTORY) {


More information about the Python-checkins mailing list