Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/IPython/utils/encoding.py: 70%
23 statements
« prev ^ index » next coverage.py v7.4.4, created at 2024-04-20 06:09 +0000
« prev ^ index » next coverage.py v7.4.4, created at 2024-04-20 06:09 +0000
1# coding: utf-8
2"""
3Utilities for dealing with text encodings
4"""
6#-----------------------------------------------------------------------------
7# Copyright (C) 2008-2012 The IPython Development Team
8#
9# Distributed under the terms of the BSD License. The full license is in
10# the file COPYING, distributed as part of this software.
11#-----------------------------------------------------------------------------
13#-----------------------------------------------------------------------------
14# Imports
15#-----------------------------------------------------------------------------
16import sys
17import locale
18import warnings
20# to deal with the possibility of sys.std* not being a stream at all
21def get_stream_enc(stream, default=None):
22 """Return the given stream's encoding or a default.
24 There are cases where ``sys.std*`` might not actually be a stream, so
25 check for the encoding attribute prior to returning it, and return
26 a default if it doesn't exist or evaluates as False. ``default``
27 is None if not provided.
28 """
29 if not hasattr(stream, 'encoding') or not stream.encoding:
30 return default
31 else:
32 return stream.encoding
34# Less conservative replacement for sys.getdefaultencoding, that will try
35# to match the environment.
36# Defined here as central function, so if we find better choices, we
37# won't need to make changes all over IPython.
38def getdefaultencoding(prefer_stream=True):
39 """Return IPython's guess for the default encoding for bytes as text.
41 If prefer_stream is True (default), asks for stdin.encoding first,
42 to match the calling Terminal, but that is often None for subprocesses.
44 Then fall back on locale.getpreferredencoding(),
45 which should be a sensible platform default (that respects LANG environment),
46 and finally to sys.getdefaultencoding() which is the most conservative option,
47 and usually UTF8 as of Python 3.
48 """
49 enc = None
50 if prefer_stream:
51 enc = get_stream_enc(sys.stdin)
52 if not enc or enc=='ascii':
53 try:
54 # There are reports of getpreferredencoding raising errors
55 # in some cases, which may well be fixed, but let's be conservative here.
56 enc = locale.getpreferredencoding()
57 except Exception:
58 pass
59 enc = enc or sys.getdefaultencoding()
60 # On windows `cp0` can be returned to indicate that there is no code page.
61 # Since cp0 is an invalid encoding return instead cp1252 which is the
62 # Western European default.
63 if enc == 'cp0':
64 warnings.warn(
65 "Invalid code page cp0 detected - using cp1252 instead."
66 "If cp1252 is incorrect please ensure a valid code page "
67 "is defined for the process.", RuntimeWarning)
68 return 'cp1252'
69 return enc
71DEFAULT_ENCODING = getdefaultencoding()