33Utilities for dealing with text encodings
44"""
55
6- #-----------------------------------------------------------------------------
6+ # -----------------------------------------------------------------------------
77# Copyright (C) 2008-2012 The IPython Development Team
88#
99# Distributed under the terms of the BSD License. The full license is in
1010# the file COPYING, distributed as part of this software.
11- #-----------------------------------------------------------------------------
11+ # -----------------------------------------------------------------------------
1212
13- #-----------------------------------------------------------------------------
13+ # -----------------------------------------------------------------------------
1414# Imports
15- #-----------------------------------------------------------------------------
15+ # -----------------------------------------------------------------------------
1616import sys
1717import locale
1818import warnings
1919
20+
2021# to deal with the possibility of sys.std* not being a stream at all
2122def get_stream_enc (stream , default = None ):
2223 """Return the given stream's encoding or a default.
@@ -26,16 +27,20 @@ def get_stream_enc(stream, default=None):
2627 a default if it doesn't exist or evaluates as False. ``default``
2728 is None if not provided.
2829 """
29- if not hasattr (stream , ' encoding' ) or not stream .encoding :
30+ if not hasattr (stream , " encoding" ) or not stream .encoding :
3031 return default
3132 else :
3233 return stream .encoding
3334
35+
36+ _sentinel = object ()
37+
38+
3439# Less conservative replacement for sys.getdefaultencoding, that will try
3540# to match the environment.
3641# Defined here as central function, so if we find better choices, we
3742# won't need to make changes all over IPython.
38- def getdefaultencoding (prefer_stream = True ):
43+ def getdefaultencoding (prefer_stream = _sentinel ):
3944 """Return IPython's guess for the default encoding for bytes as text.
4045
4146 If prefer_stream is True (default), asks for stdin.encoding first,
@@ -46,10 +51,20 @@ def getdefaultencoding(prefer_stream=True):
4651 and finally to sys.getdefaultencoding() which is the most conservative option,
4752 and usually UTF8 as of Python 3.
4853 """
54+ if prefer_stream is not _sentinel :
55+ warnings .warn (
56+ "getpreferredencoding(prefer_stream=) argument is deprecated since "
57+ "IPython 9.0, getdefaultencoding() will take no argument in the "
58+ "future. If you rely on `prefer_stream`, please open an issue on "
59+ "the IPython repo." ,
60+ DeprecationWarning ,
61+ stacklevel = 2 ,
62+ )
63+ prefer_stream = True
4964 enc = None
5065 if prefer_stream :
5166 enc = get_stream_enc (sys .stdin )
52- if not enc or enc == ' ascii' :
67+ if not enc or enc == " ascii" :
5368 try :
5469 # There are reports of getpreferredencoding raising errors
5570 # in some cases, which may well be fixed, but let's be conservative here.
@@ -60,12 +75,15 @@ def getdefaultencoding(prefer_stream=True):
6075 # On windows `cp0` can be returned to indicate that there is no code page.
6176 # Since cp0 is an invalid encoding return instead cp1252 which is the
6277 # Western European default.
63- if enc == ' cp0' :
78+ if enc == " cp0" :
6479 warnings .warn (
6580 "Invalid code page cp0 detected - using cp1252 instead."
6681 "If cp1252 is incorrect please ensure a valid code page "
67- "is defined for the process." , RuntimeWarning )
68- return 'cp1252'
82+ "is defined for the process." ,
83+ RuntimeWarning ,
84+ )
85+ return "cp1252"
6986 return enc
7087
88+
7189DEFAULT_ENCODING = getdefaultencoding ()
0 commit comments