From c2f85189fa126253312b3f4a4d72042490df2fb8 Mon Sep 17 00:00:00 2001 From: Pedro Larroy Date: Wed, 15 Sep 2021 17:48:40 +0200 Subject: [PATCH 1/2] Fix open in binary mode. --- test_pytracing.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test_pytracing.py b/test_pytracing.py index aa114d3..b3a333e 100755 --- a/test_pytracing.py +++ b/test_pytracing.py @@ -28,7 +28,7 @@ def main(): if __name__ == '__main__': - with io.open('./trace.out', mode='w', encoding='utf-8') as fh: + with io.open('./trace.out', mode='wb') as fh: tp = TraceProfiler(output=fh) tp.install() main() From 7f778745da287fca5231be3bc632987cc0ea1559 Mon Sep 17 00:00:00 2001 From: Pedro Larroy Date: Wed, 15 Sep 2021 17:53:57 +0200 Subject: [PATCH 2/2] Use default encoding for json. --- pytracing/pytracing.py | 11 ++++++++--- test_pytracing.py | 2 +- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/pytracing/pytracing.py b/pytracing/pytracing.py index 607f532..9cead8a 100644 --- a/pytracing/pytracing.py +++ b/pytracing/pytracing.py @@ -9,8 +9,11 @@ import json import time import threading +import logging from contextlib import contextmanager +logger = logging.getLogger(__name__) + try: from queue import Queue except ImportError: @@ -32,17 +35,19 @@ def __init__(self, terminator, input_queue, output_stream): def _open_collection(self): """Write the opening of a JSON array to the output.""" - self.output.write(b'[') + logger.debug('_open_collection') + self.output.write('[') def _close_collection(self): """Write the closing of a JSON array to the output.""" - self.output.write(b'{}]') # empty {} so the final entry doesn't end with a comma + logger.debug('_close_collection') + self.output.write('{}]') # empty {} so the final entry doesn't end with a comma def run(self): self._open_collection() while not self.terminator.is_set() or not self.input.empty(): item = self.input.get() - self.output.write((json.dumps(item) + ',\n').encode('ascii')) + self.output.write(json.dumps(item) + ',\n') self._close_collection() diff --git a/test_pytracing.py b/test_pytracing.py index b3a333e..aa114d3 100755 --- a/test_pytracing.py +++ b/test_pytracing.py @@ -28,7 +28,7 @@ def main(): if __name__ == '__main__': - with io.open('./trace.out', mode='wb') as fh: + with io.open('./trace.out', mode='w', encoding='utf-8') as fh: tp = TraceProfiler(output=fh) tp.install() main()