import typing


def bytes_escape(bs: bytes, *, quote: typing.Optional[str] = None) -> str:
	"""Convert a bytestring to an ASCII string,
	with non-ASCII characters hex-escaped.
	
	(We implement our own escaping mechanism here to not depend on Python's str or bytes repr.)
	"""
	
	out = []
	# The bytestring is decoded as Latin-1 instead of ASCII here so that non-ASCII characters don't cause an error.
	# The conditions inside the loop ensure that only ASCII characters are actually output.
	for byte, char in zip(bs, bs.decode("latin-1")):
		if char in {quote, "\\"}:
			out.append(f"\\{char}")
		elif char.isprintable() and byte < 0x80:
			out.append(char)
		else:
			out.append(f"\\x{byte:02x}")
	
	return "".join(out)