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
02ab7a84
Commit
02ab7a84
authored
Apr 09, 2014
by
Benjamin Peterson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
make sure fdopen always closes the fd in error cases (closes #21191)
parent
d5460096
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
22 additions
and
6 deletions
+22
-6
Doc/library/os.rst
Doc/library/os.rst
+3
-2
Lib/test/test_posix.py
Lib/test/test_posix.py
+4
-0
Misc/NEWS
Misc/NEWS
+3
-0
Modules/posixmodule.c
Modules/posixmodule.c
+12
-4
No files found.
Doc/library/os.rst
View file @
02ab7a84
...
...
@@ -463,8 +463,9 @@ These functions create new file objects. (See also :func:`open`.)
.. index:: single: I/O control; buffering
Return an open file object connected to the file descriptor *fd*. The *mode*
and *bufsize* arguments have the same meaning as the corresponding arguments to
the built-in :func:`open` function.
and *bufsize* arguments have the same meaning as the corresponding arguments
to the built-in :func:`open` function. If :func:`fdopen` raises an
exception, it closes *fd*.
Availability: Unix, Windows.
...
...
Lib/test/test_posix.py
View file @
02ab7a84
...
...
@@ -194,6 +194,10 @@ class PosixTester(unittest.TestCase):
self
.
fdopen_helper
(
'r'
)
self
.
fdopen_helper
(
'r'
,
100
)
fd
=
os
.
open
(
test_support
.
TESTFN
,
os
.
O_RDONLY
)
self
.
assertRaises
(
OSError
,
posix
.
fdopen
,
fd
,
'w'
)
self
.
assertRaises
(
OSError
,
os
.
close
,
fd
)
# fd should be closed.
@
unittest
.
skipUnless
(
hasattr
(
posix
,
'O_EXLOCK'
),
'test needs posix.O_EXLOCK'
)
def
test_osexlock
(
self
):
...
...
Misc/NEWS
View file @
02ab7a84
...
...
@@ -43,6 +43,9 @@ Core and Builtins
Library
-------
-
Issue
#
21191
:
In
os
.
fdopen
,
alwyas
close
the
file
descriptor
when
an
exception
happens
.
-
Issue
#
21149
:
Improved
thread
-
safety
in
logging
cleanup
during
interpreter
shutdown
.
Thanks
to
Devin
Jeanpierre
for
the
patch
.
...
...
Modules/posixmodule.c
View file @
02ab7a84
...
...
@@ -6841,16 +6841,21 @@ posix_fdopen(PyObject *self, PyObject *args)
/* Sanitize mode. See fileobject.c */
mode
=
PyMem_MALLOC
(
strlen
(
orgmode
)
+
3
);
if
(
!
mode
)
{
close
(
fd
);
PyErr_NoMemory
();
return
NULL
;
}
strcpy
(
mode
,
orgmode
);
if
(
_PyFile_SanitizeMode
(
mode
))
{
close
(
fd
);
PyMem_FREE
(
mode
);
return
NULL
;
}
if
(
!
_PyVerify_fd
(
fd
))
return
posix_error
();
if
(
!
_PyVerify_fd
(
fd
))
{
posix_error
();
close
(
fd
);
return
NULL
;
}
Py_BEGIN_ALLOW_THREADS
#if !defined(MS_WINDOWS) && defined(HAVE_FCNTL_H)
if
(
mode
[
0
]
==
'a'
)
{
...
...
@@ -6871,8 +6876,11 @@ posix_fdopen(PyObject *self, PyObject *args)
#endif
Py_END_ALLOW_THREADS
PyMem_FREE
(
mode
);
if
(
fp
==
NULL
)
return
posix_error
();
if
(
fp
==
NULL
)
{
posix_error
();
close
(
fd
);
return
NULL
;
}
/* The dummy filename used here must be kept in sync with the value
tested against in gzip.GzipFile.__init__() - see issue #13781. */
f
=
PyFile_FromFile
(
fp
,
"<fdopen>"
,
orgmode
,
fclose
);
...
...
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