Raise valueerrors

This commit is contained in:
Stan Ulbrych 2025-07-13 07:55:11 +01:00
parent 609d5adc7c
commit cb8197ad6d
No known key found for this signature in database
GPG key ID: B8E58DBDB2A1A0B8
8 changed files with 52 additions and 16 deletions

View file

@ -8,14 +8,20 @@
import codecs
import zlib # this codec needs the optional zlib module !
### Codec Helpers
def _assert_strict(errors):
if errors != 'strict':
raise ValueError(f'Unsupported error handling mode: "{errors}" - must be "strict"')
### Codec APIs
def zlib_encode(input, errors='strict'):
assert errors == 'strict'
_assert_strict(errors)
return (zlib.compress(input), len(input))
def zlib_decode(input, errors='strict'):
assert errors == 'strict'
_assert_strict(errors)
return (zlib.decompress(input), len(input))
class Codec(codecs.Codec):
@ -26,7 +32,7 @@ def decode(self, input, errors='strict'):
class IncrementalEncoder(codecs.IncrementalEncoder):
def __init__(self, errors='strict'):
assert errors == 'strict'
_assert_strict(errors)
self.errors = errors
self.compressobj = zlib.compressobj()
@ -42,7 +48,7 @@ def reset(self):
class IncrementalDecoder(codecs.IncrementalDecoder):
def __init__(self, errors='strict'):
assert errors == 'strict'
_assert_strict(errors)
self.errors = errors
self.decompressobj = zlib.decompressobj()