mirror of
				https://github.com/python/cpython.git
				synced 2025-10-31 05:31:20 +00:00 
			
		
		
		
	bpo-41152: IDLE: always use UTF-8 for standard IO streams (GH-21214)
(cherry picked from commit 2515a28230)
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
			
			
This commit is contained in:
		
							parent
							
								
									9a646aa82d
								
							
						
					
					
						commit
						01638ce51a
					
				
					 5 changed files with 10 additions and 52 deletions
				
			
		|  | @ -3,6 +3,9 @@ Released on 2020-10-05? | ||||||
| ====================================== | ====================================== | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|  | bpo-41152: The encoding of ``stdin``, ``stdout`` and ``stderr`` in IDLE | ||||||
|  | is now always UTF-8. | ||||||
|  | 
 | ||||||
| bpo-41144: Make Open Module open a special module such as os.path. | bpo-41144: Make Open Module open a special module such as os.path. | ||||||
| 
 | 
 | ||||||
| bpo-40723: Make test_idle pass when run after import. | bpo-40723: Make test_idle pass when run after import. | ||||||
|  |  | ||||||
|  | @ -58,11 +58,6 @@ def test_write(self): | ||||||
|         get = self.text.get |         get = self.text.get | ||||||
|         write = self.window.write |         write = self.window.write | ||||||
| 
 | 
 | ||||||
|         # Test bytes. |  | ||||||
|         b = b'Test bytes.' |  | ||||||
|         eq(write(b), len(b)) |  | ||||||
|         eq(get('1.0', '1.end'), b.decode()) |  | ||||||
| 
 |  | ||||||
|         # No new line - insert stays on same line. |         # No new line - insert stays on same line. | ||||||
|         delete('1.0', 'end') |         delete('1.0', 'end') | ||||||
|         test_text = 'test text' |         test_text = 'test text' | ||||||
|  |  | ||||||
|  | @ -13,52 +13,12 @@ | ||||||
| import idlelib | import idlelib | ||||||
| from idlelib.config import idleConf | from idlelib.config import idleConf | ||||||
| 
 | 
 | ||||||
| if idlelib.testing:  # Set True by test.test_idle to avoid setlocale. | encoding = 'utf-8' | ||||||
|     encoding = 'utf-8' | if sys.platform == 'win32': | ||||||
|     errors = 'surrogateescape' |     errors = 'surrogatepass' | ||||||
| else: | else: | ||||||
|     # Try setting the locale, so that we can find out |     errors = 'surrogateescape' | ||||||
|     # what encoding to use |  | ||||||
|     try: |  | ||||||
|         import locale |  | ||||||
|         locale.setlocale(locale.LC_CTYPE, "") |  | ||||||
|     except (ImportError, locale.Error): |  | ||||||
|         pass |  | ||||||
| 
 | 
 | ||||||
|     if sys.platform == 'win32': |  | ||||||
|         encoding = 'utf-8' |  | ||||||
|         errors = 'surrogateescape' |  | ||||||
|     else: |  | ||||||
|         try: |  | ||||||
|             # Different things can fail here: the locale module may not be |  | ||||||
|             # loaded, it may not offer nl_langinfo, or CODESET, or the |  | ||||||
|             # resulting codeset may be unknown to Python. We ignore all |  | ||||||
|             # these problems, falling back to ASCII |  | ||||||
|             locale_encoding = locale.nl_langinfo(locale.CODESET) |  | ||||||
|             if locale_encoding: |  | ||||||
|                 codecs.lookup(locale_encoding) |  | ||||||
|         except (NameError, AttributeError, LookupError): |  | ||||||
|             # Try getdefaultlocale: it parses environment variables, |  | ||||||
|             # which may give a clue. Unfortunately, getdefaultlocale has |  | ||||||
|             # bugs that can cause ValueError. |  | ||||||
|             try: |  | ||||||
|                 locale_encoding = locale.getdefaultlocale()[1] |  | ||||||
|                 if locale_encoding: |  | ||||||
|                     codecs.lookup(locale_encoding) |  | ||||||
|             except (ValueError, LookupError): |  | ||||||
|                 pass |  | ||||||
| 
 |  | ||||||
|         if locale_encoding: |  | ||||||
|             encoding = locale_encoding.lower() |  | ||||||
|             errors = 'strict' |  | ||||||
|         else: |  | ||||||
|             # POSIX locale or macOS |  | ||||||
|             encoding = 'ascii' |  | ||||||
|             errors = 'surrogateescape' |  | ||||||
|         # Encoding is used in multiple files; locale_encoding nowhere. |  | ||||||
|         # The only use of 'encoding' below is in _decode as initial value |  | ||||||
|         # of deprecated block asking user for encoding. |  | ||||||
|         # Perhaps use elsewhere should be reviewed. |  | ||||||
| 
 | 
 | ||||||
| coding_re = re.compile(r'^[ \t\f]*#.*?coding[:=][ \t]*([-\w.]+)', re.ASCII) | coding_re = re.compile(r'^[ \t\f]*#.*?coding[:=][ \t]*([-\w.]+)', re.ASCII) | ||||||
| blank_re = re.compile(r'^[ \t\f]*(?:[#\r\n]|$)', re.ASCII) | blank_re = re.compile(r'^[ \t\f]*(?:[#\r\n]|$)', re.ASCII) | ||||||
|  |  | ||||||
|  | @ -6,7 +6,6 @@ | ||||||
| from tkinter import messagebox | from tkinter import messagebox | ||||||
| 
 | 
 | ||||||
| from idlelib.editor import EditorWindow | from idlelib.editor import EditorWindow | ||||||
| from idlelib import iomenu |  | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| file_line_pats = [ | file_line_pats = [ | ||||||
|  | @ -110,8 +109,7 @@ def write(self, s, tags=(), mark="insert"): | ||||||
|         Return: |         Return: | ||||||
|             Length of text inserted. |             Length of text inserted. | ||||||
|         """ |         """ | ||||||
|         if isinstance(s, bytes): |         assert isinstance(s, str) | ||||||
|             s = s.decode(iomenu.encoding, "replace") |  | ||||||
|         self.text.insert(mark, s, tags) |         self.text.insert(mark, s, tags) | ||||||
|         self.text.see(mark) |         self.text.see(mark) | ||||||
|         self.text.update() |         self.text.update() | ||||||
|  |  | ||||||
|  | @ -0,0 +1,2 @@ | ||||||
|  | The encoding of ``stdin``, ``stdout`` and ``stderr`` in IDLE is now always | ||||||
|  | UTF-8. | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Miss Islington (bot)
						Miss Islington (bot)