MCPcopy Index your code
hub / github.com/ekalinin/nodeenv / to_utf8

Function to_utf8

nodeenv.py:68–85  ·  view source on GitHub ↗

Convert given text to UTF-8 encoding (as far as possible).

(text)

Source from the content-addressed store, hash-verified

66
67# https://github.com/jhermann/waif/blob/master/python/to_uft8.py
68def to_utf8(text):
69 """Convert given text to UTF-8 encoding (as far as possible)."""
70 if not text or is_PY3:
71 return text
72
73 try: # unicode or pure ascii
74 return text.encode("utf8")
75 except UnicodeDecodeError:
76 try: # successful UTF-8 decode means it's pretty sure UTF-8
77 text.decode("utf8")
78 return text
79 except UnicodeDecodeError:
80 try: # get desperate; and yes, this has a western hemisphere bias
81 return text.decode("cp1252").encode("utf8")
82 except UnicodeDecodeError:
83 pass
84
85 return text # return unchanged, hope for the best
86
87
88class Config(object):

Callers 6

emitFunction · 0.85
writefileFunction · 0.85
copy_node_from_prebuiltFunction · 0.85
install_node_wrappedFunction · 0.85
create_environmentFunction · 0.85
get_env_dirFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected