mirror of
				https://github.com/python/cpython.git
				synced 2025-10-31 13:41:24 +00:00 
			
		
		
		
	bpo-37388: Development mode check encoding and errors (GH-14341)
In development mode and in debug build, encoding and errors arguments are now checked on string encoding and decoding operations. Examples: open(), str.encode() and bytes.decode(). By default, for best performances, the errors argument is only checked at the first encoding/decoding error, and the encoding argument is sometimes ignored for empty strings.
This commit is contained in:
		
							parent
							
								
									e1a63c4f21
								
							
						
					
					
						commit
						22eb689cf3
					
				
					 10 changed files with 315 additions and 6 deletions
				
			
		|  | @ -12,12 +12,14 @@ | |||
| import functools | ||||
| import pickle | ||||
| import tempfile | ||||
| import textwrap | ||||
| import unittest | ||||
| 
 | ||||
| import test.support | ||||
| import test.string_tests | ||||
| import test.list_tests | ||||
| from test.support import bigaddrspacetest, MAX_Py_ssize_t | ||||
| from test.support.script_helper import assert_python_failure | ||||
| 
 | ||||
| 
 | ||||
| if sys.flags.bytes_warning: | ||||
|  | @ -315,6 +317,62 @@ def test_decode(self): | |||
|         # Default encoding is utf-8 | ||||
|         self.assertEqual(self.type2test(b'\xe2\x98\x83').decode(), '\u2603') | ||||
| 
 | ||||
|     def test_check_encoding_errors(self): | ||||
|         # bpo-37388: bytes(str) and bytes.encode() must check encoding | ||||
|         # and errors arguments in dev mode | ||||
|         invalid = 'Boom, Shaka Laka, Boom!' | ||||
|         encodings = ('ascii', 'utf8', 'latin1') | ||||
|         code = textwrap.dedent(f''' | ||||
|             import sys | ||||
|             type2test = {self.type2test.__name__} | ||||
|             encodings = {encodings!r} | ||||
| 
 | ||||
|             for data in ('', 'short string'): | ||||
|                 try: | ||||
|                     type2test(data, encoding={invalid!r}) | ||||
|                 except LookupError: | ||||
|                     pass | ||||
|                 else: | ||||
|                     sys.exit(21) | ||||
| 
 | ||||
|                 for encoding in encodings: | ||||
|                     try: | ||||
|                         type2test(data, encoding=encoding, errors={invalid!r}) | ||||
|                     except LookupError: | ||||
|                         pass | ||||
|                     else: | ||||
|                         sys.exit(22) | ||||
| 
 | ||||
|             for data in (b'', b'short string'): | ||||
|                 data = type2test(data) | ||||
|                 print(repr(data)) | ||||
|                 try: | ||||
|                     data.decode(encoding={invalid!r}) | ||||
|                 except LookupError: | ||||
|                     sys.exit(10) | ||||
|                 else: | ||||
|                     sys.exit(23) | ||||
| 
 | ||||
|                 try: | ||||
|                     data.decode(errors={invalid!r}) | ||||
|                 except LookupError: | ||||
|                     pass | ||||
|                 else: | ||||
|                     sys.exit(24) | ||||
| 
 | ||||
|                 for encoding in encodings: | ||||
|                     try: | ||||
|                         data.decode(encoding=encoding, errors={invalid!r}) | ||||
|                     except LookupError: | ||||
|                         pass | ||||
|                     else: | ||||
|                         sys.exit(25) | ||||
| 
 | ||||
|             sys.exit(10) | ||||
|         ''') | ||||
|         proc = assert_python_failure('-X', 'dev', '-c', code) | ||||
|         self.assertEqual(proc.rc, 10, proc) | ||||
| 
 | ||||
|     def test_from_int(self): | ||||
|         b = self.type2test(0) | ||||
|         self.assertEqual(b, self.type2test()) | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Victor Stinner
						Victor Stinner