diff options
author | Walter Dörwald <walter@livinglogic.de> | 2007-07-30 13:31:40 (GMT) |
---|---|---|
committer | Walter Dörwald <walter@livinglogic.de> | 2007-07-30 13:31:40 (GMT) |
commit | e78178e2c05ec2bb628b70a8b5422bb4dae63343 (patch) | |
tree | d1c8662e41b7f8f95714bc903ad048a01673c369 /Lib | |
parent | 2dbde5ea444de788721e9195d644da38061204c3 (diff) | |
download | cpython-e78178e2c05ec2bb628b70a8b5422bb4dae63343.zip cpython-e78178e2c05ec2bb628b70a8b5422bb4dae63343.tar.gz cpython-e78178e2c05ec2bb628b70a8b5422bb4dae63343.tar.bz2 |
Bytes (which are the input for decoding) are mutable now. If a decoding
error callback changes the bytes object in the exception the decoder might
use memory that's no longer in use. Change unicode_decode_call_errorhandler()
so that it fetches the adresses of the bytes array (start and end) from the
exception object and passes them back to the caller.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/test/test_codeccallbacks.py | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/Lib/test/test_codeccallbacks.py b/Lib/test/test_codeccallbacks.py index 77834c4..f76ec65 100644 --- a/Lib/test/test_codeccallbacks.py +++ b/Lib/test/test_codeccallbacks.py @@ -806,6 +806,39 @@ class CodecCallbackTest(unittest.TestCase): text = 'abc<def>ghi'*n text.translate(charmap) + def test_mutatingdecodehandler(self): + baddata = [ + ("ascii", b"\xff"), + ("utf-7", b"++"), + ("utf-8", b"\xff"), + ("utf-16", b"\xff"), + ("unicode-escape", b"\\u123g"), + ("raw-unicode-escape", b"\\u123g"), + ("unicode-internal", b"\xff"), + ] + + def replacing(exc): + if isinstance(exc, UnicodeDecodeError): + exc.object = 42 + return ("\u4242", 0) + else: + raise TypeError("don't know how to handle %r" % exc) + codecs.register_error("test.replacing", replacing) + for (encoding, data) in baddata: + self.assertRaises(TypeError, data.decode, encoding, "test.replacing") + + def mutating(exc): + if isinstance(exc, UnicodeDecodeError): + exc.object[:] = b"" + return ("\u4242", 0) + else: + raise TypeError("don't know how to handle %r" % exc) + codecs.register_error("test.mutating", mutating) + # If the decoder doesn't pick up the modified input the following + # will lead to an endless loop + for (encoding, data) in baddata: + self.assertRaises(TypeError, data.decode, encoding, "test.replacing") + def test_main(): test.test_support.run_unittest(CodecCallbackTest) |