Issue #13303: Fix bytecode file default permission.

parent 1db7c13b
...@@ -88,7 +88,7 @@ def _write_atomic(path, data): ...@@ -88,7 +88,7 @@ def _write_atomic(path, data):
# On POSIX-like platforms, renaming is atomic. id() is used to generate # On POSIX-like platforms, renaming is atomic. id() is used to generate
# a pseudo-random filename. # a pseudo-random filename.
path_tmp = '{}.{}'.format(path, id(path)) 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: try:
with _io.FileIO(fd, 'wb') as file: with _io.FileIO(fd, 'wb') as file:
file.write(data) file.write(data)
......
...@@ -97,25 +97,22 @@ class ImportTests(unittest.TestCase): ...@@ -97,25 +97,22 @@ class ImportTests(unittest.TestCase):
@unittest.skipUnless(os.name == 'posix', @unittest.skipUnless(os.name == 'posix',
"test meaningful only on posix systems") "test meaningful only on posix systems")
def test_execute_bit_not_copied(self): def test_creation_mode(self):
# Issue 6070: under posix .pyc files got their execute bit set if mask = 0o022
# the .py file had the execute bit set, but they aren't executable. with temp_umask(mask):
with temp_umask(0o022):
sys.path.insert(0, os.curdir) sys.path.insert(0, os.curdir)
try: try:
fname = TESTFN + os.extsep + "py" fname = TESTFN + os.extsep + "py"
create_empty_file(fname) 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) __import__(TESTFN)
fn = imp.cache_from_source(fname) fn = imp.cache_from_source(fname)
if not os.path.exists(fn): if not os.path.exists(fn):
self.fail("__import__ did not result in creation of " self.fail("__import__ did not result in creation of "
"either a .pyc or .pyo file") "either a .pyc or .pyo file")
s = os.stat(fn) s = os.stat(fn)
self.assertEqual( # Check that the umask is respected, and the executable bits
stat.S_IMODE(s.st_mode), # aren't set.
stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH) self.assertEqual(stat.S_IMODE(s.st_mode), 0o666 & ~mask)
finally: finally:
del sys.path[0] del sys.path[0]
remove_files(TESTFN) remove_files(TESTFN)
......
...@@ -1202,12 +1202,10 @@ write_compiled_module(PyCodeObject *co, PyObject *cpathname, ...@@ -1202,12 +1202,10 @@ write_compiled_module(PyCodeObject *co, PyObject *cpathname,
S_IXUSR | S_IXGRP | S_IXOTH | S_IXUSR | S_IXGRP | S_IXOTH |
S_IWUSR | S_IWGRP | S_IWOTH); S_IWUSR | S_IWGRP | S_IWOTH);
PyObject *dirbytes; PyObject *dirbytes;
#endif
int fd;
#ifndef MS_WINDOWS
PyObject *cpathbytes, *cpathbytes_tmp; PyObject *cpathbytes, *cpathbytes_tmp;
Py_ssize_t cpathbytes_len; Py_ssize_t cpathbytes_len;
#endif #endif
int fd;
PyObject *dirname; PyObject *dirname;
Py_UCS4 *dirsep; Py_UCS4 *dirsep;
int res, ok; int res, ok;
...@@ -1275,7 +1273,7 @@ write_compiled_module(PyCodeObject *co, PyObject *cpathname, ...@@ -1275,7 +1273,7 @@ write_compiled_module(PyCodeObject *co, PyObject *cpathname,
return; return;
} }
cpathbytes_len = PyBytes_GET_SIZE(cpathbytes); 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) { if (cpathbytes_tmp == NULL) {
Py_DECREF(cpathbytes); Py_DECREF(cpathbytes);
PyErr_Clear(); PyErr_Clear();
...@@ -1283,9 +1281,10 @@ write_compiled_module(PyCodeObject *co, PyObject *cpathname, ...@@ -1283,9 +1281,10 @@ write_compiled_module(PyCodeObject *co, PyObject *cpathname,
} }
memcpy(PyBytes_AS_STRING(cpathbytes_tmp), PyBytes_AS_STRING(cpathbytes), memcpy(PyBytes_AS_STRING(cpathbytes_tmp), PyBytes_AS_STRING(cpathbytes),
cpathbytes_len); cpathbytes_len);
memcpy(PyBytes_AS_STRING(cpathbytes_tmp) + cpathbytes_len, "XXXXXX", 6); memcpy(PyBytes_AS_STRING(cpathbytes_tmp) + cpathbytes_len, ".tmp", 4);
fd = mkstemp(PyBytes_AS_STRING(cpathbytes_tmp)); fd = open(PyBytes_AS_STRING(cpathbytes_tmp),
O_CREAT | O_EXCL | O_WRONLY, 0666);
if (0 <= fd) if (0 <= fd)
fp = fdopen(fd, "wb"); fp = fdopen(fd, "wb");
else else
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment