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
05b0a1be
Commit
05b0a1be
authored
Jun 09, 2014
by
Serhiy Storchaka
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue #21310: Fixed possible resource leak in failed open().
parent
6453a01a
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
75 additions
and
39 deletions
+75
-39
Lib/_pyio.py
Lib/_pyio.py
+37
-30
Lib/test/test_io.py
Lib/test/test_io.py
+15
-0
Misc/NEWS
Misc/NEWS
+2
-0
Modules/_io/_iomodule.c
Modules/_io/_iomodule.c
+21
-9
No files found.
Lib/_pyio.py
View file @
05b0a1be
...
@@ -192,38 +192,45 @@ def open(file, mode="r", buffering=-1,
...
@@ -192,38 +192,45 @@ def open(file, mode="r", buffering=-1,
(
appending
and
"a"
or
""
)
+
(
appending
and
"a"
or
""
)
+
(
updating
and
"+"
or
""
),
(
updating
and
"+"
or
""
),
closefd
)
closefd
)
line_buffering
=
False
result
=
raw
if
buffering
==
1
or
buffering
<
0
and
raw
.
isatty
():
try
:
buffering
=
-
1
line_buffering
=
False
line_buffering
=
True
if
buffering
==
1
or
buffering
<
0
and
raw
.
isatty
():
if
buffering
<
0
:
buffering
=
-
1
buffering
=
DEFAULT_BUFFER_SIZE
line_buffering
=
True
try
:
if
buffering
<
0
:
bs
=
os
.
fstat
(
raw
.
fileno
()).
st_blksize
buffering
=
DEFAULT_BUFFER_SIZE
except
(
os
.
error
,
AttributeError
):
try
:
pass
bs
=
os
.
fstat
(
raw
.
fileno
()).
st_blksize
except
(
os
.
error
,
AttributeError
):
pass
else
:
if
bs
>
1
:
buffering
=
bs
if
buffering
<
0
:
raise
ValueError
(
"invalid buffering size"
)
if
buffering
==
0
:
if
binary
:
return
result
raise
ValueError
(
"can't have unbuffered text I/O"
)
if
updating
:
buffer
=
BufferedRandom
(
raw
,
buffering
)
elif
writing
or
appending
:
buffer
=
BufferedWriter
(
raw
,
buffering
)
elif
reading
:
buffer
=
BufferedReader
(
raw
,
buffering
)
else
:
else
:
if
bs
>
1
:
raise
ValueError
(
"unknown mode: %r"
%
mode
)
buffering
=
bs
result
=
buffer
if
buffering
<
0
:
raise
ValueError
(
"invalid buffering size"
)
if
buffering
==
0
:
if
binary
:
if
binary
:
return
raw
return
result
raise
ValueError
(
"can't have unbuffered text I/O"
)
text
=
TextIOWrapper
(
buffer
,
encoding
,
errors
,
newline
,
line_buffering
)
if
updating
:
result
=
text
buffer
=
BufferedRandom
(
raw
,
buffering
)
text
.
mode
=
mode
elif
writing
or
appending
:
return
result
buffer
=
BufferedWriter
(
raw
,
buffering
)
except
:
elif
reading
:
result
.
close
()
buffer
=
BufferedReader
(
raw
,
buffering
)
raise
else
:
raise
ValueError
(
"unknown mode: %r"
%
mode
)
if
binary
:
return
buffer
text
=
TextIOWrapper
(
buffer
,
encoding
,
errors
,
newline
,
line_buffering
)
text
.
mode
=
mode
return
text
class
DocDescriptor
:
class
DocDescriptor
:
...
...
Lib/test/test_io.py
View file @
05b0a1be
...
@@ -29,6 +29,7 @@ import array
...
@@ -29,6 +29,7 @@ import array
import
random
import
random
import
unittest
import
unittest
import
weakref
import
weakref
import
warnings
import
abc
import
abc
import
signal
import
signal
import
errno
import
errno
...
@@ -603,6 +604,20 @@ class IOTest(unittest.TestCase):
...
@@ -603,6 +604,20 @@ class IOTest(unittest.TestCase):
fileio
.
close
()
fileio
.
close
()
f2
.
readline
()
f2
.
readline
()
def
test_nonbuffered_textio
(
self
):
with
warnings
.
catch_warnings
(
record
=
True
)
as
recorded
:
with
self
.
assertRaises
(
ValueError
):
self
.
open
(
support
.
TESTFN
,
'w'
,
buffering
=
0
)
support
.
gc_collect
()
self
.
assertEqual
(
recorded
,
[])
def
test_invalid_newline
(
self
):
with
warnings
.
catch_warnings
(
record
=
True
)
as
recorded
:
with
self
.
assertRaises
(
ValueError
):
self
.
open
(
support
.
TESTFN
,
'w'
,
newline
=
'invalid'
)
support
.
gc_collect
()
self
.
assertEqual
(
recorded
,
[])
class
CIOTest
(
IOTest
):
class
CIOTest
(
IOTest
):
...
...
Misc/NEWS
View file @
05b0a1be
...
@@ -25,6 +25,8 @@ Core and Builtins
...
@@ -25,6 +25,8 @@ Core and Builtins
Library
Library
-------
-------
- Issue #21310: Fixed possible resource leak in failed open().
- Issue #21304: Backport the key derivation function hashlib.pbkdf2_hmac from
- Issue #21304: Backport the key derivation function hashlib.pbkdf2_hmac from
Python 3 per PEP 466.
Python 3 per PEP 466.
...
...
Modules/_io/_iomodule.c
View file @
05b0a1be
...
@@ -303,7 +303,7 @@ io_open(PyObject *self, PyObject *args, PyObject *kwds)
...
@@ -303,7 +303,7 @@ io_open(PyObject *self, PyObject *args, PyObject *kwds)
int
line_buffering
;
int
line_buffering
;
long
isatty
;
long
isatty
;
PyObject
*
raw
,
*
modeobj
=
NULL
,
*
buffer
=
NULL
,
*
wrapper
=
NULL
;
PyObject
*
raw
,
*
modeobj
=
NULL
,
*
buffer
,
*
wrapper
,
*
result
=
NULL
;
if
(
!
PyArg_ParseTupleAndKeywords
(
args
,
kwds
,
"O|sizzzi:open"
,
kwlist
,
if
(
!
PyArg_ParseTupleAndKeywords
(
args
,
kwds
,
"O|sizzzi:open"
,
kwlist
,
&
file
,
&
mode
,
&
buffering
,
&
file
,
&
mode
,
&
buffering
,
...
@@ -416,6 +416,7 @@ io_open(PyObject *self, PyObject *args, PyObject *kwds)
...
@@ -416,6 +416,7 @@ io_open(PyObject *self, PyObject *args, PyObject *kwds)
"Osi"
,
file
,
rawmode
,
closefd
);
"Osi"
,
file
,
rawmode
,
closefd
);
if
(
raw
==
NULL
)
if
(
raw
==
NULL
)
return
NULL
;
return
NULL
;
result
=
raw
;
modeobj
=
PyUnicode_FromString
(
mode
);
modeobj
=
PyUnicode_FromString
(
mode
);
if
(
modeobj
==
NULL
)
if
(
modeobj
==
NULL
)
...
@@ -474,7 +475,7 @@ io_open(PyObject *self, PyObject *args, PyObject *kwds)
...
@@ -474,7 +475,7 @@ io_open(PyObject *self, PyObject *args, PyObject *kwds)
}
}
Py_DECREF
(
modeobj
);
Py_DECREF
(
modeobj
);
return
r
aw
;
return
r
esult
;
}
}
/* wraps into a buffered file */
/* wraps into a buffered file */
...
@@ -495,15 +496,16 @@ io_open(PyObject *self, PyObject *args, PyObject *kwds)
...
@@ -495,15 +496,16 @@ io_open(PyObject *self, PyObject *args, PyObject *kwds)
buffer
=
PyObject_CallFunction
(
Buffered_class
,
"Oi"
,
raw
,
buffering
);
buffer
=
PyObject_CallFunction
(
Buffered_class
,
"Oi"
,
raw
,
buffering
);
}
}
Py_CLEAR
(
raw
);
if
(
buffer
==
NULL
)
if
(
buffer
==
NULL
)
goto
error
;
goto
error
;
result
=
buffer
;
Py_DECREF
(
raw
);
/* if binary, returns the buffered file */
/* if binary, returns the buffered file */
if
(
binary
)
{
if
(
binary
)
{
Py_DECREF
(
modeobj
);
Py_DECREF
(
modeobj
);
return
buffer
;
return
result
;
}
}
/* wraps into a TextIOWrapper */
/* wraps into a TextIOWrapper */
...
@@ -512,20 +514,30 @@ io_open(PyObject *self, PyObject *args, PyObject *kwds)
...
@@ -512,20 +514,30 @@ io_open(PyObject *self, PyObject *args, PyObject *kwds)
buffer
,
buffer
,
encoding
,
errors
,
newline
,
encoding
,
errors
,
newline
,
line_buffering
);
line_buffering
);
Py_CLEAR
(
buffer
);
if
(
wrapper
==
NULL
)
if
(
wrapper
==
NULL
)
goto
error
;
goto
error
;
result
=
wrapper
;
Py_DECREF
(
buffer
);
if
(
PyObject_SetAttrString
(
wrapper
,
"mode"
,
modeobj
)
<
0
)
if
(
PyObject_SetAttrString
(
wrapper
,
"mode"
,
modeobj
)
<
0
)
goto
error
;
goto
error
;
Py_DECREF
(
modeobj
);
Py_DECREF
(
modeobj
);
return
wrapper
;
return
result
;
error:
error:
Py_XDECREF
(
raw
);
if
(
result
!=
NULL
)
{
PyObject
*
exc
,
*
val
,
*
tb
;
PyErr_Fetch
(
&
exc
,
&
val
,
&
tb
);
if
(
PyObject_CallMethod
(
result
,
"close"
,
NULL
)
!=
NULL
)
PyErr_Restore
(
exc
,
val
,
tb
);
else
{
Py_XDECREF
(
exc
);
Py_XDECREF
(
val
);
Py_XDECREF
(
tb
);
}
Py_DECREF
(
result
);
}
Py_XDECREF
(
modeobj
);
Py_XDECREF
(
modeobj
);
Py_XDECREF
(
buffer
);
Py_XDECREF
(
wrapper
);
return
NULL
;
return
NULL
;
}
}
...
...
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