Skip to content

Commit dc74e34

Browse files
committed
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).
1 parent f5853f7 commit dc74e34

File tree

4 files changed

+48
-8
lines changed

4 files changed

+48
-8
lines changed

Lib/email/Message.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@
2323
True = 1
2424
False = 0
2525

26+
try:
27+
from email._compat22 import quiet_uu_decode
28+
except SyntaxError:
29+
from email._compat21 import quiet_uu_decode
30+
31+
2632
# Regular expression used to split header parameters. BAW: this may be too
2733
# simple. It isn't strictly RFC 2045 (section 5.1) compliant, but it catches
2834
# most headers found in the wild. We may eventually need a full fledged
@@ -220,7 +226,7 @@ def get_payload(self, i=None, decode=False):
220226
elif cte in ('x-uuencode', 'uuencode', 'uue', 'x-uue'):
221227
sfp = StringIO()
222228
try:
223-
uu.decode(StringIO(payload+'\n'), sfp)
229+
quiet_uu_decode(StringIO(payload+'\n'), sfp, quiet=True)
224230
payload = sfp.getvalue()
225231
except uu.Error:
226232
# Some decoding problem

Lib/email/_compat21.py

+17-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
# Copyright (C) 2002 Python Software Foundation
2-
# Author: barry@zope.com
1+
# Copyright (C) 2002-2006 Python Software Foundation
2+
# Author: barry@python.org
33

4-
"""Module containing compatibility functions for Python 2.1.
5-
"""
4+
"""Module containing compatibility functions for Python 2.1."""
5+
6+
import uu
7+
import sys
68

79
from cStringIO import StringIO
810
from types import StringType, UnicodeType
@@ -67,3 +69,14 @@ def typed_subpart_iterator(msg, maintype='text', subtype=None):
6769
if subtype is None or subpart.get_content_subtype() == subtype:
6870
parts.append(subpart)
6971
return parts
72+
73+
74+
75+
def quiet_uu_decode(in_file, out_file, quiet):
76+
# In Python 2.1, uu.decode() does not support the quiet flag. Cheat.
77+
old_stderr = sys.stderr
78+
try:
79+
sys.stderr = StringIO()
80+
uu.decode(in_file, out_file)
81+
finally:
82+
sys.stderr = old_stderr

Lib/email/_compat22.py

+11-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
1-
# Copyright (C) 2002 Python Software Foundation
2-
# Author: barry@zope.com
1+
# Copyright (C) 2002-2006 Python Software Foundation
2+
# Author: barry@python.org
33

4-
"""Module containing compatibility functions for Python 2.2.
4+
"""Module containing compatibility functions for Python 2.2 (and possibly
5+
beyond.
56
"""
67

78
from __future__ import generators
89
from __future__ import division
910
from cStringIO import StringIO
1011
from types import StringTypes
1112

13+
import uu
14+
1215
# Python 2.2.x where x < 1 lacks True/False
1316
try:
1417
True, False
@@ -68,3 +71,8 @@ def typed_subpart_iterator(msg, maintype='text', subtype=None):
6871
if subpart.get_content_maintype() == maintype:
6972
if subtype is None or subpart.get_content_subtype() == subtype:
7073
yield subpart
74+
75+
76+
77+
def quiet_uu_decode(in_file, out_file, quiet):
78+
uu.decode(in_file, out_file, quiet=quiet)

Lib/email/test/test_email.py

+13
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,19 @@ def test_get_decoded_uu_payload(self):
222222
msg.set_payload('foo')
223223
eq(msg.get_payload(decode=True), 'foo')
224224

225+
def test_decode_bogus_uu_payload_quietly(self):
226+
msg = Message()
227+
msg.set_payload('begin 664 foo.txt\n%<W1F=0000H \n \nend\n')
228+
msg['Content-Transfer-Encoding'] = 'x-uuencode'
229+
old_stderr = sys.stderr
230+
try:
231+
sys.stderr = sfp = StringIO()
232+
# We don't care about the payload
233+
msg.get_payload(decode=True)
234+
finally:
235+
sys.stderr = old_stderr
236+
self.assertEqual(sfp.getvalue(), '')
237+
225238
def test_decoded_generator(self):
226239
eq = self.assertEqual
227240
msg = self._msgobj('msg_07.txt')

0 commit comments

Comments
 (0)