diff options
Diffstat (limited to 'Lib/test')
-rw-r--r-- | Lib/test/string_tests.py | 12 | ||||
-rw-r--r-- | Lib/test/test_calendar.py | 4 | ||||
-rw-r--r-- | Lib/test/test_codecs.py | 48 | ||||
-rw-r--r-- | Lib/test/test_fileinput.py | 7 | ||||
-rw-r--r-- | Lib/test/test_io.py | 43 |
5 files changed, 97 insertions, 17 deletions
diff --git a/Lib/test/string_tests.py b/Lib/test/string_tests.py index 6d87eb6957..b2f837bab6 100644 --- a/Lib/test/string_tests.py +++ b/Lib/test/string_tests.py @@ -1295,8 +1295,10 @@ class MixinStrUserStringTest: ('hex', '68656c6c6f20776f726c64'), ('uu', 'begin 666 <data>\n+:&5L;&\\@=V]R;&0 \n \nend\n')] for encoding, data in codecs: - self.checkequal(data, 'hello world', 'encode', encoding) - self.checkequal('hello world', data, 'decode', encoding) + with test_support.check_py3k_warnings(): + self.checkequal(data, 'hello world', 'encode', encoding) + with test_support.check_py3k_warnings(): + self.checkequal('hello world', data, 'decode', encoding) # zlib is optional, so we make the test optional too... try: import zlib @@ -1304,8 +1306,10 @@ class MixinStrUserStringTest: pass else: data = 'x\x9c\xcbH\xcd\xc9\xc9W(\xcf/\xcaI\x01\x00\x1a\x0b\x04]' - self.checkequal(data, 'hello world', 'encode', 'zlib') - self.checkequal('hello world', data, 'decode', 'zlib') + with test_support.check_py3k_warnings(): + self.checkequal(data, 'hello world', 'encode', 'zlib') + with test_support.check_py3k_warnings(): + self.checkequal('hello world', data, 'decode', 'zlib') self.checkraises(TypeError, 'xyz', 'decode', 42) self.checkraises(TypeError, 'xyz', 'encode', 42) diff --git a/Lib/test/test_calendar.py b/Lib/test/test_calendar.py index 5692642db1..46c4a6fe3b 100644 --- a/Lib/test/test_calendar.py +++ b/Lib/test/test_calendar.py @@ -513,8 +513,8 @@ class CommandLineTestCase(unittest.TestCase): def test_option_encoding(self): self.assertFailure('-e') self.assertFailure('--encoding') - stdout = self.run_ok('--encoding', 'rot-13', '2004') - self.assertEqual(stdout.strip(), conv(result_2004_text.encode('rot-13')).strip()) + stdout = self.run_ok('--encoding', 'utf-16-le', '2004') + self.assertEqual(stdout.strip(), conv(result_2004_text.encode('utf-16-le')).strip()) def test_option_locale(self): self.assertFailure('-L') diff --git a/Lib/test/test_codecs.py b/Lib/test/test_codecs.py index de80b0776c..c7072a65be 100644 --- a/Lib/test/test_codecs.py +++ b/Lib/test/test_codecs.py @@ -1395,14 +1395,14 @@ class EncodedFileTest(unittest.TestCase): class Str2StrTest(unittest.TestCase): def test_read(self): - sin = "\x80".encode("base64_codec") + sin = codecs.encode("\x80", "base64_codec") reader = codecs.getreader("base64_codec")(StringIO.StringIO(sin)) sout = reader.read() self.assertEqual(sout, "\x80") self.assertIsInstance(sout, str) def test_readline(self): - sin = "\x80".encode("base64_codec") + sin = codecs.encode("\x80", "base64_codec") reader = codecs.getreader("base64_codec")(StringIO.StringIO(sin)) sout = reader.readline() self.assertEqual(sout, "\x80") @@ -1536,6 +1536,9 @@ broken_unicode_with_streams = [ ] broken_incremental_coders = broken_unicode_with_streams[:] +if sys.flags.py3k_warning: + broken_unicode_with_streams.append("rot_13") + # The following encodings only support "strict" mode only_strict_mode = [ "idna", @@ -2135,6 +2138,47 @@ def test_main(): # Missing "begin" line self.assertRaises(ValueError, codecs.decode, "", "uu-codec") + def test_text_to_binary_blacklists_binary_transforms(self): + # Check binary -> binary codecs give a good error for str input + bad_input = "bad input type" + for encoding in bytes_transform_encodings: + fmt = (r"{!r} is not a text encoding; " + r"use codecs.encode\(\) to handle arbitrary codecs") + msg = fmt.format(encoding) + with self.assertRaisesRegex(LookupError, msg) as failure: + bad_input.encode(encoding) + self.assertIsNone(failure.exception.__cause__) + + def test_text_to_binary_blacklists_text_transforms(self): + # Check str.encode gives a good error message for str -> str codecs + msg = (r"^'rot_13' is not a text encoding; " + r"use codecs.encode\(\) to handle arbitrary codecs") + with self.assertRaisesRegex(LookupError, msg): + "just an example message".encode("rot_13") + + def test_binary_to_text_blacklists_binary_transforms(self): + # Check bytes.decode and bytearray.decode give a good error + # message for binary -> binary codecs + data = b"encode first to ensure we meet any format restrictions" + for encoding in bytes_transform_encodings: + encoded_data = codecs.encode(data, encoding) + fmt = (r"{!r} is not a text encoding; " + r"use codecs.decode\(\) to handle arbitrary codecs") + msg = fmt.format(encoding) + with self.assertRaisesRegex(LookupError, msg): + encoded_data.decode(encoding) + with self.assertRaisesRegex(LookupError, msg): + bytearray(encoded_data).decode(encoding) + + def test_binary_to_text_blacklists_text_transforms(self): + # Check str -> str codec gives a good error for binary input + for bad_input in (b"immutable", bytearray(b"mutable")): + msg = (r"^'rot_13' is not a text encoding; " + r"use codecs.decode\(\) to handle arbitrary codecs") + with self.assertRaisesRegex(LookupError, msg) as failure: + bad_input.decode("rot_13") + self.assertIsNone(failure.exception.__cause__) + if __name__ == "__main__": test_main() diff --git a/Lib/test/test_fileinput.py b/Lib/test/test_fileinput.py index c15ad847bb..facc56e24f 100644 --- a/Lib/test/test_fileinput.py +++ b/Lib/test/test_fileinput.py @@ -211,10 +211,11 @@ class FileInputTests(unittest.TestCase): except ValueError: pass try: - t1 = writeTmp(1, ["A\nB"], mode="wb") - fi = FileInput(files=t1, openhook=hook_encoded("rot13")) + # UTF-7 is a convenient, seldom used encoding + t1 = writeTmp(1, ['+AEE-\n+AEI-'], mode="wb") + fi = FileInput(files=t1, openhook=hook_encoded("utf-7")) lines = list(fi) - self.assertEqual(lines, ["N\n", "O"]) + self.assertEqual(lines, [u'A\n', u'B']) finally: remove_tempfiles(t1) diff --git a/Lib/test/test_io.py b/Lib/test/test_io.py index bbc804b6a5..1a17d814ee 100644 --- a/Lib/test/test_io.py +++ b/Lib/test/test_io.py @@ -2001,6 +2001,15 @@ class TextIOWrapperTest(unittest.TestCase): t.__init__(self.MockRawIO()) self.assertEqual(t.read(0), u'') + def test_non_text_encoding_codecs_are_rejected(self): + # Ensure the constructor complains if passed a codec that isn't + # marked as a text encoding + # http://bugs.python.org/issue20404 + r = self.BytesIO() + b = self.BufferedWriter(r) + with support.check_py3k_warnings(): + self.TextIOWrapper(b, encoding="hex_codec") + def test_detach(self): r = self.BytesIO() b = self.BufferedWriter(r) @@ -2617,19 +2626,39 @@ class TextIOWrapperTest(unittest.TestCase): def test_illegal_decoder(self): # Issue #17106 + # Bypass the early encoding check added in issue 20404 + def _make_illegal_wrapper(): + quopri = codecs.lookup("quopri_codec") + quopri._is_text_encoding = True + try: + t = self.TextIOWrapper(self.BytesIO(b'aaaaaa'), + newline='\n', encoding="quopri_codec") + finally: + quopri._is_text_encoding = False + return t # Crash when decoder returns non-string - t = self.TextIOWrapper(self.BytesIO(b'aaaaaa'), newline='\n', - encoding='quopri_codec') + with support.check_py3k_warnings(): + t = self.TextIOWrapper(self.BytesIO(b'aaaaaa'), newline='\n', + encoding='quopri_codec') with self.maybeRaises(TypeError): t.read(1) - t = self.TextIOWrapper(self.BytesIO(b'aaaaaa'), newline='\n', - encoding='quopri_codec') + with support.check_py3k_warnings(): + t = self.TextIOWrapper(self.BytesIO(b'aaaaaa'), newline='\n', + encoding='quopri_codec') with self.maybeRaises(TypeError): t.readline() - t = self.TextIOWrapper(self.BytesIO(b'aaaaaa'), newline='\n', - encoding='quopri_codec') + with support.check_py3k_warnings(): + t = self.TextIOWrapper(self.BytesIO(b'aaaaaa'), newline='\n', + encoding='quopri_codec') with self.maybeRaises(TypeError): t.read() + #else: + #t = _make_illegal_wrapper() + #self.assertRaises(TypeError, t.read, 1) + #t = _make_illegal_wrapper() + #self.assertRaises(TypeError, t.readline) + #t = _make_illegal_wrapper() + #self.assertRaises(TypeError, t.read) class CTextIOWrapperTest(TextIOWrapperTest): @@ -3002,9 +3031,11 @@ class MiscIOTest(unittest.TestCase): class CMiscIOTest(MiscIOTest): io = io + shutdown_error = "RuntimeError: could not find io module state" class PyMiscIOTest(MiscIOTest): io = pyio + shutdown_error = "LookupError: unknown encoding: ascii" @unittest.skipIf(os.name == 'nt', 'POSIX signals required for this test.') |