MCPcopy Index your code
hub / github.com/python/cpython / encode

Function encode

Lib/quopri.py:42–99  ·  view source on GitHub ↗

Read 'input', apply quoted-printable encoding, and write to 'output'. 'input' and 'output' are binary file objects. The 'quotetabs' flag indicates whether embedded tabs and spaces should be quoted. Note that line-ending tabs and spaces are always encoded, as per RFC 1521. The 'heade

(input, output, quotetabs, header=False)

Source from the content-addressed store, hash-verified

40
41
42def encode(input, output, quotetabs, header=False):
43 """Read 'input', apply quoted-printable encoding, and write to 'output'.
44
45 'input' and 'output' are binary file objects. The 'quotetabs' flag
46 indicates whether embedded tabs and spaces should be quoted. Note that
47 line-ending tabs and spaces are always encoded, as per RFC 1521.
48 The 'header' flag indicates whether we are encoding spaces as _ as per RFC
49 1522."""
50
51 if b2a_qp is not None:
52 data = input.read()
53 odata = b2a_qp(data, quotetabs=quotetabs, header=header)
54 output.write(odata)
55 return
56
57 def write(s, output=output, lineEnd=b'\n'):
58 # RFC 1521 requires that the line ending in a space or tab must have
59 # that trailing character encoded.
60 if s and s[-1:] in b' \t':
61 output.write(s[:-1] + quote(s[-1:]) + lineEnd)
62 elif s == b'.':
63 output.write(quote(s) + lineEnd)
64 else:
65 output.write(s + lineEnd)
66
67 prevline = None
68 while line := input.readline():
69 outline = []
70 # Strip off any readline induced trailing newline
71 stripped = b''
72 if line[-1:] == b'\n':
73 line = line[:-1]
74 stripped = b'\n'
75 # Calculate the un-length-limited encoded line
76 for c in line:
77 c = bytes((c,))
78 if needsquoting(c, quotetabs, header):
79 c = quote(c)
80 if header and c == b' ':
81 outline.append(b'_')
82 else:
83 outline.append(c)
84 # First, write out the previous line
85 if prevline is not None:
86 write(prevline)
87 # Now see if we need any soft line breaks because of RFC-imposed
88 # length limitations. Then do the thisline->prevline dance.
89 thisline = EMPTYSTRING.join(outline)
90 while len(thisline) > MAXLINESIZE:
91 # Don't forget to include the soft line break `=' sign in the
92 # length calculation!
93 write(thisline[:MAXLINESIZE-1], lineEnd=b'=\n')
94 thisline = thisline[MAXLINESIZE-1:]
95 # Write out the current line
96 prevline = thisline
97 # Write out the last line, without a trailing newline
98 if prevline is not None:
99 write(prevline, lineEnd=stripped)

Callers 2

encodestringFunction · 0.70
mainFunction · 0.70

Calls 8

needsquotingFunction · 0.85
quoteFunction · 0.70
writeFunction · 0.70
readMethod · 0.45
writeMethod · 0.45
readlineMethod · 0.45
appendMethod · 0.45
joinMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…