[issue32058] Faulty behaviour in email.utils.parseaddr if square brackets in subject

2017-11-17 Thread R. David Murray

R. David Murray  added the comment:

Unfortunately the imap module in the stdlib doesn't provide a whole lot in the 
way of tools for parsing the imap data, just for sending it back and forth to 
the server.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue32058] Faulty behaviour in email.utils.parseaddr if square brackets in subject

2017-11-17 Thread tom de wulf

tom de wulf  added the comment:

I do get this data from an IMAP fetch statement, see my code below:

rv, data = imap.fetch(num, "(BODY[HEADER.FIELDS (FROM SUBJECT)])")
if rv != 'OK':
logging.error("Error getting message sender and subject (" + 
num.decode("ascii") + ")")
return
logging.info("Got message " + num.decode("ascii"))

sender_subject = data[0][1].decode("utf-8")
sender = email.utils.parseaddr(sender_subject.replace('[', 
'').replace(']',''))[1].replace("\r\n", "")

Thank you for providing this new API though, I will make sure to switch to that.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue32058] Faulty behaviour in email.utils.parseaddr if square brackets in subject

2017-11-17 Thread R. David Murray

R. David Murray  added the comment:

parseaddr is for parsing the contents of an address header, not for parsing any 
additional text.  So the correct way to call it is parseaddress('someone 
').

In any case, please look in to the new email policies, which provide a much 
more convenient API:

>>> from email import message_from_bytes
>>> from email.policy import default
>>> m = message_from_bytes(b'Subject: I am a bug [Random]\r\nFrom: someone 
\r\n\r\n', policy=default)
>>> m['from']
'someone '
>>> m['from'].addresses
(Address(display_name='someone', username='some', domain='email.address'),)
>>> m['from'].addresses[0].display_name
'someone'
>>> m['from'].addresses[0].username
'some'
>>> m['from'].addresses[0].addr_spec
'some@email.address'

--
resolution:  -> not a bug
stage:  -> resolved
status: open -> closed

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue32058] Faulty behaviour in email.utils.parseaddr if square brackets in subject

2017-11-17 Thread tom de wulf

New submission from tom de wulf :

Probably a parsing bug in email.utils.parseaddr.

How to recreate:

>>> import email.utils
>>> test = 'Subject: I am a bug [Random]\r\nFrom: someone 
>>> \r\n\r\n'
>>> email.utils.parseaddr(test)
('', 'I')
>>> email.utils.parseaddr(test.replace('[', '').replace(']',''))
('someone', 'some@email.address')

Expected behaviour: no need to remove the []'s

--
components: email
messages: 306431
nosy: barry, r.david.murray, tom de wulf
priority: normal
severity: normal
status: open
title: Faulty behaviour in email.utils.parseaddr if square brackets in subject
type: behavior
versions: Python 3.5

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com