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
367f5d37
Commit
367f5d37
authored
Mar 25, 2014
by
Victor Stinner
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue #21058: Fix a leak of file descriptor in tempfile.NamedTemporaryFile(),
close the file descriptor if os.fdopen() fails
parent
74a4ebae
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
27 additions
and
2 deletions
+27
-2
Lib/tempfile.py
Lib/tempfile.py
+6
-2
Lib/test/test_tempfile.py
Lib/test/test_tempfile.py
+18
-0
Misc/NEWS
Misc/NEWS
+3
-0
No files found.
Lib/tempfile.py
View file @
367f5d37
...
...
@@ -460,8 +460,12 @@ def NamedTemporaryFile(mode='w+b', bufsize=-1, suffix="",
flags
|=
_os
.
O_TEMPORARY
(
fd
,
name
)
=
_mkstemp_inner
(
dir
,
prefix
,
suffix
,
flags
)
file
=
_os
.
fdopen
(
fd
,
mode
,
bufsize
)
return
_TemporaryFileWrapper
(
file
,
name
,
delete
)
try
:
file
=
_os
.
fdopen
(
fd
,
mode
,
bufsize
)
return
_TemporaryFileWrapper
(
file
,
name
,
delete
)
except
Exception
:
_os
.
close
(
fd
)
raise
if
_os
.
name
!=
'posix'
or
_os
.
sys
.
platform
==
'cygwin'
:
# On non-POSIX and Cygwin systems, assume that we cannot unlink a file
...
...
Lib/test/test_tempfile.py
View file @
367f5d37
...
...
@@ -771,6 +771,24 @@ class test_NamedTemporaryFile(TC):
pass
self
.
assertRaises
(
ValueError
,
use_closed
)
def
test_no_leak_fd
(
self
):
# Issue #21058: don't leak file descriptor when fdopen() fails
old_close
=
os
.
close
old_fdopen
=
os
.
fdopen
closed
=
[]
def
close
(
fd
):
closed
.
append
(
fd
)
def
fdopen
(
*
args
):
raise
ValueError
()
os
.
close
=
close
os
.
fdopen
=
fdopen
try
:
self
.
assertRaises
(
ValueError
,
tempfile
.
NamedTemporaryFile
)
self
.
assertEqual
(
len
(
closed
),
1
)
finally
:
os
.
close
=
old_close
os
.
fdopen
=
old_fdopen
# How to test the mode and bufsize parameters?
test_classes
.
append
(
test_NamedTemporaryFile
)
...
...
Misc/NEWS
View file @
367f5d37
...
...
@@ -40,6 +40,9 @@ Core and Builtins
Library
-------
-
Issue
#
21058
:
Fix
a
leak
of
file
descriptor
in
tempfile
.
NamedTemporaryFile
(),
close
the
file
descriptor
if
os
.
fdopen
()
fails
-
Issue
#
20283
:
RE
pattern
methods
now
accept
the
string
keyword
parameters
as
documented
.
The
pattern
and
source
keyword
parameters
are
left
as
deprecated
aliases
.
...
...
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