summaryrefslogtreecommitdiff
path: root/src/click/_termui_impl.py
diff options
context:
space:
mode:
authorDavid Lord <davidism@gmail.com>2020-08-08 10:53:09 -0700
committerDavid Lord <davidism@gmail.com>2020-08-08 10:59:16 -0700
commit9d4fc53be5997eaf6b7ad099e05749bcded03800 (patch)
tree1fe8960ff69c34adbc6e6517662077229b827d3c /src/click/_termui_impl.py
parent11c286a37d55357b82c7e5db7237544cd79fe25a (diff)
downloadclick-9d4fc53be5997eaf6b7ad099e05749bcded03800.tar.gz
clean up Editor.edit code
use context manager for opening file move encoding out of try block use isinstance instead of type
Diffstat (limited to 'src/click/_termui_impl.py')
-rw-r--r--src/click/_termui_impl.py43
1 files changed, 21 insertions, 22 deletions
diff --git a/src/click/_termui_impl.py b/src/click/_termui_impl.py
index 1ef020f..5394140 100644
--- a/src/click/_termui_impl.py
+++ b/src/click/_termui_impl.py
@@ -474,25 +474,26 @@ class Editor:
def edit(self, text):
import tempfile
- text = text or ""
- binary_data = type(text) in [bytes, bytearray]
+ if not text:
+ text = ""
- if not binary_data and text and not text.endswith("\n"):
- text += "\n"
+ is_bytes = isinstance(text, (bytes, bytearray))
+
+ if not is_bytes:
+ if text and not text.endswith("\n"):
+ text += "\n"
+
+ if WIN:
+ text = text.replace("\n", "\r\n").encode("utf-8-sig")
+ else:
+ text = text.encode("utf-8")
fd, name = tempfile.mkstemp(prefix="editor-", suffix=self.extension)
+
try:
- if not binary_data:
- if WIN:
- encoding = "utf-8-sig"
- text = text.replace("\n", "\r\n")
- else:
- encoding = "utf-8"
- text = text.encode(encoding)
-
- f = os.fdopen(fd, "wb")
- f.write(text)
- f.close()
+ with os.fdopen(fd, "wb") as f:
+ f.write(text)
+
# If the filesystem resolution is 1 second, like Mac OS
# 10.12 Extended, or 2 seconds, like FAT32, and the editor
# closes very fast, require_save can fail. Set the modified
@@ -507,15 +508,13 @@ class Editor:
if self.require_save and os.path.getmtime(name) == timestamp:
return None
- f = open(name, "rb")
- try:
+ with open(name, "rb") as f:
rv = f.read()
- finally:
- f.close()
- if binary_data:
+
+ if is_bytes:
return rv
- else:
- return rv.decode("utf-8-sig").replace("\r\n", "\n")
+
+ return rv.decode("utf-8-sig").replace("\r\n", "\n")
finally:
os.unlink(name)