MCPcopy Index your code
hub / github.com/ipython/ipython / getdefaultencoding

Function getdefaultencoding

IPython/utils/encoding.py:46–89  ·  view source on GitHub ↗

Return IPython's guess for the default encoding for bytes as text. If prefer_stream is True (default), asks for stdin.encoding first, to match the calling Terminal, but that is often None for subprocesses. Then fall back on locale.getpreferredencoding(), which should be a sensible

(prefer_stream: object | bool = _sentinel)

Source from the content-addressed store, hash-verified

44# Defined here as central function, so if we find better choices, we
45# won't need to make changes all over IPython.
46def getdefaultencoding(prefer_stream: object | bool = _sentinel) -> str:
47 """Return IPython's guess for the default encoding for bytes as text.
48
49 If prefer_stream is True (default), asks for stdin.encoding first,
50 to match the calling Terminal, but that is often None for subprocesses.
51
52 Then fall back on locale.getpreferredencoding(),
53 which should be a sensible platform default (that respects LANG environment),
54 and finally to sys.getdefaultencoding() which is the most conservative option,
55 and usually UTF8 as of Python 3.
56 """
57 if prefer_stream is not _sentinel:
58 warnings.warn(
59 "getpreferredencoding(prefer_stream=) argument is deprecated since "
60 "IPython 9.0, getdefaultencoding() will take no argument in the "
61 "future. If you rely on `prefer_stream`, please open an issue on "
62 "the IPython repo.",
63 DeprecationWarning,
64 stacklevel=2,
65 )
66 prefer_stream = True
67 enc: str | None = None
68 if prefer_stream:
69 enc = get_stream_enc(sys.stdin)
70 if not enc or enc == "ascii":
71 try:
72 # There are reports of getpreferredencoding raising errors
73 # in some cases, which may well be fixed, but let's be conservative here.
74 enc = locale.getpreferredencoding()
75 except Exception:
76 pass
77 enc = enc or sys.getdefaultencoding()
78 # On windows `cp0` can be returned to indicate that there is no code page.
79 # Since cp0 is an invalid encoding return instead cp1252 which is the
80 # Western European default.
81 if enc == "cp0":
82 warnings.warn(
83 "Invalid code page cp0 detected - using cp1252 instead."
84 "If cp1252 is incorrect please ensure a valid code page "
85 "is defined for the process.",
86 RuntimeWarning,
87 )
88 return "cp1252"
89 return enc
90
91
92DEFAULT_ENCODING = getdefaultencoding()

Callers 1

encoding.pyFile · 0.85

Calls 2

get_stream_encFunction · 0.85
warnMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…