Skip to content

Commit

Permalink
Fix distrial log write.
Browse files Browse the repository at this point in the history
  • Loading branch information
adiroiban committed Mar 28, 2021
1 parent d8689c6 commit 77d38c6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
8 changes: 6 additions & 2 deletions src/twisted/trial/_dist/test/test_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,13 +341,17 @@ def test_childDataReceived(self):
def test_unicodeLogFileUTF8(self):
"""
L{LocalWorker} write the log data in UTF-8 encoding regardless of the
default python encoding
default python encoding.
"""
amp = SpyDataLocalWorkerAMP()
tempDir = FilePath(self.mktemp())
logFile = tempDir.child("test.log")

def getLatin1(do_setlocale=True):
def getLatin1(do_setlocale: bool = True) -> str:
"""
A replacement for L{locale.getpreferredencoding} that always
returns the Latin-1 encoding.
"""
return "latin-1"

self.patch(_bootlocale, "getpreferredencoding", getLatin1)
Expand Down
10 changes: 9 additions & 1 deletion src/twisted/trial/_dist/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,15 @@ def connectionMade(self):
os.makedirs(self._logDirectory)
self._outLog = open(os.path.join(self._logDirectory, "out.log"), "wb")
self._errLog = open(os.path.join(self._logDirectory, "err.log"), "wb")
self._testLog = open(os.path.join(self._logDirectory, self._logFile), "w")
# Log data is received via AMP which is UTF-8 unicode.
# The log file should be written using a Unicode encoding, and not
# the default system encoding which might not be Unicode compatible.
self._testLog = open(
os.path.join(self._logDirectory, self._logFile),
"w",
encoding="utf-8",
errors="backslashreplace",
)
self._ampProtocol.setTestStream(self._testLog)
logDirectory = self._logDirectory
d = self._ampProtocol.callRemote(workercommands.Start, directory=logDirectory)
Expand Down

0 comments on commit 77d38c6

Please sign in to comment.