From 212ee1427bb1156f637acf3ad6206ad31399a1cf Mon Sep 17 00:00:00 2001 From: Paweł Redman Date: Tue, 24 Mar 2020 23:23:35 +0100 Subject: Handle bad UTF-8 sequences gracefully Tremded likes to print weird characters from a random codepage from time to time. --- rcon.py | 2 +- testing/log_simulator.py | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/rcon.py b/rcon.py index a0b8897..a84edc9 100644 --- a/rcon.py +++ b/rcon.py @@ -59,7 +59,7 @@ class JournalViewer: self.pipefd, writefd = os.pipe() fl = fcntl.fcntl(self.pipefd, fcntl.F_GETFL) fcntl.fcntl(self.pipefd, fcntl.F_SETFL, fl | os.O_NONBLOCK) - self.pipe = os.fdopen(self.pipefd) + self.pipe = os.fdopen(self.pipefd, encoding="utf-8", errors="replace") self.proc = subprocess.Popen(argv, stdout=writefd, stderr=writefd) self.loop.watch_file(self.pipefd, self.proc_write) diff --git a/testing/log_simulator.py b/testing/log_simulator.py index 8256412..68c5bc9 100644 --- a/testing/log_simulator.py +++ b/testing/log_simulator.py @@ -1,4 +1,4 @@ -import time, random +import sys, time, random # lipsum.pl pasta = [ @@ -17,6 +17,8 @@ pasta = [ "Nie muszę państwa przekonywać, że zmiana istniejących kryteriów koliduje z." ] +sys.stdout.buffer.write(b"Random trash to test invalid UTF-8 sequences: \xde\xad\xbe\xef\n") + print("Testing colors: ", end="") for ch in "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ": print("^%s%s" % (ch, ch), end="") -- cgit