summaryrefslogtreecommitdiffstats
path: root/Lib/email/Utils.py
diff options
context:
space:
mode:
authorBarry Warsaw <barry@python.org>2004-10-03 03:16:19 (GMT)
committerBarry Warsaw <barry@python.org>2004-10-03 03:16:19 (GMT)
commitbb113867305f8ab70947bffb77961a60d10730dc (patch)
tree0af1fbf0fbbd95170636205343ba827cf768bb38 /Lib/email/Utils.py
parent2cdd608601071df8e557beaaa78b54884c80e8de (diff)
downloadcpython-bb113867305f8ab70947bffb77961a60d10730dc.zip
cpython-bb113867305f8ab70947bffb77961a60d10730dc.tar.gz
cpython-bb113867305f8ab70947bffb77961a60d10730dc.tar.bz2
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.
Diffstat (limited to 'Lib/email/Utils.py')
-rw-r--r--Lib/email/Utils.py64
1 files changed, 16 insertions, 48 deletions
diff --git a/Lib/email/Utils.py b/Lib/email/Utils.py
index 3a4bbc8..e786d26 100644
--- a/Lib/email/Utils.py
+++ b/Lib/email/Utils.py
@@ -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)