Commit dcaf2ece authored by R David Murray's avatar R David Murray

#12586: Fix a small oversight in the new email policy header setting code.

This is a danger of focusing on unit tests: sometimes you forget
to do the integration tests.
parent b5267631
...@@ -104,7 +104,7 @@ class EmailPolicy(Policy): ...@@ -104,7 +104,7 @@ class EmailPolicy(Policy):
""" """
if hasattr(value, 'name') and value.name.lower() == name.lower(): if hasattr(value, 'name') and value.name.lower() == name.lower():
return (name, value) return (name, value)
if len(value.splitlines())>1: if isinstance(value, str) and len(value.splitlines())>1:
raise ValueError("Header values may not contain linefeed " raise ValueError("Header values may not contain linefeed "
"or carriage return characters") "or carriage return characters")
return (name, self.header_factory(name, value)) return (name, self.header_factory(name, value))
......
...@@ -3,6 +3,7 @@ import textwrap ...@@ -3,6 +3,7 @@ import textwrap
import unittest import unittest
from email import errors from email import errors
from email import policy from email import policy
from email.message import Message
from test.test_email import TestEmailBase from test.test_email import TestEmailBase
from email import _headerregistry from email import _headerregistry
# Address and Group are public but I'm not sure where to put them yet. # Address and Group are public but I'm not sure where to put them yet.
...@@ -168,6 +169,12 @@ class TestDateHeader(TestHeaderBase): ...@@ -168,6 +169,12 @@ class TestDateHeader(TestHeaderBase):
with self.assertRaises(AttributeError): with self.assertRaises(AttributeError):
h.datetime = 'foo' h.datetime = 'foo'
def test_set_date_header_from_datetime(self):
m = Message(policy=policy.default)
m['Date'] = self.dt
self.assertEqual(m['Date'], self.datestring)
self.assertEqual(m['Date'].datetime, self.dt)
class TestAddressHeader(TestHeaderBase): class TestAddressHeader(TestHeaderBase):
...@@ -625,6 +632,20 @@ class TestAddressAndGroup(TestEmailBase): ...@@ -625,6 +632,20 @@ class TestAddressAndGroup(TestEmailBase):
self.assertEqual(g.addresses, tuple()) self.assertEqual(g.addresses, tuple())
self.assertEqual(str(g), 'foo bar:;') self.assertEqual(str(g), 'foo bar:;')
def test_set_message_header_from_address(self):
a = Address('foo', 'bar', 'example.com')
m = Message(policy=policy.default)
m['To'] = a
self.assertEqual(m['to'], 'foo <bar@example.com>')
self.assertEqual(m['to'].addresses, (a,))
def test_set_message_header_from_group(self):
g = Group('foo bar')
m = Message(policy=policy.default)
m['To'] = g
self.assertEqual(m['to'], 'foo bar:;')
self.assertEqual(m['to'].addresses, g.addresses)
class TestFolding(TestHeaderBase): class TestFolding(TestHeaderBase):
...@@ -713,5 +734,6 @@ class TestFolding(TestHeaderBase): ...@@ -713,5 +734,6 @@ class TestFolding(TestHeaderBase):
'Date: Sat, 02 Feb 2002 17:00:06 -0800\n') 'Date: Sat, 02 Feb 2002 17:00:06 -0800\n')
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment