annotate CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/lib/python3.8/encodings/zlib_codec.py @ 69:33d812a61356

planemo upload commit 2e9511a184a1ca667c7be0c6321a36dc4e3d116d
author jpayne
date Tue, 18 Mar 2025 17:55:14 -0400
parents
children
rev   line source
jpayne@69 1 """Python 'zlib_codec' Codec - zlib compression encoding.
jpayne@69 2
jpayne@69 3 This codec de/encodes from bytes to bytes.
jpayne@69 4
jpayne@69 5 Written by Marc-Andre Lemburg (mal@lemburg.com).
jpayne@69 6 """
jpayne@69 7
jpayne@69 8 import codecs
jpayne@69 9 import zlib # this codec needs the optional zlib module !
jpayne@69 10
jpayne@69 11 ### Codec APIs
jpayne@69 12
jpayne@69 13 def zlib_encode(input, errors='strict'):
jpayne@69 14 assert errors == 'strict'
jpayne@69 15 return (zlib.compress(input), len(input))
jpayne@69 16
jpayne@69 17 def zlib_decode(input, errors='strict'):
jpayne@69 18 assert errors == 'strict'
jpayne@69 19 return (zlib.decompress(input), len(input))
jpayne@69 20
jpayne@69 21 class Codec(codecs.Codec):
jpayne@69 22 def encode(self, input, errors='strict'):
jpayne@69 23 return zlib_encode(input, errors)
jpayne@69 24 def decode(self, input, errors='strict'):
jpayne@69 25 return zlib_decode(input, errors)
jpayne@69 26
jpayne@69 27 class IncrementalEncoder(codecs.IncrementalEncoder):
jpayne@69 28 def __init__(self, errors='strict'):
jpayne@69 29 assert errors == 'strict'
jpayne@69 30 self.errors = errors
jpayne@69 31 self.compressobj = zlib.compressobj()
jpayne@69 32
jpayne@69 33 def encode(self, input, final=False):
jpayne@69 34 if final:
jpayne@69 35 c = self.compressobj.compress(input)
jpayne@69 36 return c + self.compressobj.flush()
jpayne@69 37 else:
jpayne@69 38 return self.compressobj.compress(input)
jpayne@69 39
jpayne@69 40 def reset(self):
jpayne@69 41 self.compressobj = zlib.compressobj()
jpayne@69 42
jpayne@69 43 class IncrementalDecoder(codecs.IncrementalDecoder):
jpayne@69 44 def __init__(self, errors='strict'):
jpayne@69 45 assert errors == 'strict'
jpayne@69 46 self.errors = errors
jpayne@69 47 self.decompressobj = zlib.decompressobj()
jpayne@69 48
jpayne@69 49 def decode(self, input, final=False):
jpayne@69 50 if final:
jpayne@69 51 c = self.decompressobj.decompress(input)
jpayne@69 52 return c + self.decompressobj.flush()
jpayne@69 53 else:
jpayne@69 54 return self.decompressobj.decompress(input)
jpayne@69 55
jpayne@69 56 def reset(self):
jpayne@69 57 self.decompressobj = zlib.decompressobj()
jpayne@69 58
jpayne@69 59 class StreamWriter(Codec, codecs.StreamWriter):
jpayne@69 60 charbuffertype = bytes
jpayne@69 61
jpayne@69 62 class StreamReader(Codec, codecs.StreamReader):
jpayne@69 63 charbuffertype = bytes
jpayne@69 64
jpayne@69 65 ### encodings module API
jpayne@69 66
jpayne@69 67 def getregentry():
jpayne@69 68 return codecs.CodecInfo(
jpayne@69 69 name='zlib',
jpayne@69 70 encode=zlib_encode,
jpayne@69 71 decode=zlib_decode,
jpayne@69 72 incrementalencoder=IncrementalEncoder,
jpayne@69 73 incrementaldecoder=IncrementalDecoder,
jpayne@69 74 streamreader=StreamReader,
jpayne@69 75 streamwriter=StreamWriter,
jpayne@69 76 _is_text_encoding=False,
jpayne@69 77 )