Skip to content

Commit 01638ce

Browse files
bpo-41152: IDLE: always use UTF-8 for standard IO streams (GH-21214)
(cherry picked from commit 2515a28) Co-authored-by: Serhiy Storchaka <[email protected]>
1 parent 9a646aa commit 01638ce

File tree

5 files changed

+10
-52
lines changed

5 files changed

+10
-52
lines changed

Lib/idlelib/NEWS.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ Released on 2020-10-05?
33
======================================
44

55

6+
bpo-41152: The encoding of ``stdin``, ``stdout`` and ``stderr`` in IDLE
7+
is now always UTF-8.
8+
69
bpo-41144: Make Open Module open a special module such as os.path.
710

811
bpo-40723: Make test_idle pass when run after import.

Lib/idlelib/idle_test/test_outwin.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,6 @@ def test_write(self):
5858
get = self.text.get
5959
write = self.window.write
6060

61-
# Test bytes.
62-
b = b'Test bytes.'
63-
eq(write(b), len(b))
64-
eq(get('1.0', '1.end'), b.decode())
65-
6661
# No new line - insert stays on same line.
6762
delete('1.0', 'end')
6863
test_text = 'test text'

Lib/idlelib/iomenu.py

Lines changed: 4 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -13,52 +13,12 @@
1313
import idlelib
1414
from idlelib.config import idleConf
1515

16-
if idlelib.testing: # Set True by test.test_idle to avoid setlocale.
17-
encoding = 'utf-8'
18-
errors = 'surrogateescape'
16+
encoding = 'utf-8'
17+
if sys.platform == 'win32':
18+
errors = 'surrogatepass'
1919
else:
20-
# Try setting the locale, so that we can find out
21-
# what encoding to use
22-
try:
23-
import locale
24-
locale.setlocale(locale.LC_CTYPE, "")
25-
except (ImportError, locale.Error):
26-
pass
27-
28-
if sys.platform == 'win32':
29-
encoding = 'utf-8'
30-
errors = 'surrogateescape'
31-
else:
32-
try:
33-
# Different things can fail here: the locale module may not be
34-
# loaded, it may not offer nl_langinfo, or CODESET, or the
35-
# resulting codeset may be unknown to Python. We ignore all
36-
# these problems, falling back to ASCII
37-
locale_encoding = locale.nl_langinfo(locale.CODESET)
38-
if locale_encoding:
39-
codecs.lookup(locale_encoding)
40-
except (NameError, AttributeError, LookupError):
41-
# Try getdefaultlocale: it parses environment variables,
42-
# which may give a clue. Unfortunately, getdefaultlocale has
43-
# bugs that can cause ValueError.
44-
try:
45-
locale_encoding = locale.getdefaultlocale()[1]
46-
if locale_encoding:
47-
codecs.lookup(locale_encoding)
48-
except (ValueError, LookupError):
49-
pass
20+
errors = 'surrogateescape'
5021

51-
if locale_encoding:
52-
encoding = locale_encoding.lower()
53-
errors = 'strict'
54-
else:
55-
# POSIX locale or macOS
56-
encoding = 'ascii'
57-
errors = 'surrogateescape'
58-
# Encoding is used in multiple files; locale_encoding nowhere.
59-
# The only use of 'encoding' below is in _decode as initial value
60-
# of deprecated block asking user for encoding.
61-
# Perhaps use elsewhere should be reviewed.
6222

6323
coding_re = re.compile(r'^[ \t\f]*#.*?coding[:=][ \t]*([-\w.]+)', re.ASCII)
6424
blank_re = re.compile(r'^[ \t\f]*(?:[#\r\n]|$)', re.ASCII)

Lib/idlelib/outwin.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
from tkinter import messagebox
77

88
from idlelib.editor import EditorWindow
9-
from idlelib import iomenu
109

1110

1211
file_line_pats = [
@@ -110,8 +109,7 @@ def write(self, s, tags=(), mark="insert"):
110109
Return:
111110
Length of text inserted.
112111
"""
113-
if isinstance(s, bytes):
114-
s = s.decode(iomenu.encoding, "replace")
112+
assert isinstance(s, str)
115113
self.text.insert(mark, s, tags)
116114
self.text.see(mark)
117115
self.text.update()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
The encoding of ``stdin``, ``stdout`` and ``stderr`` in IDLE is now always
2+
UTF-8.

0 commit comments

Comments
 (0)