Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Doc/library/email.policy.rst
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,11 @@ added matters. To illustrate::
values as the current instance, except where those attributes are
given new values by the keyword arguments.

This method is also used by :func:`copy.replace`.

.. versionchanged:: next
Added support for :func:`copy.replace`.


The remaining :class:`Policy` methods are called by the email package code,
and are not intended to be called by an application using the email package.
Expand Down
2 changes: 2 additions & 0 deletions Lib/email/_policybase.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ def clone(self, **kw):
object.__setattr__(newpolicy, attr, value)
return newpolicy

__replace__ = clone

def __setattr__(self, name, value):
if hasattr(self, name):
msg = "{!r} object attribute {!r} is read-only"
Expand Down
11 changes: 11 additions & 0 deletions Lib/test/test_email/test_policy.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import copy
import io
import types
import textwrap
Expand Down Expand Up @@ -254,6 +255,16 @@ def test_clone_copies_factory(self):
h = policy2.header_factory('foo', 'test')
self.assertIsInstance(h, self.Foo)

def test_copy_replace(self):
policy = email.policy.default
new = copy.replace(policy, max_line_length=100)
self.assertIsInstance(new, type(policy))
self.assertEqual(new.max_line_length, 100)
self.assertEqual(new.linesep, policy.linesep)
self.assertEqual(policy.max_line_length, 78)
with self.assertRaises(TypeError):
copy.replace(policy, no_such_attribute=1)

def test_new_factory_overrides_default(self):
mypolicy = email.policy.EmailPolicy()
myfactory = mypolicy.header_factory
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
:class:`email.policy.Policy` objects now support :func:`copy.replace`.
Loading