mirror of
https://github.com/python/cpython.git
synced 2025-08-18 15:51:23 +00:00
Issue #8796: codecs.open() calls the builtin open() function instead of using
StreamReaderWriter. Deprecate StreamReader, StreamWriter, StreamReaderWriter, StreamRecoder and EncodedFile() of the codec module. Use the builtin open() function or io.TextIOWrapper instead.
This commit is contained in:
parent
c556e10b94
commit
98fe1a0c3b
4 changed files with 148 additions and 59 deletions
|
@ -85,6 +85,9 @@ It defines the following functions:
|
||||||
In case a search function cannot find a given encoding, it should return
|
In case a search function cannot find a given encoding, it should return
|
||||||
``None``.
|
``None``.
|
||||||
|
|
||||||
|
.. deprecated:: 3.3
|
||||||
|
*streamreader* and *streamwriter* attributes are now deprecated.
|
||||||
|
|
||||||
|
|
||||||
.. function:: lookup(encoding)
|
.. function:: lookup(encoding)
|
||||||
|
|
||||||
|
@ -139,6 +142,8 @@ functions which use :func:`lookup` for the codec lookup:
|
||||||
|
|
||||||
Raises a :exc:`LookupError` in case the encoding cannot be found.
|
Raises a :exc:`LookupError` in case the encoding cannot be found.
|
||||||
|
|
||||||
|
.. deprecated:: 3.3
|
||||||
|
|
||||||
|
|
||||||
.. function:: getwriter(encoding)
|
.. function:: getwriter(encoding)
|
||||||
|
|
||||||
|
@ -147,6 +152,8 @@ functions which use :func:`lookup` for the codec lookup:
|
||||||
|
|
||||||
Raises a :exc:`LookupError` in case the encoding cannot be found.
|
Raises a :exc:`LookupError` in case the encoding cannot be found.
|
||||||
|
|
||||||
|
.. deprecated:: 3.3
|
||||||
|
|
||||||
|
|
||||||
.. function:: register_error(name, error_handler)
|
.. function:: register_error(name, error_handler)
|
||||||
|
|
||||||
|
@ -215,6 +222,11 @@ utility functions:
|
||||||
providing transparent encoding/decoding. The default file mode is ``'r'``
|
providing transparent encoding/decoding. The default file mode is ``'r'``
|
||||||
meaning to open the file in read mode.
|
meaning to open the file in read mode.
|
||||||
|
|
||||||
|
.. note::
|
||||||
|
|
||||||
|
This function is kept for backward compatibility with Python 2, the
|
||||||
|
builtin :func:`open` function should be used instead.
|
||||||
|
|
||||||
.. note::
|
.. note::
|
||||||
|
|
||||||
The wrapped version's methods will accept and return strings only. Bytes
|
The wrapped version's methods will accept and return strings only. Bytes
|
||||||
|
@ -251,6 +263,8 @@ utility functions:
|
||||||
``'strict'``, which causes :exc:`ValueError` to be raised in case an encoding
|
``'strict'``, which causes :exc:`ValueError` to be raised in case an encoding
|
||||||
error occurs.
|
error occurs.
|
||||||
|
|
||||||
|
.. deprecated:: 3.3
|
||||||
|
|
||||||
|
|
||||||
.. function:: iterencode(iterator, encoding, errors='strict', **kwargs)
|
.. function:: iterencode(iterator, encoding, errors='strict', **kwargs)
|
||||||
|
|
||||||
|
@ -563,6 +577,9 @@ The :class:`StreamWriter` class is a subclass of :class:`Codec` and defines the
|
||||||
following methods which every stream writer must define in order to be
|
following methods which every stream writer must define in order to be
|
||||||
compatible with the Python codec registry.
|
compatible with the Python codec registry.
|
||||||
|
|
||||||
|
.. deprecated:: 3.3
|
||||||
|
Use the builtin the :class:`io.TextIOWrapper` class.
|
||||||
|
|
||||||
|
|
||||||
.. class:: StreamWriter(stream[, errors])
|
.. class:: StreamWriter(stream[, errors])
|
||||||
|
|
||||||
|
@ -628,6 +645,9 @@ The :class:`StreamReader` class is a subclass of :class:`Codec` and defines the
|
||||||
following methods which every stream reader must define in order to be
|
following methods which every stream reader must define in order to be
|
||||||
compatible with the Python codec registry.
|
compatible with the Python codec registry.
|
||||||
|
|
||||||
|
.. deprecated:: 3.3
|
||||||
|
Use the builtin the :class:`io.TextIOWrapper` class.
|
||||||
|
|
||||||
|
|
||||||
.. class:: StreamReader(stream[, errors])
|
.. class:: StreamReader(stream[, errors])
|
||||||
|
|
||||||
|
@ -728,6 +748,9 @@ and write modes.
|
||||||
The design is such that one can use the factory functions returned by the
|
The design is such that one can use the factory functions returned by the
|
||||||
:func:`lookup` function to construct the instance.
|
:func:`lookup` function to construct the instance.
|
||||||
|
|
||||||
|
.. deprecated:: 3.3
|
||||||
|
Use the :class:`io.TextIOWrapper` class.
|
||||||
|
|
||||||
|
|
||||||
.. class:: StreamReaderWriter(stream, Reader, Writer, errors)
|
.. class:: StreamReaderWriter(stream, Reader, Writer, errors)
|
||||||
|
|
||||||
|
@ -752,6 +775,8 @@ which is sometimes useful when dealing with different encoding environments.
|
||||||
The design is such that one can use the factory functions returned by the
|
The design is such that one can use the factory functions returned by the
|
||||||
:func:`lookup` function to construct the instance.
|
:func:`lookup` function to construct the instance.
|
||||||
|
|
||||||
|
.. deprecated:: 3.3
|
||||||
|
|
||||||
|
|
||||||
.. class:: StreamRecoder(stream, encode, decode, Reader, Writer, errors)
|
.. class:: StreamRecoder(stream, encode, decode, Reader, Writer, errors)
|
||||||
|
|
||||||
|
|
|
@ -345,6 +345,8 @@ class StreamWriter(Codec):
|
||||||
The set of allowed parameter values can be extended via
|
The set of allowed parameter values can be extended via
|
||||||
register_error.
|
register_error.
|
||||||
"""
|
"""
|
||||||
|
import warnings
|
||||||
|
warnings.warn('use io.TextIOWrapper', DeprecationWarning, stacklevel=2)
|
||||||
self.stream = stream
|
self.stream = stream
|
||||||
self.errors = errors
|
self.errors = errors
|
||||||
|
|
||||||
|
@ -416,6 +418,8 @@ class StreamReader(Codec):
|
||||||
The set of allowed parameter values can be extended via
|
The set of allowed parameter values can be extended via
|
||||||
register_error.
|
register_error.
|
||||||
"""
|
"""
|
||||||
|
import warnings
|
||||||
|
warnings.warn('use io.TextIOWrapper', DeprecationWarning, stacklevel=2)
|
||||||
self.stream = stream
|
self.stream = stream
|
||||||
self.errors = errors
|
self.errors = errors
|
||||||
self.bytebuffer = b""
|
self.bytebuffer = b""
|
||||||
|
@ -846,7 +850,7 @@ class StreamRecoder:
|
||||||
|
|
||||||
### Shortcuts
|
### Shortcuts
|
||||||
|
|
||||||
def open(filename, mode='rb', encoding=None, errors='strict', buffering=1):
|
def open(filename, mode='r', encoding=None, errors=None, buffering=1):
|
||||||
|
|
||||||
""" Open an encoded file using the given mode and return
|
""" Open an encoded file using the given mode and return
|
||||||
a wrapped version providing transparent encoding/decoding.
|
a wrapped version providing transparent encoding/decoding.
|
||||||
|
@ -877,18 +881,13 @@ def open(filename, mode='rb', encoding=None, errors='strict', buffering=1):
|
||||||
parameter.
|
parameter.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
if encoding is not None and \
|
if encoding is not None:
|
||||||
'b' not in mode:
|
return builtins.open(filename, mode, buffering,
|
||||||
# Force opening of the file in binary mode
|
encoding, errors, newline='')
|
||||||
|
else:
|
||||||
|
if 'b' not in mode:
|
||||||
mode = mode + 'b'
|
mode = mode + 'b'
|
||||||
file = builtins.open(filename, mode, buffering)
|
return builtins.open(filename, mode, buffering, encoding, errors)
|
||||||
if encoding is None:
|
|
||||||
return file
|
|
||||||
info = lookup(encoding)
|
|
||||||
srw = StreamReaderWriter(file, info.streamreader, info.streamwriter, errors)
|
|
||||||
# Add attributes to simplify introspection
|
|
||||||
srw.encoding = encoding
|
|
||||||
return srw
|
|
||||||
|
|
||||||
def EncodedFile(file, data_encoding, file_encoding=None, errors='strict'):
|
def EncodedFile(file, data_encoding, file_encoding=None, errors='strict'):
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,10 @@
|
||||||
from test import support
|
from test import support
|
||||||
import unittest
|
import _testcapi
|
||||||
import codecs
|
import codecs
|
||||||
import sys, _testcapi, io
|
import io
|
||||||
|
import sys
|
||||||
|
import unittest
|
||||||
|
import warnings
|
||||||
|
|
||||||
class Queue(object):
|
class Queue(object):
|
||||||
"""
|
"""
|
||||||
|
@ -63,6 +66,8 @@ class ReadTest(unittest.TestCase, MixInCheckStateHandling):
|
||||||
# the StreamReader and check that the results equal the appropriate
|
# the StreamReader and check that the results equal the appropriate
|
||||||
# entries from partialresults.
|
# entries from partialresults.
|
||||||
q = Queue(b"")
|
q = Queue(b"")
|
||||||
|
with warnings.catch_warnings():
|
||||||
|
warnings.simplefilter("ignore", DeprecationWarning)
|
||||||
r = codecs.getreader(self.encoding)(q)
|
r = codecs.getreader(self.encoding)(q)
|
||||||
result = ""
|
result = ""
|
||||||
for (c, partialresult) in zip(input.encode(self.encoding), partialresults):
|
for (c, partialresult) in zip(input.encode(self.encoding), partialresults):
|
||||||
|
@ -106,6 +111,8 @@ class ReadTest(unittest.TestCase, MixInCheckStateHandling):
|
||||||
return codecs.getreader(self.encoding)(stream)
|
return codecs.getreader(self.encoding)(stream)
|
||||||
|
|
||||||
def readalllines(input, keepends=True, size=None):
|
def readalllines(input, keepends=True, size=None):
|
||||||
|
with warnings.catch_warnings():
|
||||||
|
warnings.simplefilter("ignore", DeprecationWarning)
|
||||||
reader = getreader(input)
|
reader = getreader(input)
|
||||||
lines = []
|
lines = []
|
||||||
while True:
|
while True:
|
||||||
|
@ -215,12 +222,16 @@ class ReadTest(unittest.TestCase, MixInCheckStateHandling):
|
||||||
' \r\n',
|
' \r\n',
|
||||||
]
|
]
|
||||||
stream = io.BytesIO("".join(s).encode(self.encoding))
|
stream = io.BytesIO("".join(s).encode(self.encoding))
|
||||||
|
with warnings.catch_warnings():
|
||||||
|
warnings.simplefilter("ignore", DeprecationWarning)
|
||||||
reader = codecs.getreader(self.encoding)(stream)
|
reader = codecs.getreader(self.encoding)(stream)
|
||||||
for (i, line) in enumerate(reader):
|
for (i, line) in enumerate(reader):
|
||||||
self.assertEqual(line, s[i])
|
self.assertEqual(line, s[i])
|
||||||
|
|
||||||
def test_readlinequeue(self):
|
def test_readlinequeue(self):
|
||||||
q = Queue(b"")
|
q = Queue(b"")
|
||||||
|
with warnings.catch_warnings():
|
||||||
|
warnings.simplefilter("ignore", DeprecationWarning)
|
||||||
writer = codecs.getwriter(self.encoding)(q)
|
writer = codecs.getwriter(self.encoding)(q)
|
||||||
reader = codecs.getreader(self.encoding)(q)
|
reader = codecs.getreader(self.encoding)(q)
|
||||||
|
|
||||||
|
@ -253,6 +264,8 @@ class ReadTest(unittest.TestCase, MixInCheckStateHandling):
|
||||||
|
|
||||||
s = (s1+s2+s3).encode(self.encoding)
|
s = (s1+s2+s3).encode(self.encoding)
|
||||||
stream = io.BytesIO(s)
|
stream = io.BytesIO(s)
|
||||||
|
with warnings.catch_warnings():
|
||||||
|
warnings.simplefilter("ignore", DeprecationWarning)
|
||||||
reader = codecs.getreader(self.encoding)(stream)
|
reader = codecs.getreader(self.encoding)(stream)
|
||||||
self.assertEqual(reader.readline(), s1)
|
self.assertEqual(reader.readline(), s1)
|
||||||
self.assertEqual(reader.readline(), s2)
|
self.assertEqual(reader.readline(), s2)
|
||||||
|
@ -268,6 +281,8 @@ class ReadTest(unittest.TestCase, MixInCheckStateHandling):
|
||||||
|
|
||||||
s = (s1+s2+s3+s4+s5).encode(self.encoding)
|
s = (s1+s2+s3+s4+s5).encode(self.encoding)
|
||||||
stream = io.BytesIO(s)
|
stream = io.BytesIO(s)
|
||||||
|
with warnings.catch_warnings():
|
||||||
|
warnings.simplefilter("ignore", DeprecationWarning)
|
||||||
reader = codecs.getreader(self.encoding)(stream)
|
reader = codecs.getreader(self.encoding)(stream)
|
||||||
self.assertEqual(reader.readline(), s1)
|
self.assertEqual(reader.readline(), s1)
|
||||||
self.assertEqual(reader.readline(), s2)
|
self.assertEqual(reader.readline(), s2)
|
||||||
|
@ -290,6 +305,8 @@ class UTF32Test(ReadTest):
|
||||||
_,_,reader,writer = codecs.lookup(self.encoding)
|
_,_,reader,writer = codecs.lookup(self.encoding)
|
||||||
# encode some stream
|
# encode some stream
|
||||||
s = io.BytesIO()
|
s = io.BytesIO()
|
||||||
|
with warnings.catch_warnings():
|
||||||
|
warnings.simplefilter("ignore", DeprecationWarning)
|
||||||
f = writer(s)
|
f = writer(s)
|
||||||
f.write("spam")
|
f.write("spam")
|
||||||
f.write("spam")
|
f.write("spam")
|
||||||
|
@ -298,15 +315,21 @@ class UTF32Test(ReadTest):
|
||||||
self.assertTrue(d == self.spamle or d == self.spambe)
|
self.assertTrue(d == self.spamle or d == self.spambe)
|
||||||
# try to read it back
|
# try to read it back
|
||||||
s = io.BytesIO(d)
|
s = io.BytesIO(d)
|
||||||
|
with warnings.catch_warnings():
|
||||||
|
warnings.simplefilter("ignore", DeprecationWarning)
|
||||||
f = reader(s)
|
f = reader(s)
|
||||||
self.assertEqual(f.read(), "spamspam")
|
self.assertEqual(f.read(), "spamspam")
|
||||||
|
|
||||||
def test_badbom(self):
|
def test_badbom(self):
|
||||||
s = io.BytesIO(4*b"\xff")
|
s = io.BytesIO(4*b"\xff")
|
||||||
|
with warnings.catch_warnings():
|
||||||
|
warnings.simplefilter("ignore", DeprecationWarning)
|
||||||
f = codecs.getreader(self.encoding)(s)
|
f = codecs.getreader(self.encoding)(s)
|
||||||
self.assertRaises(UnicodeError, f.read)
|
self.assertRaises(UnicodeError, f.read)
|
||||||
|
|
||||||
s = io.BytesIO(8*b"\xff")
|
s = io.BytesIO(8*b"\xff")
|
||||||
|
with warnings.catch_warnings():
|
||||||
|
warnings.simplefilter("ignore", DeprecationWarning)
|
||||||
f = codecs.getreader(self.encoding)(s)
|
f = codecs.getreader(self.encoding)(s)
|
||||||
self.assertRaises(UnicodeError, f.read)
|
self.assertRaises(UnicodeError, f.read)
|
||||||
|
|
||||||
|
@ -454,6 +477,8 @@ class UTF16Test(ReadTest):
|
||||||
_,_,reader,writer = codecs.lookup(self.encoding)
|
_,_,reader,writer = codecs.lookup(self.encoding)
|
||||||
# encode some stream
|
# encode some stream
|
||||||
s = io.BytesIO()
|
s = io.BytesIO()
|
||||||
|
with warnings.catch_warnings():
|
||||||
|
warnings.simplefilter("ignore", DeprecationWarning)
|
||||||
f = writer(s)
|
f = writer(s)
|
||||||
f.write("spam")
|
f.write("spam")
|
||||||
f.write("spam")
|
f.write("spam")
|
||||||
|
@ -462,15 +487,21 @@ class UTF16Test(ReadTest):
|
||||||
self.assertTrue(d == self.spamle or d == self.spambe)
|
self.assertTrue(d == self.spamle or d == self.spambe)
|
||||||
# try to read it back
|
# try to read it back
|
||||||
s = io.BytesIO(d)
|
s = io.BytesIO(d)
|
||||||
|
with warnings.catch_warnings():
|
||||||
|
warnings.simplefilter("ignore", DeprecationWarning)
|
||||||
f = reader(s)
|
f = reader(s)
|
||||||
self.assertEqual(f.read(), "spamspam")
|
self.assertEqual(f.read(), "spamspam")
|
||||||
|
|
||||||
def test_badbom(self):
|
def test_badbom(self):
|
||||||
s = io.BytesIO(b"\xff\xff")
|
s = io.BytesIO(b"\xff\xff")
|
||||||
|
with warnings.catch_warnings():
|
||||||
|
warnings.simplefilter("ignore", DeprecationWarning)
|
||||||
f = codecs.getreader(self.encoding)(s)
|
f = codecs.getreader(self.encoding)(s)
|
||||||
self.assertRaises(UnicodeError, f.read)
|
self.assertRaises(UnicodeError, f.read)
|
||||||
|
|
||||||
s = io.BytesIO(b"\xff\xff\xff\xff")
|
s = io.BytesIO(b"\xff\xff\xff\xff")
|
||||||
|
with warnings.catch_warnings():
|
||||||
|
warnings.simplefilter("ignore", DeprecationWarning)
|
||||||
f = codecs.getreader(self.encoding)(s)
|
f = codecs.getreader(self.encoding)(s)
|
||||||
self.assertRaises(UnicodeError, f.read)
|
self.assertRaises(UnicodeError, f.read)
|
||||||
|
|
||||||
|
@ -517,7 +548,8 @@ class UTF16Test(ReadTest):
|
||||||
self.addCleanup(support.unlink, support.TESTFN)
|
self.addCleanup(support.unlink, support.TESTFN)
|
||||||
with open(support.TESTFN, 'wb') as fp:
|
with open(support.TESTFN, 'wb') as fp:
|
||||||
fp.write(s)
|
fp.write(s)
|
||||||
with codecs.open(support.TESTFN, 'U', encoding=self.encoding) as reader:
|
with codecs.open(support.TESTFN, 'U',
|
||||||
|
encoding=self.encoding) as reader:
|
||||||
self.assertEqual(reader.read(), s1)
|
self.assertEqual(reader.read(), s1)
|
||||||
|
|
||||||
class UTF16LETest(ReadTest):
|
class UTF16LETest(ReadTest):
|
||||||
|
@ -705,6 +737,8 @@ class UTF8SigTest(ReadTest):
|
||||||
reader = codecs.getreader("utf-8-sig")
|
reader = codecs.getreader("utf-8-sig")
|
||||||
for sizehint in [None] + list(range(1, 11)) + \
|
for sizehint in [None] + list(range(1, 11)) + \
|
||||||
[64, 128, 256, 512, 1024]:
|
[64, 128, 256, 512, 1024]:
|
||||||
|
with warnings.catch_warnings():
|
||||||
|
warnings.simplefilter("ignore", DeprecationWarning)
|
||||||
istream = reader(io.BytesIO(bytestring))
|
istream = reader(io.BytesIO(bytestring))
|
||||||
ostream = io.StringIO()
|
ostream = io.StringIO()
|
||||||
while 1:
|
while 1:
|
||||||
|
@ -727,6 +761,8 @@ class UTF8SigTest(ReadTest):
|
||||||
reader = codecs.getreader("utf-8-sig")
|
reader = codecs.getreader("utf-8-sig")
|
||||||
for sizehint in [None] + list(range(1, 11)) + \
|
for sizehint in [None] + list(range(1, 11)) + \
|
||||||
[64, 128, 256, 512, 1024]:
|
[64, 128, 256, 512, 1024]:
|
||||||
|
with warnings.catch_warnings():
|
||||||
|
warnings.simplefilter("ignore", DeprecationWarning)
|
||||||
istream = reader(io.BytesIO(bytestring))
|
istream = reader(io.BytesIO(bytestring))
|
||||||
ostream = io.StringIO()
|
ostream = io.StringIO()
|
||||||
while 1:
|
while 1:
|
||||||
|
@ -749,6 +785,8 @@ class EscapeDecodeTest(unittest.TestCase):
|
||||||
class RecodingTest(unittest.TestCase):
|
class RecodingTest(unittest.TestCase):
|
||||||
def test_recoding(self):
|
def test_recoding(self):
|
||||||
f = io.BytesIO()
|
f = io.BytesIO()
|
||||||
|
with warnings.catch_warnings():
|
||||||
|
warnings.simplefilter("ignore", DeprecationWarning)
|
||||||
f2 = codecs.EncodedFile(f, "unicode_internal", "utf-8")
|
f2 = codecs.EncodedFile(f, "unicode_internal", "utf-8")
|
||||||
f2.write("a")
|
f2.write("a")
|
||||||
f2.close()
|
f2.close()
|
||||||
|
@ -1126,6 +1164,8 @@ class IDNACodecTest(unittest.TestCase):
|
||||||
self.assertEqual("pyth\xf6n.org.".encode("idna"), b"xn--pythn-mua.org.")
|
self.assertEqual("pyth\xf6n.org.".encode("idna"), b"xn--pythn-mua.org.")
|
||||||
|
|
||||||
def test_stream(self):
|
def test_stream(self):
|
||||||
|
with warnings.catch_warnings():
|
||||||
|
warnings.simplefilter("ignore", DeprecationWarning)
|
||||||
r = codecs.getreader("idna")(io.BytesIO(b"abc"))
|
r = codecs.getreader("idna")(io.BytesIO(b"abc"))
|
||||||
r.read(3)
|
r.read(3)
|
||||||
self.assertEqual(r.read(), "")
|
self.assertEqual(r.read(), "")
|
||||||
|
@ -1233,10 +1273,14 @@ class CodecsModuleTest(unittest.TestCase):
|
||||||
class StreamReaderTest(unittest.TestCase):
|
class StreamReaderTest(unittest.TestCase):
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
|
with warnings.catch_warnings():
|
||||||
|
warnings.simplefilter("ignore", DeprecationWarning)
|
||||||
self.reader = codecs.getreader('utf-8')
|
self.reader = codecs.getreader('utf-8')
|
||||||
self.stream = io.BytesIO(b'\xed\x95\x9c\n\xea\xb8\x80')
|
self.stream = io.BytesIO(b'\xed\x95\x9c\n\xea\xb8\x80')
|
||||||
|
|
||||||
def test_readlines(self):
|
def test_readlines(self):
|
||||||
|
with warnings.catch_warnings():
|
||||||
|
warnings.simplefilter("ignore", DeprecationWarning)
|
||||||
f = self.reader(self.stream)
|
f = self.reader(self.stream)
|
||||||
self.assertEqual(f.readlines(), ['\ud55c\n', '\uae00'])
|
self.assertEqual(f.readlines(), ['\ud55c\n', '\uae00'])
|
||||||
|
|
||||||
|
@ -1244,6 +1288,8 @@ class EncodedFileTest(unittest.TestCase):
|
||||||
|
|
||||||
def test_basic(self):
|
def test_basic(self):
|
||||||
f = io.BytesIO(b'\xed\x95\x9c\n\xea\xb8\x80')
|
f = io.BytesIO(b'\xed\x95\x9c\n\xea\xb8\x80')
|
||||||
|
with warnings.catch_warnings():
|
||||||
|
warnings.simplefilter("ignore", DeprecationWarning)
|
||||||
ef = codecs.EncodedFile(f, 'utf-16-le', 'utf-8')
|
ef = codecs.EncodedFile(f, 'utf-16-le', 'utf-8')
|
||||||
self.assertEqual(ef.read(), b'\\\xd5\n\x00\x00\xae')
|
self.assertEqual(ef.read(), b'\\\xd5\n\x00\x00\xae')
|
||||||
|
|
||||||
|
@ -1388,6 +1434,8 @@ class BasicUnicodeTest(unittest.TestCase, MixInCheckStateHandling):
|
||||||
if encoding not in broken_unicode_with_streams:
|
if encoding not in broken_unicode_with_streams:
|
||||||
# check stream reader/writer
|
# check stream reader/writer
|
||||||
q = Queue(b"")
|
q = Queue(b"")
|
||||||
|
with warnings.catch_warnings():
|
||||||
|
warnings.simplefilter("ignore", DeprecationWarning)
|
||||||
writer = codecs.getwriter(encoding)(q)
|
writer = codecs.getwriter(encoding)(q)
|
||||||
encodedresult = b""
|
encodedresult = b""
|
||||||
for c in s:
|
for c in s:
|
||||||
|
@ -1396,6 +1444,8 @@ class BasicUnicodeTest(unittest.TestCase, MixInCheckStateHandling):
|
||||||
self.assertTrue(type(chunk) is bytes, type(chunk))
|
self.assertTrue(type(chunk) is bytes, type(chunk))
|
||||||
encodedresult += chunk
|
encodedresult += chunk
|
||||||
q = Queue(b"")
|
q = Queue(b"")
|
||||||
|
with warnings.catch_warnings():
|
||||||
|
warnings.simplefilter("ignore", DeprecationWarning)
|
||||||
reader = codecs.getreader(encoding)(q)
|
reader = codecs.getreader(encoding)(q)
|
||||||
decodedresult = ""
|
decodedresult = ""
|
||||||
for c in encodedresult:
|
for c in encodedresult:
|
||||||
|
@ -1470,6 +1520,8 @@ class BasicUnicodeTest(unittest.TestCase, MixInCheckStateHandling):
|
||||||
continue
|
continue
|
||||||
if encoding in broken_unicode_with_streams:
|
if encoding in broken_unicode_with_streams:
|
||||||
continue
|
continue
|
||||||
|
with warnings.catch_warnings():
|
||||||
|
warnings.simplefilter("ignore", DeprecationWarning)
|
||||||
reader = codecs.getreader(encoding)(io.BytesIO(s.encode(encoding)))
|
reader = codecs.getreader(encoding)(io.BytesIO(s.encode(encoding)))
|
||||||
for t in range(5):
|
for t in range(5):
|
||||||
# Test that calling seek resets the internal codec state and buffers
|
# Test that calling seek resets the internal codec state and buffers
|
||||||
|
@ -1539,12 +1591,16 @@ class CharmapTest(unittest.TestCase):
|
||||||
class WithStmtTest(unittest.TestCase):
|
class WithStmtTest(unittest.TestCase):
|
||||||
def test_encodedfile(self):
|
def test_encodedfile(self):
|
||||||
f = io.BytesIO(b"\xc3\xbc")
|
f = io.BytesIO(b"\xc3\xbc")
|
||||||
|
with warnings.catch_warnings():
|
||||||
|
warnings.simplefilter("ignore", DeprecationWarning)
|
||||||
with codecs.EncodedFile(f, "latin-1", "utf-8") as ef:
|
with codecs.EncodedFile(f, "latin-1", "utf-8") as ef:
|
||||||
self.assertEqual(ef.read(), b"\xfc")
|
self.assertEqual(ef.read(), b"\xfc")
|
||||||
|
|
||||||
def test_streamreaderwriter(self):
|
def test_streamreaderwriter(self):
|
||||||
f = io.BytesIO(b"\xc3\xbc")
|
f = io.BytesIO(b"\xc3\xbc")
|
||||||
info = codecs.lookup("utf-8")
|
info = codecs.lookup("utf-8")
|
||||||
|
with warnings.catch_warnings():
|
||||||
|
warnings.simplefilter("ignore", DeprecationWarning)
|
||||||
with codecs.StreamReaderWriter(f, info.streamreader,
|
with codecs.StreamReaderWriter(f, info.streamreader,
|
||||||
info.streamwriter, 'strict') as srw:
|
info.streamwriter, 'strict') as srw:
|
||||||
self.assertEqual(srw.read(), "\xfc")
|
self.assertEqual(srw.read(), "\xfc")
|
||||||
|
@ -1644,15 +1700,15 @@ class BomTest(unittest.TestCase):
|
||||||
|
|
||||||
# (StreamWriter) Check that the BOM is written after a seek(0)
|
# (StreamWriter) Check that the BOM is written after a seek(0)
|
||||||
with codecs.open(support.TESTFN, 'w+', encoding=encoding) as f:
|
with codecs.open(support.TESTFN, 'w+', encoding=encoding) as f:
|
||||||
f.writer.write(data[0])
|
f.write(data[0])
|
||||||
self.assertNotEqual(f.writer.tell(), 0)
|
self.assertNotEqual(f.tell(), 0)
|
||||||
f.writer.seek(0)
|
f.seek(0)
|
||||||
f.writer.write(data)
|
f.write(data)
|
||||||
f.seek(0)
|
f.seek(0)
|
||||||
self.assertEqual(f.read(), data)
|
self.assertEqual(f.read(), data)
|
||||||
|
|
||||||
# Check that the BOM is not written after a seek() at a position
|
# Check that the BOM is not written after a seek() at a
|
||||||
# different than the start
|
# position different than the start
|
||||||
with codecs.open(support.TESTFN, 'w+', encoding=encoding) as f:
|
with codecs.open(support.TESTFN, 'w+', encoding=encoding) as f:
|
||||||
f.write(data)
|
f.write(data)
|
||||||
f.seek(f.tell())
|
f.seek(f.tell())
|
||||||
|
@ -1660,12 +1716,12 @@ class BomTest(unittest.TestCase):
|
||||||
f.seek(0)
|
f.seek(0)
|
||||||
self.assertEqual(f.read(), data * 2)
|
self.assertEqual(f.read(), data * 2)
|
||||||
|
|
||||||
# (StreamWriter) Check that the BOM is not written after a seek()
|
# (StreamWriter) Check that the BOM is not written after a
|
||||||
# at a position different than the start
|
# seek() at a position different than the start
|
||||||
with codecs.open(support.TESTFN, 'w+', encoding=encoding) as f:
|
with codecs.open(support.TESTFN, 'w+', encoding=encoding) as f:
|
||||||
f.writer.write(data)
|
f.write(data)
|
||||||
f.writer.seek(f.writer.tell())
|
f.seek(f.tell())
|
||||||
f.writer.write(data)
|
f.write(data)
|
||||||
f.seek(0)
|
f.seek(0)
|
||||||
self.assertEqual(f.read(), data * 2)
|
self.assertEqual(f.read(), data * 2)
|
||||||
|
|
||||||
|
@ -1704,6 +1760,8 @@ class TransformCodecTest(unittest.TestCase):
|
||||||
def test_read(self):
|
def test_read(self):
|
||||||
for encoding in bytes_transform_encodings:
|
for encoding in bytes_transform_encodings:
|
||||||
sin = codecs.encode(b"\x80", encoding)
|
sin = codecs.encode(b"\x80", encoding)
|
||||||
|
with warnings.catch_warnings():
|
||||||
|
warnings.simplefilter("ignore", DeprecationWarning)
|
||||||
reader = codecs.getreader(encoding)(io.BytesIO(sin))
|
reader = codecs.getreader(encoding)(io.BytesIO(sin))
|
||||||
sout = reader.read()
|
sout = reader.read()
|
||||||
self.assertEqual(sout, b"\x80")
|
self.assertEqual(sout, b"\x80")
|
||||||
|
@ -1713,6 +1771,8 @@ class TransformCodecTest(unittest.TestCase):
|
||||||
if encoding in ['uu_codec', 'zlib_codec']:
|
if encoding in ['uu_codec', 'zlib_codec']:
|
||||||
continue
|
continue
|
||||||
sin = codecs.encode(b"\x80", encoding)
|
sin = codecs.encode(b"\x80", encoding)
|
||||||
|
with warnings.catch_warnings():
|
||||||
|
warnings.simplefilter("ignore", DeprecationWarning)
|
||||||
reader = codecs.getreader(encoding)(io.BytesIO(sin))
|
reader = codecs.getreader(encoding)(io.BytesIO(sin))
|
||||||
sout = reader.readline()
|
sout = reader.readline()
|
||||||
self.assertEqual(sout, b"\x80")
|
self.assertEqual(sout, b"\x80")
|
||||||
|
|
|
@ -161,6 +161,11 @@ Core and Builtins
|
||||||
Library
|
Library
|
||||||
-------
|
-------
|
||||||
|
|
||||||
|
- Issue #8796: codecs.open() calls the builtin open() function instead of using
|
||||||
|
StreamReaderWriter. Deprecate StreamReader, StreamWriter, StreamReaderWriter,
|
||||||
|
StreamRecoder and EncodedFile() of the codec module. Use the builtin open()
|
||||||
|
function or io.TextIOWrapper instead.
|
||||||
|
|
||||||
- Issue #12175: BufferedReader.read(-1) now calls raw.readall() if available.
|
- Issue #12175: BufferedReader.read(-1) now calls raw.readall() if available.
|
||||||
|
|
||||||
- Issue #12175: FileIO.readall() now only reads the file position and size
|
- Issue #12175: FileIO.readall() now only reads the file position and size
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue