mirror of
https://github.com/python/cpython.git
synced 2025-12-08 06:10:17 +00:00
Remaining asserts
This commit is contained in:
parent
cb8197ad6d
commit
f7d77c4841
5 changed files with 39 additions and 20 deletions
|
|
@ -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):
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue