From 22eb689cf3de7972a2789db3ad01a86949508ab7 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Wed, 26 Jun 2019 00:51:05 +0200 Subject: 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. --- Lib/test/test_io.py | 49 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) (limited to 'Lib/test/test_io.py') diff --git a/Lib/test/test_io.py b/Lib/test/test_io.py index fc474c9905..1fe1cba516 100644 --- a/Lib/test/test_io.py +++ b/Lib/test/test_io.py @@ -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 -- cgit v1.2.1