Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
e695eec2
Commit
e695eec2
authored
Oct 31, 2011
by
Charles-François Natali
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue #13303: Fix a race condition in the bytecode file creation.
parent
59142db6
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
12 additions
and
45 deletions
+12
-45
Lib/importlib/_bootstrap.py
Lib/importlib/_bootstrap.py
+4
-3
Python/import.c
Python/import.c
+8
-42
No files found.
Lib/importlib/_bootstrap.py
View file @
e695eec2
...
...
@@ -85,10 +85,11 @@ def _write_atomic(path, data):
Be prepared to handle a FileExistsError if concurrent writing of the
temporary file is attempted."""
if
not
sys
.
platform
.
startswith
(
'win'
):
# On POSIX-like platforms, renaming is atomic
path_tmp
=
path
+
'.tmp'
# 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
)
try
:
fd
=
_os
.
open
(
path_tmp
,
_os
.
O_EXCL
|
_os
.
O_CREAT
|
_os
.
O_WRONLY
)
with
_io
.
FileIO
(
fd
,
'wb'
)
as
file
:
file
.
write
(
data
)
_os
.
rename
(
path_tmp
,
path
)
...
...
Python/import.c
View file @
e695eec2
...
...
@@ -1183,42 +1183,6 @@ parse_source_module(PyObject *pathname, FILE *fp)
return
co
;
}
/* Helper to open a bytecode file for writing in exclusive mode */
#ifndef MS_WINDOWS
static
FILE
*
open_exclusive
(
char
*
filename
,
mode_t
mode
)
{
#if defined(O_EXCL)&&defined(O_CREAT)&&defined(O_WRONLY)&&defined(O_TRUNC)
/* Use O_EXCL to avoid a race condition when another process tries to
write the same file. When that happens, our open() call fails,
which is just fine (since it's only a cache).
XXX If the file exists and is writable but the directory is not
writable, the file will never be written. Oh well.
*/
int
fd
;
(
void
)
unlink
(
filename
);
fd
=
open
(
filename
,
O_EXCL
|
O_CREAT
|
O_WRONLY
|
O_TRUNC
#ifdef O_BINARY
|
O_BINARY
/* necessary for Windows */
#endif
#ifdef __VMS
,
mode
,
"ctxt=bin"
,
"shr=nil"
#else
,
mode
#endif
);
if
(
fd
<
0
)
return
NULL
;
return
fdopen
(
fd
,
"wb"
);
#else
/* Best we can do -- on Windows this can't happen anyway */
return
fopen
(
filename
,
"wb"
);
#endif
}
#endif
/* Write a compiled module to a file, placing the time of last
modification of its source into the header.
Errors are ignored, if a write error occurs an attempt is made to
...
...
@@ -1234,15 +1198,13 @@ write_compiled_module(PyCodeObject *co, PyObject *cpathname,
#ifdef MS_WINDOWS
/* since Windows uses different permissions */
mode_t
mode
=
srcstat
->
st_mode
&
~
S_IEXEC
;
#else
mode_t
mode
=
srcstat
->
st_mode
&
~
S_IXUSR
&
~
S_IXGRP
&
~
S_IXOTH
;
mode_t
dirmode
=
(
srcstat
->
st_mode
|
S_IXUSR
|
S_IXGRP
|
S_IXOTH
|
S_IWUSR
|
S_IWGRP
|
S_IWOTH
);
PyObject
*
dirbytes
;
#endif
#ifdef MS_WINDOWS
int
fd
;
#
else
#
ifndef MS_WINDOWS
PyObject
*
cpathbytes
,
*
cpathbytes_tmp
;
Py_ssize_t
cpathbytes_len
;
#endif
...
...
@@ -1313,7 +1275,7 @@ write_compiled_module(PyCodeObject *co, PyObject *cpathname,
return
;
}
cpathbytes_len
=
PyBytes_GET_SIZE
(
cpathbytes
);
cpathbytes_tmp
=
PyBytes_FromStringAndSize
(
NULL
,
cpathbytes_len
+
4
);
cpathbytes_tmp
=
PyBytes_FromStringAndSize
(
NULL
,
cpathbytes_len
+
6
);
if
(
cpathbytes_tmp
==
NULL
)
{
Py_DECREF
(
cpathbytes
);
PyErr_Clear
();
...
...
@@ -1321,9 +1283,13 @@ write_compiled_module(PyCodeObject *co, PyObject *cpathname,
}
memcpy
(
PyBytes_AS_STRING
(
cpathbytes_tmp
),
PyBytes_AS_STRING
(
cpathbytes
),
cpathbytes_len
);
memcpy
(
PyBytes_AS_STRING
(
cpathbytes_tmp
)
+
cpathbytes_len
,
"
.tmp"
,
4
);
memcpy
(
PyBytes_AS_STRING
(
cpathbytes_tmp
)
+
cpathbytes_len
,
"
XXXXXX"
,
6
);
fp
=
open_exclusive
(
PyBytes_AS_STRING
(
cpathbytes_tmp
),
mode
);
fd
=
mkstemp
(
PyBytes_AS_STRING
(
cpathbytes_tmp
));
if
(
0
<=
fd
)
fp
=
fdopen
(
fd
,
"wb"
);
else
fp
=
NULL
;
#endif
if
(
fp
==
NULL
)
{
if
(
Py_VerboseFlag
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment