mirror of
https://github.com/python/cpython.git
synced 2025-08-24 02:35:59 +00:00
bpo-1613500: Don't hardcode output file mode in fileinput.FileInput (GH-12986)
This commit is contained in:
parent
88c0937056
commit
be6dbfb43b
3 changed files with 16 additions and 2 deletions
|
@ -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:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue