Abhilash Raj <[email protected]> added the comment:
It is actually parsed correctly and serialized back when you try to convert it
to a string representation:
from email.parser import BytesFeedParser
import email.policy
def main():
eml_string = 'From: Nobody <""@example.org>'
parser = BytesFeedParser(policy = email.policy.default)
parser.feed(eml_string.encode())
msg = parser.close()
print(msg.get('From').addresses[0].addr_spec)
print(repr(msg.get('From')._parse_tree))
print(msg.as_string())
Running this gives me:
@example.org
AddressList([Address([Mailbox([NameAddr([DisplayName([Atom([ValueTerminal('Nobody'),
CFWSList([WhiteSpaceTerminal(' ')])])]), AngleAddr([ValueTerminal('<'),
AddrSpec([LocalPart([QuotedString([BareQuotedString([ValueTerminal('')])])]),
ValueTerminal('@'), Domain([DotAtom([DotAtomText([ValueTerminal('example'),
ValueTerminal('.'), ValueTerminal('org')])])])]), ValueTerminal('>')])])])])])
From: Nobody <""@example.org>
Notice the :
AddrSpec([LocalPart([QuotedString([BareQuotedString([ValueTerminal('')])])])
print() converts the addr-spec into a string, which omits the quotes. This is
true for any non-none string too:
[email protected]
AddressList([Address([Mailbox([NameAddr([DisplayName([Atom([ValueTerminal('Nobody'),
CFWSList([WhiteSpaceTerminal(' ')])])]), AngleAddr([ValueTerminal('<'),
AddrSpec([LocalPart([QuotedString([BareQuotedString([ValueTerminal('hello')])])]),
ValueTerminal('@'), Domain([DotAtom([DotAtomText([ValueTerminal('example'),
ValueTerminal('.'), ValueTerminal('org')])])])]), ValueTerminal('>')])])])])])
From: Nobody <"hello"@example.org>
If you prefer the string representation of the header's parsed value, you can
try:
print(msg.get('From').fold(policy=email.policy.default))
Which prints:
From: Nobody <""@example.org>
----------
_______________________________________
Python tracker <[email protected]>
<https://bugs.python.org/issue38232>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com