mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2025-02-25 18:55:22 -06:00
Remove unnecessary bytes/str type check in FileAvoidWrite.write()
All calls to FileAvoidWrite.write() always pass a text string. Additionally, the type signature only allows type str.
This commit is contained in:
parent
6461ea233b
commit
e5bf235ba0
@ -18,11 +18,9 @@ import shutil
|
|||||||
import sys
|
import sys
|
||||||
import time
|
import time
|
||||||
import warnings
|
import warnings
|
||||||
from io import BytesIO, StringIO
|
from io import StringIO
|
||||||
from os import path
|
from os import path
|
||||||
|
|
||||||
from six import text_type
|
|
||||||
|
|
||||||
from sphinx.deprecation import RemovedInSphinx30Warning, RemovedInSphinx40Warning
|
from sphinx.deprecation import RemovedInSphinx30Warning, RemovedInSphinx40Warning
|
||||||
|
|
||||||
if False:
|
if False:
|
||||||
@ -238,17 +236,13 @@ class FileAvoidWrite:
|
|||||||
def __init__(self, path):
|
def __init__(self, path):
|
||||||
# type: (str) -> None
|
# type: (str) -> None
|
||||||
self._path = path
|
self._path = path
|
||||||
self._io = None # type: Union[StringIO, BytesIO]
|
self._io = None # type: StringIO
|
||||||
|
|
||||||
def write(self, data):
|
def write(self, data):
|
||||||
# type: (Union[str, str]) -> None
|
# type: (str) -> None
|
||||||
if not self._io:
|
if not self._io:
|
||||||
if isinstance(data, text_type):
|
self._io = StringIO()
|
||||||
self._io = StringIO()
|
self._io.write(data)
|
||||||
else:
|
|
||||||
self._io = BytesIO()
|
|
||||||
|
|
||||||
self._io.write(data) # type: ignore
|
|
||||||
|
|
||||||
def close(self):
|
def close(self):
|
||||||
# type: () -> None
|
# type: () -> None
|
||||||
@ -259,23 +253,15 @@ class FileAvoidWrite:
|
|||||||
buf = self.getvalue()
|
buf = self.getvalue()
|
||||||
self._io.close()
|
self._io.close()
|
||||||
|
|
||||||
r_mode = 'r'
|
|
||||||
w_mode = 'w'
|
|
||||||
if isinstance(self._io, BytesIO):
|
|
||||||
r_mode = 'rb'
|
|
||||||
w_mode = 'wb'
|
|
||||||
|
|
||||||
old_content = None
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
with open(self._path, r_mode) as old_f:
|
with open(self._path) as old_f:
|
||||||
old_content = old_f.read()
|
old_content = old_f.read()
|
||||||
if old_content == buf:
|
if old_content == buf:
|
||||||
return
|
return
|
||||||
except IOError:
|
except IOError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
with open(self._path, w_mode) as f:
|
with open(self._path, 'w') as f:
|
||||||
f.write(buf)
|
f.write(buf)
|
||||||
|
|
||||||
def __enter__(self):
|
def __enter__(self):
|
||||||
|
Loading…
Reference in New Issue
Block a user