@@ -2911,12 +2911,24 @@ def _refold_with_ew(parse_tree, lines, maxlen, encoding, *, policy):
29112911 last_ew = None # if there is an encoded word in the last line of lines,
29122912 # points to the encoded word's first character
29132913 last_charset = None
2914+ last_comment_end = None # (line count, column) of the last emitted comment
29142915 wrap_as_ew_blocked = 0
29152916 want_encoding = False # This is set to True if we need to encode this part
29162917 end_ew_not_allowed = Terminal ('' , 'wrap_as_ew_blocked' )
29172918 parts = list (parse_tree )
29182919 while parts :
29192920 part = parts .pop (0 )
2921+ if part .token_type == 'cfws' :
2922+ # Visit comments themselves before changing encoded-word state.
2923+ parts = list (part ) + parts
2924+ continue
2925+ if part .token_type == 'comment' :
2926+ # Encoded words must not combine across comment boundaries.
2927+ if (last_word_is_ew and parse_tree .token_type != 'comment' and
2928+ not lines [- 1 ].endswith ((' ' , '\t ' ))):
2929+ lines [- 1 ] += ' '
2930+ last_ew = None
2931+ last_word_is_ew = False
29202932 if part is end_ew_not_allowed :
29212933 wrap_as_ew_blocked -= 1
29222934 continue
@@ -2946,6 +2958,24 @@ def _refold_with_ew(parse_tree, lines, maxlen, encoding, *, policy):
29462958 charset = 'utf-8'
29472959 want_encoding = True
29482960
2961+ if (part .token_type == 'comment' and not wrap_as_ew_blocked and
2962+ (want_encoding or
2963+ (parse_tree .token_type == 'comment' and
2964+ len (tstr ) > maxlen - len (lines [- 1 ])))):
2965+ # Comment.__str__ supplies the parentheses. Keep them outside
2966+ # encoded words, including those in nested comments.
2967+ if len (lines [- 1 ]) >= maxlen :
2968+ newline = _steal_trailing_WSP_if_exists (lines )
2969+ lines .append (newline or ' ' )
2970+ lines [- 1 ] += '('
2971+ _refold_with_ew (part , lines , maxlen , encoding , policy = policy )
2972+ if len (lines [- 1 ]) >= maxlen :
2973+ lines .append (' ' )
2974+ lines [- 1 ] += ')'
2975+ last_comment_end = (len (lines ), len (lines [- 1 ]))
2976+ want_encoding = False
2977+ continue
2978+
29492979 if want_encoding and not wrap_as_ew_blocked :
29502980 if any (
29512981 not x .as_ew_allowed for x in part
@@ -2955,7 +2985,10 @@ def _refold_with_ew(parse_tree, lines, maxlen, encoding, *, policy):
29552985 last_ew = None
29562986 if part .syntactic_break :
29572987 encoded_part = part .fold (policy = policy )[:- len (policy .linesep )]
2958- if policy .linesep not in encoded_part :
2988+ if (policy .linesep not in encoded_part and
2989+ (not part .contains_token_type ('comment' ) or
2990+ len (encoded_part ) <= maxlen - len (lines [- 1 ]) or
2991+ len (encoded_part ) + 1 <= maxlen )):
29592992 # It fits on a single line
29602993 if len (encoded_part ) > maxlen - len (lines [- 1 ]):
29612994 # But not on this one, so start a new one.
@@ -2977,6 +3010,9 @@ def _refold_with_ew(parse_tree, lines, maxlen, encoding, *, policy):
29773010 elif part .as_ew_allowed :
29783011 # It's a terminal, wrap it as an encoded word, possibly
29793012 # combining it with previously encoded words if allowed.
3013+ if (parse_tree .token_type != 'comment' and
3014+ last_comment_end == (len (lines ), len (lines [- 1 ]))):
3015+ lines [- 1 ] += ' '
29803016 if (last_ew is not None and
29813017 charset != last_charset and
29823018 (last_charset == 'unknown-8bit' or
@@ -3004,6 +3040,8 @@ def _refold_with_ew(parse_tree, lines, maxlen, encoding, *, policy):
30043040
30053041 if len (tstr ) <= maxlen - len (lines [- 1 ]):
30063042 lines [- 1 ] += tstr
3043+ if part .token_type == 'comment' :
3044+ last_comment_end = (len (lines ), len (lines [- 1 ]))
30073045 last_word_is_ew = last_word_is_ew and not bool (tstr .strip (_WSP ))
30083046 continue
30093047
@@ -3015,6 +3053,8 @@ def _refold_with_ew(parse_tree, lines, maxlen, encoding, *, policy):
30153053 newline = _steal_trailing_WSP_if_exists (lines )
30163054 if newline or part .startswith_fws ():
30173055 lines .append (newline + tstr )
3056+ if part .token_type == 'comment' :
3057+ last_comment_end = (len (lines ), len (lines [- 1 ]))
30183058 last_word_is_ew = (last_word_is_ew
30193059 and not bool (lines [- 1 ].strip (_WSP )))
30203060 last_ew = None
@@ -3116,21 +3156,23 @@ def _fold_as_ew(to_encode, lines, maxlen, last_ew, ew_combine_allowed, charset,
31163156 while to_encode :
31173157 remaining_space = maxlen - len (lines [- 1 ])
31183158 text_space = remaining_space - chrome_len
3119- if text_space <= 0 :
3120- newline = _steal_trailing_WSP_if_exists (lines )
3121- lines .append (newline or ' ' )
3122- new_last_ew = len (lines [- 1 ])
3123- continue
3124-
3125- to_encode_word = to_encode [:text_space ]
3159+ to_encode_word = to_encode [:max (1 , text_space )]
31263160 encoded_word = _ew .encode (to_encode_word , charset = encode_as )
31273161 excess = len (encoded_word ) - remaining_space
3128- while excess > 0 :
3162+ while excess > 0 and len ( to_encode_word ) > 1 :
31293163 # Since the chunk to encode is guaranteed to fit into less than 100 characters,
31303164 # shrinking it by one at a time shouldn't take long.
31313165 to_encode_word = to_encode_word [:- 1 ]
31323166 encoded_word = _ew .encode (to_encode_word , charset = encode_as )
31333167 excess = len (encoded_word ) - remaining_space
3168+ if excess > 0 :
3169+ if not lines [- 1 ].strip (_WSP ):
3170+ raise errors .HeaderParseError (
3171+ "max_line_length is too small to fit an encoded word" )
3172+ newline = _steal_trailing_WSP_if_exists (lines )
3173+ lines .append (newline or ' ' )
3174+ new_last_ew = len (lines [- 1 ])
3175+ continue
31343176 lines [- 1 ] += encoded_word
31353177 to_encode = to_encode [len (to_encode_word ):]
31363178
0 commit comments