diff options
author | Barry Warsaw <barry@python.org> | 2002-09-06 03:38:12 (GMT) |
---|---|---|
committer | Barry Warsaw <barry@python.org> | 2002-09-06 03:38:12 (GMT) |
commit | 229727fa0738562c22b7bdc672a8762886020eac (patch) | |
tree | c9cafc75f54841f98bb3c4dd545f275d1b0adc8c | |
parent | b567392bbf3c595a47b86ea4d6038af0ec28b383 (diff) | |
download | cpython-229727fa0738562c22b7bdc672a8762886020eac.zip cpython-229727fa0738562c22b7bdc672a8762886020eac.tar.gz cpython-229727fa0738562c22b7bdc672a8762886020eac.tar.bz2 |
replace_header(): New method given by Skip Montanaro in SF patch
#601959. Modified slightly by Barry (who added the KeyError in case
the header is missing.
-rw-r--r-- | Lib/email/Message.py | 16 |
1 files changed, 15 insertions, 1 deletions
diff --git a/Lib/email/Message.py b/Lib/email/Message.py index 65e3c6d..0a8d90b 100644 --- a/Lib/email/Message.py +++ b/Lib/email/Message.py @@ -350,7 +350,6 @@ class Message: Example: msg.add_header('content-disposition', 'attachment', filename='bud.gif') - """ parts = [] for k, v in _params.items(): @@ -362,6 +361,21 @@ class Message: parts.insert(0, _value) self._headers.append((_name, SEMISPACE.join(parts))) + def replace_header(self, _name, _value): + """Replace a header. + + Replace the first matching header found in the message, retaining + header order and case. If no matching header was found, a KeyError is + raised. + """ + _name = _name.lower() + for i, (k, v) in zip(range(len(self._headers)), self._headers): + if k.lower() == _name: + self._headers[i] = (k, _value) + break + else: + raise KeyError, _name + # # These methods are silently deprecated in favor of get_content_type() and # friends (see below). They will be noisily deprecated in email 3.0. |