Mark Sapiro pushed to branch master at GNU Mailman / Mailman Core


Commits:
f856c27c by Mark Sapiro at 2021-08-12T15:03:58-07:00
Use email.message_from_bytes to parse messages from usenet.

- - - - -
26fd9a15 by Mark Sapiro at 2021-08-12T22:48:07+00:00
Merge branch 'prefix' into 'master'

Use email.message_from_bytes to parse messages from usenet.

Closes #934

See merge request mailman/mailman!902
- - - - -


3 changed files:

- src/mailman/commands/cli_gatenews.py
- src/mailman/commands/tests/test_cli_gatenews.py
- src/mailman/docs/NEWS.rst


Changes:

=====================================
src/mailman/commands/cli_gatenews.py
=====================================
@@ -24,7 +24,7 @@ import logging
 import nntplib
 import datetime
 
-from email import errors, parser, policy
+from email import errors, message_from_bytes
 from flufl.lock import Lock, TimeOutError
 from mailman.config import config
 from mailman.core.i18n import _
@@ -91,9 +91,8 @@ def poll_newsgroup(mlist, conn, first, last, glock):
                     break
             if not beenthere:
                 lines = conn.article(num)[1].lines
-                p = parser.BytesParser(message.Message, policy=policy.default)
                 try:
-                    msg = p.parsebytes(NL.join(lines))
+                    msg = message_from_bytes(NL.join(lines), message.Message)
                 except errors.MessageError as e:
                     log.error('email package exception for %s:%d\n%s',
                               mlist.linked_newsgroup, num, e)


=====================================
src/mailman/commands/tests/test_cli_gatenews.py
=====================================
@@ -110,21 +110,6 @@ def get_nntplib_nntp(fail=0):
     return patcher
 
 
-def get_email_exception():
-    """Create a mock for email.parser.BytesParser to raise an exception."""
-
-    class BytesParser:
-        def __init__(self, factory, policy):
-            self.factory = factory
-            self.policy = policy
-
-        def parsebytes(self, msg_bytes):
-            raise MessageError('Bad message')
-
-    patcher = patch('email.parser.BytesParser', BytesParser)
-    return patcher
-
-
 class Test_gatenews(TestCase):
     """Test gating messages from usenet."""
 
@@ -144,6 +129,8 @@ class Test_gatenews(TestCase):
         # Create a second list without gateway for test coverage purposes.
         create_list('otherl...@example.com')
         os.environ['_MAILMAN_GATENEWS_NNTP'] = 'yes'
+        # Also get the subject_prefix process.
+        self._process = config.handlers['subject-prefix'].process
 
     def test_bad_nntp_connect(self):
         mark = LogFileMark('mailman.fromusenet')
@@ -230,7 +217,8 @@ class Test_gatenews(TestCase):
 
     def test_email_parser_exception(self):
         mark = LogFileMark('mailman.fromusenet')
-        with get_email_exception():
+        with patch('mailman.commands.cli_gatenews.message_from_bytes',
+                   side_effect=MessageError('Bad message')):
             with get_nntplib_nntp():
                 self._command.invoke(gatenews)
         lines = mark.read().splitlines()
@@ -260,3 +248,12 @@ class Test_gatenews(TestCase):
         self.assertNotEqual(result.exit_code, 0)
         self.assertIn('Error: The gatenews command is run periodically',
                       result.output)
+
+    def test_subject_prefixing(self):
+        with get_nntplib_nntp():
+            self._command.invoke(gatenews)
+        items = get_queue_messages('in', expected_count=1)
+        msgdata = items[0].msgdata
+        msg = items[0].msg
+        self._process(self.mlist, msg, msgdata)
+        self.assertEqual(msg['subject'], '[Mylist] A Message')


=====================================
src/mailman/docs/NEWS.rst
=====================================
@@ -100,6 +100,8 @@ Bugs
   (Closes #932)
 * The email ``join`` command now handles RFC2047 encoded display names with
   embedded commas.  (Closes #933)
+* The ``gatenews`` command now parses messages with email.message_from_bytes
+  without specifying policy.  (Closes #934)
 
 Command line
 ------------



View it on GitLab: 
https://gitlab.com/mailman/mailman/-/compare/36056099377cda5e58ef60d6b1e5c826b141c520...26fd9a15a2e2d2f8c6a09da3a4232aa39ef80c71

-- 
View it on GitLab: 
https://gitlab.com/mailman/mailman/-/compare/36056099377cda5e58ef60d6b1e5c826b141c520...26fd9a15a2e2d2f8c6a09da3a4232aa39ef80c71
You're receiving this email because of your account on gitlab.com.


_______________________________________________
Mailman-checkins mailing list -- mailman-checkins@python.org
To unsubscribe send an email to mailman-checkins-le...@python.org
https://mail.python.org/mailman3/lists/mailman-checkins.python.org/
Member address: arch...@jab.org

Reply via email to