make sure sl_formataddr always return str (#1269)
This commit is contained in:
parent
313a928070
commit
192d03fd68
|
@ -1466,6 +1466,7 @@ def get_verp_info_from_email(email: str) -> Optional[Tuple[VerpType, int]]:
|
|||
|
||||
|
||||
def sl_formataddr(name_address_tuple: Tuple[str, str]):
|
||||
"""Same as formataddr but use utf-8 encoding by default"""
|
||||
"""Same as formataddr but use utf-8 encoding by default and always return str (and never Header)"""
|
||||
name, addr = name_address_tuple
|
||||
return formataddr((name, Header(addr, "utf-8")))
|
||||
# formataddr can return Header, make sure to convert to str
|
||||
return str(formataddr((name, Header(addr, "utf-8"))))
|
||||
|
|
|
@ -782,6 +782,10 @@ def test_add_header_multipart_with_invalid_part():
|
|||
|
||||
|
||||
def test_sl_formataddr():
|
||||
# when the name part (first element in the tuple) is empty, formataddr() returns a Header
|
||||
# this makes sure sl_formataddr always returns str
|
||||
assert sl_formataddr(("", "a@b.c")) == "a@b.c"
|
||||
|
||||
assert sl_formataddr(("é", "è@ç.à")) == "=?utf-8?b?w6k=?= <è@ç.à>"
|
||||
# test that the same name-address can't be handled by the built-in formataddr
|
||||
with pytest.raises(UnicodeEncodeError):
|
||||
|
|
Loading…
Reference in a new issue