mirror of
https://github.com/python/cpython.git
synced 2025-10-09 16:34:44 +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
|
@ -29,6 +29,7 @@ import random
|
|||
import signal
|
||||
import sys
|
||||
import sysconfig
|
||||
import textwrap
|
||||
import threading
|
||||
import time
|
||||
import unittest
|
||||
|
@ -37,7 +38,8 @@ import weakref
|
|||
from collections import deque, UserList
|
||||
from itertools import cycle, count
|
||||
from test import support
|
||||
from test.support.script_helper import assert_python_ok, run_python_until_end
|
||||
from test.support.script_helper import (
|
||||
assert_python_ok, assert_python_failure, run_python_until_end)
|
||||
from test.support import FakePath
|
||||
|
||||
import codecs
|
||||
|
@ -4130,6 +4132,51 @@ class MiscIOTest(unittest.TestCase):
|
|||
# there used to be a buffer overflow in the parser for rawmode
|
||||
self.assertRaises(ValueError, self.open, support.TESTFN, 'rwax+')
|
||||
|
||||
def test_check_encoding_errors(self):
|
||||
# bpo-37388: open() and TextIOWrapper must check encoding and errors
|
||||
# arguments in dev mode
|
||||
mod = self.io.__name__
|
||||
filename = __file__
|
||||
invalid = 'Boom, Shaka Laka, Boom!'
|
||||
code = textwrap.dedent(f'''
|
||||
import sys
|
||||
from {mod} import open, TextIOWrapper
|
||||
|
||||
try:
|
||||
open({filename!r}, encoding={invalid!r})
|
||||
except LookupError:
|
||||
pass
|
||||
else:
|
||||
sys.exit(21)
|
||||
|
||||
try:
|
||||
open({filename!r}, errors={invalid!r})
|
||||
except LookupError:
|
||||
pass
|
||||
else:
|
||||
sys.exit(22)
|
||||
|
||||
fp = open({filename!r}, "rb")
|
||||
with fp:
|
||||
try:
|
||||
TextIOWrapper(fp, encoding={invalid!r})
|
||||
except LookupError:
|
||||
pass
|
||||
else:
|
||||
sys.exit(23)
|
||||
|
||||
try:
|
||||
TextIOWrapper(fp, errors={invalid!r})
|
||||
except LookupError:
|
||||
pass
|
||||
else:
|
||||
sys.exit(24)
|
||||
|
||||
sys.exit(10)
|
||||
''')
|
||||
proc = assert_python_failure('-X', 'dev', '-c', code)
|
||||
self.assertEqual(proc.rc, 10, proc)
|
||||
|
||||
|
||||
class CMiscIOTest(MiscIOTest):
|
||||
io = io
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue