Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def sanitize_text(text, encoding=DEFAULT_ENCODING):
text = stringify(text, encoding_default=encoding)
if text is not None:
try:
text = compose_nfc(text)
except (SystemError, Exception) as ex:
log.warning("Cannot NFC text: %s", ex)
return None
text = remove_unsafe_chars(text)
text = text.encode(DEFAULT_ENCODING, "replace")
return text.decode(DEFAULT_ENCODING, "replace")