diff options
author | Nick Coghlan <ncoghlan@gmail.com> | 2015-01-06 14:37:01 (GMT) |
---|---|---|
committer | Nick Coghlan <ncoghlan@gmail.com> | 2015-01-06 14:37:01 (GMT) |
commit | 582acb75e9a03e21281fe3c3877b6d48a8f0fa8f (patch) | |
tree | c8e497645e1ce8c0a6384a1063cac52d09dd02fe /Lib/test/test_codecs.py | |
parent | 5d575399bca935fec36d6a1ba538115406673415 (diff) | |
parent | b9fdb7a452c2b6f7a628118b5f695bd061b62cc8 (diff) | |
download | cpython-582acb75e9a03e21281fe3c3877b6d48a8f0fa8f.zip cpython-582acb75e9a03e21281fe3c3877b6d48a8f0fa8f.tar.gz cpython-582acb75e9a03e21281fe3c3877b6d48a8f0fa8f.tar.bz2 |
Merge issue 19548 changes from 3.4
Diffstat (limited to 'Lib/test/test_codecs.py')
-rw-r--r-- | Lib/test/test_codecs.py | 46 |
1 files changed, 37 insertions, 9 deletions
diff --git a/Lib/test/test_codecs.py b/Lib/test/test_codecs.py index 4865ea1..7fed1f7 100644 --- a/Lib/test/test_codecs.py +++ b/Lib/test/test_codecs.py @@ -1140,6 +1140,8 @@ class RecodingTest(unittest.TestCase): # Python used to crash on this at exit because of a refcount # bug in _codecsmodule.c + self.assertTrue(f.closed) + # From RFC 3492 punycode_testcases = [ # A Arabic (Egyptian): @@ -1592,6 +1594,16 @@ class IDNACodecTest(unittest.TestCase): self.assertEqual(encoder.encode("ample.org."), b"xn--xample-9ta.org.") self.assertEqual(encoder.encode("", True), b"") + def test_errors(self): + """Only supports "strict" error handler""" + "python.org".encode("idna", "strict") + b"python.org".decode("idna", "strict") + for errors in ("ignore", "replace", "backslashreplace", + "surrogateescape"): + self.assertRaises(Exception, "python.org".encode, "idna", errors) + self.assertRaises(Exception, + b"python.org".decode, "idna", errors) + class CodecsModuleTest(unittest.TestCase): def test_decode(self): @@ -1682,6 +1694,24 @@ class CodecsModuleTest(unittest.TestCase): for api in codecs.__all__: getattr(codecs, api) + def test_open(self): + self.addCleanup(support.unlink, support.TESTFN) + for mode in ('w', 'r', 'r+', 'w+', 'a', 'a+'): + with self.subTest(mode), \ + codecs.open(support.TESTFN, mode, 'ascii') as file: + self.assertIsInstance(file, codecs.StreamReaderWriter) + + def test_undefined(self): + self.assertRaises(UnicodeError, codecs.encode, 'abc', 'undefined') + self.assertRaises(UnicodeError, codecs.decode, b'abc', 'undefined') + self.assertRaises(UnicodeError, codecs.encode, '', 'undefined') + self.assertRaises(UnicodeError, codecs.decode, b'', 'undefined') + for errors in ('strict', 'ignore', 'replace', 'backslashreplace'): + self.assertRaises(UnicodeError, + codecs.encode, 'abc', 'undefined', errors) + self.assertRaises(UnicodeError, + codecs.decode, b'abc', 'undefined', errors) + class StreamReaderTest(unittest.TestCase): def setUp(self): @@ -1815,13 +1845,10 @@ if hasattr(codecs, "mbcs_encode"): # "undefined" # The following encodings don't work in stateful mode -broken_unicode_with_streams = [ +broken_unicode_with_stateful = [ "punycode", "unicode_internal" ] -broken_incremental_coders = broken_unicode_with_streams + [ - "idna", -] class BasicUnicodeTest(unittest.TestCase, MixInCheckStateHandling): def test_basics(self): @@ -1841,7 +1868,7 @@ class BasicUnicodeTest(unittest.TestCase, MixInCheckStateHandling): (chars, size) = codecs.getdecoder(encoding)(b) self.assertEqual(chars, s, "encoding=%r" % encoding) - if encoding not in broken_unicode_with_streams: + if encoding not in broken_unicode_with_stateful: # check stream reader/writer q = Queue(b"") writer = codecs.getwriter(encoding)(q) @@ -1859,7 +1886,7 @@ class BasicUnicodeTest(unittest.TestCase, MixInCheckStateHandling): decodedresult += reader.read() self.assertEqual(decodedresult, s, "encoding=%r" % encoding) - if encoding not in broken_incremental_coders: + if encoding not in broken_unicode_with_stateful: # check incremental decoder/encoder and iterencode()/iterdecode() try: encoder = codecs.getincrementalencoder(encoding)() @@ -1908,7 +1935,7 @@ class BasicUnicodeTest(unittest.TestCase, MixInCheckStateHandling): from _testcapi import codec_incrementalencoder, codec_incrementaldecoder s = "abc123" # all codecs should be able to encode these for encoding in all_unicode_encodings: - if encoding not in broken_incremental_coders: + if encoding not in broken_unicode_with_stateful: # check incremental decoder/encoder (fetched via the C API) try: cencoder = codec_incrementalencoder(encoding) @@ -1948,7 +1975,7 @@ class BasicUnicodeTest(unittest.TestCase, MixInCheckStateHandling): for encoding in all_unicode_encodings: if encoding == "idna": # FIXME: See SF bug #1163178 continue - if encoding in broken_unicode_with_streams: + if encoding in broken_unicode_with_stateful: continue reader = codecs.getreader(encoding)(io.BytesIO(s.encode(encoding))) for t in range(5): @@ -1981,7 +2008,7 @@ class BasicUnicodeTest(unittest.TestCase, MixInCheckStateHandling): # Check that getstate() and setstate() handle the state properly u = "abc123" for encoding in all_unicode_encodings: - if encoding not in broken_incremental_coders: + if encoding not in broken_unicode_with_stateful: self.check_state_handling_decode(encoding, u, u.encode(encoding)) self.check_state_handling_encode(encoding, u, u.encode(encoding)) @@ -2185,6 +2212,7 @@ class WithStmtTest(unittest.TestCase): f = io.BytesIO(b"\xc3\xbc") with codecs.EncodedFile(f, "latin-1", "utf-8") as ef: self.assertEqual(ef.read(), b"\xfc") + self.assertTrue(f.closed) def test_streamreaderwriter(self): f = io.BytesIO(b"\xc3\xbc") |