Commit 19690593 authored by Antoine Pitrou's avatar Antoine Pitrou

Issue #6215: backport the 3.1 io lib

parent 55bd1efb
......@@ -573,6 +573,10 @@ Text I/O
The name of the encoding used to decode the stream's bytes into
strings, and to encode strings into bytes.
.. attribute:: errors
The error setting of the decoder or encoder.
.. attribute:: newlines
A string, a tuple of strings, or ``None``, indicating the newlines
......@@ -625,13 +629,9 @@ Text I/O
If *line_buffering* is ``True``, :meth:`flush` is implied when a call to
write contains a newline character.
:class:`TextIOWrapper` provides these data attributes in addition to those of
:class:`TextIOWrapper` provides one attribute in addition to those of
:class:`TextIOBase` and its parents:
.. attribute:: errors
The encoding and decoding error setting.
.. attribute:: line_buffering
Whether line buffering is enabled.
......
This diff is collapsed.
This diff is collapsed.
import unittest
from test import test_support
from test import test_support as support
# Simple test to ensure that optimizations in fileobject.c deliver
# the expected results. For best testing, run this under a debug-build
# Python too (to exercise asserts in the C code).
import io # C implementation.
import _pyio as pyio # Python implementation.
lengths = range(1, 257) + [512, 1000, 1024, 2048, 4096, 8192, 10000,
16384, 32768, 65536, 1000000]
# Simple test to ensure that optimizations in the IO library deliver the
# expected results. For best testing, run this under a debug-build Python too
# (to exercise asserts in the C code).
lengths = list(range(1, 257)) + [512, 1000, 1024, 2048, 4096, 8192, 10000,
16384, 32768, 65536, 1000000]
class BufferSizeTest(unittest.TestCase):
def try_one(self, s):
......@@ -14,27 +17,27 @@ class BufferSizeTest(unittest.TestCase):
# .readline()s deliver what we wrote.
# Ensure we can open TESTFN for writing.
test_support.unlink(test_support.TESTFN)
support.unlink(support.TESTFN)
# Since C doesn't guarantee we can write/read arbitrary bytes in text
# files, use binary mode.
f = open(test_support.TESTFN, "wb")
f = self.open(support.TESTFN, "wb")
try:
# write once with \n and once without
f.write(s)
f.write("\n")
f.write(b"\n")
f.write(s)
f.close()
f = open(test_support.TESTFN, "rb")
f = open(support.TESTFN, "rb")
line = f.readline()
self.assertEqual(line, s + "\n")
self.assertEqual(line, s + b"\n")
line = f.readline()
self.assertEqual(line, s)
line = f.readline()
self.assert_(not line) # Must be at EOF
f.close()
finally:
test_support.unlink(test_support.TESTFN)
support.unlink(support.TESTFN)
def drive_one(self, pattern):
for length in lengths:
......@@ -47,19 +50,27 @@ class BufferSizeTest(unittest.TestCase):
teststring = pattern * q + pattern[:r]
self.assertEqual(len(teststring), length)
self.try_one(teststring)
self.try_one(teststring + "x")
self.try_one(teststring + b"x")
self.try_one(teststring[:-1])
def test_primepat(self):
# A pattern with prime length, to avoid simple relationships with
# stdio buffer sizes.
self.drive_one("1234567890\00\01\02\03\04\05\06")
self.drive_one(b"1234567890\00\01\02\03\04\05\06")
def test_nullpat(self):
self.drive_one("\0" * 1000)
self.drive_one(bytes(1000))
class CBufferSizeTest(BufferSizeTest):
open = io.open
class PyBufferSizeTest(BufferSizeTest):
open = staticmethod(pyio.open)
def test_main():
test_support.run_unittest(BufferSizeTest)
support.run_unittest(CBufferSizeTest, PyBufferSizeTest)
if __name__ == "__main__":
test_main()
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
......@@ -29,7 +29,7 @@ __all__ = ["Error", "TestFailed", "ResourceDenied", "import_module",
"run_with_locale", "set_memlimit", "bigmemtest", "bigaddrspacetest",
"BasicTestRunner", "run_unittest", "run_doctest", "threading_setup",
"threading_cleanup", "reap_children", "cpython_only",
"check_impl_detail", "get_attribute"]
"check_impl_detail", "get_attribute", "py3k_bytes"]
class Error(Exception):
"""Base class for regression test exceptions."""
......@@ -968,3 +968,18 @@ def reap_children():
break
except:
break
def py3k_bytes(b):
"""Emulate the py3k bytes() constructor.
NOTE: This is only a best effort function.
"""
try:
# memoryview?
return b.tobytes()
except AttributeError:
try:
# iterable of ints?
return b"".join(chr(x) for x in b)
except TypeError:
return bytes(b)
This diff is collapsed.
......@@ -317,6 +317,10 @@ Core and Builtins
Library
-------
- Issue #6215: All bug fixes and enhancements from the Python 3.1 io library
(including the fast C implementation) have been backported to the standard
``io`` module.
- Issue #6258: Support AMD64 in bdist_msi.
- Issue #5262: Fixed bug in next rollover time computation in
......
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
......@@ -438,10 +438,11 @@ class PyBuildExt(build_ext):
exts.append( Extension("_heapq", ["_heapqmodule.c"]) )
# operator.add() and similar goodies
exts.append( Extension('operator', ['operator.c']) )
# Python 3.0 _fileio module
exts.append( Extension("_fileio", ["_fileio.c"]) )
# Python 3.0 _bytesio module
exts.append( Extension("_bytesio", ["_bytesio.c"]) )
# Python 3.1 _io library
exts.append( Extension("_io",
["_io/bufferedio.c", "_io/bytesio.c", "_io/fileio.c",
"_io/iobase.c", "_io/_iomodule.c", "_io/stringio.c", "_io/textio.c"],
depends=["_io/_iomodule.h"], include_dirs=["Modules/_io"]))
# _functools
exts.append( Extension("_functools", ["_functoolsmodule.c"]) )
# _json speedups
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment