PyXR

c:\python24\lib \ encodings \ utf_16.py



0001 """ Python 'utf-16' Codec
0002 
0003 
0004 Written by Marc-Andre Lemburg (mal@lemburg.com).
0005 
0006 (c) Copyright CNRI, All Rights Reserved. NO WARRANTY.
0007 
0008 """
0009 import codecs, sys
0010 
0011 ### Codec APIs
0012 
0013 encode = codecs.utf_16_encode
0014 
0015 def decode(input, errors='strict'):
0016     return codecs.utf_16_decode(input, errors, True)
0017 
0018 class StreamWriter(codecs.StreamWriter):
0019     def __init__(self, stream, errors='strict'):
0020         self.bom_written = False
0021         codecs.StreamWriter.__init__(self, stream, errors)
0022 
0023     def encode(self, input, errors='strict'):
0024         self.bom_written = True
0025         result = codecs.utf_16_encode(input, errors)
0026         if sys.byteorder == 'little':
0027             self.encode = codecs.utf_16_le_encode
0028         else:
0029             self.encode = codecs.utf_16_be_encode
0030         return result
0031 
0032 class StreamReader(codecs.StreamReader):
0033 
0034     def decode(self, input, errors='strict'):
0035         (object, consumed, byteorder) = \
0036             codecs.utf_16_ex_decode(input, errors, 0, False)
0037         if byteorder == -1:
0038             self.decode = codecs.utf_16_le_decode
0039         elif byteorder == 1:
0040             self.decode = codecs.utf_16_be_decode
0041         elif consumed>=2:
0042             raise UnicodeError,"UTF-16 stream does not start with BOM"
0043         return (object, consumed)
0044 
0045 ### encodings module API
0046 
0047 def getregentry():
0048 
0049     return (encode,decode,StreamReader,StreamWriter)
0050 

Generated by PyXR 0.9.4
SourceForge.net Logo