# SPDX-License-Identifier: MIT
text = "café 東京 😀 ' \" \\ \n \x00"
print(ascii(text))
print(f"<{text!a}>")
print(ascii(b"\x00\x7f\x80\xff'\""))
print(ascii((1, True, None, "é")))
print(ascii([["é", "😀"], ["雪"]]))
print(ascii({"é": "😀", "雪": "中"}))
print(ascii({"é"}), ascii(frozenset(["😀"])))
print(ascii(range(-2, 7, 2)))
print(ascii(123456789012345678901234567890), ascii(False), ascii(None))
print(ascii(ValueError("é\n😀")))

events = []
class Label:
    def __init__(self, label):
        self.label = label
    def __repr__(self):
        events.append(self.label)
        return "<" + self.label + ">"
    def __str__(self):
        return "wrong"

def make_label(label):
    events.append("make:" + label)
    return Label(label)
print(ascii(make_label("é")), events)
events.clear()
print(f"{make_label('α')!a}|{make_label('😀')!a}", events)
events.clear()
print(ascii([Label("東京"), Label("é")]), events)

class Raw:
    def __repr__(self):
        return "\x00\t\n\r\\'\"\x7féÿĀ雪😀\U0010ffff"
print(repr(ascii(Raw())))
print(repr(f"{Raw()!a}"))

escape = ascii
def make_formatter(operation):
    def format(value):
        return operation(value)
    return format
format_value = make_formatter(escape)
print(format_value("é"), escape(*("雪",)))

class Broken:
    def __repr__(self):
        raise ValueError("repr failed")
try:
    print(ascii(Broken()))
except ValueError as error:
    print(str(error))
try:
    print(f"prefix{Broken()!a}suffix")
except ValueError as error:
    print(str(error))

class Invalid:
    def __repr__(self):
        return 7
try:
    print(ascii(Invalid()))
except TypeError as error:
    print(str(error))
