mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
UnixMailbox: don't be fooled by lines that begin with "From " but
otherwise don't look like headers at all... Also robustify the test code a bit.
This commit is contained in:
parent
7e07b3845b
commit
fbe63de37d
1 changed files with 22 additions and 6 deletions
|
@ -85,7 +85,7 @@ class UnixMailbox(_Mailbox):
|
||||||
line = self.fp.readline()
|
line = self.fp.readline()
|
||||||
if not line:
|
if not line:
|
||||||
raise EOFError
|
raise EOFError
|
||||||
if line[:5] == 'From ':
|
if line[:5] == 'From ' and self._isrealfromline(line):
|
||||||
return
|
return
|
||||||
|
|
||||||
def _search_end(self):
|
def _search_end(self):
|
||||||
|
@ -94,10 +94,26 @@ class UnixMailbox(_Mailbox):
|
||||||
line = self.fp.readline()
|
line = self.fp.readline()
|
||||||
if not line:
|
if not line:
|
||||||
return
|
return
|
||||||
if line[:5] == 'From ':
|
if line[:5] == 'From ' and self._isrealfromline(line):
|
||||||
self.fp.seek(pos)
|
self.fp.seek(pos)
|
||||||
return
|
return
|
||||||
|
|
||||||
|
# An overridable mechanism to test for From-line-ness.
|
||||||
|
# You can either specify a different regular expression
|
||||||
|
# or define a whole new _isrealfromline() method.
|
||||||
|
# Note that this only gets called for lines starting with
|
||||||
|
# the 5 characters "From ".
|
||||||
|
|
||||||
|
_fromlinepattern = r"From \s*[^\s]+\s+\w\w\w\s+\w\w\w\s+\d?\d\s+" \
|
||||||
|
r"\d?\d:\d\d:\d\d(\s+[^\s]+)?\s+\d\d\d\d\s*$"
|
||||||
|
_regexp = None
|
||||||
|
|
||||||
|
def _isrealfromline(self, line):
|
||||||
|
if not self._regexp:
|
||||||
|
import re
|
||||||
|
self._regexp = re.compile(self._fromlinepattern)
|
||||||
|
return self._regexp.match(line)
|
||||||
|
|
||||||
class MmdfMailbox(_Mailbox):
|
class MmdfMailbox(_Mailbox):
|
||||||
|
|
||||||
def _search_start(self):
|
def _search_start(self):
|
||||||
|
@ -190,7 +206,7 @@ def _test():
|
||||||
msgs = []
|
msgs = []
|
||||||
while 1:
|
while 1:
|
||||||
msg = mb.next()
|
msg = mb.next()
|
||||||
if not msg:
|
if msg is None:
|
||||||
break
|
break
|
||||||
msgs.append(msg)
|
msgs.append(msg)
|
||||||
msg.fp = None
|
msg.fp = None
|
||||||
|
@ -203,9 +219,9 @@ def _test():
|
||||||
else:
|
else:
|
||||||
print 'Mailbox',mbox,'has',len(msgs),'messages:'
|
print 'Mailbox',mbox,'has',len(msgs),'messages:'
|
||||||
for msg in msgs:
|
for msg in msgs:
|
||||||
f = msg.getheader('from')
|
f = msg.getheader('from') or ""
|
||||||
s = msg.getheader('subject')
|
s = msg.getheader('subject') or ""
|
||||||
d = (msg.getheader('date'))
|
d = msg.getheader('date') or ""
|
||||||
print '%20.20s %18.18s %-30.30s'%(f, d[5:], s)
|
print '%20.20s %18.18s %-30.30s'%(f, d[5:], s)
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue