~sambuddhabasu1/mailman/fix_mailman_run_error

« back to all changes in this revision

Viewing changes to src/mailman/interfaces/address.py

  • Committer: Barry Warsaw
  • Date: 2012-03-22 22:29:58 UTC
  • Revision ID: barry@list.org-20120322222958-k1ufc2xivw8cv2d5
Several fixes and cleanups, ostensibly to fix Python 2.6 support.

- email.iterators.body_line_iterator() cannot handle unicodes in Python 2.6,
  because it uses cStringIO.StringIO under the covers, and *that* can't handle
  unicode.  This works fine in Python 2.7, so I override this for the tests
  only under 2.6 (the code itself isn't affected).

- AddressError needs to str() its IAddress attribute explicitly in the
  __str__() method, otherwise under Python 2.6, you'll get unprintable reprs
  in the doctests.  Again, this works correctly in 2.7, but EIBTI, so it can't
  hurt either way.

- EmailError: a new exception, not related to AddressError.  The reason for
  this it to conform to current nomenclature: "address" means an IAddress
  while "email" means a text email address.  So InvalidEmailAddressError
  now derives from EmailError instead of AddressError because it gets passed a
  text email address, and because that is invalid, it never gets turned into
  an IAddress.  The __str__() of this new base exception class does some
  tricky encoding to keep it compatible between Python 2.6 and 2.7.

- UnverifiedAddressError derives from AddressError instead of the more generic
  MailmanError.

- A few random code cleanups are included.

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
 
18
18
"""Interface for email address related information."""
19
19
 
20
 
from __future__ import absolute_import, unicode_literals
 
20
from __future__ import absolute_import, print_function, unicode_literals
21
21
 
22
22
__metaclass__ = type
23
23
__all__ = [
24
24
    'AddressAlreadyLinkedError',
25
25
    'AddressError',
26
26
    'AddressNotLinkedError',
 
27
    'EmailError',
27
28
    'ExistingAddressError',
28
29
    'IAddress',
29
30
    'IEmailValidator',
37
38
 
38
39
 
39
40
 
 
41
class EmailError(MailmanError):
 
42
    """A generic text email address-related error occurred."""
 
43
 
 
44
    def __init__(self, email):
 
45
        super(EmailError, self).__init__()
 
46
        self.email = email
 
47
 
 
48
    def __str__(self):
 
49
        # This is a workaround for Python 2.6 support.  When self.email
 
50
        # contains non-ascii characters, this will cause unprintable output in
 
51
        # doctests.  Python 2.7 can handle it but we haven't dropped support
 
52
        # for 2.6 yet.
 
53
        return self.email.encode('us-ascii', 'backslashreplace')
 
54
 
 
55
 
40
56
class AddressError(MailmanError):
41
 
    """A general address-related error occurred."""
 
57
    """A generic IAddress-related error occurred."""
42
58
 
43
59
    def __init__(self, address):
44
60
        super(AddressError, self).__init__()
45
61
        self.address = address
46
62
 
47
63
    def __str__(self):
48
 
        return self.address
 
64
        return str(self.address)
49
65
 
50
66
 
51
67
class ExistingAddressError(AddressError):
60
76
    """The address is not linked to the user."""
61
77
 
62
78
 
63
 
class InvalidEmailAddressError(AddressError):
 
79
class InvalidEmailAddressError(EmailError):
64
80
    """Email address is invalid."""
65
81
 
66
82