Remaining asserts

This commit is contained in:
Stan Ulbrych 2025-07-24 10:22:15 +02:00
parent cb8197ad6d
commit f7d77c4841
No known key found for this signature in database
GPG key ID: B8E58DBDB2A1A0B8
5 changed files with 39 additions and 20 deletions

View file

@ -8,16 +8,20 @@
import codecs
import base64
### Codec Helpers
def _assert_strict(errors):
if errors != 'strict':
raise ValueError(f'Unsupported error handling mode: "{errors}" - must be "strict"')
### Codec APIs
def base64_encode(input, errors='strict'):
if errors != 'strict':
raise ValueError(f'Unsupported error handling mode: "{errors}" - must be "strict"')
_assert_strict(errors)
return (base64.encodebytes(input), len(input))
def base64_decode(input, errors='strict'):
if errors != 'strict':
raise ValueError(f'Unsupported error handling mode: "{errors}" - must be "strict"')
_assert_strict(errors)
return (base64.decodebytes(input), len(input))
class Codec(codecs.Codec):
@ -28,12 +32,12 @@ def decode(self, input, errors='strict'):
class IncrementalEncoder(codecs.IncrementalEncoder):
def encode(self, input, final=False):
assert self.errors == 'strict'
_assert_strict(self.errors)
return base64.encodebytes(input)
class IncrementalDecoder(codecs.IncrementalDecoder):
def decode(self, input, final=False):
assert self.errors == 'strict'
_assert_strict(self.errors)
return base64.decodebytes(input)
class StreamWriter(Codec, codecs.StreamWriter):