[Python-checkins] cpython: Close #11619: write_compiled_module() doesn't encode the filename

victor.stinner python-checkins at python.org
Wed Apr 20 03:28:31 CEST 2011


http://hg.python.org/cpython/rev/e4e92d68ba3a
changeset:   69463:e4e92d68ba3a
user:        Victor Stinner <victor.stinner at haypocalc.com>
date:        Wed Apr 20 03:27:51 2011 +0200
summary:
  Close #11619: write_compiled_module() doesn't encode the filename

Reimplement open_exclusive() using _wopen() to avoid encoding the filename to
the filesystem encoding: use the Unicode version of the Windows API.

files:
  Python/import.c |  26 ++++++++++++++++++++++++--
  1 files changed, 24 insertions(+), 2 deletions(-)


diff --git a/Python/import.c b/Python/import.c
--- a/Python/import.c
+++ b/Python/import.c
@@ -1198,6 +1198,7 @@
 
 /* Helper to open a bytecode file for writing in exclusive mode */
 
+#ifndef MS_WINDOWS
 static FILE *
 open_exclusive(char *filename, mode_t mode)
 {
@@ -1228,6 +1229,7 @@
     return fopen(filename, "wb");
 #endif
 }
+#endif
 
 
 /* Write a compiled module to a file, placing the time of last
@@ -1250,7 +1252,12 @@
                       S_IWUSR | S_IWGRP | S_IWOTH);
     PyObject *dirbytes;
 #endif
-    PyObject *cpathbytes, *dirname;
+#ifdef MS_WINDOWS
+    int fd;
+#else
+    PyObject *cpathbytes;
+#endif
+    PyObject *dirname;
     Py_UNICODE *dirsep;
     int res, ok;
 
@@ -1294,6 +1301,16 @@
     }
     Py_DECREF(dirname);
 
+#ifdef MS_WINDOWS
+    (void)DeleteFileW(PyUnicode_AS_UNICODE(cpathname));
+    fd = _wopen(PyUnicode_AS_UNICODE(cpathname),
+                 O_EXCL | O_CREAT | O_WRONLY | O_TRUNC | O_BINARY,
+                 mode);
+    if (0 <= fd)
+        fp = fdopen(fd, "wb");
+    else
+        fp = NULL;
+#else
     cpathbytes = PyUnicode_EncodeFSDefault(cpathname);
     if (cpathbytes == NULL) {
         PyErr_Clear();
@@ -1301,11 +1318,14 @@
     }
 
     fp = open_exclusive(PyBytes_AS_STRING(cpathbytes), mode);
+#endif
     if (fp == NULL) {
         if (Py_VerboseFlag)
             PySys_FormatStderr(
                 "# can't create %R\n", cpathname);
+#ifndef MS_WINDOWS
         Py_DECREF(cpathbytes);
+#endif
         return;
     }
     PyMarshal_WriteLongToFile(pyc_magic, fp, Py_MARSHAL_VERSION);
@@ -1321,11 +1341,13 @@
         (void)DeleteFileW(PyUnicode_AS_UNICODE(cpathname));
 #else
         (void) unlink(PyBytes_AS_STRING(cpathbytes));
+        Py_DECREF(cpathbytes);
 #endif
-        Py_DECREF(cpathbytes);
         return;
     }
+#ifndef MS_WINDOWS
     Py_DECREF(cpathbytes);
+#endif
     /* Now write the true mtime */
     fseek(fp, 4L, 0);
     assert(mtime < LONG_MAX);

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list