Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
1bd00aa2
Commit
1bd00aa2
authored
Dec 30, 2002
by
Barry Warsaw
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
A code cleansing pass
parent
019d5b44
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
74 additions
and
55 deletions
+74
-55
Lib/email/_parseaddr.py
Lib/email/_parseaddr.py
+74
-55
No files found.
Lib/email/_parseaddr.py
View file @
1bd00aa2
...
@@ -6,6 +6,17 @@ Lifted directly from rfc822.py. This should eventually be rewritten.
...
@@ -6,6 +6,17 @@ Lifted directly from rfc822.py. This should eventually be rewritten.
"""
"""
import
time
import
time
from
types
import
TupleType
try
:
True
,
False
except
NameError
:
True
=
1
False
=
0
SPACE
=
' '
EMPTYSTRING
=
''
COMMASPACE
=
', '
# Parse a date field
# Parse a date field
_monthnames
=
[
'jan'
,
'feb'
,
'mar'
,
'apr'
,
'may'
,
'jun'
,
'jul'
,
_monthnames
=
[
'jan'
,
'feb'
,
'mar'
,
'apr'
,
'may'
,
'jun'
,
'jul'
,
...
@@ -55,12 +66,13 @@ def parsedate_tz(data):
...
@@ -55,12 +66,13 @@ def parsedate_tz(data):
data
=
data
[:
5
]
data
=
data
[:
5
]
[
dd
,
mm
,
yy
,
tm
,
tz
]
=
data
[
dd
,
mm
,
yy
,
tm
,
tz
]
=
data
mm
=
mm
.
lower
()
mm
=
mm
.
lower
()
if
not
mm
in
_monthnames
:
if
mm
not
in
_monthnames
:
dd
,
mm
=
mm
,
dd
.
lower
()
dd
,
mm
=
mm
,
dd
.
lower
()
if
not
mm
in
_monthnames
:
if
mm
not
in
_monthnames
:
return
None
return
None
mm
=
_monthnames
.
index
(
mm
)
+
1
mm
=
_monthnames
.
index
(
mm
)
+
1
if
mm
>
12
:
mm
=
mm
-
12
if
mm
>
12
:
mm
-=
12
if
dd
[
-
1
]
==
','
:
if
dd
[
-
1
]
==
','
:
dd
=
dd
[:
-
1
]
dd
=
dd
[:
-
1
]
i
=
yy
.
find
(
':'
)
i
=
yy
.
find
(
':'
)
...
@@ -112,9 +124,10 @@ def parsedate_tz(data):
...
@@ -112,9 +124,10 @@ def parsedate_tz(data):
def
parsedate
(
data
):
def
parsedate
(
data
):
"""Convert a time string to a time tuple."""
"""Convert a time string to a time tuple."""
t
=
parsedate_tz
(
data
)
t
=
parsedate_tz
(
data
)
if
type
(
t
)
==
type
(
()
):
if
isinstance
(
t
,
TupleType
):
return
t
[:
9
]
return
t
[:
9
]
else
:
return
t
else
:
return
t
def
mktime_tz
(
data
):
def
mktime_tz
(
data
):
...
@@ -164,10 +177,11 @@ class AddrlistClass:
...
@@ -164,10 +177,11 @@ class AddrlistClass:
"""Parse up to the start of the next address."""
"""Parse up to the start of the next address."""
while
self
.
pos
<
len
(
self
.
field
):
while
self
.
pos
<
len
(
self
.
field
):
if
self
.
field
[
self
.
pos
]
in
self
.
LWS
+
'
\
n
\
r
'
:
if
self
.
field
[
self
.
pos
]
in
self
.
LWS
+
'
\
n
\
r
'
:
self
.
pos
=
self
.
pos
+
1
self
.
pos
+=
1
elif
self
.
field
[
self
.
pos
]
==
'('
:
elif
self
.
field
[
self
.
pos
]
==
'('
:
self
.
commentlist
.
append
(
self
.
getcomment
())
self
.
commentlist
.
append
(
self
.
getcomment
())
else
:
break
else
:
break
def
getaddrlist
(
self
):
def
getaddrlist
(
self
):
"""Parse all addresses.
"""Parse all addresses.
...
@@ -175,7 +189,7 @@ class AddrlistClass:
...
@@ -175,7 +189,7 @@ class AddrlistClass:
Returns a list containing all of the addresses.
Returns a list containing all of the addresses.
"""
"""
result
=
[]
result
=
[]
while
1
:
while
True
:
ad
=
self
.
getaddress
()
ad
=
self
.
getaddress
()
if
ad
:
if
ad
:
result
+=
ad
result
+=
ad
...
@@ -198,7 +212,7 @@ class AddrlistClass:
...
@@ -198,7 +212,7 @@ class AddrlistClass:
if
self
.
pos
>=
len
(
self
.
field
):
if
self
.
pos
>=
len
(
self
.
field
):
# Bad email address technically, no domain.
# Bad email address technically, no domain.
if
plist
:
if
plist
:
returnlist
=
[(
' '
.
join
(
self
.
commentlist
),
plist
[
0
])]
returnlist
=
[(
SPACE
.
join
(
self
.
commentlist
),
plist
[
0
])]
elif
self
.
field
[
self
.
pos
]
in
'.@'
:
elif
self
.
field
[
self
.
pos
]
in
'.@'
:
# email address is just an addrspec
# email address is just an addrspec
...
@@ -206,18 +220,18 @@ class AddrlistClass:
...
@@ -206,18 +220,18 @@ class AddrlistClass:
self
.
pos
=
oldpos
self
.
pos
=
oldpos
self
.
commentlist
=
oldcl
self
.
commentlist
=
oldcl
addrspec
=
self
.
getaddrspec
()
addrspec
=
self
.
getaddrspec
()
returnlist
=
[(
' '
.
join
(
self
.
commentlist
),
addrspec
)]
returnlist
=
[(
SPACE
.
join
(
self
.
commentlist
),
addrspec
)]
elif
self
.
field
[
self
.
pos
]
==
':'
:
elif
self
.
field
[
self
.
pos
]
==
':'
:
# address is a group
# address is a group
returnlist
=
[]
returnlist
=
[]
fieldlen
=
len
(
self
.
field
)
fieldlen
=
len
(
self
.
field
)
self
.
pos
=
self
.
pos
+
1
self
.
pos
+=
1
while
self
.
pos
<
len
(
self
.
field
):
while
self
.
pos
<
len
(
self
.
field
):
self
.
gotonext
()
self
.
gotonext
()
if
self
.
pos
<
fieldlen
and
self
.
field
[
self
.
pos
]
==
';'
:
if
self
.
pos
<
fieldlen
and
self
.
field
[
self
.
pos
]
==
';'
:
self
.
pos
=
self
.
pos
+
1
self
.
pos
+=
1
break
break
returnlist
=
returnlist
+
self
.
getaddress
()
returnlist
=
returnlist
+
self
.
getaddress
()
...
@@ -226,19 +240,20 @@ class AddrlistClass:
...
@@ -226,19 +240,20 @@ class AddrlistClass:
routeaddr
=
self
.
getrouteaddr
()
routeaddr
=
self
.
getrouteaddr
()
if
self
.
commentlist
:
if
self
.
commentlist
:
returnlist
=
[(
' '
.
join
(
plist
)
+
' ('
+
\
returnlist
=
[(
SPACE
.
join
(
plist
)
+
' ('
+
' '
.
join
(
self
.
commentlist
)
+
')'
,
routeaddr
)]
' '
.
join
(
self
.
commentlist
)
+
')'
,
routeaddr
)]
else
:
returnlist
=
[(
' '
.
join
(
plist
),
routeaddr
)]
else
:
returnlist
=
[(
SPACE
.
join
(
plist
),
routeaddr
)]
else
:
else
:
if
plist
:
if
plist
:
returnlist
=
[(
' '
.
join
(
self
.
commentlist
),
plist
[
0
])]
returnlist
=
[(
SPACE
.
join
(
self
.
commentlist
),
plist
[
0
])]
elif
self
.
field
[
self
.
pos
]
in
self
.
specials
:
elif
self
.
field
[
self
.
pos
]
in
self
.
specials
:
self
.
pos
=
self
.
pos
+
1
self
.
pos
+=
1
self
.
gotonext
()
self
.
gotonext
()
if
self
.
pos
<
len
(
self
.
field
)
and
self
.
field
[
self
.
pos
]
==
','
:
if
self
.
pos
<
len
(
self
.
field
)
and
self
.
field
[
self
.
pos
]
==
','
:
self
.
pos
=
self
.
pos
+
1
self
.
pos
+=
1
return
returnlist
return
returnlist
def
getrouteaddr
(
self
):
def
getrouteaddr
(
self
):
...
@@ -249,25 +264,25 @@ class AddrlistClass:
...
@@ -249,25 +264,25 @@ class AddrlistClass:
if
self
.
field
[
self
.
pos
]
!=
'<'
:
if
self
.
field
[
self
.
pos
]
!=
'<'
:
return
return
expectroute
=
0
expectroute
=
False
self
.
pos
=
self
.
pos
+
1
self
.
pos
+=
1
self
.
gotonext
()
self
.
gotonext
()
adlist
=
""
adlist
=
''
while
self
.
pos
<
len
(
self
.
field
):
while
self
.
pos
<
len
(
self
.
field
):
if
expectroute
:
if
expectroute
:
self
.
getdomain
()
self
.
getdomain
()
expectroute
=
0
expectroute
=
False
elif
self
.
field
[
self
.
pos
]
==
'>'
:
elif
self
.
field
[
self
.
pos
]
==
'>'
:
self
.
pos
=
self
.
pos
+
1
self
.
pos
+=
1
break
break
elif
self
.
field
[
self
.
pos
]
==
'@'
:
elif
self
.
field
[
self
.
pos
]
==
'@'
:
self
.
pos
=
self
.
pos
+
1
self
.
pos
+=
1
expectroute
=
1
expectroute
=
True
elif
self
.
field
[
self
.
pos
]
==
':'
:
elif
self
.
field
[
self
.
pos
]
==
':'
:
self
.
pos
=
self
.
pos
+
1
self
.
pos
+=
1
else
:
else
:
adlist
=
self
.
getaddrspec
()
adlist
=
self
.
getaddrspec
()
self
.
pos
=
self
.
pos
+
1
self
.
pos
+=
1
break
break
self
.
gotonext
()
self
.
gotonext
()
...
@@ -281,41 +296,43 @@ class AddrlistClass:
...
@@ -281,41 +296,43 @@ class AddrlistClass:
while
self
.
pos
<
len
(
self
.
field
):
while
self
.
pos
<
len
(
self
.
field
):
if
self
.
field
[
self
.
pos
]
==
'.'
:
if
self
.
field
[
self
.
pos
]
==
'.'
:
aslist
.
append
(
'.'
)
aslist
.
append
(
'.'
)
self
.
pos
=
self
.
pos
+
1
self
.
pos
+=
1
elif
self
.
field
[
self
.
pos
]
==
'"'
:
elif
self
.
field
[
self
.
pos
]
==
'"'
:
aslist
.
append
(
'"%s"'
%
self
.
getquote
())
aslist
.
append
(
'"%s"'
%
self
.
getquote
())
elif
self
.
field
[
self
.
pos
]
in
self
.
atomends
:
elif
self
.
field
[
self
.
pos
]
in
self
.
atomends
:
break
break
else
:
aslist
.
append
(
self
.
getatom
())
else
:
aslist
.
append
(
self
.
getatom
())
self
.
gotonext
()
self
.
gotonext
()
if
self
.
pos
>=
len
(
self
.
field
)
or
self
.
field
[
self
.
pos
]
!=
'@'
:
if
self
.
pos
>=
len
(
self
.
field
)
or
self
.
field
[
self
.
pos
]
!=
'@'
:
return
''
.
join
(
aslist
)
return
EMPTYSTRING
.
join
(
aslist
)
aslist
.
append
(
'@'
)
aslist
.
append
(
'@'
)
self
.
pos
=
self
.
pos
+
1
self
.
pos
+=
1
self
.
gotonext
()
self
.
gotonext
()
return
''
.
join
(
aslist
)
+
self
.
getdomain
()
return
EMPTYSTRING
.
join
(
aslist
)
+
self
.
getdomain
()
def
getdomain
(
self
):
def
getdomain
(
self
):
"""Get the complete domain name from an address."""
"""Get the complete domain name from an address."""
sdlist
=
[]
sdlist
=
[]
while
self
.
pos
<
len
(
self
.
field
):
while
self
.
pos
<
len
(
self
.
field
):
if
self
.
field
[
self
.
pos
]
in
self
.
LWS
:
if
self
.
field
[
self
.
pos
]
in
self
.
LWS
:
self
.
pos
=
self
.
pos
+
1
self
.
pos
+=
1
elif
self
.
field
[
self
.
pos
]
==
'('
:
elif
self
.
field
[
self
.
pos
]
==
'('
:
self
.
commentlist
.
append
(
self
.
getcomment
())
self
.
commentlist
.
append
(
self
.
getcomment
())
elif
self
.
field
[
self
.
pos
]
==
'['
:
elif
self
.
field
[
self
.
pos
]
==
'['
:
sdlist
.
append
(
self
.
getdomainliteral
())
sdlist
.
append
(
self
.
getdomainliteral
())
elif
self
.
field
[
self
.
pos
]
==
'.'
:
elif
self
.
field
[
self
.
pos
]
==
'.'
:
self
.
pos
=
self
.
pos
+
1
self
.
pos
+=
1
sdlist
.
append
(
'.'
)
sdlist
.
append
(
'.'
)
elif
self
.
field
[
self
.
pos
]
in
self
.
atomends
:
elif
self
.
field
[
self
.
pos
]
in
self
.
atomends
:
break
break
else
:
sdlist
.
append
(
self
.
getatom
())
else
:
return
''
.
join
(
sdlist
)
sdlist
.
append
(
self
.
getatom
())
return
EMPTYSTRING
.
join
(
sdlist
)
def
getdelimited
(
self
,
beginchar
,
endchars
,
allowcomments
=
1
):
def
getdelimited
(
self
,
beginchar
,
endchars
,
allowcomments
=
True
):
"""Parse a header fragment delimited by special characters.
"""Parse a header fragment delimited by special characters.
`beginchar' is the start character for the fragment.
`beginchar' is the start character for the fragment.
...
@@ -332,36 +349,36 @@ class AddrlistClass:
...
@@ -332,36 +349,36 @@ class AddrlistClass:
return
''
return
''
slist
=
[
''
]
slist
=
[
''
]
quote
=
0
quote
=
False
self
.
pos
=
self
.
pos
+
1
self
.
pos
+=
1
while
self
.
pos
<
len
(
self
.
field
):
while
self
.
pos
<
len
(
self
.
field
):
if
quote
==
1
:
if
quote
:
slist
.
append
(
self
.
field
[
self
.
pos
])
slist
.
append
(
self
.
field
[
self
.
pos
])
quote
=
0
quote
=
False
elif
self
.
field
[
self
.
pos
]
in
endchars
:
elif
self
.
field
[
self
.
pos
]
in
endchars
:
self
.
pos
=
self
.
pos
+
1
self
.
pos
+=
1
break
break
elif
allowcomments
and
self
.
field
[
self
.
pos
]
==
'('
:
elif
allowcomments
and
self
.
field
[
self
.
pos
]
==
'('
:
slist
.
append
(
self
.
getcomment
())
slist
.
append
(
self
.
getcomment
())
elif
self
.
field
[
self
.
pos
]
==
'
\
\
'
:
elif
self
.
field
[
self
.
pos
]
==
'
\
\
'
:
quote
=
1
quote
=
True
else
:
else
:
slist
.
append
(
self
.
field
[
self
.
pos
])
slist
.
append
(
self
.
field
[
self
.
pos
])
self
.
pos
=
self
.
pos
+
1
self
.
pos
+=
1
return
''
.
join
(
slist
)
return
EMPTYSTRING
.
join
(
slist
)
def
getquote
(
self
):
def
getquote
(
self
):
"""Get a quote-delimited fragment from self's field."""
"""Get a quote-delimited fragment from self's field."""
return
self
.
getdelimited
(
'"'
,
'"
\
r
'
,
0
)
return
self
.
getdelimited
(
'"'
,
'"
\
r
'
,
False
)
def
getcomment
(
self
):
def
getcomment
(
self
):
"""Get a parenthesis-delimited fragment from self's field."""
"""Get a parenthesis-delimited fragment from self's field."""
return
self
.
getdelimited
(
'('
,
')
\
r
'
,
1
)
return
self
.
getdelimited
(
'('
,
')
\
r
'
,
True
)
def
getdomainliteral
(
self
):
def
getdomainliteral
(
self
):
"""Parse an RFC 2822 domain-literal."""
"""Parse an RFC 2822 domain-literal."""
return
'[%s]'
%
self
.
getdelimited
(
'['
,
']
\
r
'
,
0
)
return
'[%s]'
%
self
.
getdelimited
(
'['
,
']
\
r
'
,
False
)
def
getatom
(
self
,
atomends
=
None
):
def
getatom
(
self
,
atomends
=
None
):
"""Parse an RFC 2822 atom.
"""Parse an RFC 2822 atom.
...
@@ -377,10 +394,11 @@ class AddrlistClass:
...
@@ -377,10 +394,11 @@ class AddrlistClass:
while
self
.
pos
<
len
(
self
.
field
):
while
self
.
pos
<
len
(
self
.
field
):
if
self
.
field
[
self
.
pos
]
in
atomends
:
if
self
.
field
[
self
.
pos
]
in
atomends
:
break
break
else
:
atomlist
.
append
(
self
.
field
[
self
.
pos
])
else
:
self
.
pos
=
self
.
pos
+
1
atomlist
.
append
(
self
.
field
[
self
.
pos
])
self
.
pos
+=
1
return
''
.
join
(
atomlist
)
return
EMPTYSTRING
.
join
(
atomlist
)
def
getphraselist
(
self
):
def
getphraselist
(
self
):
"""Parse a sequence of RFC 2822 phrases.
"""Parse a sequence of RFC 2822 phrases.
...
@@ -393,14 +411,15 @@ class AddrlistClass:
...
@@ -393,14 +411,15 @@ class AddrlistClass:
while
self
.
pos
<
len
(
self
.
field
):
while
self
.
pos
<
len
(
self
.
field
):
if
self
.
field
[
self
.
pos
]
in
self
.
LWS
:
if
self
.
field
[
self
.
pos
]
in
self
.
LWS
:
self
.
pos
=
self
.
pos
+
1
self
.
pos
+=
1
elif
self
.
field
[
self
.
pos
]
==
'"'
:
elif
self
.
field
[
self
.
pos
]
==
'"'
:
plist
.
append
(
self
.
getquote
())
plist
.
append
(
self
.
getquote
())
elif
self
.
field
[
self
.
pos
]
==
'('
:
elif
self
.
field
[
self
.
pos
]
==
'('
:
self
.
commentlist
.
append
(
self
.
getcomment
())
self
.
commentlist
.
append
(
self
.
getcomment
())
elif
self
.
field
[
self
.
pos
]
in
self
.
phraseends
:
elif
self
.
field
[
self
.
pos
]
in
self
.
phraseends
:
break
break
else
:
plist
.
append
(
self
.
getatom
(
self
.
phraseends
))
else
:
plist
.
append
(
self
.
getatom
(
self
.
phraseends
))
return
plist
return
plist
...
@@ -417,7 +436,7 @@ class AddressList(AddrlistClass):
...
@@ -417,7 +436,7 @@ class AddressList(AddrlistClass):
return
len
(
self
.
addresslist
)
return
len
(
self
.
addresslist
)
def
__str__
(
self
):
def
__str__
(
self
):
return
", "
.
join
(
map
(
dump_address_pair
,
self
.
addresslist
))
return
COMMASPACE
.
join
(
map
(
dump_address_pair
,
self
.
addresslist
))
def
__add__
(
self
,
other
):
def
__add__
(
self
,
other
):
# Set union
# Set union
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment