From abfc37491cecb0a052d54c3bf0525ac140e94f7e Mon Sep 17 00:00:00 2001 From: R David Murray Date: Tue, 29 May 2012 09:14:44 -0400 Subject: #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. --- Lib/email/message.py | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'Lib/email/message.py') diff --git a/Lib/email/message.py b/Lib/email/message.py index 62b82b79c1..9b06207d14 100644 --- a/Lib/email/message.py +++ b/Lib/email/message.py @@ -346,6 +346,16 @@ class Message: Note: this does not overwrite an existing header with the same field name. Use __delitem__() first to delete any existing headers. """ + max_count = self.policy.header_max_count(name) + if max_count: + lname = name.lower() + found = 0 + for k, v in self._headers: + if k.lower() == lname: + found += 1 + if found >= max_count: + raise ValueError("There may be at most {} {} headers " + "in a message".format(max_count, name)) self._headers.append(self.policy.header_store_parse(name, val)) def __delitem__(self, name): -- cgit v1.2.1