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)
| 194 | |
| 195 | |
| 196 | def 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 | |
| 219 | def get_real_func(obj): |
no outgoing calls
no test coverage detected