[Python-checkins] cpython: Issue #13303: Fix bytecode file default permission.

charles-francois.natali python-checkins at python.org
Thu Nov 10 19:23:25 CET 2011


http://hg.python.org/cpython/rev/a9f10c3eff69
changeset:   73477:a9f10c3eff69
user:        Charles-François Natali <neologix at free.fr>
date:        Thu Nov 10 19:12:29 2011 +0100
summary:
  Issue #13303: Fix bytecode file default permission.

files:
  Lib/importlib/_bootstrap.py |   2 +-
  Lib/test/test_import.py     |  17 +++++++----------
  Python/import.c             |  13 ++++++-------
  3 files changed, 14 insertions(+), 18 deletions(-)


diff --git a/Lib/importlib/_bootstrap.py b/Lib/importlib/_bootstrap.py
--- a/Lib/importlib/_bootstrap.py
+++ b/Lib/importlib/_bootstrap.py
@@ -88,7 +88,7 @@
         # On POSIX-like platforms, renaming is atomic. id() is used to generate
         # a pseudo-random filename.
         path_tmp = '{}.{}'.format(path, id(path))
-        fd = _os.open(path_tmp, _os.O_EXCL | _os.O_CREAT | _os.O_WRONLY)
+        fd = _os.open(path_tmp, _os.O_EXCL | _os.O_CREAT | _os.O_WRONLY, 0o666)
         try:
             with _io.FileIO(fd, 'wb') as file:
                 file.write(data)
diff --git a/Lib/test/test_import.py b/Lib/test/test_import.py
--- a/Lib/test/test_import.py
+++ b/Lib/test/test_import.py
@@ -97,25 +97,22 @@
 
     @unittest.skipUnless(os.name == 'posix',
                          "test meaningful only on posix systems")
-    def test_execute_bit_not_copied(self):
-        # Issue 6070: under posix .pyc files got their execute bit set if
-        # the .py file had the execute bit set, but they aren't executable.
-        with temp_umask(0o022):
+    def test_creation_mode(self):
+        mask = 0o022
+        with temp_umask(mask):
             sys.path.insert(0, os.curdir)
             try:
                 fname = TESTFN + os.extsep + "py"
                 create_empty_file(fname)
-                os.chmod(fname, (stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH |
-                                 stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH))
                 __import__(TESTFN)
                 fn = imp.cache_from_source(fname)
                 if not os.path.exists(fn):
                     self.fail("__import__ did not result in creation of "
                               "either a .pyc or .pyo file")
-                    s = os.stat(fn)
-                    self.assertEqual(
-                        stat.S_IMODE(s.st_mode),
-                        stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH)
+                s = os.stat(fn)
+                # Check that the umask is respected, and the executable bits
+                # aren't set.
+                self.assertEqual(stat.S_IMODE(s.st_mode), 0o666 & ~mask)
             finally:
                 del sys.path[0]
                 remove_files(TESTFN)
diff --git a/Python/import.c b/Python/import.c
--- a/Python/import.c
+++ b/Python/import.c
@@ -1202,12 +1202,10 @@
                       S_IXUSR | S_IXGRP | S_IXOTH |
                       S_IWUSR | S_IWGRP | S_IWOTH);
     PyObject *dirbytes;
-#endif
-    int fd;
-#ifndef MS_WINDOWS
     PyObject *cpathbytes, *cpathbytes_tmp;
     Py_ssize_t cpathbytes_len;
 #endif
+    int fd;
     PyObject *dirname;
     Py_UCS4 *dirsep;
     int res, ok;
@@ -1275,7 +1273,7 @@
         return;
     }
     cpathbytes_len = PyBytes_GET_SIZE(cpathbytes);
-    cpathbytes_tmp = PyBytes_FromStringAndSize(NULL, cpathbytes_len + 6);
+    cpathbytes_tmp = PyBytes_FromStringAndSize(NULL, cpathbytes_len + 4);
     if (cpathbytes_tmp == NULL) {
         Py_DECREF(cpathbytes);
         PyErr_Clear();
@@ -1283,9 +1281,10 @@
     }
     memcpy(PyBytes_AS_STRING(cpathbytes_tmp), PyBytes_AS_STRING(cpathbytes),
            cpathbytes_len);
-    memcpy(PyBytes_AS_STRING(cpathbytes_tmp) + cpathbytes_len, "XXXXXX", 6);
-
-    fd = mkstemp(PyBytes_AS_STRING(cpathbytes_tmp));
+    memcpy(PyBytes_AS_STRING(cpathbytes_tmp) + cpathbytes_len, ".tmp", 4);
+
+    fd = open(PyBytes_AS_STRING(cpathbytes_tmp),
+              O_CREAT | O_EXCL | O_WRONLY, 0666);
     if (0 <= fd)
         fp = fdopen(fd, "wb");
     else

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


More information about the Python-checkins mailing list