mailpipe: add extra attributes to errors even if current attribute is None.
authorW. Trevor King <wking@tremily.us>
Sun, 2 Sep 2012 15:30:50 +0000 (11:30 -0400)
committerW. Trevor King <wking@tremily.us>
Sun, 2 Sep 2012 15:30:50 +0000 (11:30 -0400)
With the old impementation, a default value of None would keep the
attribute from being added.  For example, if the error is raised with

  error.subject == None

`mailpipe` and `_parse_message` would not override that with their
known value.  Now they will.

pygrader/mailpipe.py

index 3f9f7c3d9bb69732ed1819a9157567bd03645c7b..f2a967188bba4ac71b3da6c65a81983482d78548 100644 (file)
@@ -590,12 +590,12 @@ def mailpipe(basedir, course, stream=None, mailbox=None, input_=None,
         except _InvalidMessage as error:
             error.course = course
             error.message = original
         except _InvalidMessage as error:
             error.course = course
             error.message = original
-            if person is not None and not hasattr(error, 'person'):
-                error.person = person
-            if subject is not None and not hasattr(error, 'subject'):
-                error.subject = subject
-            if target is not None and not hasattr(error, 'target'):
-                error.target = target
+            for attribute,value in [('person', person),
+                                    ('subject', subject),
+                                    ('target', target)]:
+                if (value is not None and
+                    getattr(error, attribute, None) is None):
+                    setattr(error, attribute, value)
             _LOG.warn('invalid message {}'.format(error.message_id()))
             if not continue_after_invalid_message:
                 raise
             _LOG.warn('invalid message {}'.format(error.message_id()))
             if not continue_after_invalid_message:
                 raise
@@ -710,12 +710,12 @@ def _parse_message(course, message, trust_email_infrastructure=False):
     except _InvalidMessage as error:
         error.course = course
         error.message = original
     except _InvalidMessage as error:
         error.course = course
         error.message = original
-        if person is not None and not hasattr(error, 'person'):
-            error.person = person
-        if subject is not None and not hasattr(error, 'subject'):
-            error.subject = subject
-        if target is not None and not hasattr(error, 'target'):
-            error.target = target
+        for attribute,value in [('person', person),
+                                ('subject', subject),
+                                ('target', target)]:
+            if (value is not None and
+                getattr(error, attribute, None) is None):
+                setattr(error, attribute, value)
         raise
     return (original, message, person, subject, target)
 
         raise
     return (original, message, person, subject, target)