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
7cc767a5
Commit
7cc767a5
authored
Sep 15, 1997
by
Guido van Rossum
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Entirely rewritten parseaddr() function by Sjoerd Mullender.
(Includes a patch he sent me a few days later.)
parent
b0dac34b
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
92 additions
and
53 deletions
+92
-53
Lib/rfc822.py
Lib/rfc822.py
+92
-53
No files found.
Lib/rfc822.py
View file @
7cc767a5
...
...
@@ -307,90 +307,129 @@ def unquote(str):
# 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
):
import
string
str
=
''
email
=
''
comment
=
''
token
=
[]
# the current token
tokens
=
[]
# the list of tokens
backslash
=
0
dquote
=
0
was_quoted
=
0
space
=
0
paren
=
0
bracket
=
0
seen_bracket
=
0
for
c
in
address
:
if
backslash
:
str
=
str
+
c
token
.
append
(
c
)
backslash
=
0
continue
if
c
==
'
\
\
'
:
backslash
=
1
was_quoted
=
1
continue
if
dquote
:
if
c
==
'"'
:
dquote
=
0
else
:
str
=
str
+
c
token
.
append
(
c
)
continue
if
c
==
'"'
:
dquote
=
1
was_quoted
=
1
continue
if
c
in
string
.
whitespace
:
space
=
1
continue
if
space
:
str
=
str
+
' '
space
=
0
if
paren
:
if
c
==
'('
:
paren
=
paren
+
1
str
=
str
+
c
continue
if
c
==
')'
:
elif
c
==
')'
:
paren
=
paren
-
1
if
paren
==
0
:
comment
=
comment
+
str
str
=
''
token
=
string
.
join
(
token
,
''
)
tokens
.
append
((
2
,
token
))
token
=
[]
continue
token
.
append
(
c
)
continue
if
c
==
'('
:
paren
=
paren
+
1
if
bracket
:
email
=
email
+
str
str
=
''
elif
not
seen_bracket
:
email
=
email
+
str
str
=
''
paren
=
1
token
=
string
.
join
(
token
,
''
)
tokens
.
append
((
was_quoted
,
token
))
was_quoted
=
0
token
=
[]
continue
if
bracket
:
if
c
==
'>'
:
bracket
=
0
email
=
email
+
str
str
=
''
continue
if
c
==
'<'
:
bracket
=
1
seen_bracket
=
1
comment
=
comment
+
str
str
=
''
email
=
''
if
c
in
string
.
whitespace
:
space
=
1
continue
if
c
in
'<>@,;:.[]'
:
token
=
string
.
join
(
token
,
''
)
tokens
.
append
((
was_quoted
,
token
))
was_quoted
=
0
token
=
[]
tokens
.
append
((
0
,
c
))
space
=
0
continue
if
c
==
'#'
and
not
bracket
and
not
paren
:
# rest is comment
break
str
=
str
+
c
if
str
:
if
seen_bracket
:
if
bracket
:
email
=
str
if
space
:
token
=
string
.
join
(
token
,
''
)
tokens
.
append
((
was_quoted
,
token
))
was_quoted
=
0
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
if
token
==
(
0
,
'<'
):
if
addr
:
raise
error
,
'syntax error'
cur
=
addr
elif
token
==
(
0
,
'>'
):
if
cur
is
not
addr
:
raise
error
,
'syntax error'
cur
=
name
elif
token
[
0
]
==
2
:
if
cur
is
name
:
name
.
append
(
'('
+
token
[
1
]
+
')'
)
else
:
name
.
append
(
token
[
1
])
elif
token
[
0
]
==
1
and
cur
is
addr
:
if
specials
.
search
(
token
[
1
])
>=
0
:
cur
.
append
(
quote
(
token
[
1
]))
else
:
cur
.
append
(
token
[
1
])
else
:
comment
=
comment
+
str
else
:
if
paren
:
comment
=
comment
+
str
cur
.
append
(
token
[
1
])
else
:
name
=
[]
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
:
email
=
email
+
str
return
string
.
strip
(
comment
),
string
.
strip
(
email
)
addr
.
append
(
token
[
1
])
return
string
.
join
(
name
,
' '
),
string
.
join
(
addr
,
''
)
# Parse a date field
...
...
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