MCPcopy Create free account
hub / github.com/RT-Thread/env-windows / text_encoding

Function text_encoding

tools/python-3.11.9-amd64/Lib/_pyio.py:43–67  ·  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

(encoding, stacklevel=2)

Source from the content-addressed store, hash-verified

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

Callers 2

openFunction · 0.85
__init__Method · 0.85

Calls 1

warnMethod · 0.45

Tested by

no test coverage detected