mirror of https://github.com/python/cpython
#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.
This commit is contained in:
parent
97dfad7856
commit
8a97896a76
|
@ -70,11 +70,15 @@ this module.
|
||||||
the following methods:
|
the following methods:
|
||||||
|
|
||||||
|
|
||||||
.. attribute:: is_attachment
|
.. method:: is_attachment
|
||||||
|
|
||||||
Set to ``True`` if there is a :mailheader:`Content-Disposition` header
|
Return ``True`` if there is a :mailheader:`Content-Disposition` header
|
||||||
and its (case insensitive) value is ``attachment``, ``False`` otherwise.
|
and its (case insensitive) value is ``attachment``, ``False`` otherwise.
|
||||||
|
|
||||||
|
.. versionchanged:: 3.4.2
|
||||||
|
is_attachment is now a method instead of a property, for consistency
|
||||||
|
with :meth:`~email.message.Message.is_multipart`.
|
||||||
|
|
||||||
|
|
||||||
.. method:: get_body(preferencelist=('related', 'html', 'plain'))
|
.. method:: get_body(preferencelist=('related', 'html', 'plain'))
|
||||||
|
|
||||||
|
|
|
@ -9,6 +9,7 @@ __all__ = ['Message']
|
||||||
import re
|
import re
|
||||||
import uu
|
import uu
|
||||||
import quopri
|
import quopri
|
||||||
|
import warnings
|
||||||
from io import BytesIO, StringIO
|
from io import BytesIO, StringIO
|
||||||
|
|
||||||
# Intrapackage imports
|
# Intrapackage imports
|
||||||
|
@ -929,6 +930,17 @@ class Message:
|
||||||
# I.e. def walk(self): ...
|
# I.e. def walk(self): ...
|
||||||
from email.iterators import walk
|
from email.iterators import walk
|
||||||
|
|
||||||
|
# XXX Support for temporary deprecation hack for is_attachment property.
|
||||||
|
class _IsAttachment:
|
||||||
|
def __init__(self, value):
|
||||||
|
self.value = value
|
||||||
|
def __call__(self):
|
||||||
|
return self.value
|
||||||
|
def __bool__(self):
|
||||||
|
warnings.warn("is_attachment will be a method, not a property, in 3.5",
|
||||||
|
DeprecationWarning,
|
||||||
|
stacklevel=3)
|
||||||
|
return self.value
|
||||||
|
|
||||||
class MIMEPart(Message):
|
class MIMEPart(Message):
|
||||||
|
|
||||||
|
@ -941,10 +953,12 @@ class MIMEPart(Message):
|
||||||
@property
|
@property
|
||||||
def is_attachment(self):
|
def is_attachment(self):
|
||||||
c_d = self.get('content-disposition')
|
c_d = self.get('content-disposition')
|
||||||
return False if c_d is None else c_d.content_disposition == 'attachment'
|
result = False if c_d is None else c_d.content_disposition == 'attachment'
|
||||||
|
# XXX transitional hack to raise deprecation if not called.
|
||||||
|
return _IsAttachment(result)
|
||||||
|
|
||||||
def _find_body(self, part, preferencelist):
|
def _find_body(self, part, preferencelist):
|
||||||
if part.is_attachment:
|
if part.is_attachment():
|
||||||
return
|
return
|
||||||
maintype, subtype = part.get_content_type().split('/')
|
maintype, subtype = part.get_content_type().split('/')
|
||||||
if maintype == 'text':
|
if maintype == 'text':
|
||||||
|
@ -1037,7 +1051,7 @@ class MIMEPart(Message):
|
||||||
for part in parts:
|
for part in parts:
|
||||||
maintype, subtype = part.get_content_type().split('/')
|
maintype, subtype = part.get_content_type().split('/')
|
||||||
if ((maintype, subtype) in self._body_types and
|
if ((maintype, subtype) in self._body_types and
|
||||||
not part.is_attachment and subtype not in seen):
|
not part.is_attachment() and subtype not in seen):
|
||||||
seen.append(subtype)
|
seen.append(subtype)
|
||||||
continue
|
continue
|
||||||
yield part
|
yield part
|
||||||
|
|
|
@ -722,14 +722,24 @@ class TestEmailMessageBase:
|
||||||
|
|
||||||
def test_is_attachment(self):
|
def test_is_attachment(self):
|
||||||
m = self._make_message()
|
m = self._make_message()
|
||||||
|
self.assertFalse(m.is_attachment())
|
||||||
|
with self.assertWarns(DeprecationWarning):
|
||||||
self.assertFalse(m.is_attachment)
|
self.assertFalse(m.is_attachment)
|
||||||
m['Content-Disposition'] = 'inline'
|
m['Content-Disposition'] = 'inline'
|
||||||
|
self.assertFalse(m.is_attachment())
|
||||||
|
with self.assertWarns(DeprecationWarning):
|
||||||
self.assertFalse(m.is_attachment)
|
self.assertFalse(m.is_attachment)
|
||||||
m.replace_header('Content-Disposition', 'attachment')
|
m.replace_header('Content-Disposition', 'attachment')
|
||||||
|
self.assertTrue(m.is_attachment())
|
||||||
|
with self.assertWarns(DeprecationWarning):
|
||||||
self.assertTrue(m.is_attachment)
|
self.assertTrue(m.is_attachment)
|
||||||
m.replace_header('Content-Disposition', 'AtTachMent')
|
m.replace_header('Content-Disposition', 'AtTachMent')
|
||||||
|
self.assertTrue(m.is_attachment())
|
||||||
|
with self.assertWarns(DeprecationWarning):
|
||||||
self.assertTrue(m.is_attachment)
|
self.assertTrue(m.is_attachment)
|
||||||
m.set_param('filename', 'abc.png', 'Content-Disposition')
|
m.set_param('filename', 'abc.png', 'Content-Disposition')
|
||||||
|
self.assertTrue(m.is_attachment())
|
||||||
|
with self.assertWarns(DeprecationWarning):
|
||||||
self.assertTrue(m.is_attachment)
|
self.assertTrue(m.is_attachment)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -32,6 +32,11 @@ Core and Builtins
|
||||||
Library
|
Library
|
||||||
-------
|
-------
|
||||||
|
|
||||||
|
- Issue #21091: Fix API bug: email.message.EmailMessage.is_attachment is now
|
||||||
|
a method. Since EmailMessage is provisional, we can change the API in a
|
||||||
|
maintenance release, but we use a trick to remain backward compatible with
|
||||||
|
3.4.0/1.
|
||||||
|
|
||||||
- Issue #21079: Fix email.message.EmailMessage.is_attachment to return the
|
- Issue #21079: Fix email.message.EmailMessage.is_attachment to return the
|
||||||
correct result when the header has parameters as well as a value.
|
correct result when the header has parameters as well as a value.
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue