This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: email.Utils.parseaddr() gives arcane result
Type: Stage: resolved
Components: Library (Lib) Versions: Python 2.4
process
Status: closed Resolution: out of date
Dependencies: 1464708 Superseder:
Assigned To: barry Nosy List: ajaksu2, barry, eric.araujo, msapiro, r.david.murray
Priority: normal Keywords:

Created on 2006-01-18 22:19 by msapiro, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (7)
msg27314 - (view) Author: Mark Sapiro (msapiro) * (Python triager) Date: 2006-01-18 22:19
email.Utils.parseaddr('Real Name ((comment))
<addr...@example.com>')

returns

('comment <addr...@example.com>', 'Real')

Granted the string above is invalid as RFC 2822 does
not allow parentheses within comments, but most mail
agents seem to at least take the contents of the angle
brackets as the address in this case.

rfc822.parseaddr() returns the same result in this case.

If these functions aren't going to return their
respective failure indication in this case, I think
they should at least return 'addr...@example.com' as
the second item of the returned tuple.

Note that

parseaddr('Real Name <addr...@example.com> ((comment))')

does return

('Real Name', 'addr...@example.com')

as 'expected'.
msg27315 - (view) Author: Mark Sapiro (msapiro) * (Python triager) Date: 2006-04-21 17:01
Logged In: YES 
user_id=1123998

It looks like this is addressed by patch 1464708
http://sourceforge.net/tracker/index.php?func=detail&aid=1464708&group_id=5470&atid=305470
msg83892 - (view) Author: Daniel Diniz (ajaksu2) * (Python triager) Date: 2009-03-20 23:51
Fixed in trunk, so issue 1464708 was probably forward ported.
msg110548 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2010-07-17 11:38
I wonder if this bug should be reopened. This behavior does not seem right to me:

parsing 'merwok'
 expected ('merwok', '')
 got      ('', 'merwok')

parsing 'merwok wok@rusty'
 expected ('', 'wok@rusty')
 got      ('', 'merwokwok@rusty')

(Generated with a twenty-line script just doing a loop and prints, not attached because boring.)

Are my expectations wrong? I don’t know if a string like “merwok” in my first example is a legal address in the relevant RFCs, nor do I know if the folding done in the second example is okay.

For background, the thing I’m trying to achieve is to take a string and parse it into name and email address, and print a warning if there is no email. It’d be nice if I could always test for “not parseaddr(s)[1]”, or pass an argument to the function to get an exception. Maybe I’ll have to restrict my format and do my own parsing with str.[r]partition.
msg110556 - (view) Author: Mark Sapiro (msapiro) * (Python triager) Date: 2010-07-17 14:07
> parsing 'merwok'
>  expected ('merwok', '')
>  got      ('', 'merwok')


I think ('', 'merwok') is the correct result. I think most if not all MUAs/MTAs will interpret an address without an '@', albeit invalid, as a local-part in the local domain, thus parsing 'merwok' as the address 'merwok' with no real name is probably the right thing to do with this input. The alternative would be to return ('', '') indicating failure.


> parsing 'merwok wok@rusty'
>  expected ('', 'wok@rusty')
>  got      ('', 'merwokwok@rusty')


Here, I think failure is a more appropriate return.

In any case, I think this is a new bug deserving of a new report. It is not really relevant to this issue which has to do with nested parentheses.
msg110558 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2010-07-17 14:25
Thank you for the reply. The problem is that parseaddr is designed to not fail IIUC, that’s why it may return empty strings. Client code has to check for these values instead of catching an exception—a mere style issue, weren’t it for the second bug. So I’m going to report that separately, and restrict the format in my project so that my code can just use string methods. I think I won’t open a feature request for a parameter to get exceptions, since parseaddr is designed not to.
msg110562 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2010-07-17 14:36
Copied my inquiry and part of your reply in #9286.
History
Date User Action Args
2022-04-11 14:56:15adminsetgithub: 42808
2010-07-17 14:36:48eric.araujosetmessages: + msg110562
stage: resolved
2010-07-17 14:25:22eric.araujosetmessages: + msg110558
2010-07-17 14:07:22msapirosetmessages: + msg110556
2010-07-17 11:38:35eric.araujosetnosy: + eric.araujo, r.david.murray
messages: + msg110548
2009-03-20 23:51:04ajaksu2setstatus: open -> closed

nosy: + ajaksu2
messages: + msg83892

dependencies: + fixed handling of nested comments in mail addresses
resolution: out of date
2006-01-18 22:19:07msapirocreate