diff options
author | Guido van Rossum <guido@python.org> | 1998-04-03 16:04:05 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 1998-04-03 16:04:05 (GMT) |
commit | fbe63de37decd3e54d349e83b3e9100842013730 (patch) | |
tree | 1ed3e038259066d28f1a008d7074cff8f7fe2aa5 /Lib/mailbox.py | |
parent | 7e07b3845b27f1f9c30733431ebd4dccbae8b9d4 (diff) | |
download | cpython-fbe63de37decd3e54d349e83b3e9100842013730.zip cpython-fbe63de37decd3e54d349e83b3e9100842013730.tar.gz cpython-fbe63de37decd3e54d349e83b3e9100842013730.tar.bz2 |
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.
Diffstat (limited to 'Lib/mailbox.py')
-rwxr-xr-x | Lib/mailbox.py | 28 |
1 files changed, 22 insertions, 6 deletions
diff --git a/Lib/mailbox.py b/Lib/mailbox.py index d1315d0..dd8e5e1 100755 --- a/Lib/mailbox.py +++ b/Lib/mailbox.py @@ -85,7 +85,7 @@ class UnixMailbox(_Mailbox): line = self.fp.readline() if not line: raise EOFError - if line[:5] == 'From ': + if line[:5] == 'From ' and self._isrealfromline(line): return def _search_end(self): @@ -94,10 +94,26 @@ class UnixMailbox(_Mailbox): line = self.fp.readline() if not line: return - if line[:5] == 'From ': + if line[:5] == 'From ' and self._isrealfromline(line): self.fp.seek(pos) 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): def _search_start(self): @@ -190,7 +206,7 @@ def _test(): msgs = [] while 1: msg = mb.next() - if not msg: + if msg is None: break msgs.append(msg) msg.fp = None @@ -203,9 +219,9 @@ def _test(): else: print 'Mailbox',mbox,'has',len(msgs),'messages:' for msg in msgs: - f = msg.getheader('from') - s = msg.getheader('subject') - d = (msg.getheader('date')) + f = msg.getheader('from') or "" + s = msg.getheader('subject') or "" + d = msg.getheader('date') or "" print '%20.20s %18.18s %-30.30s'%(f, d[5:], s) |