From 0a9846b3428daa80ba3b7c9d113302d55baccbc6 Mon Sep 17 00:00:00 2001 From: Solderpunk Date: Sun, 19 Nov 2023 12:29:48 +0100 Subject: [PATCH] Correctly report downloaded file size in debug messages. Previously, the return value of fp.write() was used, however for text files this counts the number of characters written, not the number of bytes, and for non-ASCII content these differ. --- av98.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/av98.py b/av98.py index b20bf0e..470a14d 100755 --- a/av98.py +++ b/av98.py @@ -649,6 +649,7 @@ you'll be able to transparently follow links to Gopherspace!""") else: print("{} Received {} MiB...".format(spinner, chunk_count/10.0), end="\r") print(" "*80, end="\r") # Clean up prompt space + size = len(body) # Determine file mode if mime.startswith("text/"): @@ -666,9 +667,8 @@ you'll be able to transparently follow links to Gopherspace!""") encoding = None # Write - fp = open(destination or self.raw_file_buffer, mode=mode, encoding=encoding) - size = fp.write(body) - fp.close() + with open(destination or self.raw_file_buffer, mode=mode, encoding=encoding) as fp: + fp.write(body) return size