1# coding: utf-8
2"""
3Utilities for dealing with text encodings
4"""
5
6from __future__ import annotations
7
8# -----------------------------------------------------------------------------
9# Copyright (C) 2008-2012 The IPython Development Team
10#
11# Distributed under the terms of the BSD License. The full license is in
12# the file COPYING, distributed as part of this software.
13# -----------------------------------------------------------------------------
14
15# -----------------------------------------------------------------------------
16# Imports
17# -----------------------------------------------------------------------------
18import sys
19import locale
20import warnings
21from typing import Any, Literal
22
23
24# to deal with the possibility of sys.std* not being a stream at all
25def get_stream_enc(stream: Any, default: str | None = None) -> str | None:
26 """Return the given stream's encoding or a default.
27
28 There are cases where ``sys.std*`` might not actually be a stream, so
29 check for the encoding attribute prior to returning it, and return
30 a default if it doesn't exist or evaluates as False. ``default``
31 is None if not provided.
32 """
33 if not hasattr(stream, "encoding") or not stream.encoding:
34 return default
35 else:
36 return stream.encoding
37
38
39_sentinel: object = object()
40
41
42# Less conservative replacement for sys.getdefaultencoding, that will try
43# to match the environment.
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()