diff options
| author | robot-piglet <[email protected]> | 2025-08-20 23:34:30 +0300 |
|---|---|---|
| committer | robot-piglet <[email protected]> | 2025-08-20 23:44:54 +0300 |
| commit | afccfc3d1a5a75ea4d6d80664f3fb3e3767ef502 (patch) | |
| tree | 0d536177172d1ca51ffd3a21faff45d077fc3c55 /yt/python | |
| parent | a77be389755d1ae713dcf36f940ce6a9ca0d65ca (diff) | |
Intermediate changes
commit_hash:3f564773a8aca49f5e0d33f7f83114b16497d01c
Diffstat (limited to 'yt/python')
| -rw-r--r-- | yt/python/yt/common.py | 27 |
1 files changed, 24 insertions, 3 deletions
diff --git a/yt/python/yt/common.py b/yt/python/yt/common.py index 043a94b0b37..3a409e4d091 100644 --- a/yt/python/yt/common.py +++ b/yt/python/yt/common.py @@ -5,8 +5,6 @@ except ImportError: from six import iteritems, PY3, text_type, binary_type, string_types from six.moves import map as imap -import yt.json_wrapper as json - try: from library.python.prctl import prctl except ImportError: @@ -31,6 +29,8 @@ import ctypes import errno import functools import inspect +# Intentionally use python-native json module because custom JSONEncoder is required. +import json import os import re import signal @@ -50,6 +50,8 @@ YT_NULL_TRANSACTION_ID = "0-0-0-0" _T = typing.TypeVar('_T') +BYTES_PRINTABLE = set(bytes(string.printable, "ascii")) + # Deprecation stuff. class YtDeprecationWarning(DeprecationWarning): @@ -455,10 +457,29 @@ def _pretty_format_escape(value): return "".join(imap(escape, value)) +def _pretty_format_bytes(value): + def escape(byte): + if byte in BYTES_PRINTABLE: + return chr(byte) + return "\\x{0:02x}".format(byte) + + try: + return value.decode("utf-8") + except UnicodeDecodeError: + return "".join(imap(escape, value)) + + def _pretty_format_attribute(name, value, attribute_length_limit): name = to_native_str(name) if isinstance(value, PrettyPrintableDict): - value = json.dumps(value, indent=2) + class BytesEncoder(json.JSONEncoder): + def default(self, obj): + if isinstance(obj, bytes): + return _pretty_format_bytes(obj) + else: + return super().default(obj) + + value = json.dumps(value, indent=2, cls=BytesEncoder) value = value.replace("\n", "\n" + " " * (15 + 1 + 4)) else: # YsonStringProxy attribute formatting. |
