Skip to content

Commit

Permalink
Merge imap4-utf7-registration-3663
Browse files Browse the repository at this point in the history
Author: exarkun
Reviewer: glyph
Fixes: twisted#3663

Update the IMAP4-UTF7 codec registration to use the preferred Python 2.5 behavior
of returning a CodecInfo instance instead of a four tuple.  This causes the
codecs.getreader and codecs.getwriter functions to work for this encoding (again).
Also fix the implementation of the stream writer to define the correct method,
`encode`, instead of the incorrect one, `decode`.


git-svn-id: svn://svn.twistedmatrix.com/svn/Twisted/trunk@26479 bbbe8e31-12d6-0310-92fd-ac37d47ddeeb
  • Loading branch information
exarkun committed Mar 20, 2009
1 parent d4daf6d commit 203a6dc
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 7 deletions.
12 changes: 10 additions & 2 deletions twisted/mail/imap4.py
Original file line number Diff line number Diff line change
Expand Up @@ -5471,12 +5471,20 @@ def decode(self, s, errors='strict'):
return decoder(s)

class StreamWriter(codecs.StreamWriter):
def decode(self, s, errors='strict'):
def encode(self, s, errors='strict'):
return encoder(s)

_codecInfo = (encoder, decoder, StreamReader, StreamWriter)
try:
_codecInfoClass = codecs.CodecInfo
except AttributeError:
pass
else:
_codecInfo = _codecInfoClass(*_codecInfo)

def imap4_utf_7(name):
if name == 'imap4-utf-7':
return (encoder, decoder, StreamReader, StreamWriter)
return _codecInfo
codecs.register(imap4_utf_7)

__all__ = [
Expand Down
46 changes: 41 additions & 5 deletions twisted/mail/test/test_imap.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import os
import types
import codecs

from zope.interface import implements

Expand Down Expand Up @@ -87,23 +88,58 @@ def test_decodeWithErrors(self):
bytes.decode('imap4-utf-7'))


def testEncode(self):
def test_getreader(self):
"""
C{codecs.getreader('imap4-utf-7')} returns the I{imap4-utf-7} stream
reader class.
"""
reader = codecs.getreader('imap4-utf-7')(StringIO('Hello&AP8-world'))
self.assertEquals(reader.read(), u'Hello\xffworld')


def test_getwriter(self):
"""
C{codecs.getwriter('imap4-utf-7')} returns the I{imap4-utf-7} stream
writer class.
"""
output = StringIO()
writer = codecs.getwriter('imap4-utf-7')(output)
writer.write(u'Hello\xffworld')
self.assertEquals(output.getvalue(), 'Hello&AP8-world')


def test_encode(self):
"""
The I{imap4-utf-7} can be used to encode a unicode string into a byte
string according to the IMAP4 modified UTF-7 encoding rules.
"""
for (input, output) in self.tests:
self.assertEquals(input.encode('imap4-utf-7'), output)

def testDecode(self):

def test_decode(self):
"""
The I{imap4-utf-7} can be used to decode a byte string into a unicode
string according to the IMAP4 modified UTF-7 encoding rules.
"""
for (input, output) in self.tests:
# XXX - Piece of *crap* 2.1
self.assertEquals(input, imap4.decoder(output)[0])
self.assertEquals(input, output.decode('imap4-utf-7'))


def testPrintableSingletons(self):
def test_printableSingletons(self):
"""
The IMAP4 modified UTF-7 implementation encodes all printable
characters which are in ASCII using the corresponding ASCII byte.
"""
# All printables represent themselves
for o in range(0x20, 0x26) + range(0x27, 0x7f):
self.failUnlessEqual(chr(o), chr(o).encode('imap4-utf-7'))
self.failUnlessEqual(chr(o), chr(o).decode('imap4-utf-7'))
self.failUnlessEqual('&'.encode('imap4-utf-7'), '&-')
self.failUnlessEqual('&-'.decode('imap4-utf-7'), '&')



class BufferingConsumer:
def __init__(self):
self.buffer = []
Expand Down

0 comments on commit 203a6dc

Please sign in to comment.