So, umm, I went ahead and had a crack at this.

It turns out this is hideously difficult to get right. But this plus my
other patch to fix Thomas' problem should have things working on Py2 and
Py3 with this series.

It's a bit of a work in progress: I need to close the file at the end
of the function, the logging needs to be added again, etc.

Tests to come.

Stephen: I can do this up into a proper patch if you like or you can
fold it into your series.


diff --git a/patchwork/management/commands/ 
index a60e2ad11f06..2b957473167f 100644
--- a/patchwork/management/commands/
+++ b/patchwork/management/commands/
@@ -18,10 +18,11 @@
 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 import argparse
-from email import message_from_file
+import email
 import logging
 from optparse import make_option
 import sys
+import codecs
 import django
 from import base
@@ -48,9 +49,9 @@ class Command(base.BaseCommand):
-                type=argparse.FileType('r'),
-                default=sys.stdin,
-                help='input mbox file (a filename or stdin)')
+                type=str,
+                default=None,
+                help='input mbox file (stdin if not provided)')
                 help='mailing list ID. If not supplied, this will be '
@@ -59,17 +60,19 @@ class Command(base.BaseCommand):
     def handle(self, *args, **options):
         infile = args[0] if args else options['infile']
-        # Attempt to parse the path if provided, and fallback to stdin if not
-        if infile and isinstance(infile, six.string_types):  # Django < 1.8
-  'Parsing mail loaded by filename')
-            with open(infile, 'r+') as file_:
-                mail = message_from_file(file_)
+        if six.PY3:
+            if infile:
+                file_ = open(infile, 'r', encoding='utf-8', errors='replace')
+            else:
+                file_ = codecs.getreader('utf-8')(sys.stdin.buffer, 
-            if infile == sys.stdin:
-      'Parsing mail loaded from stdin')
-            else:  # Djano >= 1.8
-      'Parsing mail loaded by filename')
-            mail = message_from_file(infile)
+            if infile:
+                file_ = open(infile, 'r')
+            else:
+                file_ = codecs.getreader('utf-8')(sys.stdin, errors='replace')
+        mail = email.message_from_file(file_)
             result = parse_mail(mail, options['list_id'])
             if result:

Attachment: signature.asc
Description: PGP signature

Patchwork mailing list

Reply via email to