Re: fixing the current email module

Barry Warsaw <[email protected]> Thu, 8 Oct 2009 09:30:12 -0400
Newsgroups gmane.comp.python.mime.devel
Message-ID <[email protected]>
On Oct 8, 2009, at 8:31 AM, Oleg Broytman wrote:

>   Not exactly. One can see an AttributeError, but what was the  
> cause? why
> a parser has created a broken object? AttributeError doesn't preserve
> information from parser.

But if you got the AttributeError, you'd still have the original  
object around to ask more detailed questions about.

On first blush, what I think I like about this is that it fits in with  
an interesting generic API design.  For example, if you have a message  
instance (and remember, parts-is-parts-is-messages) that you think is  
an image, you might just do something like:

 >>> image = msg.decoded_image

and then 'image' is the png that its Content-Type: image/png implies.   
If the data wasn't actually parseable as a png, this would raise an  
AttributeError and you'd then have to do:

 >>> bytes = msg.raw_bytes

to get the raw data, but you'd still have the msg object around to do  
that with.

The one possible problem is that Message may have to implement a  
__getattribute__() to handle this, since you can't know when the class  
is written whether the data its instances will contain will be valid  
or not.

>> I can think of no input for which the parser should *ever* throw an
>> exception.
>
>   Are you saying that even a random garbage would be parsed to a  
> Message
> of some kind? No headers, a single unparsed body?..

Sure, why not?  It's valid RFC 822 :)

-Barry
PGP.sig (application/pgp-signature, 832 B) - not displayed