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
fcb27645
Commit
fcb27645
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 io.open() fails
parent
abd32d9f
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
35 additions
and
3 deletions
+35
-3
Lib/tempfile.py
Lib/tempfile.py
+7
-3
Lib/test/test_tempfile.py
Lib/test/test_tempfile.py
+24
-0
Misc/NEWS
Misc/NEWS
+4
-0
No files found.
Lib/tempfile.py
View file @
fcb27645
...
...
@@ -458,10 +458,14 @@ def NamedTemporaryFile(mode='w+b', buffering=-1, encoding=None,
flags
|=
_os
.
O_TEMPORARY
(
fd
,
name
)
=
_mkstemp_inner
(
dir
,
prefix
,
suffix
,
flags
)
file
=
_io
.
open
(
fd
,
mode
,
buffering
=
buffering
,
newline
=
newline
,
encoding
=
encoding
)
try
:
file
=
_io
.
open
(
fd
,
mode
,
buffering
=
buffering
,
newline
=
newline
,
encoding
=
encoding
)
return
_TemporaryFileWrapper
(
file
,
name
,
delete
)
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 @
fcb27645
...
...
@@ -9,6 +9,7 @@ import re
import
warnings
import
contextlib
import
weakref
from
unittest
import
mock
import
unittest
from
test
import
support
,
script_helper
...
...
@@ -758,6 +759,17 @@ class TestNamedTemporaryFile(BaseTestCase):
pass
self
.
assertRaises
(
ValueError
,
use_closed
)
def
test_no_leak_fd
(
self
):
# Issue #21058: don't leak file descriptor when io.pen() fails
closed
=
[]
def
close
(
fd
):
closed
.
append
(
fd
)
with
mock
.
patch
(
'os.close'
,
side_effect
=
close
):
with
mock
.
patch
(
'io.open'
,
side_effect
=
ValueError
):
self
.
assertRaises
(
ValueError
,
tempfile
.
NamedTemporaryFile
)
self
.
assertEqual
(
len
(
closed
),
1
)
# How to test the mode and bufsize parameters?
...
...
@@ -1061,6 +1073,18 @@ if tempfile.NamedTemporaryFile is not tempfile.TemporaryFile:
roundtrip
(
"
\
u039B
"
,
"w+"
,
encoding
=
"utf-16"
)
roundtrip
(
"foo
\
r
\
n
"
,
"w+"
,
newline
=
""
)
def
test_no_leak_fd
(
self
):
# Issue #21058: don't leak file descriptor when io.open() fails
closed
=
[]
def
close
(
fd
):
closed
.
append
(
fd
)
with
mock
.
patch
(
'os.close'
,
side_effect
=
close
):
with
mock
.
patch
(
'io.open'
,
side_effect
=
ValueError
):
self
.
assertRaises
(
ValueError
,
tempfile
.
TemporaryFile
)
self
.
assertEqual
(
len
(
closed
),
1
)
# Helper for test_del_on_shutdown
class
NulledModules
:
...
...
Misc/NEWS
View file @
fcb27645
...
...
@@ -24,6 +24,10 @@ Core and Builtins
Library
-------
- Issue #21058: Fix a leak of file descriptor in
:func:`tempfile.NamedTemporaryFile`, close the file descriptor if
:func:`io.open` fails
- Issue #21013: Enhance ssl.create_default_context() when used for server side
sockets to provide better security by default.
...
...
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