mirror of
https://github.com/python/cpython.git
synced 2025-11-02 03:01:58 +00:00
Big email 3.0 API changes, with updated unit tests and documentation.
Briefly (from the NEWS file):
- Updates for the email package:
+ All deprecated APIs that in email 2.x issued warnings have been removed:
_encoder argument to the MIMEText constructor, Message.add_payload(),
Utils.dump_address_pair(), Utils.decode(), Utils.encode()
+ New deprecations: Generator.__call__(), Message.get_type(),
Message.get_main_type(), Message.get_subtype(), the 'strict' argument to
the Parser constructor. These will be removed in email 3.1.
+ Support for Python earlier than 2.3 has been removed (see PEP 291).
+ All defect classes have been renamed to end in 'Defect'.
+ Some FeedParser fixes; also a MultipartInvariantViolationDefect will be
added to messages that claim to be multipart but really aren't.
+ Updates to documentation.
This commit is contained in:
parent
2cdd608601
commit
bb11386730
32 changed files with 438 additions and 452 deletions
|
|
@ -1,5 +1,6 @@
|
|||
# Copyright (C) 2001-2004 Python Software Foundation
|
||||
# Author: barry@python.org (Barry Warsaw)
|
||||
# Author: Barry Warsaw
|
||||
# Contact: email-sig@python.org
|
||||
|
||||
"""Miscellaneous utilities."""
|
||||
|
||||
|
|
@ -80,12 +81,6 @@ def formataddr(pair):
|
|||
return '%s%s%s <%s>' % (quotes, name, quotes, address)
|
||||
return address
|
||||
|
||||
# For backwards compatibility
|
||||
def dump_address_pair(pair):
|
||||
warnings.warn('Use email.Utils.formataddr() instead',
|
||||
DeprecationWarning, 2)
|
||||
return formataddr(pair)
|
||||
|
||||
|
||||
|
||||
def getaddresses(fieldvalues):
|
||||
|
|
@ -107,46 +102,6 @@ ecre = re.compile(r'''
|
|||
''', re.VERBOSE | re.IGNORECASE)
|
||||
|
||||
|
||||
def decode(s):
|
||||
"""Return a decoded string according to RFC 2047, as a unicode string.
|
||||
|
||||
NOTE: This function is deprecated. Use Header.decode_header() instead.
|
||||
"""
|
||||
warnings.warn('Use Header.decode_header() instead.', DeprecationWarning, 2)
|
||||
# Intra-package import here to avoid circular import problems.
|
||||
from email.Header import decode_header
|
||||
L = decode_header(s)
|
||||
if not isinstance(L, list):
|
||||
# s wasn't decoded
|
||||
return s
|
||||
|
||||
rtn = []
|
||||
for atom, charset in L:
|
||||
if charset is None:
|
||||
rtn.append(atom)
|
||||
else:
|
||||
# Convert the string to Unicode using the given encoding. Leave
|
||||
# Unicode conversion errors to strict.
|
||||
rtn.append(unicode(atom, charset))
|
||||
# Now that we've decoded everything, we just need to join all the parts
|
||||
# together into the final string.
|
||||
return UEMPTYSTRING.join(rtn)
|
||||
|
||||
|
||||
|
||||
def encode(s, charset='iso-8859-1', encoding='q'):
|
||||
"""Encode a string according to RFC 2047."""
|
||||
warnings.warn('Use Header.Header.encode() instead.', DeprecationWarning, 2)
|
||||
encoding = encoding.lower()
|
||||
if encoding == 'q':
|
||||
estr = _qencode(s)
|
||||
elif encoding == 'b':
|
||||
estr = _bencode(s)
|
||||
else:
|
||||
raise ValueError, 'Illegal encoding code: ' + encoding
|
||||
return '=?%s?%s?%s?=' % (charset.lower(), encoding, estr)
|
||||
|
||||
|
||||
|
||||
def formatdate(timeval=None, localtime=False):
|
||||
"""Returns a date string as specified by RFC 2822, e.g.:
|
||||
|
|
@ -179,7 +134,7 @@ def formatdate(timeval=None, localtime=False):
|
|||
sign = '-'
|
||||
else:
|
||||
sign = '+'
|
||||
zone = '%s%02d%02d' % (sign, hours, minutes / 60)
|
||||
zone = '%s%02d%02d' % (sign, hours, minutes // 60)
|
||||
else:
|
||||
now = time.gmtime(timeval)
|
||||
# Timezone offset is always -0000
|
||||
|
|
@ -314,3 +269,16 @@ def decode_params(params):
|
|||
new_params.append(
|
||||
(name, (charset, language, '"%s"' % quote(value))))
|
||||
return new_params
|
||||
|
||||
def collapse_rfc2231_value(value, errors='replace',
|
||||
fallback_charset='us-ascii'):
|
||||
if isinstance(value, tuple):
|
||||
rawval = unquote(value[2])
|
||||
charset = value[0] or 'us-ascii'
|
||||
try:
|
||||
return unicode(rawval, charset, errors)
|
||||
except LookupError:
|
||||
# XXX charset is unknown to Python.
|
||||
return unicode(rawval, fallback_charset, errors)
|
||||
else:
|
||||
return unquote(value)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue