MCPcopy
hub / github.com/pytest-dev/pytest / ascii_escaped

Function ascii_escaped

src/_pytest/compat.py:196–216  ·  view source on GitHub ↗

r"""If val is pure ASCII, return it as an str, otherwise, escape bytes objects into a sequence of escaped bytes: b'\xc3\xb4\xc5\xd6' -> r'\xc3\xb4\xc5\xd6' and escapes strings into a sequence of escaped unicode ids, e.g.: r'4\nV\U00043efa\x0eMXWB\x1e\u3028\u15fd\xcd\U0007d944'

(val: bytes | str)

Source from the content-addressed store, hash-verified

194
195
196def ascii_escaped(val: bytes | str) -> str:
197 r"""If val is pure ASCII, return it as an str, otherwise, escape
198 bytes objects into a sequence of escaped bytes:
199
200 b'\xc3\xb4\xc5\xd6' -> r'\xc3\xb4\xc5\xd6'
201
202 and escapes strings into a sequence of escaped unicode ids, e.g.:
203
204 r'4\nV\U00043efa\x0eMXWB\x1e\u3028\u15fd\xcd\U0007d944'
205
206 Note:
207 The obvious "v.decode('unicode-escape')" will return
208 valid UTF-8 unicode if it finds them in bytes, but we
209 want to return escaped bytes for any byte, even if they match
210 a UTF-8 string.
211 """
212 if isinstance(val, bytes):
213 ret = val.decode("ascii", "backslashreplace")
214 else:
215 ret = val.encode("unicode_escape").decode("ascii")
216 return ret.translate(_non_printable_ascii_translate_table)
217
218
219def get_real_func(obj):

Callers 2

_idval_from_valueMethod · 0.90
_ascii_escaped_by_configFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected