Changeset 1456

Show
Ignore:
Timestamp:
07/05/08 09:42:59 (5 months ago)
Author:
rob
Message:

Normalize the encoding name by running it through the standard python codec list

Location:
trunk
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • trunk/peppy/stcbase.py

    r1454 r1456  
    11# peppy Copyright (c) 2006-2008 Rob McMullen 
    22# Licenced under the GPLv2; see http://peppy.flipturn.org for more info 
    3 import os, re, time 
     3import os, re, time, codecs 
    44 
    55import wx 
     
    219219         
    220220        if encoding: 
    221             self.refstc.encoding = encoding 
     221            # Normalize the encoding name by running it through the codecs list 
     222            self.refstc.encoding = codecs.lookup(encoding).name 
    222223        if not self.refstc.encoding: 
    223224            self.refstc.encoding = detectEncoding(header) 
     
    305306                    self.refstc.encoding = encoding 
    306307                    self.decodeText(txt) 
     308            elif self.refstc.encoding: 
     309                if self.refstc.encoding != 'utf-8': 
     310                    txt = txt.encode(self.refstc.encoding) 
    307311            else: 
    308312                # Have to use GetStyledText because GetText will truncate the 
  • trunk/tests/test_unicode.py

    r1039 r1456  
    1818        self.utf8 = '# -*- coding: UTF-8 -*-\naacute:\xc3\xa1 ntilde:\xc3\xb1' 
    1919        self.latin1= '# -*- coding: latin-1 -*-\n' + ''.join([chr(i) for i in range(160, 255)]) 
     20        self.latin1_unmarked= ''.join([chr(i) for i in range(160, 255)]) 
    2021 
    2122    def testUTF8(self): 
     
    2728     
    2829    def testChangeLatin(self): 
    29         self.stc.encoding = detectEncoding(self.utf8) 
    30         print self.stc.encoding 
     30        self.stc.refstc.encoding = detectEncoding(self.utf8) 
     31        print self.stc.refstc.encoding 
    3132        self.stc.decodeText(self.utf8) 
    3233        unicode1 = self.stc.GetLine(1) 
     
    4344 
    4445    def testLatin1(self): 
    45         self.stc.encoding = detectEncoding(self.latin1) 
     46        self.stc.refstc.encoding = detectEncoding(self.latin1) 
    4647        print repr(self.latin1) 
    4748        utf8 = unicode(self.latin1, "iso-8859-1").encode('utf-8') 
     
    5152        self.stc.decodeText(self.latin1) 
    5253        print repr(self.stc.GetText()) 
     54        print self.stc.refstc.encoding 
    5355        self.stc.prepareEncoding() 
    5456        print "encoded: " + repr(self.stc.refstc.encoded) 
    5557        assert self.latin1 == self.stc.refstc.encoded 
     58 
     59    def testLatin1Unmarked(self): 
     60        self.stc.refstc.encoding = detectEncoding(self.latin1_unmarked) 
     61        print repr(self.latin1_unmarked) 
     62        utf8 = self.latin1_unmarked.decode('latin-1') 
     63        print repr(utf8) 
     64        print repr(utf8.encode('latin-1')) 
     65        assert utf8.encode('latin-1') == self.latin1_unmarked 
     66        self.stc.decodeText(self.latin1_unmarked) 
     67        print repr(self.stc.GetText()) 
     68        # There's no encoding marker in the text, so there shouldn't be an 
     69        # encoding listed 
     70        assert self.stc.refstc.encoding == None 
     71        self.stc.prepareEncoding() 
     72        print "encoded: " + repr(self.stc.refstc.encoded) 
     73        assert self.latin1_unmarked == self.stc.refstc.encoded