Skip to content

Commit 7886083

Browse files
gh-155042: Support copy.replace() for email.policy.Policy
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
1 parent 7d76013 commit 7886083

4 files changed

Lines changed: 19 additions & 0 deletions

File tree

Doc/library/email.policy.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,11 @@ added matters. To illustrate::
254254
values as the current instance, except where those attributes are
255255
given new values by the keyword arguments.
256256

257+
This method is also used by :func:`copy.replace`.
258+
259+
.. versionchanged:: next
260+
Added support of :func:`copy.replace`.
261+
257262

258263
The remaining :class:`Policy` methods are called by the email package code,
259264
and are not intended to be called by an application using the email package.

Lib/email/_policybase.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ def clone(self, **kw):
8484
object.__setattr__(newpolicy, attr, value)
8585
return newpolicy
8686

87+
__replace__ = clone
88+
8789
def __setattr__(self, name, value):
8890
if hasattr(self, name):
8991
msg = "{!r} object attribute {!r} is read-only"

Lib/test/test_email/test_policy.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import copy
12
import io
23
import types
34
import textwrap
@@ -254,6 +255,16 @@ def test_clone_copies_factory(self):
254255
h = policy2.header_factory('foo', 'test')
255256
self.assertIsInstance(h, self.Foo)
256257

258+
def test_copy_replace(self):
259+
policy = email.policy.default
260+
new = copy.replace(policy, max_line_length=100)
261+
self.assertIsInstance(new, type(policy))
262+
self.assertEqual(new.max_line_length, 100)
263+
self.assertEqual(new.linesep, policy.linesep)
264+
self.assertEqual(policy.max_line_length, 78)
265+
with self.assertRaises(TypeError):
266+
copy.replace(policy, no_such_attribute=1)
267+
257268
def test_new_factory_overrides_default(self):
258269
mypolicy = email.policy.EmailPolicy()
259270
myfactory = mypolicy.header_factory
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
:class:`email.policy.Policy` objects now support :func:`copy.replace`.

0 commit comments

Comments
 (0)