python: Open text files as text to skip decode operations.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg 2020-10-29 18:02:10 -07:00 committed by Tim Abbott
parent 18d0e4664c
commit cc55393671
4 changed files with 8 additions and 8 deletions

View File

@ -57,8 +57,8 @@ def get_requirements_hash(tmp_dir: str, use_test_lock_files: bool = False) -> st
lock_files_path = tmp_dir lock_files_path = tmp_dir
reqs_files.extend(sorted(glob.glob(os.path.join(lock_files_path, "*.txt")))) reqs_files.extend(sorted(glob.glob(os.path.join(lock_files_path, "*.txt"))))
for file_path in reqs_files: for file_path in reqs_files:
with open(file_path) as fp: with open(file_path, 'rb') as fp:
sha1.update(fp.read().encode("utf-8")) sha1.update(fp.read())
return sha1.hexdigest() return sha1.hexdigest()
def may_be_setup_cache() -> None: def may_be_setup_cache() -> None:

View File

@ -16,11 +16,11 @@ def start_server(logfile_name: str) -> Tuple[bool, str]:
failure = True failure = True
key = "Quit the server with CTRL-C." key = "Quit the server with CTRL-C."
datalog = [] datalog = []
with open(logfile_name, 'rb', buffering=0) as logfile: with open(logfile_name, 'r', buffering=0) as logfile:
for i in range(200): for i in range(200):
time.sleep(0.5) time.sleep(0.5)
print(f"{i}. Polling run-dev...") print(f"{i}. Polling run-dev...")
new_data = logfile.read().decode() new_data = logfile.read()
if new_data: if new_data:
datalog.append(new_data) datalog.append(new_data)

View File

@ -78,7 +78,7 @@ for msg in result['messages']:
messages.append(msg) messages.append(msg)
filename = f"zulip-{options.stream}.json" filename = f"zulip-{options.stream}.json"
with open(filename, 'wb') as f: with open(filename, 'w') as f:
f.write(json.dumps(messages, indent=0, sort_keys=False).encode('utf-8')) json.dump(messages, f, indent=0, sort_keys=False)
print(f"{len(messages)} messages exported to {filename}") print(f"{len(messages)} messages exported to {filename}")
sys.exit(0) sys.exit(0)

View File

@ -329,8 +329,8 @@ class QueueProcessingWorker(ABC):
line = f'{time.asctime()}\t{orjson.dumps(events).decode()}\n' line = f'{time.asctime()}\t{orjson.dumps(events).decode()}\n'
lock_fn = fn + '.lock' lock_fn = fn + '.lock'
with lockfile(lock_fn): with lockfile(lock_fn):
with open(fn, 'ab') as f: with open(fn, 'a') as f:
f.write(line.encode('utf-8')) f.write(line)
check_and_send_restart_signal() check_and_send_restart_signal()
def setup(self) -> None: def setup(self) -> None: