ยปCore Development>Code coverage>Lib/encodings/uu_codec.py

Python code coverage for Lib/encodings/uu_codec.py

#countcontent
1n/a"""Python 'uu_codec' Codec - UU content transfer encoding.
2n/a
3n/aThis codec de/encodes from bytes to bytes.
4n/a
5n/aWritten by Marc-Andre Lemburg (mal@lemburg.com). Some details were
6n/aadapted from uu.py which was written by Lance Ellinghouse and
7n/amodified by Jack Jansen and Fredrik Lundh.
8n/a"""
9n/a
10n/aimport codecs
11n/aimport binascii
12n/afrom io import BytesIO
13n/a
14n/a### Codec APIs
15n/a
16n/adef uu_encode(input, errors='strict', filename='<data>', mode=0o666):
17n/a assert errors == 'strict'
18n/a infile = BytesIO(input)
19n/a outfile = BytesIO()
20n/a read = infile.read
21n/a write = outfile.write
22n/a
23n/a # Encode
24n/a write(('begin %o %s\n' % (mode & 0o777, filename)).encode('ascii'))
25n/a chunk = read(45)
26n/a while chunk:
27n/a write(binascii.b2a_uu(chunk))
28n/a chunk = read(45)
29n/a write(b' \nend\n')
30n/a
31n/a return (outfile.getvalue(), len(input))
32n/a
33n/adef uu_decode(input, errors='strict'):
34n/a assert errors == 'strict'
35n/a infile = BytesIO(input)
36n/a outfile = BytesIO()
37n/a readline = infile.readline
38n/a write = outfile.write
39n/a
40n/a # Find start of encoded data
41n/a while 1:
42n/a s = readline()
43n/a if not s:
44n/a raise ValueError('Missing "begin" line in input data')
45n/a if s[:5] == b'begin':
46n/a break
47n/a
48n/a # Decode
49n/a while True:
50n/a s = readline()
51n/a if not s or s == b'end\n':
52n/a break
53n/a try:
54n/a data = binascii.a2b_uu(s)
55n/a except binascii.Error as v:
56n/a # Workaround for broken uuencoders by /Fredrik Lundh
57n/a nbytes = (((s[0]-32) & 63) * 4 + 5) // 3
58n/a data = binascii.a2b_uu(s[:nbytes])
59n/a #sys.stderr.write("Warning: %s\n" % str(v))
60n/a write(data)
61n/a if not s:
62n/a raise ValueError('Truncated input data')
63n/a
64n/a return (outfile.getvalue(), len(input))
65n/a
66n/aclass Codec(codecs.Codec):
67n/a def encode(self, input, errors='strict'):
68n/a return uu_encode(input, errors)
69n/a
70n/a def decode(self, input, errors='strict'):
71n/a return uu_decode(input, errors)
72n/a
73n/aclass IncrementalEncoder(codecs.IncrementalEncoder):
74n/a def encode(self, input, final=False):
75n/a return uu_encode(input, self.errors)[0]
76n/a
77n/aclass IncrementalDecoder(codecs.IncrementalDecoder):
78n/a def decode(self, input, final=False):
79n/a return uu_decode(input, self.errors)[0]
80n/a
81n/aclass StreamWriter(Codec, codecs.StreamWriter):
82n/a charbuffertype = bytes
83n/a
84n/aclass StreamReader(Codec, codecs.StreamReader):
85n/a charbuffertype = bytes
86n/a
87n/a### encodings module API
88n/a
89n/adef getregentry():
90n/a return codecs.CodecInfo(
91n/a name='uu',
92n/a encode=uu_encode,
93n/a decode=uu_decode,
94n/a incrementalencoder=IncrementalEncoder,
95n/a incrementaldecoder=IncrementalDecoder,
96n/a streamreader=StreamReader,
97n/a streamwriter=StreamWriter,
98n/a _is_text_encoding=False,
99n/a )