]> granicus.if.org Git - python/commitdiff
Improve pattern used for mbox 'From' lines; add a simple test
authorAndrew M. Kuchling <amk@amk.ca>
Mon, 22 Jan 2007 20:26:40 +0000 (20:26 +0000)
committerAndrew M. Kuchling <amk@amk.ca>
Mon, 22 Jan 2007 20:26:40 +0000 (20:26 +0000)
Lib/mailbox.py
Lib/test/test_old_mailbox.py

index f8d4df3d97ed0e628838e3b6e6a4e8cb2a27c802..17e062ed2172dfb893999aca050a81e1e229f8bb 100755 (executable)
@@ -1995,8 +1995,10 @@ class UnixMailbox(_Mailbox):
     # necessary.  For convenience, we've added a PortableUnixMailbox class
     # which uses the more lenient _fromlinepattern regular expression.
 
-    _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*$"
+    _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*"
+                        r"[^\s]*\s*"
+                        "$")
     _regexp = None
 
     def _strict_isrealfromline(self, line):
index cca68979613218a671935be6db209a390afd53cb..c8f6bac64bcefcf7965a1a0df72b78897af29588 100644 (file)
@@ -109,11 +109,44 @@ class MaildirTestCase(unittest.TestCase):
             self.assertEqual(len(str(msg)), len(FROM_)+len(DUMMY_MESSAGE))
         self.assertEqual(n, 1)
 
+class MboxTestCase(unittest.TestCase):
+    def setUp(self):
+        # create a new maildir mailbox to work with:
+        self._path = test_support.TESTFN
+
+    def tearDown(self):
+        os.unlink(self._path)
+    
+    def test_from_regex (self):
+        # Testing new regex from bug #1633678
+        f = open(self._path, 'w')
+        f.write("""From fred@example.com Mon May 31 13:24:50 2004 +0200
+Subject: message 1
+
+body1
+From fred@example.com Mon May 31 13:24:50 2004 -0200
+Subject: message 2
+
+body2
+From fred@example.com Mon May 31 13:24:50 2004
+Subject: message 3
+
+body3
+From fred@example.com Mon May 31 13:24:50 2004
+Subject: message 4
+
+body4
+""")
+        f.close()
+        box = mailbox.UnixMailbox(open(self._path, 'r'))
+        self.assert_(len(list(iter(box))) == 4)
+
+
     # XXX We still need more tests!
 
 
 def test_main():
-    test_support.run_unittest(MaildirTestCase)
+    test_support.run_unittest(MaildirTestCase, MboxTestCase)
 
 
 if __name__ == "__main__":