mirror of
https://github.com/python/cpython.git
synced 2025-12-01 23:32:16 +00:00
Issue #6213: Implement getstate() and setstate() methods of utf-8-sig and
utf-16 incremental encoders.
This commit is contained in:
parent
3ff4463b35
commit
73363e817a
4 changed files with 28 additions and 5 deletions
|
|
@ -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)
|
||||||
|
|
|
||||||
|
|
@ -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'):
|
||||||
|
|
|
||||||
|
|
@ -2087,7 +2087,6 @@ class TextIOWrapperTest(unittest.TestCase):
|
||||||
|
|
||||||
self.assertEqual(buffer.seekable(), txt.seekable())
|
self.assertEqual(buffer.seekable(), txt.seekable())
|
||||||
|
|
||||||
@unittest.skip("Issue #6213 with incremental encoders")
|
|
||||||
def test_append_bom(self):
|
def test_append_bom(self):
|
||||||
# The BOM is not written again when appending to a non-empty file
|
# The BOM is not written again when appending to a non-empty file
|
||||||
filename = support.TESTFN
|
filename = support.TESTFN
|
||||||
|
|
@ -2103,7 +2102,6 @@ class TextIOWrapperTest(unittest.TestCase):
|
||||||
with self.open(filename, 'rb') as f:
|
with self.open(filename, 'rb') as f:
|
||||||
self.assertEquals(f.read(), 'aaaxxx'.encode(charset))
|
self.assertEquals(f.read(), 'aaaxxx'.encode(charset))
|
||||||
|
|
||||||
@unittest.skip("Issue #6213 with incremental encoders")
|
|
||||||
def test_seek_bom(self):
|
def test_seek_bom(self):
|
||||||
# Same test, but when seeking manually
|
# Same test, but when seeking manually
|
||||||
filename = support.TESTFN
|
filename = support.TESTFN
|
||||||
|
|
|
||||||
|
|
@ -18,6 +18,9 @@ Core and Builtins
|
||||||
Library
|
Library
|
||||||
-------
|
-------
|
||||||
|
|
||||||
|
- Issue #6213: Implement getstate() and setstate() methods of utf-8-sig and
|
||||||
|
utf-16 incremental encoders.
|
||||||
|
|
||||||
- Issue #7113: Speed up loading in ConfigParser. Patch by Łukasz Langa.
|
- Issue #7113: Speed up loading in ConfigParser. Patch by Łukasz Langa.
|
||||||
|
|
||||||
- Issue #3704: cookielib was not properly handling URLs with a / in the
|
- Issue #3704: cookielib was not properly handling URLs with a / in the
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue