Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0c929d9

Browse files
author
Charles-François Natali
committed
Issue #13303: Fix bytecode file default permission.
1 parent 1db7c13 commit 0c929d9

3 files changed

Lines changed: 13 additions & 17 deletions

File tree

Lib/importlib/_bootstrap.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def _write_atomic(path, data):
8888
# On POSIX-like platforms, renaming is atomic. id() is used to generate
8989
# a pseudo-random filename.
9090
path_tmp = '{}.{}'.format(path, id(path))
91-
fd = _os.open(path_tmp, _os.O_EXCL | _os.O_CREAT | _os.O_WRONLY)
91+
fd = _os.open(path_tmp, _os.O_EXCL | _os.O_CREAT | _os.O_WRONLY, 0o666)
9292
try:
9393
with _io.FileIO(fd, 'wb') as file:
9494
file.write(data)

Lib/test/test_import.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -97,25 +97,22 @@ def test_with_extension(ext):
9797

9898
@unittest.skipUnless(os.name == 'posix',
9999
"test meaningful only on posix systems")
100-
def test_execute_bit_not_copied(self):
101-
# Issue 6070: under posix .pyc files got their execute bit set if
102-
# the .py file had the execute bit set, but they aren't executable.
103-
with temp_umask(0o022):
100+
def test_creation_mode(self):
101+
mask = 0o022
102+
with temp_umask(mask):
104103
sys.path.insert(0, os.curdir)
105104
try:
106105
fname = TESTFN + os.extsep + "py"
107106
create_empty_file(fname)
108-
os.chmod(fname, (stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH |
109-
stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH))
110107
__import__(TESTFN)
111108
fn = imp.cache_from_source(fname)
112109
if not os.path.exists(fn):
113110
self.fail("__import__ did not result in creation of "
114111
"either a .pyc or .pyo file")
115-
s = os.stat(fn)
116-
self.assertEqual(
117-
stat.S_IMODE(s.st_mode),
118-
stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH)
112+
s = os.stat(fn)
113+
# Check that the umask is respected, and the executable bits
114+
# aren't set.
115+
self.assertEqual(stat.S_IMODE(s.st_mode), 0o666 & ~mask)
119116
finally:
120117
del sys.path[0]
121118
remove_files(TESTFN)

Python/import.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1202,12 +1202,10 @@ write_compiled_module(PyCodeObject *co, PyObject *cpathname,
12021202
S_IXUSR | S_IXGRP | S_IXOTH |
12031203
S_IWUSR | S_IWGRP | S_IWOTH);
12041204
PyObject *dirbytes;
1205-
#endif
1206-
int fd;
1207-
#ifndef MS_WINDOWS
12081205
PyObject *cpathbytes, *cpathbytes_tmp;
12091206
Py_ssize_t cpathbytes_len;
12101207
#endif
1208+
int fd;
12111209
PyObject *dirname;
12121210
Py_UCS4 *dirsep;
12131211
int res, ok;
@@ -1275,17 +1273,18 @@ write_compiled_module(PyCodeObject *co, PyObject *cpathname,
12751273
return;
12761274
}
12771275
cpathbytes_len = PyBytes_GET_SIZE(cpathbytes);
1278-
cpathbytes_tmp = PyBytes_FromStringAndSize(NULL, cpathbytes_len + 6);
1276+
cpathbytes_tmp = PyBytes_FromStringAndSize(NULL, cpathbytes_len + 4);
12791277
if (cpathbytes_tmp == NULL) {
12801278
Py_DECREF(cpathbytes);
12811279
PyErr_Clear();
12821280
return;
12831281
}
12841282
memcpy(PyBytes_AS_STRING(cpathbytes_tmp), PyBytes_AS_STRING(cpathbytes),
12851283
cpathbytes_len);
1286-
memcpy(PyBytes_AS_STRING(cpathbytes_tmp) + cpathbytes_len, "XXXXXX", 6);
1284+
memcpy(PyBytes_AS_STRING(cpathbytes_tmp) + cpathbytes_len, ".tmp", 4);
12871285

1288-
fd = mkstemp(PyBytes_AS_STRING(cpathbytes_tmp));
1286+
fd = open(PyBytes_AS_STRING(cpathbytes_tmp),
1287+
O_CREAT | O_EXCL | O_WRONLY, 0666);
12891288
if (0 <= fd)
12901289
fp = fdopen(fd, "wb");
12911290
else

0 commit comments

Comments
 (0)