summaryrefslogtreecommitdiffstats
path: root/Lib/email
diff options
context:
space:
mode:
authorR. David Murray <rdmurray@bitdance.com>2010-10-02 15:58:26 (GMT)
committerR. David Murray <rdmurray@bitdance.com>2010-10-02 15:58:26 (GMT)
commit5397e862e2bae29b278cf8756761764e06441db4 (patch)
tree94d9416ac02051a5617ae03c73162266c34ecc88 /Lib/email
parent8f7bcb3f8557ebae74a710610aeb4db63421e496 (diff)
downloadcpython-5397e862e2bae29b278cf8756761764e06441db4.zip
cpython-5397e862e2bae29b278cf8756761764e06441db4.tar.gz
cpython-5397e862e2bae29b278cf8756761764e06441db4.tar.bz2
#1050268: make parseaddr 'quote' the contents of quoted strings in addresses.
Also made the doc string for email._parseaddr's 'quote' function more accurate; I'd love to make the function match the old docstring instead, but other code uses it according the existing semantics.
Diffstat (limited to 'Lib/email')
-rw-r--r--Lib/email/_parseaddr.py9
-rw-r--r--Lib/email/test/test_email.py18
2 files changed, 25 insertions, 2 deletions
diff --git a/Lib/email/_parseaddr.py b/Lib/email/_parseaddr.py
index ac2e524..3bd4ba4 100644
--- a/Lib/email/_parseaddr.py
+++ b/Lib/email/_parseaddr.py
@@ -160,7 +160,12 @@ def mktime_tz(data):
def quote(str):
- """Add quotes around a string."""
+ """Prepare string to be used in a quoted string.
+
+ Turns backslash and double quote characters into quoted pairs. These
+ are the only characters that need to be quoted inside a quoted string.
+ Does not add the surrounding double quotes.
+ """
return str.replace('\\', '\\\\').replace('"', '\\"')
@@ -318,7 +323,7 @@ class AddrlistClass:
aslist.append('.')
self.pos += 1
elif self.field[self.pos] == '"':
- aslist.append('"%s"' % self.getquote())
+ aslist.append('"%s"' % quote(self.getquote()))
elif self.field[self.pos] in self.atomends:
break
else:
diff --git a/Lib/email/test/test_email.py b/Lib/email/test/test_email.py
index b6e4831..8f95125 100644
--- a/Lib/email/test/test_email.py
+++ b/Lib/email/test/test_email.py
@@ -2287,6 +2287,24 @@ class TestMiscellaneous(TestEmailBase):
# formataddr() quotes the name if there's a dot in it
self.assertEqual(utils.formataddr((a, b)), y)
+ def test_parseaddr_preserves_quoted_pairs_in_addresses(self):
+ # issue 10005. Note that in the third test the second pair of
+ # backslashes is not actually a quoted pair because it is not inside a
+ # comment or quoted string: the address being parsed has a quoted
+ # string containing a quoted backslash, followed by 'example' and two
+ # backslashes, followed by another quoted string containing a space and
+ # the word 'example'. parseaddr copies those two backslashes
+ # literally. Per rfc5322 this is not technically correct since a \ may
+ # not appear in an address outside of a quoted string. It is probably
+ # a sensible Postel interpretation, though.
+ eq = self.assertEqual
+ eq(utils.parseaddr('""example" example"@example.com'),
+ ('', '""example" example"@example.com'))
+ eq(utils.parseaddr('"\\"example\\" example"@example.com'),
+ ('', '"\\"example\\" example"@example.com'))
+ eq(utils.parseaddr('"\\\\"example\\\\" example"@example.com'),
+ ('', '"\\\\"example\\\\" example"@example.com'))
+
def test_multiline_from_comment(self):
x = """\
Foo