Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit dac67ac

Browse files
committed
encode(): Fixed the handling of soft line breaks for lines over 76
characters in length. Remember that when calculating the soft breaks, the trailing `=' sign counts against the max length!
1 parent fb3e54f commit dac67ac

1 file changed

Lines changed: 12 additions & 14 deletions

File tree

Lib/quopri.py

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ def write(s, output=output, lineEnd='\n'):
4848
output.write(s + lineEnd)
4949

5050
prevline = None
51-
linelen = 0
5251
while 1:
5352
line = input.readline()
5453
if not line:
@@ -59,25 +58,24 @@ def write(s, output=output, lineEnd='\n'):
5958
if line[-1:] == '\n':
6059
line = line[:-1]
6160
stripped = '\n'
61+
# Calculate the un-length-limited encoded line
6262
for c in line:
6363
if needsquoting(c, quotetabs):
6464
c = quote(c)
65-
# Have we hit the RFC 1521 encoded line maximum?
66-
if linelen + len(c) >= MAXLINESIZE:
67-
# Write out the previous line
68-
if prevline is not None:
69-
write(prevline)
70-
prevline = EMPTYSTRING.join(outline)
71-
linelen = 0
72-
outline = []
7365
outline.append(c)
74-
linelen += len(c)
75-
# Write out the current line
66+
# First, write out the previous line
7667
if prevline is not None:
7768
write(prevline)
78-
prevline = EMPTYSTRING.join(outline)
79-
linelen = 0
80-
outline = []
69+
# Now see if we need any soft line breaks because of RFC-imposed
70+
# length limitations. Then do the thisline->prevline dance.
71+
thisline = EMPTYSTRING.join(outline)
72+
while len(thisline) > MAXLINESIZE:
73+
# Don't forget to include the soft line break `=' sign in the
74+
# length calculation!
75+
write(thisline[:MAXLINESIZE-1], lineEnd='=\n')
76+
thisline = thisline[MAXLINESIZE-1:]
77+
# Write out the current line
78+
prevline = thisline
8179
# Write out the last line, without a trailing newline
8280
if prevline is not None:
8381
write(prevline, lineEnd=stripped)

0 commit comments

Comments
 (0)