[Python-checkins] r57360 - sandbox/trunk/emailpkg/5_0-exp/email/utils.py

barry.warsaw python-checkins at python.org
Fri Aug 24 00:11:09 CEST 2007


Author: barry.warsaw
Date: Fri Aug 24 00:11:08 2007
New Revision: 57360

Modified:
   sandbox/trunk/emailpkg/5_0-exp/email/utils.py
Log:
collapse_rfc2231_value(): Make this work with the existing test suite, though
perhaps the solution is dodgy because it requires a straight unicode -> bytes
conversion.


Modified: sandbox/trunk/emailpkg/5_0-exp/email/utils.py
==============================================================================
--- sandbox/trunk/emailpkg/5_0-exp/email/utils.py	(original)
+++ sandbox/trunk/emailpkg/5_0-exp/email/utils.py	Fri Aug 24 00:11:08 2007
@@ -311,13 +311,15 @@
 
 def collapse_rfc2231_value(value, errors='replace',
                            fallback_charset='us-ascii'):
-    if isinstance(value, tuple):
-        rawval = bytes(unquote(value[2]))
-        charset = value[0] or 'us-ascii'
-        try:
-            return str(rawval, charset, errors)
-        except LookupError:
-            # XXX charset is unknown to Python.
-            return str(rawval, fallback_charset, errors)
-    else:
+    if not isinstance(value, tuple) or len(value) != 3:
         return unquote(value)
+    # While value comes to us as a unicode string, we need it to be a bytes
+    # object.  We do not want bytes() normal utf-8 decoder, we want a straight
+    # interpretation of the string as character bytes.
+    charset, language, text = value
+    rawbytes = bytes(ord(c) for c in text)
+    try:
+        return str(rawbytes, charset, errors)
+    except LookupError:
+        # charset is not a known codec.
+        return unquote(text)


More information about the Python-checkins mailing list