[Python-checkins] r42279 - in python/branches/release23-maint/Lib/email: Message.py _compat21.py _compat22.py test/test_email.py

barry.warsaw python-checkins at python.org
Thu Feb 9 05:03:24 CET 2006


Author: barry.warsaw
Date: Thu Feb  9 05:03:22 2006
New Revision: 42279

Modified:
   python/branches/release23-maint/Lib/email/Message.py
   python/branches/release23-maint/Lib/email/_compat21.py
   python/branches/release23-maint/Lib/email/_compat22.py
   python/branches/release23-maint/Lib/email/test/test_email.py
Log:
Resolve SF bug 1409403: email.Message should supress warning from uu.decode.

However, the patch in that tracker item is elaborated such that the newly
included unit test pass on Python 2.1 through 2.5.  Note that Python 2.1's
uu.decode() does not have a 'quiet' argument, so we have to be sneaky.

Will port to email 3.0 (although without the backward compatible sneakiness).


Modified: python/branches/release23-maint/Lib/email/Message.py
==============================================================================
--- python/branches/release23-maint/Lib/email/Message.py	(original)
+++ python/branches/release23-maint/Lib/email/Message.py	Thu Feb  9 05:03:22 2006
@@ -23,6 +23,12 @@
     True = 1
     False = 0
 
+try:
+    from email._compat22 import quiet_uu_decode
+except SyntaxError:
+    from email._compat21 import quiet_uu_decode
+
+
 # Regular expression used to split header parameters.  BAW: this may be too
 # simple.  It isn't strictly RFC 2045 (section 5.1) compliant, but it catches
 # most headers found in the wild.  We may eventually need a full fledged
@@ -220,7 +226,7 @@
             elif cte in ('x-uuencode', 'uuencode', 'uue', 'x-uue'):
                 sfp = StringIO()
                 try:
-                    uu.decode(StringIO(payload+'\n'), sfp)
+                    quiet_uu_decode(StringIO(payload+'\n'), sfp, quiet=True)
                     payload = sfp.getvalue()
                 except uu.Error:
                     # Some decoding problem

Modified: python/branches/release23-maint/Lib/email/_compat21.py
==============================================================================
--- python/branches/release23-maint/Lib/email/_compat21.py	(original)
+++ python/branches/release23-maint/Lib/email/_compat21.py	Thu Feb  9 05:03:22 2006
@@ -1,8 +1,10 @@
-# Copyright (C) 2002 Python Software Foundation
-# Author: barry at zope.com
+# Copyright (C) 2002-2006 Python Software Foundation
+# Author: barry at python.org
 
-"""Module containing compatibility functions for Python 2.1.
-"""
+"""Module containing compatibility functions for Python 2.1."""
+
+import uu
+import sys
 
 from cStringIO import StringIO
 from types import StringType, UnicodeType
@@ -67,3 +69,14 @@
             if subtype is None or subpart.get_content_subtype() == subtype:
                 parts.append(subpart)
     return parts
+
+
+
+def quiet_uu_decode(in_file, out_file, quiet):
+    # In Python 2.1, uu.decode() does not support the quiet flag.  Cheat.
+    old_stderr = sys.stderr
+    try:
+        sys.stderr = StringIO()
+        uu.decode(in_file, out_file)
+    finally:
+        sys.stderr = old_stderr

Modified: python/branches/release23-maint/Lib/email/_compat22.py
==============================================================================
--- python/branches/release23-maint/Lib/email/_compat22.py	(original)
+++ python/branches/release23-maint/Lib/email/_compat22.py	Thu Feb  9 05:03:22 2006
@@ -1,7 +1,8 @@
-# Copyright (C) 2002 Python Software Foundation
-# Author: barry at zope.com
+# Copyright (C) 2002-2006 Python Software Foundation
+# Author: barry at python.org
 
-"""Module containing compatibility functions for Python 2.2.
+"""Module containing compatibility functions for Python 2.2 (and possibly
+beyond.
 """
 
 from __future__ import generators
@@ -9,6 +10,8 @@
 from cStringIO import StringIO
 from types import StringTypes
 
+import uu
+
 # Python 2.2.x where x < 1 lacks True/False
 try:
     True, False
@@ -68,3 +71,8 @@
         if subpart.get_content_maintype() == maintype:
             if subtype is None or subpart.get_content_subtype() == subtype:
                 yield subpart
+
+
+
+def quiet_uu_decode(in_file, out_file, quiet):
+    uu.decode(in_file, out_file, quiet=quiet)

Modified: python/branches/release23-maint/Lib/email/test/test_email.py
==============================================================================
--- python/branches/release23-maint/Lib/email/test/test_email.py	(original)
+++ python/branches/release23-maint/Lib/email/test/test_email.py	Thu Feb  9 05:03:22 2006
@@ -222,6 +222,19 @@
         msg.set_payload('foo')
         eq(msg.get_payload(decode=True), 'foo')
 
+    def test_decode_bogus_uu_payload_quietly(self):
+        msg = Message()
+        msg.set_payload('begin 664 foo.txt\n%<W1F=0000H \n \nend\n')
+        msg['Content-Transfer-Encoding'] = 'x-uuencode'
+        old_stderr = sys.stderr
+        try:
+            sys.stderr = sfp = StringIO()
+            # We don't care about the payload
+            msg.get_payload(decode=True)
+        finally:
+            sys.stderr = old_stderr
+        self.assertEqual(sfp.getvalue(), '')
+
     def test_decoded_generator(self):
         eq = self.assertEqual
         msg = self._msgobj('msg_07.txt')


More information about the Python-checkins mailing list