mirror of https://github.com/python/cpython.git
Entirely rewritten parseaddr() function by Sjoerd Mullender.
(Includes a patch he sent me a few days later.)
This commit is contained in:
parent
a1dbe50ec2
commit
7883e1dfbd
151
Lib/rfc822.py
151
Lib/rfc822.py
|
@ -307,90 +307,129 @@ def unquote(str):
|
||||||
|
|
||||||
|
|
||||||
# Parse an address into (name, address) tuple
|
# Parse an address into (name, address) tuple
|
||||||
|
# (By Sjoerd Mullender)
|
||||||
|
|
||||||
|
error = 'parseaddr.error'
|
||||||
|
|
||||||
|
specials = regex.compile('[][()<>,.;:@\\" \000-\037\177-\377]')
|
||||||
|
|
||||||
|
def quote(str):
|
||||||
|
return '"%s"' % string.join(
|
||||||
|
string.split(
|
||||||
|
string.join(
|
||||||
|
string.split(str, '\\'),
|
||||||
|
'\\\\'),
|
||||||
|
'"'),
|
||||||
|
'\\"')
|
||||||
|
|
||||||
def parseaddr(address):
|
def parseaddr(address):
|
||||||
import string
|
token = [] # the current token
|
||||||
str = ''
|
tokens = [] # the list of tokens
|
||||||
email = ''
|
|
||||||
comment = ''
|
|
||||||
backslash = 0
|
backslash = 0
|
||||||
dquote = 0
|
dquote = 0
|
||||||
|
was_quoted = 0
|
||||||
space = 0
|
space = 0
|
||||||
paren = 0
|
paren = 0
|
||||||
bracket = 0
|
|
||||||
seen_bracket = 0
|
|
||||||
for c in address:
|
for c in address:
|
||||||
if backslash:
|
if backslash:
|
||||||
str = str + c
|
token.append(c)
|
||||||
backslash = 0
|
backslash = 0
|
||||||
continue
|
|
||||||
if c == '\\':
|
if c == '\\':
|
||||||
backslash = 1
|
backslash = 1
|
||||||
|
was_quoted = 1
|
||||||
continue
|
continue
|
||||||
if dquote:
|
if dquote:
|
||||||
if c == '"':
|
if c == '"':
|
||||||
dquote = 0
|
dquote = 0
|
||||||
else:
|
else:
|
||||||
str = str + c
|
token.append(c)
|
||||||
continue
|
continue
|
||||||
if c == '"':
|
if c == '"':
|
||||||
dquote = 1
|
dquote = 1
|
||||||
|
was_quoted = 1
|
||||||
|
continue
|
||||||
|
if paren:
|
||||||
|
if c == '(':
|
||||||
|
paren = paren + 1
|
||||||
|
elif c == ')':
|
||||||
|
paren = paren - 1
|
||||||
|
if paren == 0:
|
||||||
|
token = string.join(token, '')
|
||||||
|
tokens.append((2, token))
|
||||||
|
token = []
|
||||||
|
continue
|
||||||
|
token.append(c)
|
||||||
|
continue
|
||||||
|
if c == '(':
|
||||||
|
paren = 1
|
||||||
|
token = string.join(token, '')
|
||||||
|
tokens.append((was_quoted, token))
|
||||||
|
was_quoted = 0
|
||||||
|
token = []
|
||||||
continue
|
continue
|
||||||
if c in string.whitespace:
|
if c in string.whitespace:
|
||||||
space = 1
|
space = 1
|
||||||
continue
|
continue
|
||||||
if space:
|
if c in '<>@,;:.[]':
|
||||||
str = str + ' '
|
token = string.join(token, '')
|
||||||
|
tokens.append((was_quoted, token))
|
||||||
|
was_quoted = 0
|
||||||
|
token = []
|
||||||
|
tokens.append((0, c))
|
||||||
space = 0
|
space = 0
|
||||||
if paren:
|
|
||||||
if c == '(':
|
|
||||||
paren = paren + 1
|
|
||||||
str = str + c
|
|
||||||
continue
|
|
||||||
if c == ')':
|
|
||||||
paren = paren - 1
|
|
||||||
if paren == 0:
|
|
||||||
comment = comment + str
|
|
||||||
str = ''
|
|
||||||
continue
|
|
||||||
if c == '(':
|
|
||||||
paren = paren + 1
|
|
||||||
if bracket:
|
|
||||||
email = email + str
|
|
||||||
str = ''
|
|
||||||
elif not seen_bracket:
|
|
||||||
email = email + str
|
|
||||||
str = ''
|
|
||||||
continue
|
continue
|
||||||
if bracket:
|
if space:
|
||||||
if c == '>':
|
token = string.join(token, '')
|
||||||
bracket = 0
|
tokens.append((was_quoted, token))
|
||||||
email = email + str
|
was_quoted = 0
|
||||||
str = ''
|
token = []
|
||||||
|
space = 0
|
||||||
|
token.append(c)
|
||||||
|
token = string.join(token, '')
|
||||||
|
tokens.append((was_quoted, token))
|
||||||
|
if (0, '<') in tokens:
|
||||||
|
name = []
|
||||||
|
addr = []
|
||||||
|
cur = name
|
||||||
|
for token in tokens:
|
||||||
|
if token[1] == '':
|
||||||
continue
|
continue
|
||||||
if c == '<':
|
if token == (0, '<'):
|
||||||
bracket = 1
|
if addr:
|
||||||
seen_bracket = 1
|
raise error, 'syntax error'
|
||||||
comment = comment + str
|
cur = addr
|
||||||
str = ''
|
elif token == (0, '>'):
|
||||||
email = ''
|
if cur is not addr:
|
||||||
continue
|
raise error, 'syntax error'
|
||||||
if c == '#' and not bracket and not paren:
|
cur = name
|
||||||
# rest is comment
|
elif token[0] == 2:
|
||||||
break
|
if cur is name:
|
||||||
str = str + c
|
name.append('(' + token[1] + ')')
|
||||||
if str:
|
else:
|
||||||
if seen_bracket:
|
name.append(token[1])
|
||||||
if bracket:
|
elif token[0] == 1 and cur is addr:
|
||||||
email = str
|
if specials.search(token[1]) >= 0:
|
||||||
|
cur.append(quote(token[1]))
|
||||||
|
else:
|
||||||
|
cur.append(token[1])
|
||||||
else:
|
else:
|
||||||
comment = comment + str
|
cur.append(token[1])
|
||||||
else:
|
else:
|
||||||
if paren:
|
name = []
|
||||||
comment = comment + str
|
addr = []
|
||||||
|
for token in tokens:
|
||||||
|
if token[1] == '':
|
||||||
|
continue
|
||||||
|
if token[0] == 2:
|
||||||
|
name.append(token[1])
|
||||||
|
elif token[0] == 1:
|
||||||
|
if specials.search(token[1]) >= 0:
|
||||||
|
addr.append(quote(token[1]))
|
||||||
|
else:
|
||||||
|
addr.append(token[1])
|
||||||
else:
|
else:
|
||||||
email = email + str
|
addr.append(token[1])
|
||||||
return string.strip(comment), string.strip(email)
|
return string.join(name, ' '), string.join(addr, '')
|
||||||
|
|
||||||
|
|
||||||
# Parse a date field
|
# Parse a date field
|
||||||
|
|
Loading…
Reference in New Issue