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
be6dbfb4
Commit
be6dbfb4
authored
Apr 29, 2019
by
Berker Peksag
Committed by
GitHub
Apr 29, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
bpo-1613500: Don't hardcode output file mode in fileinput.FileInput (GH-12986)
parent
88c09370
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
16 additions
and
2 deletions
+16
-2
Lib/fileinput.py
Lib/fileinput.py
+3
-2
Lib/test/test_fileinput.py
Lib/test/test_fileinput.py
+10
-0
Misc/NEWS.d/next/Library/2019-04-27-21-09-33.bpo-1613500.Ogp4P0.rst
...d/next/Library/2019-04-27-21-09-33.bpo-1613500.Ogp4P0.rst
+3
-0
No files found.
Lib/fileinput.py
View file @
be6dbfb4
...
...
@@ -222,6 +222,7 @@ class FileInput:
warnings
.
warn
(
"'U' mode is deprecated"
,
DeprecationWarning
,
2
)
self
.
_mode
=
mode
self
.
_write_mode
=
mode
.
replace
(
'r'
,
'w'
)
if
'U'
not
in
mode
else
'w'
if
openhook
:
if
inplace
:
raise
ValueError
(
"FileInput cannot use an opening hook in inplace mode"
)
...
...
@@ -348,14 +349,14 @@ class FileInput:
try
:
perm
=
os
.
fstat
(
self
.
_file
.
fileno
()).
st_mode
except
OSError
:
self
.
_output
=
open
(
self
.
_filename
,
"w"
)
self
.
_output
=
open
(
self
.
_filename
,
self
.
_write_mode
)
else
:
mode
=
os
.
O_CREAT
|
os
.
O_WRONLY
|
os
.
O_TRUNC
if
hasattr
(
os
,
'O_BINARY'
):
mode
|=
os
.
O_BINARY
fd
=
os
.
open
(
self
.
_filename
,
mode
,
perm
)
self
.
_output
=
os
.
fdopen
(
fd
,
"w"
)
self
.
_output
=
os
.
fdopen
(
fd
,
self
.
_write_mode
)
try
:
os
.
chmod
(
self
.
_filename
,
perm
)
except
OSError
:
...
...
Lib/test/test_fileinput.py
View file @
be6dbfb4
...
...
@@ -329,6 +329,16 @@ class FileInputTests(BaseTests, unittest.TestCase):
self
.
assertEqual
(
fi
.
readline
(),
b''
)
self
.
assertEqual
(
fi
.
readline
(),
b''
)
def
test_inplace_binary_write_mode
(
self
):
temp_file
=
self
.
writeTmp
(
b'Initial text.'
,
mode
=
'wb'
)
with
FileInput
(
temp_file
,
mode
=
'rb'
,
inplace
=
True
)
as
fobj
:
line
=
fobj
.
readline
()
self
.
assertEqual
(
line
,
b'Initial text.'
)
# print() cannot be used with files opened in binary mode.
sys
.
stdout
.
write
(
b'New line.'
)
with
open
(
temp_file
,
'rb'
)
as
f
:
self
.
assertEqual
(
f
.
read
(),
b'New line.'
)
def
test_context_manager
(
self
):
t1
=
self
.
writeTmp
(
"A
\
n
B
\
n
C"
)
t2
=
self
.
writeTmp
(
"D
\
n
E
\
n
F"
)
...
...
Misc/NEWS.d/next/Library/2019-04-27-21-09-33.bpo-1613500.Ogp4P0.rst
0 → 100644
View file @
be6dbfb4
:class:`fileinput.FileInput` now uses the input file mode to correctly set
the output file mode (previously it was hardcoded to ``'w'``) when
``inplace=True`` is passed to its constructor.
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