Fixed issue-number mistake in NEWS update.
[python.git] / Lib / encodings / quopri_codec.py
blobd8683fd56d325415a25116c129ab41ae45126016
1 """Codec for quoted-printable encoding.
3 Like base64 and rot13, this returns Python strings, not Unicode.
4 """
6 import codecs, quopri
7 try:
8 from cStringIO import StringIO
9 except ImportError:
10 from StringIO import StringIO
12 def quopri_encode(input, errors='strict'):
13 """Encode the input, returning a tuple (output object, length consumed).
15 errors defines the error handling to apply. It defaults to
16 'strict' handling which is the only currently supported
17 error handling for this codec.
19 """
20 assert errors == 'strict'
21 # using str() because of cStringIO's Unicode undesired Unicode behavior.
22 f = StringIO(str(input))
23 g = StringIO()
24 quopri.encode(f, g, 1)
25 output = g.getvalue()
26 return (output, len(input))
28 def quopri_decode(input, errors='strict'):
29 """Decode the input, returning a tuple (output object, length consumed).
31 errors defines the error handling to apply. It defaults to
32 'strict' handling which is the only currently supported
33 error handling for this codec.
35 """
36 assert errors == 'strict'
37 f = StringIO(str(input))
38 g = StringIO()
39 quopri.decode(f, g)
40 output = g.getvalue()
41 return (output, len(input))
43 class Codec(codecs.Codec):
45 def encode(self, input,errors='strict'):
46 return quopri_encode(input,errors)
47 def decode(self, input,errors='strict'):
48 return quopri_decode(input,errors)
50 class IncrementalEncoder(codecs.IncrementalEncoder):
51 def encode(self, input, final=False):
52 return quopri_encode(input, self.errors)[0]
54 class IncrementalDecoder(codecs.IncrementalDecoder):
55 def decode(self, input, final=False):
56 return quopri_decode(input, self.errors)[0]
58 class StreamWriter(Codec, codecs.StreamWriter):
59 pass
61 class StreamReader(Codec,codecs.StreamReader):
62 pass
64 # encodings module API
66 def getregentry():
67 return codecs.CodecInfo(
68 name='quopri',
69 encode=quopri_encode,
70 decode=quopri_decode,
71 incrementalencoder=IncrementalEncoder,
72 incrementaldecoder=IncrementalDecoder,
73 streamwriter=StreamWriter,
74 streamreader=StreamReader,