|
10 | 10 | import tkinter.messagebox as tkMessageBox |
11 | 11 | from tkinter.simpledialog import askstring |
12 | 12 |
|
| 13 | +import idlelib |
13 | 14 | from idlelib.config import idleConf |
14 | 15 |
|
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' |
36 | 18 | else: |
| 19 | + # Try setting the locale, so that we can find out |
| 20 | + # what encoding to use |
37 | 21 | 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 |
51 | 31 | try: |
52 | 32 | 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) |
53 | 43 | if locale_encoding is None or locale_encoding is '': |
54 | 44 | # situation occurs on Mac OS X |
55 | 45 | locale_encoding = 'ascii' |
56 | 46 | 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 |
59 | 59 |
|
60 | | -locale_encoding = locale_encoding.lower() |
| 60 | + locale_encoding = locale_encoding.lower() |
61 | 61 |
|
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. |
64 | 67 |
|
65 | 68 | coding_re = re.compile(r'^[ \t\f]*#.*?coding[:=][ \t]*([-\w.]+)', re.ASCII) |
66 | 69 | blank_re = re.compile(r'^[ \t\f]*(?:[#\r\n]|$)', re.ASCII) |
@@ -304,7 +307,7 @@ def _decode(self, two_lines, bytes): |
304 | 307 | "The file's encoding is invalid for Python 3.x.\n" |
305 | 308 | "IDLE will convert it to UTF-8.\n" |
306 | 309 | "What is the current encoding of the file?", |
307 | | - initialvalue = locale_encoding, |
| 310 | + initialvalue = encoding, |
308 | 311 | parent = self.editwin.text) |
309 | 312 |
|
310 | 313 | if enc: |
@@ -564,5 +567,8 @@ def savecopy(self, event): |
564 | 567 | IOBinding(editwin) |
565 | 568 |
|
566 | 569 | if __name__ == "__main__": |
| 570 | + import unittest |
| 571 | + unittest.main('idlelib.idle_test.test_iomenu', verbosity=2, exit=False) |
| 572 | + |
567 | 573 | from idlelib.idle_test.htest import run |
568 | 574 | run(_io_binding) |
0 commit comments