~sambuddhabasu1/mailman/fix_mailman_run_error

« back to all changes in this revision

Viewing changes to src/mailman/testing/helpers.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:
 
1
# Copyright (C) 2008-2012 by the Free Software Foundation, Inc.
1
2
#
2
3
# This file is part of GNU Mailman.
3
4
#
22
23
__all__ = [
23
24
    'LogFileMark',
24
25
    'TestableMaster',
 
26
    'body_line_iterator',
25
27
    'call_api',
26
28
    'digest_mbox',
27
29
    'event_subscribers',
412
414
        with open(self._filename) as fp:
413
415
            fp.seek(self._filepos)
414
416
            return fp.readline()
 
417
 
 
418
 
 
419
 
 
420
# In Python 2.6, body_line_iterator() uses a cStringIO.StringIO() which cannot
 
421
# handle unicode.  In Python 2.7 this works fine.  I hate version checks but
 
422
# this is the easiest way to handle it.  OTOH, we could just use the manual
 
423
# way for all Python versions instead.
 
424
import sys
 
425
if sys.hexversion >= 0x2070000:
 
426
    from email.iterators import body_line_iterator
 
427
else:
 
428
    def body_line_iterator(msg, decode=False):
 
429
        payload = msg.get_payload(decode=decode)
 
430
        bytes_payload = payload.encode('utf-8')
 
431
        for line in bytes_payload.splitlines():
 
432
            yield line