MCPcopy Index your code
hub / github.com/RustPython/RustPython / _fold_mime_parameters

Function _fold_mime_parameters

Lib/email/_header_value_parser.py:3024–3097  ·  view source on GitHub ↗

Fold TokenList 'part' into the 'lines' list as mime parameters. Using the decoded list of parameters and values, format them according to the RFC rules, including using RFC2231 encoding if the value cannot be expressed in 'encoding' and/or the parameter+value is too long to fit with

(part, lines, maxlen, encoding)

Source from the content-addressed store, hash-verified

3022 return new_last_ew if ew_combine_allowed else None
3023
3024def _fold_mime_parameters(part, lines, maxlen, encoding):
3025 """Fold TokenList 'part' into the 'lines' list as mime parameters.
3026
3027 Using the decoded list of parameters and values, format them according to
3028 the RFC rules, including using RFC2231 encoding if the value cannot be
3029 expressed in 'encoding' and/or the parameter+value is too long to fit
3030 within 'maxlen'.
3031
3032 """
3033 # Special case for RFC2231 encoding: start from decoded values and use
3034 # RFC2231 encoding iff needed.
3035 #
3036 # Note that the 1 and 2s being added to the length calculations are
3037 # accounting for the possibly-needed spaces and semicolons we'll be adding.
3038 #
3039 for name, value in part.params:
3040 # XXX What if this ';' puts us over maxlen the first time through the
3041 # loop? We should split the header value onto a newline in that case,
3042 # but to do that we need to recognize the need earlier or reparse the
3043 # header, so I'm going to ignore that bug for now. It'll only put us
3044 # one character over.
3045 if not lines[-1].rstrip().endswith(';'):
3046 lines[-1] += ';'
3047 charset = encoding
3048 error_handler = 'strict'
3049 try:
3050 value.encode(encoding)
3051 encoding_required = False
3052 except UnicodeEncodeError:
3053 encoding_required = True
3054 if utils._has_surrogates(value):
3055 charset = 'unknown-8bit'
3056 error_handler = 'surrogateescape'
3057 else:
3058 charset = 'utf-8'
3059 if encoding_required:
3060 encoded_value = urllib.parse.quote(
3061 value, safe='', errors=error_handler)
3062 tstr = "{}*={}''{}".format(name, charset, encoded_value)
3063 else:
3064 tstr = '{}={}'.format(name, quote_string(value))
3065 if len(lines[-1]) + len(tstr) + 1 < maxlen:
3066 lines[-1] = lines[-1] + ' ' + tstr
3067 continue
3068 elif len(tstr) + 2 <= maxlen:
3069 lines.append(' ' + tstr)
3070 continue
3071 # We need multiple sections. We are allowed to mix encoded and
3072 # non-encoded sections, but we aren't going to. We'll encode them all.
3073 section = 0
3074 extra_chrome = charset + "''"
3075 while value:
3076 chrome_len = len(name) + len(str(section)) + 3 + len(extra_chrome)
3077 if maxlen <= chrome_len + 3:
3078 # We need room for the leading blank, the trailing semicolon,
3079 # and at least one character of the value. If we don't
3080 # have that, we'd be stuck, so in that case fall back to
3081 # the RFC standard width.

Callers 1

_refold_parse_treeFunction · 0.85

Calls 9

quote_stringFunction · 0.85
lenFunction · 0.85
strFunction · 0.85
quoteMethod · 0.80
endswithMethod · 0.45
rstripMethod · 0.45
encodeMethod · 0.45
formatMethod · 0.45
appendMethod · 0.45

Tested by

no test coverage detected