Merged revisions 83198 via svnmerge from

svn+ssh://pythondev@svn.python.org/python/branches/release27-maint

........
  r83198 | victor.stinner | 2010-07-28 03:39:45 +0200 (mer., 28 juil. 2010) | 3 lines

  Issue #6213: Implement getstate() and setstate() methods of utf-8-sig and
  utf-16 incremental encoders.
........
This commit is contained in:
Victor Stinner 2010-07-28 01:55:43 +00:00
parent 7e384677cd
commit 082a65ab1f
3 changed files with 28 additions and 3 deletions

View File

@ -34,6 +34,22 @@ class IncrementalEncoder(codecs.IncrementalEncoder):
codecs.IncrementalEncoder.reset(self) codecs.IncrementalEncoder.reset(self)
self.encoder = None self.encoder = None
def getstate(self):
# state info we return to the caller:
# 0: stream is in natural order for this platform
# 2: endianness hasn't been determined yet
# (we're never writing in unnatural order)
return (2 if self.encoder is None else 0)
def setstate(self, state):
if state:
self.encoder = None
else:
if sys.byteorder == 'little':
self.encoder = codecs.utf_16_le_encode
else:
self.encoder = codecs.utf_16_be_encode
class IncrementalDecoder(codecs.BufferedIncrementalDecoder): class IncrementalDecoder(codecs.BufferedIncrementalDecoder):
def __init__(self, errors='strict'): def __init__(self, errors='strict'):
codecs.BufferedIncrementalDecoder.__init__(self, errors) codecs.BufferedIncrementalDecoder.__init__(self, errors)

View File

@ -25,18 +25,24 @@ def decode(input, errors='strict'):
class IncrementalEncoder(codecs.IncrementalEncoder): class IncrementalEncoder(codecs.IncrementalEncoder):
def __init__(self, errors='strict'): def __init__(self, errors='strict'):
codecs.IncrementalEncoder.__init__(self, errors) codecs.IncrementalEncoder.__init__(self, errors)
self.first = True self.first = 1
def encode(self, input, final=False): def encode(self, input, final=False):
if self.first: if self.first:
self.first = False self.first = 0
return codecs.BOM_UTF8 + codecs.utf_8_encode(input, self.errors)[0] return codecs.BOM_UTF8 + codecs.utf_8_encode(input, self.errors)[0]
else: else:
return codecs.utf_8_encode(input, self.errors)[0] return codecs.utf_8_encode(input, self.errors)[0]
def reset(self): def reset(self):
codecs.IncrementalEncoder.reset(self) codecs.IncrementalEncoder.reset(self)
self.first = True self.first = 1
def getstate(self):
return self.first
def setstate(self, state):
self.first = state
class IncrementalDecoder(codecs.BufferedIncrementalDecoder): class IncrementalDecoder(codecs.BufferedIncrementalDecoder):
def __init__(self, errors='strict'): def __init__(self, errors='strict'):

View File

@ -12,6 +12,9 @@ What's New in Python 2.6.6 alpha 1?
Core and Builtins Core and Builtins
----------------- -----------------
- Issue #6213: Implement getstate() and setstate() methods of utf-8-sig and
utf-16 incremental encoders.
- Issue #8271: during the decoding of an invalid UTF-8 byte sequence, only the - Issue #8271: during the decoding of an invalid UTF-8 byte sequence, only the
start byte and the continuation byte(s) are now considered invalid, instead start byte and the continuation byte(s) are now considered invalid, instead
of the number of bytes specified by the start byte. of the number of bytes specified by the start byte.