MCPcopy Index your code
hub / github.com/python/cpython / text_encoding

Function text_encoding

Lib/_pyio.py:41–65  ·  view source on GitHub ↗

A helper function to choose the text encoding. When encoding is not None, this function returns it. Otherwise, this function returns the default text encoding (i.e. "locale" or "utf-8" depends on UTF-8 mode). This function emits an EncodingWarning if *encoding* is None and

(encoding, stacklevel=2)

Source from the content-addressed store, hash-verified

39
40
41def text_encoding(encoding, stacklevel=2):
42 """
43 A helper function to choose the text encoding.
44
45 When encoding is not None, this function returns it.
46 Otherwise, this function returns the default text encoding
47 (i.e. "locale" or "utf-8" depends on UTF-8 mode).
48
49 This function emits an EncodingWarning if *encoding* is None and
50 sys.flags.warn_default_encoding is true.
51
52 This can be used in APIs with an encoding=None parameter
53 that pass it to TextIOWrapper or open.
54 However, please consider using encoding="utf-8" for new APIs.
55 """
56 if encoding is None:
57 if sys.flags.utf8_mode:
58 encoding = "utf-8"
59 else:
60 encoding = "locale"
61 if sys.flags.warn_default_encoding:
62 import warnings
63 warnings.warn("'encoding' argument not specified.",
64 EncodingWarning, stacklevel + 1)
65 return encoding
66
67
68# Wrapper for builtins.open

Callers 5

vfsopenFunction · 0.90
read_textMethod · 0.90
write_textMethod · 0.90
openFunction · 0.85
__init__Method · 0.85

Calls 1

warnMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…