mirror of
https://github.com/python/cpython.git
synced 2025-12-08 06:10:17 +00:00
Raise valueerrors
This commit is contained in:
parent
609d5adc7c
commit
cb8197ad6d
8 changed files with 52 additions and 16 deletions
|
|
@ -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()
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue