Use the existing Tahoe JSON encoder.

This commit is contained in:
Itamar Turner-Trauring 2023-11-16 13:53:51 -05:00
parent 3ddfb92484
commit 83fa028925
7 changed files with 17 additions and 37 deletions

View File

@ -125,5 +125,5 @@ if sys.platform == "win32":
initialize()
from eliot import to_file
from allmydata.util.eliotutil import BytesEliotJSONEncoder
to_file(open("eliot.log", "wb"), encoder=BytesEliotJSONEncoder)
from allmydata.util.jsonbytes import AnyBytesJSONEncoder
to_file(open("eliot.log", "wb"), encoder=AnyBytesJSONEncoder)

View File

@ -38,8 +38,8 @@ from twisted.python.monkey import (
MonkeyPatcher,
)
from ..util.eliotutil import (
BytesEliotJSONEncoder
from ..util.jsonbytes import (
AnyBytesJSONEncoder
)
_NAME = Field.for_types(
@ -147,7 +147,7 @@ def with_logging(
"""
@wraps(test_method)
def run_with_logging(*args, **kwargs):
validating_logger = MemoryLogger(encoder=BytesEliotJSONEncoder)
validating_logger = MemoryLogger(encoder=AnyBytesJSONEncoder)
original = swap_logger(None)
try:
swap_logger(_TwoLoggers(original, validating_logger))

View File

@ -67,7 +67,7 @@ from allmydata.util import (
configutil,
jsonbytes as json,
)
from allmydata.util.eliotutil import capture_logging, BytesEliotJSONEncoder
from allmydata.util.eliotutil import capture_logging
from allmydata.util.fileutil import abspath_expanduser_unicode
from allmydata.interfaces import IFilesystemNode, IFileNode, \
IImmutableFileNode, IMutableFileNode, IDirectoryNode
@ -850,7 +850,7 @@ class StorageClients(SyncTestCase):
actionType=u"storage-client:broker:set-static-servers",
succeeded=True,
),
encoder_=BytesEliotJSONEncoder
encoder_=json.AnyBytesJSONEncoder
)
def test_static_servers(self, logger):
"""
@ -885,7 +885,7 @@ class StorageClients(SyncTestCase):
actionType=u"storage-client:broker:make-storage-server",
succeeded=False,
),
encoder_=BytesEliotJSONEncoder
encoder_=json.AnyBytesJSONEncoder
)
def test_invalid_static_server(self, logger):
"""

View File

@ -63,7 +63,6 @@ from twisted.internet.task import deferLater
from twisted.internet import reactor
from ..util.eliotutil import (
BytesEliotJSONEncoder,
log_call_deferred,
_parse_destination_description,
_EliotLogging,
@ -87,21 +86,6 @@ def passes():
return AfterPreprocessing(run, Equals(True))
class BytesEliotJSONEncoderTests(TestCase):
"""Tests for ``BytesEliotJSONEncoder``."""
def test_encoding_bytes(self):
"""``BytesEliotJSONEncoder`` can encode bytes."""
encoder = BytesEliotJSONEncoder()
self.assertEqual(encoder.default(b"xxx"), "xxx")
self.assertEqual(encoder.default(bytes([12])), "\x0c")
def test_encoding_sets(self):
"""``BytesEliotJSONEncoder`` can encode sets."""
encoder = BytesEliotJSONEncoder()
self.assertIn(encoder.default({1, 2}), ([1, 2], [2, 1]))
class EliotLoggedTestTests(TestCase):
"""
Tests for the automatic log-related provided by ``AsyncTestCase``.

View File

@ -117,7 +117,7 @@ class TestStreamingLogs(AsyncTestCase):
proto.transport.loseConnection()
yield proto.is_closed
self.assertThat(len(messages), Equals(3))
self.assertThat(len(messages), Equals(3), messages)
self.assertThat(messages[0]["action_type"], Equals("test:cli:some-exciting-action"))
self.assertThat(messages[0]["arguments"],
Equals(["hello", "good-\\xff-day", 123, {"a": 35}, [None]]))

View File

@ -50,7 +50,6 @@ from eliot.testing import (
MemoryLogger,
capture_logging,
)
from eliot.json import EliotJSONEncoder
from eliot._validation import (
ValidationError,
@ -78,16 +77,7 @@ from twisted.internet.defer import (
)
from twisted.application.service import Service
class BytesEliotJSONEncoder(EliotJSONEncoder):
"""Support encoding bytes."""
def default(self, o):
if isinstance(o, bytes):
return o.decode("utf-8", "backslashreplace")
if isinstance(o, set):
return list(o)
return EliotJSONEncoder.default(self, o)
from .jsonbytes import AnyBytesJSONEncoder
def validateInstanceOf(t):
@ -306,7 +296,7 @@ class _DestinationParser(object):
rotateLength=rotate_length,
maxRotatedFiles=max_rotated_files,
)
return lambda reactor: FileDestination(get_file(), encoder=BytesEliotJSONEncoder)
return lambda reactor: FileDestination(get_file(), encoder=AnyBytesJSONEncoder)
_parse_destination_description = _DestinationParser().parse

View File

@ -61,6 +61,9 @@ class UTF8BytesJSONEncoder(json.JSONEncoder):
"""
A JSON encoder than can also encode UTF-8 encoded strings.
"""
def default(self, o):
return bytes_to_unicode(False, o)
def encode(self, o, **kwargs):
return json.JSONEncoder.encode(
self, bytes_to_unicode(False, o), **kwargs)
@ -77,6 +80,9 @@ class AnyBytesJSONEncoder(json.JSONEncoder):
Bytes are decoded to strings using UTF-8, if that fails to decode then the
bytes are quoted.
"""
def default(self, o):
return bytes_to_unicode(True, o)
def encode(self, o, **kwargs):
return json.JSONEncoder.encode(
self, bytes_to_unicode(True, o), **kwargs)