def _to_bytes_impl(msg): if isinstance(msg, str): return codecs.latin_1_encode(msg, 'replace')[0] else: return msg