[Email-SIG] Generator.HeaderParsedGenerator

Jason R. Mastaler jason at mastaler.com
Sun Oct 5 02:51:27 EDT 2003


Can the attached patch be considered for inclusion in email?  This
issue is a former mimelib tracker item, but those trackers are now
disabled.  I've included the previous commentary leading to the patch
below.  FWIW, we've been using this in TMDA successfully for months
now.

----------------------------------------------------------------------

Category: None
Group: None
Status: Open
Priority: 5
Submitted By: Jason R. Mastaler (jasonrm)
>Assigned to: Barry A. Warsaw (bwarsaw)
Summary: TypeError: 0 with Message.as_string()

Initial Comment:
Trying to flatten the attached message (crashes-tmda2.txt)
results in a "TypeError: 0" exception.  See the attached
typeerror.txt for how to reproduce the error.

Is this a bug in email?  If not, is there a better way
to handle
this than simply "TypeError: 0"?


----------------------------------------------------------------------

Comment By: Jason R. Mastaler (jasonrm)
Date: 2003-07-28 14:56

Message:
Logged In: YES 
user_id=85984

Barry, I've now uploaded the patch (hdrgen.diff).


----------------------------------------------------------------------

Comment By: Barry A. Warsaw (bwarsaw)
Date: 2003-07-25 06:40

Message:
Logged In: YES 
user_id=12800

In general, please upload patches instead of pasting them
into a comment field, otherwise they're basically unusable.

----------------------------------------------------------------------

Comment By: Timothy Legant (tlegant)
Date: 2003-07-24 19:00

Message:
Logged In: YES 
user_id=435234

Would the following work for a HeaderParsedGenerator?  It 
seems to work fine here, but perhaps someone with a more 
intimate knowledge of the email package will spot something 
that could trip this up.  Seems simple enough, though...

The diff is against CVS Generator.py.  Apologies in advance if 
the comment entry system wraps the diff. :(

Index: Generator.py
===================================================================
RCS file:
/cvsroot/python/python/dist/src/Lib/email/Generator.py,v
retrieving revision 1.21
diff -u -r1.21 Generator.py
--- Generator.py	24 Jun 2003 20:19:34 -0000	1.21
+++ Generator.py	25 Jul 2003 00:57:00 -0000
@@ -356,6 +356,26 @@
 
 
 
+class HeaderParsedGenerator(Generator):
+    """Generate text from a Message created by HeaderParser.
+
+    Header is generated as usual (by Generator).  The
payload of a Message
+    created by HeaderParser is a raw string.  No encoding
is necessary.  If it
+    came in valid, it goes out valid.  Conversely, if it
came in bogus, it goes
+    out bogus.
+    """
+    def _dispatch(self, msg):
+        payload = msg.get_payload()
+        if payload is None:
+            return
+        if not _isstring(payload):
+            raise TypeError, 'string payload expected: %s'
% type(payload)
+        if self._mangle_from_:
+            payload = fcre.sub('>From ', payload)
+        self._fp.write(payload)
+
+
+
 # Helper
 _width = len(repr(sys.maxint-1))
 _fmt = '%%0%dd' % _width


----------------------------------------------------------------------

Comment By: Barry A. Warsaw (bwarsaw)
Date: 2003-06-20 15:05

Message:
Logged In: YES 
user_id=12800

Moving this to version 3.0 feature requests.  Specifically,
add a Generator that can handle HeaderParser parsed messages.

----------------------------------------------------------------------

Comment By: Timo C. Metzemakers (tcmetzemakers)
Date: 2003-06-19 02:18

Message:
Logged In: YES 
user_id=804319

I just got bitten by this, too, and I'd like to suggest that this 
might be a documentation bug.

If you're a casual user like myself, and you just want to 
examine and/or modify a message's headers before passing it 
on, it only seems natural to use the HeaderParser, do your 
thing, and then call the as_string method.

A short note in the documentation about this would be useful, 
IMHO.  A ready-to-use Generator subclass that does what 
Barry says would be nice to have, too.


----------------------------------------------------------------------

Comment By: Barry A. Warsaw (bwarsaw)
Date: 2003-06-10 10:32

Message:
Logged In: YES 
user_id=12800

Good idea.  Done.

----------------------------------------------------------------------

Comment By: Anthony Baxter (anthonybaxter)
Date: 2003-06-10 03:20

Message:
Logged In: YES 
user_id=29957

Assuming this is the same problem I hit, the following fix
to Message.py means you get something more than 'TypeError: n'.
         elif not isinstance(self._payload, ListType):
-            raise TypeError, i
+            raise TypeError, "Expected list, got
%s"%type(self._payload)



----------------------------------------------------------------------

Comment By: Barry A. Warsaw (bwarsaw)
Date: 2003-06-09 14:01

Message:
Logged In: YES 
user_id=12800

It's not a bug.  This is caused by the use of the default
Generator with a message parsed by the HeaderParser. 
Generator flattens by looking at the Content-Type headers of
the constituent parts.  It expects to see a message object
model that jives with the Content-Type headers.  But your
model doesn't because you've got a message/rfc822 content
type with a string payload.

You should probably use a Generator subclass that overrides
_dispatch().

----------------------------------------------------------------------

-------------- next part --------------
A non-text attachment was scrubbed...
Name: hdrgen.diff
Type: text/x-patch
Size: 1120 bytes
Desc: not available
Url : http://mail.python.org/pipermail/email-sig/attachments/20031005/23bee402/hdrgen.bin


More information about the Email-SIG mailing list