Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7c15341

Browse files
committed
Issue 27372: Stop test_idle from changing locale, so test passes.
In 3.6, the warning is now called an error, making it harder to ignore.
1 parent b554fad commit 7c15341

4 files changed

Lines changed: 62 additions & 48 deletions

File tree

Lib/idlelib/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@
77
The other files are private implementations. Their details are subject to
88
change. See PEP 434 for more. Import them at your own risk.
99
"""
10+
testing = False # Set True by test.test_idle.

Lib/idlelib/editor.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@
2626

2727
# The default tab setting for a Text widget, in average-width characters.
2828
TK_TABWIDTH_DEFAULT = 8
29-
3029
_py_version = ' (%s)' % platform.python_version()
3130

31+
3232
def _sphinx_version():
3333
"Format sys.version_info to produce the Sphinx version string used to install the chm docs"
3434
major, minor, micro, level, serial = sys.version_info
@@ -45,11 +45,12 @@ class EditorWindow(object):
4545
from idlelib.percolator import Percolator
4646
from idlelib.colorizer import ColorDelegator, color_config
4747
from idlelib.undo import UndoDelegator
48-
from idlelib.iomenu import IOBinding, filesystemencoding, encoding
48+
from idlelib.iomenu import IOBinding, encoding
4949
from idlelib import mainmenu
5050
from tkinter import Toplevel
5151
from idlelib.statusbar import MultiStatusBar
5252

53+
filesystemencoding = sys.getfilesystemencoding() # for file names
5354
help_url = None
5455

5556
def __init__(self, flist=None, filename=None, key=None, root=None):
@@ -1649,5 +1650,8 @@ def _editor_window(parent): # htest #
16491650
# edit.text.bind("<<close-window>>", edit.close_event)
16501651

16511652
if __name__ == '__main__':
1653+
import unittest
1654+
unittest.main('idlelib.idle_test.test_editor', verbosity=2, exit=False)
1655+
16521656
from idlelib.idle_test.htest import run
16531657
run(_editor_window)

Lib/idlelib/iomenu.py

Lines changed: 46 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -10,57 +10,60 @@
1010
import tkinter.messagebox as tkMessageBox
1111
from tkinter.simpledialog import askstring
1212

13+
import idlelib
1314
from idlelib.config import idleConf
1415

15-
16-
# Try setting the locale, so that we can find out
17-
# what encoding to use
18-
try:
19-
import locale
20-
locale.setlocale(locale.LC_CTYPE, "")
21-
except (ImportError, locale.Error):
22-
pass
23-
24-
# Encoding for file names
25-
filesystemencoding = sys.getfilesystemencoding() ### currently unused
26-
27-
locale_encoding = 'ascii'
28-
if sys.platform == 'win32':
29-
# On Windows, we could use "mbcs". However, to give the user
30-
# a portable encoding name, we need to find the code page
31-
try:
32-
locale_encoding = locale.getdefaultlocale()[1]
33-
codecs.lookup(locale_encoding)
34-
except LookupError:
35-
pass
16+
if idlelib.testing: # Set True by test.test_idle to avoid setlocale.
17+
encoding = 'utf-8'
3618
else:
19+
# Try setting the locale, so that we can find out
20+
# what encoding to use
3721
try:
38-
# Different things can fail here: the locale module may not be
39-
# loaded, it may not offer nl_langinfo, or CODESET, or the
40-
# resulting codeset may be unknown to Python. We ignore all
41-
# these problems, falling back to ASCII
42-
locale_encoding = locale.nl_langinfo(locale.CODESET)
43-
if locale_encoding is None or locale_encoding is '':
44-
# situation occurs on Mac OS X
45-
locale_encoding = 'ascii'
46-
codecs.lookup(locale_encoding)
47-
except (NameError, AttributeError, LookupError):
48-
# Try getdefaultlocale: it parses environment variables,
49-
# which may give a clue. Unfortunately, getdefaultlocale has
50-
# bugs that can cause ValueError.
22+
import locale
23+
locale.setlocale(locale.LC_CTYPE, "")
24+
except (ImportError, locale.Error):
25+
pass
26+
27+
locale_decode = 'ascii'
28+
if sys.platform == 'win32':
29+
# On Windows, we could use "mbcs". However, to give the user
30+
# a portable encoding name, we need to find the code page
5131
try:
5232
locale_encoding = locale.getdefaultlocale()[1]
33+
codecs.lookup(locale_encoding)
34+
except LookupError:
35+
pass
36+
else:
37+
try:
38+
# Different things can fail here: the locale module may not be
39+
# loaded, it may not offer nl_langinfo, or CODESET, or the
40+
# resulting codeset may be unknown to Python. We ignore all
41+
# these problems, falling back to ASCII
42+
locale_encoding = locale.nl_langinfo(locale.CODESET)
5343
if locale_encoding is None or locale_encoding is '':
5444
# situation occurs on Mac OS X
5545
locale_encoding = 'ascii'
5646
codecs.lookup(locale_encoding)
57-
except (ValueError, LookupError):
58-
pass
47+
except (NameError, AttributeError, LookupError):
48+
# Try getdefaultlocale: it parses environment variables,
49+
# which may give a clue. Unfortunately, getdefaultlocale has
50+
# bugs that can cause ValueError.
51+
try:
52+
locale_encoding = locale.getdefaultlocale()[1]
53+
if locale_encoding is None or locale_encoding is '':
54+
# situation occurs on Mac OS X
55+
locale_encoding = 'ascii'
56+
codecs.lookup(locale_encoding)
57+
except (ValueError, LookupError):
58+
pass
5959

60-
locale_encoding = locale_encoding.lower()
60+
locale_encoding = locale_encoding.lower()
6161

62-
encoding = locale_encoding ### KBK 07Sep07 This is used all over IDLE, check!
63-
### 'encoding' is used below in encode(), check!
62+
encoding = locale_encoding
63+
# Encoding is used in multiple files; locale_encoding nowhere.
64+
# The only use of 'encoding' below is in _decode as initial value
65+
# of deprecated block asking user for encoding.
66+
# Perhaps use elsewhere should be reviewed.
6467

6568
coding_re = re.compile(r'^[ \t\f]*#.*?coding[:=][ \t]*([-\w.]+)', re.ASCII)
6669
blank_re = re.compile(r'^[ \t\f]*(?:[#\r\n]|$)', re.ASCII)
@@ -304,7 +307,7 @@ def _decode(self, two_lines, bytes):
304307
"The file's encoding is invalid for Python 3.x.\n"
305308
"IDLE will convert it to UTF-8.\n"
306309
"What is the current encoding of the file?",
307-
initialvalue = locale_encoding,
310+
initialvalue = encoding,
308311
parent = self.editwin.text)
309312

310313
if enc:
@@ -564,5 +567,8 @@ def savecopy(self, event):
564567
IOBinding(editwin)
565568

566569
if __name__ == "__main__":
570+
import unittest
571+
unittest.main('idlelib.idle_test.test_iomenu', verbosity=2, exit=False)
572+
567573
from idlelib.idle_test.htest import run
568574
run(_io_binding)

Lib/test/test_idle.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
import unittest
22
from test.support import import_module
33

4-
# Skip test if _thread or _tkinter wasn't built or idlelib was deleted.
4+
# Skip test if _thread or _tkinter wasn't built, or idlelib is missing,
5+
# or if tcl/tk version before 8.5, which is needed for ttk widgets.
6+
57
import_module('threading') # imported by PyShell, imports _thread
68
tk = import_module('tkinter') # imports _tkinter
79
if tk.TkVersion < 8.5:
810
raise unittest.SkipTest("IDLE requires tk 8.5 or later.")
911
tk.NoDefaultRoot()
10-
idletest = import_module('idlelib.idle_test')
12+
idlelib = import_module('idlelib')
13+
idlelib.testing = True # Avoid locale-changed test error
1114

12-
# Without test_main present, regrtest.runtest_inner (line1219) calls
13-
# unittest.TestLoader().loadTestsFromModule(this_module) which calls
14-
# load_tests() if it finds it. (Unittest.main does the same.)
15-
load_tests = idletest.load_tests
15+
# Without test_main present, test.libregrtest.runtest.runtest_inner
16+
# calls (line 173) unittest.TestLoader().loadTestsFromModule(module)
17+
# which calls load_tests() if it finds it. (Unittest.main does the same.)
18+
from idlelib.idle_test import load_tests
1619

1720
if __name__ == '__main__':
1821
unittest.main(verbosity=2, exit=False)

0 commit comments

Comments
 (0)