summaryrefslogtreecommitdiffstats
path: root/Doc
diff options
context:
space:
mode:
authorR David Murray <rdmurray@bitdance.com>2012-05-29 13:14:44 (GMT)
committerR David Murray <rdmurray@bitdance.com>2012-05-29 13:14:44 (GMT)
commitabfc37491cecb0a052d54c3bf0525ac140e94f7e (patch)
tree90ffe814aff0fd3f921e29e619582236f6f3ebe5 /Doc
parent6066fe1258d7d8dcec7459267f159d5d73d920e2 (diff)
downloadcpython-abfc37491cecb0a052d54c3bf0525ac140e94f7e.zip
cpython-abfc37491cecb0a052d54c3bf0525ac140e94f7e.tar.gz
cpython-abfc37491cecb0a052d54c3bf0525ac140e94f7e.tar.bz2
#10839: raise an error on add of duplicate unique headers in new email policies
This feature was supposed to be part of the initial email6 checkin, but it got lost in my big refactoring. In this patch I'm not providing an easy way to turn off the errors, but they only happen when a header is added programmatically, and it is almost never the right thing to do to allow the duplicate to be added. An application that needs to add duplicates of unique headers can create a policy subclass to allow it.
Diffstat (limited to 'Doc')
-rw-r--r--Doc/library/email.policy.rst25
1 files changed, 25 insertions, 0 deletions
diff --git a/Doc/library/email.policy.rst b/Doc/library/email.policy.rst
index 9ab949e..31b13c1 100644
--- a/Doc/library/email.policy.rst
+++ b/Doc/library/email.policy.rst
@@ -196,6 +196,25 @@ added matters. To illustrate::
custom ``Message`` objects) should also provide such an attribute,
otherwise defects in parsed messages will raise unexpected errors.
+ .. method:: header_max_count(name)
+
+ Return the maximum allowed number of headers named *name*.
+
+ Called when a header is added to a :class:`~email.message.Message`
+ object. If the returned value is not ``0`` or ``None``, and there are
+ already a number of headers with the name *name* equal to the value
+ returned, a :exc:`ValueError` is raised.
+
+ Because the default behavior of ``Message.__setitem__`` is to append the
+ value to the list of headers, it is easy to create duplicate headers
+ without realizing it. This method allows certain headers to be limited
+ in the number of instances of that header that may be added to a
+ ``Message`` programmatically. (The limit is not observed by the parser,
+ which will faithfully produce as many headers as exist in the message
+ being parsed.)
+
+ The default implementation returns ``None`` for all header names.
+
.. method:: header_source_parse(sourcelines)
The email package calls this method with a list of strings, each string
@@ -366,6 +385,12 @@ added matters. To illustrate::
The class provides the following concrete implementations of the abstract
methods of :class:`Policy`:
+ .. method:: header_max_count(name)
+
+ Returns the value of the
+ :attr:`~email.headerregistry.BaseHeader.max_count` attribute of the
+ specialized class used to represent the header with the given name.
+
.. method:: header_source_parse(sourcelines)
The implementation of this method is the same as that for the