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

Function get_encoded_word

Lib/email/_header_value_parser.py:1075–1122  ·  view source on GitHub ↗

encoded-word = "=?" charset "?" encoding "?" encoded-text "?="

(value, terminal_type='vtext')

Source from the content-addressed store, hash-verified

1073 return fws, newvalue
1074
1075def get_encoded_word(value, terminal_type='vtext'):
1076 """ encoded-word = "=?" charset "?" encoding "?" encoded-text "?="
1077
1078 """
1079 ew = EncodedWord()
1080 if not value.startswith('=?'):
1081 raise errors.HeaderParseError(
1082 "expected encoded word but found {}".format(value))
1083 tok, *remainder = value[2:].split('?=', 1)
1084 if tok == value[2:]:
1085 raise errors.HeaderParseError(
1086 "expected encoded word but found {}".format(value))
1087 remstr = ''.join(remainder)
1088 if (len(remstr) > 1 and
1089 remstr[0] in hexdigits and
1090 remstr[1] in hexdigits and
1091 tok.count('?') < 2):
1092 # The ? after the CTE was followed by an encoded word escape (=XX).
1093 rest, *remainder = remstr.split('?=', 1)
1094 tok = tok + '?=' + rest
1095 if len(tok.split()) > 1:
1096 ew.defects.append(errors.InvalidHeaderDefect(
1097 "whitespace inside encoded word"))
1098 ew.cte = value
1099 value = ''.join(remainder)
1100 try:
1101 text, charset, lang, defects = _ew.decode('=?' + tok + '?=')
1102 except (ValueError, KeyError):
1103 raise _InvalidEwError(
1104 "encoded word format invalid: '{}'".format(ew.cte))
1105 ew.charset = charset
1106 ew.lang = lang
1107 ew.defects.extend(defects)
1108 while text:
1109 if text[0] in WSP:
1110 token, text = get_fws(text)
1111 ew.append(token)
1112 continue
1113 chars, *remainder = _wsp_splitter(text, 1)
1114 vtext = ValueTerminal(chars, terminal_type)
1115 _validate_xtext(vtext)
1116 ew.append(vtext)
1117 text = ''.join(remainder)
1118 # Encoded words should be followed by a WS
1119 if value and value[0] not in WSP:
1120 ew.defects.append(errors.InvalidHeaderDefect(
1121 "missing trailing whitespace after encoded-word"))
1122 return ew, value
1123
1124def get_unstructured(value):
1125 """unstructured = (*([FWS] vchar) *WSP) / obs-unstruct

Callers 4

get_unstructuredFunction · 0.85
get_bare_quoted_stringFunction · 0.85
get_atomFunction · 0.85
get_dot_atomFunction · 0.85

Calls 13

EncodedWordClass · 0.85
_InvalidEwErrorClass · 0.85
get_fwsFunction · 0.85
ValueTerminalClass · 0.85
_validate_xtextFunction · 0.85
startswithMethod · 0.45
formatMethod · 0.45
splitMethod · 0.45
joinMethod · 0.45
countMethod · 0.45
appendMethod · 0.45
decodeMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…