cpython: a3df1c24d586 (original) (raw)
Mercurial > cpython
changeset 92494:a3df1c24d586 3.4
#21091: make is_attachment a method. Since EmailMessage is a provisional API we can fix API bugs in a maintenance release, but I used a trick suggested by Serhiy to maintain backward compatibility with 3.4.0/1. [#21091]
R David Murray rdmurray@bitdance.com | |
---|---|
date | Sat, 20 Sep 2014 18:05:28 -0400 |
parents | 0044ed0af96f |
children | f7aff40609e7 7af0315bdfe0 |
files | Doc/library/email.contentmanager.rst Lib/email/message.py Lib/test/test_email/test_message.py Misc/NEWS |
diffstat | 4 files changed, 43 insertions(+), 10 deletions(-)[+] [-] Doc/library/email.contentmanager.rst 8 Lib/email/message.py 20 Lib/test/test_email/test_message.py 20 Misc/NEWS 5 |
line wrap: on
line diff
--- a/Doc/library/email.contentmanager.rst +++ b/Doc/library/email.contentmanager.rst @@ -70,11 +70,15 @@ this module. the following methods:
Set to ``True`` if there is a :mailheader:`Content-Disposition` header[](#l1.10)
Return ``True`` if there is a :mailheader:`Content-Disposition` header[](#l1.11) and its (case insensitive) value is ``attachment``, ``False`` otherwise.[](#l1.12)
.. versionchanged:: 3.4.2[](#l1.14)
is_attachment is now a method instead of a property, for consistency[](#l1.15)
with :meth:`~email.message.Message.is_multipart`.[](#l1.16)
+ .. method:: get_body(preferencelist=('related', 'html', 'plain'))
--- a/Lib/email/message.py +++ b/Lib/email/message.py @@ -9,6 +9,7 @@ import re import uu import quopri +import warnings from io import BytesIO, StringIO
Intrapackage imports
@@ -929,6 +930,17 @@ class Message: # I.e. def walk(self): ... from email.iterators import walk +# XXX Support for temporary deprecation hack for is_attachment property. +class _IsAttachment:
- def init(self, value):
self.value = value[](#l2.18)
- def call(self):
return self.value[](#l2.20)
- def bool(self):
warnings.warn("is_attachment will be a method, not a property, in 3.5",[](#l2.22)
DeprecationWarning,[](#l2.23)
stacklevel=3)[](#l2.24)
return self.value[](#l2.25)
class MIMEPart(Message): @@ -941,10 +953,12 @@ class MIMEPart(Message): @property def is_attachment(self): c_d = self.get('content-disposition')
return False if c_d is None else c_d.content_disposition == 'attachment'[](#l2.33)
result = False if c_d is None else c_d.content_disposition == 'attachment'[](#l2.34)
# XXX transitional hack to raise deprecation if not called.[](#l2.35)
return _IsAttachment(result)[](#l2.36)
def _find_body(self, part, preferencelist):
if part.is_attachment:[](#l2.39)
if part.is_attachment():[](#l2.40) return[](#l2.41) maintype, subtype = part.get_content_type().split('/')[](#l2.42) if maintype == 'text':[](#l2.43)
@@ -1037,7 +1051,7 @@ class MIMEPart(Message): for part in parts: maintype, subtype = part.get_content_type().split('/') if ((maintype, subtype) in self._body_types and
not part.is_attachment and subtype not in seen):[](#l2.48)
not part.is_attachment() and subtype not in seen):[](#l2.49) seen.append(subtype)[](#l2.50) continue[](#l2.51) yield part[](#l2.52)
--- a/Lib/test/test_email/test_message.py +++ b/Lib/test/test_email/test_message.py @@ -722,15 +722,25 @@ class TestEmailMessageBase: def test_is_attachment(self): m = self._make_message()
self.assertFalse(m.is_attachment)[](#l3.7)
self.assertFalse(m.is_attachment())[](#l3.8)
with self.assertWarns(DeprecationWarning):[](#l3.9)
self.assertFalse(m.is_attachment)[](#l3.10) m['Content-Disposition'] = 'inline'[](#l3.11)
self.assertFalse(m.is_attachment)[](#l3.12)
self.assertFalse(m.is_attachment())[](#l3.13)
with self.assertWarns(DeprecationWarning):[](#l3.14)
self.assertFalse(m.is_attachment)[](#l3.15) m.replace_header('Content-Disposition', 'attachment')[](#l3.16)
self.assertTrue(m.is_attachment)[](#l3.17)
self.assertTrue(m.is_attachment())[](#l3.18)
with self.assertWarns(DeprecationWarning):[](#l3.19)
self.assertTrue(m.is_attachment)[](#l3.20) m.replace_header('Content-Disposition', 'AtTachMent')[](#l3.21)
self.assertTrue(m.is_attachment)[](#l3.22)
self.assertTrue(m.is_attachment())[](#l3.23)
with self.assertWarns(DeprecationWarning):[](#l3.24)
self.assertTrue(m.is_attachment)[](#l3.25) m.set_param('filename', 'abc.png', 'Content-Disposition')[](#l3.26)
self.assertTrue(m.is_attachment)[](#l3.27)
self.assertTrue(m.is_attachment())[](#l3.28)
with self.assertWarns(DeprecationWarning):[](#l3.29)
self.assertTrue(m.is_attachment)[](#l3.30)
class TestEmailMessage(TestEmailMessageBase, TestEmailBase):
--- a/Misc/NEWS +++ b/Misc/NEWS @@ -32,6 +32,11 @@ Core and Builtins Library ------- +- Issue #21091: Fix API bug: email.message.EmailMessage.is_attachment is now