diff -r ae8b054155c1 Lib/encodings/base64_codec.py --- a/Lib/encodings/base64_codec.py Sat Jul 06 10:25:04 2013 +0200 +++ b/Lib/encodings/base64_codec.py Fri Jul 12 16:47:53 2013 +0100 @@ -9,14 +9,19 @@ import codecs import base64 +def _check_strict(errors): + if errors != 'strict': + raise ValueError( + 'Unsupported error handling mode %s - must be "strict"' % errors) + ### Codec APIs def base64_encode(input, errors='strict'): - assert errors == 'strict' + _check_strict(errors) return (base64.encodebytes(input), len(input)) def base64_decode(input, errors='strict'): - assert errors == 'strict' + _check_strict(errors) return (base64.decodebytes(input), len(input)) class Codec(codecs.Codec): @@ -25,14 +30,17 @@ def decode(self, input, errors='strict'): return base64_decode(input, errors) -class IncrementalEncoder(codecs.IncrementalEncoder): +class _StrictErrors(object): + def __init__(self, errors='strict', *args, **kwargs): + _check_strict(errors) + super(_StrictErrors, self).__init__(errors=errors, *args, **kwargs) + +class IncrementalEncoder(_StrictErrors, codecs.IncrementalEncoder): def encode(self, input, final=False): - assert self.errors == 'strict' return base64.encodebytes(input) -class IncrementalDecoder(codecs.IncrementalDecoder): +class IncrementalDecoder(_StrictErrors, codecs.IncrementalDecoder): def decode(self, input, final=False): - assert self.errors == 'strict' return base64.decodebytes(input) class StreamWriter(Codec, codecs.StreamWriter): diff -r ae8b054155c1 Lib/encodings/bz2_codec.py --- a/Lib/encodings/bz2_codec.py Sat Jul 06 10:25:04 2013 +0200 +++ b/Lib/encodings/bz2_codec.py Fri Jul 12 16:47:53 2013 +0100 @@ -10,14 +10,19 @@ import codecs import bz2 # this codec needs the optional bz2 module ! +def _check_strict(errors): + if errors != 'strict': + raise ValueError( + 'Unsupported error handling mode %s - must be "strict"' % errors) + ### Codec APIs def bz2_encode(input, errors='strict'): - assert errors == 'strict' + _check_strict(errors) return (bz2.compress(input), len(input)) def bz2_decode(input, errors='strict'): - assert errors == 'strict' + _check_strict(errors) return (bz2.decompress(input), len(input)) class Codec(codecs.Codec): @@ -28,8 +33,8 @@ class IncrementalEncoder(codecs.IncrementalEncoder): def __init__(self, errors='strict'): - assert errors == 'strict' - self.errors = errors + _check_strict(errors) + super(IncrementalEncoder, self).__init__(errors=errors) self.compressobj = bz2.BZ2Compressor() def encode(self, input, final=False): @@ -44,8 +49,8 @@ class IncrementalDecoder(codecs.IncrementalDecoder): def __init__(self, errors='strict'): - assert errors == 'strict' - self.errors = errors + _check_strict(errors) + super(IncrementalDecoder, self).__init__(errors=errors) self.decompressobj = bz2.BZ2Decompressor() def decode(self, input, final=False): diff -r ae8b054155c1 Lib/encodings/hex_codec.py --- a/Lib/encodings/hex_codec.py Sat Jul 06 10:25:04 2013 +0200 +++ b/Lib/encodings/hex_codec.py Fri Jul 12 16:47:53 2013 +0100 @@ -9,14 +9,19 @@ import codecs import binascii +def _check_strict(errors): + if errors != 'strict': + raise ValueError( + 'Unsupported error handling mode %s - must be "strict"' % errors) + ### Codec APIs def hex_encode(input, errors='strict'): - assert errors == 'strict' + _check_strict(errors) return (binascii.b2a_hex(input), len(input)) def hex_decode(input, errors='strict'): - assert errors == 'strict' + _check_strict(errors) return (binascii.a2b_hex(input), len(input)) class Codec(codecs.Codec): @@ -25,14 +30,17 @@ def decode(self, input, errors='strict'): return hex_decode(input, errors) -class IncrementalEncoder(codecs.IncrementalEncoder): +class _StrictErrors(object): + def __init__(self, errors='strict', *args, **kwargs): + _check_strict(errors) + super(_StrictErrors, self).__init__(errors=errors, *args, **kwargs) + +class IncrementalEncoder(_StrictErrors, codecs.IncrementalEncoder): def encode(self, input, final=False): - assert self.errors == 'strict' return binascii.b2a_hex(input) -class IncrementalDecoder(codecs.IncrementalDecoder): +class IncrementalDecoder(_StrictErrors, codecs.IncrementalDecoder): def decode(self, input, final=False): - assert self.errors == 'strict' return binascii.a2b_hex(input) class StreamWriter(Codec, codecs.StreamWriter): diff -r ae8b054155c1 Lib/encodings/quopri_codec.py --- a/Lib/encodings/quopri_codec.py Sat Jul 06 10:25:04 2013 +0200 +++ b/Lib/encodings/quopri_codec.py Fri Jul 12 16:47:53 2013 +0100 @@ -8,15 +8,22 @@ import quopri from io import BytesIO +def _check_strict(errors): + if errors != 'strict': + raise ValueError( + 'Unsupported error handling mode %s - must be "strict"' % errors) + +### Codec APIs + def quopri_encode(input, errors='strict'): - assert errors == 'strict' + _check_strict(errors) f = BytesIO(input) g = BytesIO() quopri.encode(f, g, 1) return (g.getvalue(), len(input)) def quopri_decode(input, errors='strict'): - assert errors == 'strict' + _check_strict(errors) f = BytesIO(input) g = BytesIO() quopri.decode(f, g) @@ -28,11 +35,16 @@ def decode(self, input, errors='strict'): return quopri_decode(input, errors) -class IncrementalEncoder(codecs.IncrementalEncoder): +class _StrictErrors(object): + def __init__(self, errors='strict', *args, **kwargs): + _check_strict(errors) + super(_StrictErrors, self).__init__(errors=errors, *args, **kwargs) + +class IncrementalEncoder(_StrictErrors, codecs.IncrementalEncoder): def encode(self, input, final=False): return quopri_encode(input, self.errors)[0] -class IncrementalDecoder(codecs.IncrementalDecoder): +class IncrementalDecoder(_StrictErrors, codecs.IncrementalDecoder): def decode(self, input, final=False): return quopri_decode(input, self.errors)[0] diff -r ae8b054155c1 Lib/encodings/uu_codec.py --- a/Lib/encodings/uu_codec.py Sat Jul 06 10:25:04 2013 +0200 +++ b/Lib/encodings/uu_codec.py Fri Jul 12 16:47:53 2013 +0100 @@ -12,10 +12,15 @@ import binascii from io import BytesIO +def _check_strict(errors): + if errors != 'strict': + raise ValueError( + 'Unsupported error handling mode %s - must be "strict"' % errors) + ### Codec APIs def uu_encode(input, errors='strict', filename='', mode=0o666): - assert errors == 'strict' + _check_strict(errors) infile = BytesIO(input) outfile = BytesIO() read = infile.read @@ -32,7 +37,7 @@ return (outfile.getvalue(), len(input)) def uu_decode(input, errors='strict'): - assert errors == 'strict' + _check_strict(errors) infile = BytesIO(input) outfile = BytesIO() readline = infile.readline @@ -71,11 +76,16 @@ def decode(self, input, errors='strict'): return uu_decode(input, errors) -class IncrementalEncoder(codecs.IncrementalEncoder): +class _StrictErrors(object): + def __init__(self, errors='strict', *args, **kwargs): + _check_strict(errors) + super(_StrictErrors, self).__init__(errors=errors, *args, **kwargs) + +class IncrementalEncoder(_StrictErrors, codecs.IncrementalEncoder): def encode(self, input, final=False): return uu_encode(input, self.errors)[0] -class IncrementalDecoder(codecs.IncrementalDecoder): +class IncrementalDecoder(_StrictErrors, codecs.IncrementalDecoder): def decode(self, input, final=False): return uu_decode(input, self.errors)[0] diff -r ae8b054155c1 Lib/encodings/zlib_codec.py --- a/Lib/encodings/zlib_codec.py Sat Jul 06 10:25:04 2013 +0200 +++ b/Lib/encodings/zlib_codec.py Fri Jul 12 16:47:53 2013 +0100 @@ -9,14 +9,19 @@ import codecs import zlib # this codec needs the optional zlib module ! +def _check_strict(errors): + if errors != 'strict': + raise ValueError( + 'Unsupported error handling mode %s - must be "strict"' % errors) + ### Codec APIs def zlib_encode(input, errors='strict'): - assert errors == 'strict' + _check_strict(errors) return (zlib.compress(input), len(input)) def zlib_decode(input, errors='strict'): - assert errors == 'strict' + _check_strict(errors) return (zlib.decompress(input), len(input)) class Codec(codecs.Codec): @@ -27,8 +32,8 @@ class IncrementalEncoder(codecs.IncrementalEncoder): def __init__(self, errors='strict'): - assert errors == 'strict' - self.errors = errors + _check_strict(errors) + super(IncrementalEncoder, self).__init__(errors=errors) self.compressobj = zlib.compressobj() def encode(self, input, final=False): @@ -43,8 +48,8 @@ class IncrementalDecoder(codecs.IncrementalDecoder): def __init__(self, errors='strict'): - assert errors == 'strict' - self.errors = errors + _check_strict(errors) + super(IncrementalDecoder, self).__init__(errors=errors) self.decompressobj = zlib.decompressobj() def decode(self, input, final=False): diff -r ae8b054155c1 Lib/test/test_codecs.py --- a/Lib/test/test_codecs.py Sat Jul 06 10:25:04 2013 +0200 +++ b/Lib/test/test_codecs.py Fri Jul 12 16:47:53 2013 +0100 @@ -2263,11 +2263,15 @@ binput = bytes(range(256)) for encoding in bytes_transform_encodings: # generic codecs interface - (o, size) = codecs.getencoder(encoding)(binput) + encoder = codecs.getencoder(encoding) + self.assertRaises(ValueError, encoder, binput, errors="ignore") + (o, size) = encoder(binput) self.assertEqual(size, len(binput)) - (i, size) = codecs.getdecoder(encoding)(o) + decoder = codecs.getdecoder(encoding) + self.assertRaises(ValueError, decoder, binput, errors="ignore") + (i, size) = decoder(o) self.assertEqual(size, len(o)) - self.assertEqual(i, binput) + self.assertEqual(i, binput) def test_read(self): for encoding in bytes_transform_encodings: @@ -2285,6 +2289,15 @@ sout = reader.readline() self.assertEqual(sout, b"\x80") + def test_incremental(self): + for encoding in bytes_transform_encodings: + encoder = codecs.getincrementalencoder(encoding) + self.assertRaises(ValueError, encoder, errors="ignore") + encoder() + decoder = codecs.getincrementaldecoder(encoding) + self.assertRaises(ValueError, decoder, errors="ignore") + decoder() + @unittest.skipUnless(sys.platform == 'win32', 'code pages are specific to Windows')