def _to_bytes_impl(msg): if msg is not None and isinstance(msg, str_type): return codecs.latin_1_encode(msg, 'replace')[0] else: return msg