merge the io-c branch: C implementation of the io module

The main io module now uses the C implementation.  The Python one still exists
in Lib/_pyio.py for ease of testing new features and usefulness to other
implementers.

The rewrite was done by Antoine Pitrou and Amaury Forgeot d'Arc.  I was slightly
helpful at the end. :)


Following are the log messages from the io-c branch:

Merged revisions 68683-68685,68687-68689,68693,68704,68741-68743,68745,68747,68752-68754,68756,68758,68812,68816-68817,68820-68822,68824-68825,68828,68876-68877,69037,69044,69104,69115,69194,69626-69629,69636,69638,69641-69642,69644-69654,69656-69661,69671,69677,69812-69815,69817,69827-69830,69839,69841-69845,69848,69850,69852,69854,69860,69865-69866,69868,69872-69873,69885,69888,69891-69893,69911,69913-69916,69963,70033,70035,70038,70041-70048,70067-70070,70075,70112,70133,70135,70140 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/branches/io-c

........
  r68683 | antoine.pitrou | 2009-01-17 17:13:48 -0600 (Sat, 17 Jan 2009) | 3 lines

  Merge in changes from the io-c sandbox. Tests will follow in separate commits.
........
  r68684 | antoine.pitrou | 2009-01-17 17:17:26 -0600 (Sat, 17 Jan 2009) | 3 lines

  Fixes and additions to test_io.py
........
  r68685 | antoine.pitrou | 2009-01-17 17:22:04 -0600 (Sat, 17 Jan 2009) | 1 line

  Fix test_fileio
........
  r68687 | antoine.pitrou | 2009-01-17 17:35:11 -0600 (Sat, 17 Jan 2009) | 3 lines

  Add dependency to _iomodule.h for the various C sources
........
  r68688 | antoine.pitrou | 2009-01-17 17:38:18 -0600 (Sat, 17 Jan 2009) | 3 lines

  These precautions are not needed anymore!
........
  r68689 | antoine.pitrou | 2009-01-17 17:41:48 -0600 (Sat, 17 Jan 2009) | 3 lines

  Fix another test
........
  r68693 | antoine.pitrou | 2009-01-17 17:49:58 -0600 (Sat, 17 Jan 2009) | 3 lines

  Fix test_uu (which was using private attributes of TextIOWrapper)
........
  r68704 | antoine.pitrou | 2009-01-17 18:45:29 -0600 (Sat, 17 Jan 2009) | 3 lines

  Most io sources are Py_ssize_t-clean (I don't know about bytesio and stringio)
........
  r68741 | antoine.pitrou | 2009-01-18 15:20:30 -0600 (Sun, 18 Jan 2009) | 3 lines

  Check return type in TextIOWrapper.__next__
........
  r68742 | antoine.pitrou | 2009-01-18 15:28:48 -0600 (Sun, 18 Jan 2009) | 4 lines

  Make binary buffered readline and iteration much faster
  (8x as fast as the IOBase generic implementation)
........
  r68743 | antoine.pitrou | 2009-01-18 15:47:47 -0600 (Sun, 18 Jan 2009) | 3 lines

  Reinsert test_io_after_close (was removed by mistake)
........
  r68745 | antoine.pitrou | 2009-01-18 16:16:06 -0600 (Sun, 18 Jan 2009) | 3 lines

  Add read, read1 and write methods to BufferedIOBase
........
  r68747 | antoine.pitrou | 2009-01-18 16:35:58 -0600 (Sun, 18 Jan 2009) | 3 lines

  Kill test failure
........
  r68752 | amaury.forgeotdarc | 2009-01-18 17:05:43 -0600 (Sun, 18 Jan 2009) | 3 lines

  Fix a segfault when e.g a BufferedReader is created with a FileIO in
  read mode.
........
  r68753 | antoine.pitrou | 2009-01-18 17:13:09 -0600 (Sun, 18 Jan 2009) | 3 lines

  Add truncate() to text IO objects
........
  r68754 | antoine.pitrou | 2009-01-18 17:51:08 -0600 (Sun, 18 Jan 2009) | 3 lines

  Remove IOBase.__del__ and replace it with custom code with tp_dealloc
........
  r68756 | antoine.pitrou | 2009-01-18 18:10:16 -0600 (Sun, 18 Jan 2009) | 3 lines

  Remove irrelevant comment.
........
  r68758 | antoine.pitrou | 2009-01-18 18:36:16 -0600 (Sun, 18 Jan 2009) | 3 lines

  in importlib:_fileio._FileIO -> _io.FileIO
........
  r68812 | antoine.pitrou | 2009-01-20 14:15:51 -0600 (Tue, 20 Jan 2009) | 3 lines

  Add garbage collection support to FileIO objects
........
  r68816 | antoine.pitrou | 2009-01-20 14:56:28 -0600 (Tue, 20 Jan 2009) | 3 lines

  Add GC support to Buffered and Text IO objects
........
  r68817 | antoine.pitrou | 2009-01-20 15:19:45 -0600 (Tue, 20 Jan 2009) | 3 lines

  Add some file headers
........
  r68820 | antoine.pitrou | 2009-01-20 15:29:59 -0600 (Tue, 20 Jan 2009) | 3 lines

  Add class TextIOBase
........
  r68821 | antoine.pitrou | 2009-01-20 15:36:16 -0600 (Tue, 20 Jan 2009) | 3 lines

  Add properties to TextIOBase
........
  r68822 | antoine.pitrou | 2009-01-20 15:41:19 -0600 (Tue, 20 Jan 2009) | 3 lines

  Disable the pure Python TextIOBase class, and inject C the implementation instead
........
  r68824 | antoine.pitrou | 2009-01-20 16:36:28 -0600 (Tue, 20 Jan 2009) | 3 lines

  Fix two leaks
........
  r68825 | antoine.pitrou | 2009-01-20 16:38:29 -0600 (Tue, 20 Jan 2009) | 3 lines

  FileIO.name is just a plain attribute, we can set it directly
........
  r68828 | antoine.pitrou | 2009-01-20 17:06:33 -0600 (Tue, 20 Jan 2009) | 3 lines

  Speed up closed checks on text IO objects. Good for a 25% speedup on small ops.
........
  r68876 | antoine.pitrou | 2009-01-23 17:01:25 -0600 (Fri, 23 Jan 2009) | 3 lines

  Two typos
........
  r68877 | antoine.pitrou | 2009-01-23 18:13:20 -0600 (Fri, 23 Jan 2009) | 3 lines

  Remove two unused functions
........
  r69037 | amaury.forgeotdarc | 2009-01-27 17:10:25 -0600 (Tue, 27 Jan 2009) | 2 lines

  Update the win32 project files
........
  r69044 | antoine.pitrou | 2009-01-27 18:51:07 -0600 (Tue, 27 Jan 2009) | 3 lines

  Improve heuristic in IncrementalNewlineDecoder + some micro-optimizations
........
  r69104 | antoine.pitrou | 2009-01-29 15:23:42 -0600 (Thu, 29 Jan 2009) | 3 lines

  Fix some crashers found by Victor
........
  r69115 | hirokazu.yamamoto | 2009-01-29 20:36:28 -0600 (Thu, 29 Jan 2009) | 1 line

  Updated VC6 project file.
........
  r69194 | antoine.pitrou | 2009-02-01 16:57:18 -0600 (Sun, 01 Feb 2009) | 3 lines

  Fix downcasting warnings in 32-bit mode with 64-bit offsets (Windows)
........
  r69626 | benjamin.peterson | 2009-02-14 17:33:34 -0600 (Sat, 14 Feb 2009) | 1 line

  only catch AttributeError and UnsupportedOperation
........
  r69627 | benjamin.peterson | 2009-02-14 21:35:28 -0600 (Sat, 14 Feb 2009) | 1 line

  give the IO module its own state and store the os and locale modules in it
........
  r69628 | benjamin.peterson | 2009-02-14 22:08:32 -0600 (Sat, 14 Feb 2009) | 1 line

  put interned strings in the module state structure
........
  r69629 | benjamin.peterson | 2009-02-14 22:15:29 -0600 (Sat, 14 Feb 2009) | 1 line

  put UnsupportedOperation in the module state
........
  r69636 | benjamin.peterson | 2009-02-15 08:31:42 -0600 (Sun, 15 Feb 2009) | 1 line

  dealloc unsupported_operation
........
  r69638 | benjamin.peterson | 2009-02-15 09:24:45 -0600 (Sun, 15 Feb 2009) | 1 line

  actually test the C implementation
........
  r69641 | benjamin.peterson | 2009-02-15 10:12:37 -0600 (Sun, 15 Feb 2009) | 5 lines

  make interned strings globals again ;(

  putting them in the module state was asking for trouble when the module
  was dealloced before the classes in it were
........
  r69642 | benjamin.peterson | 2009-02-15 10:19:45 -0600 (Sun, 15 Feb 2009) | 1 line

  actually test the python implementations
........
  r69644 | antoine.pitrou | 2009-02-15 11:59:30 -0600 (Sun, 15 Feb 2009) | 3 lines

  Fix memory leak in destructor when a Python class inherits from IOBase (or an IOBase-derived type)
........
  r69645 | antoine.pitrou | 2009-02-15 12:23:26 -0600 (Sun, 15 Feb 2009) | 3 lines

  Add a warning about the embarassing state of IOBase finalization
........
  r69646 | antoine.pitrou | 2009-02-15 13:14:42 -0600 (Sun, 15 Feb 2009) | 3 lines

  Fix opening of 8-bit filenames with FileIO
........
  r69647 | antoine.pitrou | 2009-02-15 13:20:22 -0600 (Sun, 15 Feb 2009) | 3 lines

  Fix leak in FileIO constructor
........
  r69648 | antoine.pitrou | 2009-02-15 13:58:16 -0600 (Sun, 15 Feb 2009) | 3 lines

  Fix some refleaks
........
  r69649 | antoine.pitrou | 2009-02-15 14:05:13 -0600 (Sun, 15 Feb 2009) | 3 lines

  Fix a leak in IOBase.writelines
........
  r69650 | antoine.pitrou | 2009-02-15 14:11:56 -0600 (Sun, 15 Feb 2009) | 3 lines

  Fix memory leak in BufferedWriter.truncate
........
  r69651 | antoine.pitrou | 2009-02-15 14:25:34 -0600 (Sun, 15 Feb 2009) | 3 lines

  Fix a leak in TextIOWrapper.seek
........
  r69652 | antoine.pitrou | 2009-02-15 14:26:28 -0600 (Sun, 15 Feb 2009) | 3 lines

  Unify implementations of truncate for buffered objects
........
  r69653 | antoine.pitrou | 2009-02-15 15:15:15 -0600 (Sun, 15 Feb 2009) | 3 lines

  Fix more leaks in TextIOWrapper
........
  r69654 | antoine.pitrou | 2009-02-15 15:21:57 -0600 (Sun, 15 Feb 2009) | 3 lines

  Smaller chunk size for a faster test
........
  r69656 | benjamin.peterson | 2009-02-15 17:29:48 -0600 (Sun, 15 Feb 2009) | 1 line

  braces make this much clearer
........
  r69657 | benjamin.peterson | 2009-02-15 17:46:07 -0600 (Sun, 15 Feb 2009) | 1 line

  use the correct macro
........
  r69658 | antoine.pitrou | 2009-02-15 19:38:59 -0600 (Sun, 15 Feb 2009) | 5 lines

  Fix crash in test_urllib2_localnet in debug mode. It was due to an HTTPResponse
  object being revived when calling its close() method in IOBase's tp_dealloc.
  _PyIOBase_finalize() starts looking scary...
........
  r69659 | benjamin.peterson | 2009-02-15 20:55:48 -0600 (Sun, 15 Feb 2009) | 1 line

  fix segfault on initialization failing
........
  r69660 | benjamin.peterson | 2009-02-15 21:09:31 -0600 (Sun, 15 Feb 2009) | 1 line

  apparently locale.getprefferedencoding() can raise a ImportError, too
........
  r69661 | benjamin.peterson | 2009-02-15 21:54:15 -0600 (Sun, 15 Feb 2009) | 1 line

  it's amazing this worked at all; I was using the wrong structs!
........
  r69671 | benjamin.peterson | 2009-02-16 08:38:27 -0600 (Mon, 16 Feb 2009) | 1 line

  add garbage collection support to bytesio
........
  r69677 | benjamin.peterson | 2009-02-16 10:31:03 -0600 (Mon, 16 Feb 2009) | 5 lines

  reduce ImportError catching code duplication

  I'm not sure this makes the code clearer with its new gotos, but
  at least I added a big fat comment
........
  r69812 | antoine.pitrou | 2009-02-20 13:50:16 -0600 (Fri, 20 Feb 2009) | 3 lines

  _StringIO now belongs to the _io modules, rather to its own _stringio module
........
  r69813 | antoine.pitrou | 2009-02-20 13:58:22 -0600 (Fri, 20 Feb 2009) | 3 lines

  Add a test for StringIO properties
........
  r69814 | antoine.pitrou | 2009-02-20 14:06:03 -0600 (Fri, 20 Feb 2009) | 3 lines

  Reimplement a few trivial StringIO functions and properties in C
........
  r69815 | antoine.pitrou | 2009-02-20 14:13:11 -0600 (Fri, 20 Feb 2009) | 3 lines

  Add the line_buffering property to TextIOWrapper, and test for it
........
  r69817 | antoine.pitrou | 2009-02-20 14:45:50 -0600 (Fri, 20 Feb 2009) | 4 lines

  Allow IncrementalNewlineDecoder to take unicode objects as decoding input if the decoder parameter is None
  This will help rewriting StringIO to C
........
  r69827 | antoine.pitrou | 2009-02-20 19:00:30 -0600 (Fri, 20 Feb 2009) | 3 lines

  Rewrite most of StringIO in C. Some almost empty stubs remain to be converted.
........
  r69828 | antoine.pitrou | 2009-02-20 19:09:25 -0600 (Fri, 20 Feb 2009) | 3 lines

  Plug a leak, and remove an unused string
........
  r69829 | benjamin.peterson | 2009-02-20 20:02:28 -0600 (Fri, 20 Feb 2009) | 1 line

  this assertions makes more sense here
........
  r69830 | benjamin.peterson | 2009-02-20 20:03:04 -0600 (Fri, 20 Feb 2009) | 1 line

  PyModule_AddObject can fail; simplify this code with a macro
........
  r69839 | antoine.pitrou | 2009-02-21 12:54:01 -0600 (Sat, 21 Feb 2009) | 3 lines

  StringIO is now written entirely in C (and blazingly fast)
........
  r69841 | benjamin.peterson | 2009-02-21 14:05:40 -0600 (Sat, 21 Feb 2009) | 1 line

  split the Python implementation of io into another module and rewrite the tests to test both implementations
........
  r69842 | benjamin.peterson | 2009-02-21 14:10:00 -0600 (Sat, 21 Feb 2009) | 1 line

  closed is not a function
........
  r69843 | benjamin.peterson | 2009-02-21 14:13:04 -0600 (Sat, 21 Feb 2009) | 1 line

  fix __all__ test
........
  r69844 | benjamin.peterson | 2009-02-21 14:21:24 -0600 (Sat, 21 Feb 2009) | 1 line

  fix the rest of the Misc tests
........
  r69845 | benjamin.peterson | 2009-02-21 14:26:59 -0600 (Sat, 21 Feb 2009) | 1 line

  RawIOBase is better for FileIO
........
  r69848 | benjamin.peterson | 2009-02-21 15:33:53 -0600 (Sat, 21 Feb 2009) | 1 line

  fix some more tests broken by bag argument validation
........
  r69850 | benjamin.peterson | 2009-02-21 16:16:42 -0600 (Sat, 21 Feb 2009) | 1 line

  make the python IncrementalNewineDecoder support a None decoder
........
  r69852 | benjamin.peterson | 2009-02-21 16:36:09 -0600 (Sat, 21 Feb 2009) | 1 line

  fix a BlockingIOError.characters_written bug
........
  r69854 | benjamin.peterson | 2009-02-21 16:49:02 -0600 (Sat, 21 Feb 2009) | 1 line

  check whence
........
  r69860 | benjamin.peterson | 2009-02-21 17:42:50 -0600 (Sat, 21 Feb 2009) | 1 line

  fix some of these Misbehaving io tests
........
  r69865 | benjamin.peterson | 2009-02-21 18:59:52 -0600 (Sat, 21 Feb 2009) | 1 line

  don't use super here()
........
  r69866 | benjamin.peterson | 2009-02-21 19:05:28 -0600 (Sat, 21 Feb 2009) | 1 line

  use implementation specific classes
........
  r69868 | benjamin.peterson | 2009-02-21 22:12:05 -0600 (Sat, 21 Feb 2009) | 1 line

  use a more DRY friendly approach to injecting module contents into test classes
........
  r69872 | antoine.pitrou | 2009-02-22 13:39:45 -0600 (Sun, 22 Feb 2009) | 3 lines

  Sanitize destructor behaviour of IOBase. Now Python-defined attributes can be accessed from close().
........
  r69873 | antoine.pitrou | 2009-02-22 13:50:14 -0600 (Sun, 22 Feb 2009) | 4 lines

  Only set the internal fd after it has been checked to be valid
  (otherwise, the destructor will attempt to close it)
........
  r69885 | benjamin.peterson | 2009-02-22 15:30:14 -0600 (Sun, 22 Feb 2009) | 1 line

  convert some other tests to use both io implementations
........
  r69888 | antoine.pitrou | 2009-02-22 17:03:16 -0600 (Sun, 22 Feb 2009) | 3 lines

  Silence all exceptions when finalizing
........
  r69891 | benjamin.peterson | 2009-02-22 17:27:24 -0600 (Sun, 22 Feb 2009) | 1 line

  convert another test to test both io implementations
........
  r69892 | benjamin.peterson | 2009-02-22 17:32:15 -0600 (Sun, 22 Feb 2009) | 1 line

  help poor people like me to find their io tests (did I miss any?)
........
  r69893 | benjamin.peterson | 2009-02-22 17:37:56 -0600 (Sun, 22 Feb 2009) | 1 line

  put a big note in the test telling people to write tests for both implementations now
........
  r69911 | antoine.pitrou | 2009-02-23 13:57:18 -0600 (Mon, 23 Feb 2009) | 3 lines

  expose DEFAULT_BUFFER_SIZE again (fixes a bunch of test failures)
........
  r69913 | antoine.pitrou | 2009-02-23 14:10:30 -0600 (Mon, 23 Feb 2009) | 4 lines

  Do the cyclic garbage collection tests only on the C version.
  The Python version is helpless as it uses __del__.
........
  r69914 | antoine.pitrou | 2009-02-23 14:21:41 -0600 (Mon, 23 Feb 2009) | 3 lines

  Adapt test_largefile to test both implementations
........
  r69915 | antoine.pitrou | 2009-02-23 14:25:14 -0600 (Mon, 23 Feb 2009) | 3 lines

  One small failure
........
  r69916 | antoine.pitrou | 2009-02-23 14:28:33 -0600 (Mon, 23 Feb 2009) | 3 lines

  Add a comment, at BP's request
........
  r69963 | antoine.pitrou | 2009-02-25 09:42:59 -0600 (Wed, 25 Feb 2009) | 3 lines

  Add a test of ABC inheritance
........
  r70033 | antoine.pitrou | 2009-02-27 15:49:50 -0600 (Fri, 27 Feb 2009) | 3 lines

  The base classes now are ABCs.
........
  r70035 | benjamin.peterson | 2009-02-27 15:57:41 -0600 (Fri, 27 Feb 2009) | 1 line

  good house keeping
........
  r70038 | antoine.pitrou | 2009-02-27 17:05:23 -0600 (Fri, 27 Feb 2009) | 4 lines

  Make the buffer allocation overflow tests specific to the C implementation, since the Python implementation resizes its buffers when needed rather than allocating them up front.
........
  r70041 | benjamin.peterson | 2009-02-27 18:26:12 -0600 (Fri, 27 Feb 2009) | 1 line

  kill java naming for sanity
........
  r70042 | benjamin.peterson | 2009-02-27 18:28:53 -0600 (Fri, 27 Feb 2009) | 2 lines

  timingTest is superseded by iobench
........
  r70043 | antoine.pitrou | 2009-02-27 19:13:50 -0600 (Fri, 27 Feb 2009) | 3 lines

  Remove the last traces of java naming in test_io
........
  r70044 | antoine.pitrou | 2009-02-27 19:18:34 -0600 (Fri, 27 Feb 2009) | 3 lines

  Better resource cleanup
........
  r70045 | antoine.pitrou | 2009-02-27 19:29:00 -0600 (Fri, 27 Feb 2009) | 3 lines

  Remove dubious uses of super(), and fix one test
........
  r70046 | antoine.pitrou | 2009-02-27 19:31:00 -0600 (Fri, 27 Feb 2009) | 3 lines

  Bump up CHUNK_SIZE (no need to make the Python version look slower than it is)
........
  r70047 | benjamin.peterson | 2009-02-27 20:03:26 -0600 (Fri, 27 Feb 2009) | 1 line

  fix typo
........
  r70048 | benjamin.peterson | 2009-02-27 21:35:11 -0600 (Fri, 27 Feb 2009) | 1 line

  move code to a better place
........
  r70067 | benjamin.peterson | 2009-02-28 10:43:20 -0600 (Sat, 28 Feb 2009) | 4 lines

  1. make sure to undo buffered read aheads in BufferedRandom.seek()
  2. refill the buffer if have <= 0
  3. fix the last failing test_io test!
........
  r70068 | benjamin.peterson | 2009-02-28 10:57:50 -0600 (Sat, 28 Feb 2009) | 1 line

  define read1() on the python implementation's BufferedIOBase
........
  r70069 | benjamin.peterson | 2009-02-28 11:01:17 -0600 (Sat, 28 Feb 2009) | 1 line

  document read1() in BufferedIOBase
........
  r70070 | benjamin.peterson | 2009-02-28 11:06:42 -0600 (Sat, 28 Feb 2009) | 1 line

  give credit where credit is due
........
  r70075 | antoine.pitrou | 2009-02-28 13:34:59 -0600 (Sat, 28 Feb 2009) | 3 lines

  Amaury's name
........
  r70112 | antoine.pitrou | 2009-03-02 17:11:55 -0600 (Mon, 02 Mar 2009) | 4 lines

  Looks like this is necessary in order to build cleanly under Windows
  (someone correct this if it's wrong, I'm no Windows user)
........
  r70133 | benjamin.peterson | 2009-03-03 15:23:32 -0600 (Tue, 03 Mar 2009) | 1 line

  fix test_newline_property on _pyio.StringIO
........
  r70135 | benjamin.peterson | 2009-03-03 15:47:30 -0600 (Tue, 03 Mar 2009) | 1 line

  fix typos and inconsistencies. thanks to Daniel Diniz
........
  r70140 | benjamin.peterson | 2009-03-03 16:21:10 -0600 (Tue, 03 Mar 2009) | 1 line

  add the test from #5266
........
diff --git a/Modules/_bufferedio.c b/Modules/_bufferedio.c
new file mode 100644
index 0000000..88f4fbc
--- /dev/null
+++ b/Modules/_bufferedio.c
@@ -0,0 +1,2130 @@
+/*
+    An implementation of Buffered I/O as defined by PEP 3116 - "New I/O"
+    
+    Classes defined here: BufferedIOBase, BufferedReader, BufferedWriter,
+    BufferedRandom.
+    
+    Written by Amaury Forgeot d'Arc and Antoine Pitrou
+*/
+
+#define PY_SSIZE_T_CLEAN
+#include "Python.h"
+#include "structmember.h"
+#include "pythread.h"
+#include "_iomodule.h"
+
+/*
+ * BufferedIOBase class, inherits from IOBase.
+ */
+PyDoc_STRVAR(BufferedIOBase_doc,
+    "Base class for buffered IO objects.\n"
+    "\n"
+    "The main difference with RawIOBase is that the read() method\n"
+    "supports omitting the size argument, and does not have a default\n"
+    "implementation that defers to readinto().\n"
+    "\n"
+    "In addition, read(), readinto() and write() may raise\n"
+    "BlockingIOError if the underlying raw stream is in non-blocking\n"
+    "mode and not ready; unlike their raw counterparts, they will never\n"
+    "return None.\n"
+    "\n"
+    "A typical implementation should not inherit from a RawIOBase\n"
+    "implementation, but wrap one.\n"
+    );
+
+static PyObject *
+BufferedIOBase_readinto(PyObject *self, PyObject *args)
+{
+    Py_buffer buf;
+    Py_ssize_t len;
+    PyObject *data;
+
+    if (!PyArg_ParseTuple(args, "w*:readinto", &buf)) {
+        return NULL;
+    }
+
+    data = PyObject_CallMethod(self, "read", "n", buf.len);
+    if (data == NULL)
+        goto error;
+
+    if (!PyBytes_Check(data)) {
+        Py_DECREF(data);
+        PyErr_SetString(PyExc_TypeError, "read() should return bytes");
+        goto error;
+    }
+
+    len = Py_SIZE(data);
+    memcpy(buf.buf, PyBytes_AS_STRING(data), len);
+
+    PyBuffer_Release(&buf);
+    Py_DECREF(data);
+
+    return PyLong_FromSsize_t(len);
+
+  error:
+    PyBuffer_Release(&buf);
+    return NULL;
+}
+
+static PyObject *
+BufferedIOBase_unsupported(const char *message)
+{
+    PyErr_SetString(IO_STATE->unsupported_operation, message);
+    return NULL;
+}
+
+PyDoc_STRVAR(BufferedIOBase_read_doc,
+    "Read and return up to n bytes.\n"
+    "\n"
+    "If the argument is omitted, None, or negative, reads and\n"
+    "returns all data until EOF.\n"
+    "\n"
+    "If the argument is positive, and the underlying raw stream is\n"
+    "not 'interactive', multiple raw reads may be issued to satisfy\n"
+    "the byte count (unless EOF is reached first).  But for\n"
+    "interactive raw streams (as well as sockets and pipes), at most\n"
+    "one raw read will be issued, and a short result does not imply\n"
+    "that EOF is imminent.\n"
+    "\n"
+    "Returns an empty bytes object on EOF.\n"
+    "\n"
+    "Returns None if the underlying raw stream was open in non-blocking\n"
+    "mode and no data is available at the moment.\n");
+
+static PyObject *
+BufferedIOBase_read(PyObject *self, PyObject *args)
+{
+    return BufferedIOBase_unsupported("read");
+}
+
+PyDoc_STRVAR(BufferedIOBase_read1_doc,
+    "Read and return up to n bytes, with at most one read() call\n"
+    "to the underlying raw stream. A short result does not imply\n"
+    "that EOF is imminent.\n"
+    "\n"
+    "Returns an empty bytes object on EOF.\n");
+
+static PyObject *
+BufferedIOBase_read1(PyObject *self, PyObject *args)
+{
+    return BufferedIOBase_unsupported("read1");
+}
+
+PyDoc_STRVAR(BufferedIOBase_write_doc,
+    "Write the given buffer to the IO stream.\n"
+    "\n"
+    "Returns the number of bytes written, which is never less than\n"
+    "len(b).\n"
+    "\n"
+    "Raises BlockingIOError if the buffer is full and the\n"
+    "underlying raw stream cannot accept more data at the moment.\n");
+
+static PyObject *
+BufferedIOBase_write(PyObject *self, PyObject *args)
+{
+    return BufferedIOBase_unsupported("write");
+}
+
+
+static PyMethodDef BufferedIOBase_methods[] = {
+    {"read", BufferedIOBase_read, METH_VARARGS, BufferedIOBase_read_doc},
+    {"read1", BufferedIOBase_read1, METH_VARARGS, BufferedIOBase_read1_doc},
+    {"readinto", BufferedIOBase_readinto, METH_VARARGS, NULL},
+    {"write", BufferedIOBase_write, METH_VARARGS, BufferedIOBase_write_doc},
+    {NULL, NULL}
+};
+
+PyTypeObject PyBufferedIOBase_Type = {
+    PyVarObject_HEAD_INIT(NULL, 0)
+    "_io._BufferedIOBase",      /*tp_name*/
+    0,                          /*tp_basicsize*/
+    0,                          /*tp_itemsize*/
+    0,                          /*tp_dealloc*/
+    0,                          /*tp_print*/
+    0,                          /*tp_getattr*/
+    0,                          /*tp_setattr*/
+    0,                          /*tp_compare */
+    0,                          /*tp_repr*/
+    0,                          /*tp_as_number*/
+    0,                          /*tp_as_sequence*/
+    0,                          /*tp_as_mapping*/
+    0,                          /*tp_hash */
+    0,                          /*tp_call*/
+    0,                          /*tp_str*/
+    0,                          /*tp_getattro*/
+    0,                          /*tp_setattro*/
+    0,                          /*tp_as_buffer*/
+    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,  /*tp_flags*/
+    BufferedIOBase_doc,         /* tp_doc */
+    0,                          /* tp_traverse */
+    0,                          /* tp_clear */
+    0,                          /* tp_richcompare */
+    0,                          /* tp_weaklistoffset */
+    0,                          /* tp_iter */
+    0,                          /* tp_iternext */
+    BufferedIOBase_methods,     /* tp_methods */
+    0,                          /* tp_members */
+    0,                          /* tp_getset */
+    &PyIOBase_Type,             /* tp_base */
+    0,                          /* tp_dict */
+    0,                          /* tp_descr_get */
+    0,                          /* tp_descr_set */
+    0,                          /* tp_dictoffset */
+    0,                          /* tp_init */
+    0,                          /* tp_alloc */
+    0,                          /* tp_new */
+};
+
+
+typedef struct {
+    PyObject_HEAD
+
+    PyObject *raw;
+    int ok;    /* Initialized? */
+    int readable;
+    int writable;
+
+    /* Absolute position inside the raw stream (-1 if unknown). */
+    Py_off_t abs_pos;
+
+    /* A static buffer of size `buffer_size` */
+    char *buffer;
+    /* Current logical position in the buffer. */
+    Py_off_t pos;
+    /* Position of the raw stream in the buffer. */
+    Py_off_t raw_pos;
+
+    /* Just after the last buffered byte in the buffer, or -1 if the buffer
+       isn't ready for reading. */
+    Py_off_t read_end;
+
+    /* Just after the last byte actually written */
+    Py_off_t write_pos;
+    /* Just after the last byte waiting to be written, or -1 if the buffer
+       isn't ready for writing. */
+    Py_off_t write_end;
+
+    PyThread_type_lock lock;
+
+    Py_ssize_t buffer_size;
+    Py_ssize_t buffer_mask;
+
+    PyObject *dict;
+    PyObject *weakreflist;
+} BufferedObject;
+
+/*
+    Implementation notes:
+    
+    * BufferedReader, BufferedWriter and BufferedRandom try to share most
+      methods (this is helped by the members `readable` and `writable`, which
+      are initialized in the respective constructors)
+    * They also share a single buffer for reading and writing. This enables
+      interleaved reads and writes without flushing. It also makes the logic
+      a bit trickier to get right.
+    * The absolute position of the raw stream is cached, if possible, in the
+      `abs_pos` member. It must be updated every time an operation is done
+      on the raw stream. If not sure, it can be reinitialized by calling
+      _Buffered_raw_tell(), which queries the raw stream (_Buffered_raw_seek()
+      also does it). To read it, use RAW_TELL().
+    * Three helpers, _BufferedReader_raw_read, _BufferedWriter_raw_write and
+      _BufferedWriter_flush_unlocked do a lot of useful housekeeping.
+
+    NOTE: we should try to maintain block alignment of reads and writes to the
+    raw stream (according to the buffer size), but for now it is only done
+    in read() and friends.
+    
+    XXX: method naming is a bit messy.
+*/
+
+/* These macros protect the BufferedObject against concurrent operations. */
+
+#define ENTER_BUFFERED(self) \
+    Py_BEGIN_ALLOW_THREADS \
+    PyThread_acquire_lock(self->lock, 1); \
+    Py_END_ALLOW_THREADS
+
+#define LEAVE_BUFFERED(self) \
+    PyThread_release_lock(self->lock);
+
+#define CHECK_INITIALIZED(self) \
+    if (self->ok <= 0) { \
+        PyErr_SetString(PyExc_ValueError, \
+            "I/O operation on uninitialized object"); \
+        return NULL; \
+    }
+
+#define CHECK_INITIALIZED_INT(self) \
+    if (self->ok <= 0) { \
+        PyErr_SetString(PyExc_ValueError, \
+            "I/O operation on uninitialized object"); \
+        return -1; \
+    }
+
+#define VALID_READ_BUFFER(self) \
+    (self->readable && self->read_end != -1)
+
+#define VALID_WRITE_BUFFER(self) \
+    (self->writable && self->write_end != -1)
+
+#define ADJUST_POSITION(self, _new_pos) \
+    do { \
+        self->pos = _new_pos; \
+        if (VALID_READ_BUFFER(self) && self->read_end < self->pos) \
+            self->read_end = self->pos; \
+    } while(0)
+
+#define READAHEAD(self) \
+    ((self->readable && VALID_READ_BUFFER(self)) \
+        ? (self->read_end - self->pos) : 0)
+
+#define RAW_OFFSET(self) \
+    (((VALID_READ_BUFFER(self) || VALID_WRITE_BUFFER(self)) \
+        && self->raw_pos >= 0) ? self->raw_pos - self->pos : 0)
+
+#define RAW_TELL(self) \
+    (self->abs_pos != -1 ? self->abs_pos : _Buffered_raw_tell(self))
+
+#define MINUS_LAST_BLOCK(self, size) \
+    (self->buffer_mask ? \
+        (size & ~self->buffer_mask) : \
+        (self->buffer_size * (size / self->buffer_size)))
+
+
+static void
+BufferedObject_dealloc(BufferedObject *self)
+{
+    if (self->ok && _PyIOBase_finalize((PyObject *) self) < 0)
+        return;
+    _PyObject_GC_UNTRACK(self);
+    self->ok = 0;
+    if (self->weakreflist != NULL)
+        PyObject_ClearWeakRefs((PyObject *)self);
+    Py_CLEAR(self->raw);
+    if (self->buffer) {
+        PyMem_Free(self->buffer);
+        self->buffer = NULL;
+    }
+    if (self->lock) {
+        PyThread_free_lock(self->lock);
+        self->lock = NULL;
+    }
+    Py_CLEAR(self->dict);
+    Py_TYPE(self)->tp_free((PyObject *)self);
+}
+
+static int
+Buffered_traverse(BufferedObject *self, visitproc visit, void *arg)
+{
+    Py_VISIT(self->raw);
+    Py_VISIT(self->dict);
+    return 0;
+}
+
+static int
+Buffered_clear(BufferedObject *self)
+{
+    if (self->ok && _PyIOBase_finalize((PyObject *) self) < 0)
+        return -1;
+    self->ok = 0;
+    Py_CLEAR(self->raw);
+    Py_CLEAR(self->dict);
+    return 0;
+}
+
+/*
+ * _BufferedIOMixin methods
+ * This is not a class, just a collection of methods that will be reused
+ * by BufferedReader and BufferedWriter
+ */
+
+/* Flush and close */
+
+static PyObject *
+BufferedIOMixin_flush(BufferedObject *self, PyObject *args)
+{
+    CHECK_INITIALIZED(self)
+    return PyObject_CallMethodObjArgs(self->raw, _PyIO_str_flush, NULL);
+}
+
+static int
+BufferedIOMixin_closed(BufferedObject *self)
+{
+    int closed;
+    PyObject *res;
+    CHECK_INITIALIZED_INT(self)
+    res = PyObject_GetAttr(self->raw, _PyIO_str_closed);
+    if (res == NULL)
+        return -1;
+    closed = PyObject_IsTrue(res);
+    Py_DECREF(res);
+    return closed;
+}
+
+static PyObject *
+BufferedIOMixin_closed_get(BufferedObject *self, void *context)
+{
+    CHECK_INITIALIZED(self)
+    return PyObject_GetAttr(self->raw, _PyIO_str_closed);
+}
+
+static PyObject *
+BufferedIOMixin_close(BufferedObject *self, PyObject *args)
+{
+    PyObject *res = NULL;
+    int r;
+
+    CHECK_INITIALIZED(self)
+    ENTER_BUFFERED(self)
+
+    r = BufferedIOMixin_closed(self);
+    if (r < 0)
+        goto end;
+    if (r > 0) {
+        res = Py_None;
+        Py_INCREF(res);
+        goto end;
+    }
+    /* flush() will most probably re-take the lock, so drop it first */
+    LEAVE_BUFFERED(self)
+    res = PyObject_CallMethodObjArgs((PyObject *)self, _PyIO_str_flush, NULL);
+    ENTER_BUFFERED(self)
+    if (res == NULL) {
+        /* If flush() fails, just give up */
+        if (PyErr_ExceptionMatches(PyExc_IOError))
+            PyErr_Clear();
+        else
+            goto end;
+    }
+    Py_XDECREF(res);
+
+    res = PyObject_CallMethodObjArgs(self->raw, _PyIO_str_close, NULL);
+
+end:
+    LEAVE_BUFFERED(self)
+    return res;
+}
+
+/* Inquiries */
+
+static PyObject *
+BufferedIOMixin_seekable(BufferedObject *self, PyObject *args)
+{
+    CHECK_INITIALIZED(self)
+    return PyObject_CallMethodObjArgs(self->raw, _PyIO_str_seekable, NULL);
+}
+
+static PyObject *
+BufferedIOMixin_readable(BufferedObject *self, PyObject *args)
+{
+    CHECK_INITIALIZED(self)
+    return PyObject_CallMethodObjArgs(self->raw, _PyIO_str_readable, NULL);
+}
+
+static PyObject *
+BufferedIOMixin_writable(BufferedObject *self, PyObject *args)
+{
+    CHECK_INITIALIZED(self)
+    return PyObject_CallMethodObjArgs(self->raw, _PyIO_str_writable, NULL);
+}
+
+static PyObject *
+BufferedIOMixin_name_get(BufferedObject *self, void *context)
+{
+    CHECK_INITIALIZED(self)
+    return PyObject_GetAttrString(self->raw, "name");
+}
+
+static PyObject *
+BufferedIOMixin_mode_get(BufferedObject *self, void *context)
+{
+    CHECK_INITIALIZED(self)
+    return PyObject_GetAttrString(self->raw, "mode");
+}
+
+/* Lower-level APIs */
+
+static PyObject *
+BufferedIOMixin_fileno(BufferedObject *self, PyObject *args)
+{
+    CHECK_INITIALIZED(self)
+    return PyObject_CallMethodObjArgs(self->raw, _PyIO_str_fileno, NULL);
+}
+
+static PyObject *
+BufferedIOMixin_isatty(BufferedObject *self, PyObject *args)
+{
+    CHECK_INITIALIZED(self)
+    return PyObject_CallMethodObjArgs(self->raw, _PyIO_str_isatty, NULL);
+}
+
+
+/* Forward decls */
+static PyObject *
+_BufferedWriter_flush_unlocked(BufferedObject *, int);
+static Py_ssize_t
+_BufferedReader_fill_buffer(BufferedObject *self);
+static void
+_BufferedReader_reset_buf(BufferedObject *self);
+static void
+_BufferedWriter_reset_buf(BufferedObject *self);
+static PyObject *
+_BufferedReader_peek_unlocked(BufferedObject *self, Py_ssize_t);
+static PyObject *
+_BufferedReader_read_unlocked(BufferedObject *self, Py_ssize_t);
+
+
+/*
+ * Helpers
+ */
+
+/* Returns the address of the `written` member if a BlockingIOError was
+   raised, NULL otherwise. The error is always re-raised. */
+static Py_ssize_t *
+_Buffered_check_blocking_error(void)
+{
+    PyObject *t, *v, *tb;
+    PyBlockingIOErrorObject *err;
+
+    PyErr_Fetch(&t, &v, &tb);
+    if (v == NULL || !PyErr_GivenExceptionMatches(v, PyExc_BlockingIOError)) {
+        PyErr_Restore(t, v, tb);
+        return NULL;
+    }
+    err = (PyBlockingIOErrorObject *) v;
+    /* TODO: sanity check (err->written >= 0) */
+    PyErr_Restore(t, v, tb);
+    return &err->written;
+}
+
+static Py_off_t
+_Buffered_raw_tell(BufferedObject *self)
+{
+    PyObject *res;
+    Py_off_t n;
+    res = PyObject_CallMethodObjArgs(self->raw, _PyIO_str_tell, NULL);
+    if (res == NULL)
+        return -1;
+    n = PyNumber_AsOff_t(res, PyExc_ValueError);
+    Py_DECREF(res);
+    if (n < 0) {
+        if (!PyErr_Occurred())
+            PyErr_Format(PyExc_IOError,
+                         "Raw stream returned invalid position %zd", n);
+        return -1;
+    }
+    self->abs_pos = n;
+    return n;
+}
+
+static Py_off_t
+_Buffered_raw_seek(BufferedObject *self, Py_off_t target, int whence)
+{
+    PyObject *res, *posobj, *whenceobj;
+    Py_off_t n;
+
+    posobj = PyLong_FromOff_t(target);
+    if (posobj == NULL)
+        return -1;
+    whenceobj = PyLong_FromLong(whence);
+    if (whenceobj == NULL) {
+        Py_DECREF(posobj);
+        return -1;
+    }
+    res = PyObject_CallMethodObjArgs(self->raw, _PyIO_str_seek,
+                                     posobj, whenceobj, NULL);
+    Py_DECREF(posobj);
+    Py_DECREF(whenceobj);
+    if (res == NULL)
+        return -1;
+    n = PyNumber_AsOff_t(res, PyExc_ValueError);
+    Py_DECREF(res);
+    if (n < 0) {
+        if (!PyErr_Occurred())
+            PyErr_Format(PyExc_IOError,
+                         "Raw stream returned invalid position %zd", n);
+        return -1;
+    }
+    self->abs_pos = n;
+    return n;
+}
+
+static int
+_Buffered_init(BufferedObject *self)
+{
+    Py_ssize_t n;
+    if (self->buffer_size <= 0) {
+        PyErr_SetString(PyExc_ValueError,
+            "buffer size must be strictly positive");
+        return -1;
+    }
+    if (self->buffer)
+        PyMem_Free(self->buffer);
+    self->buffer = PyMem_Malloc(self->buffer_size);
+    if (self->buffer == NULL) {
+        PyErr_NoMemory();
+        return -1;
+    }
+    self->lock = PyThread_allocate_lock();
+    if (self->lock == NULL) {
+        PyErr_SetString(PyExc_RuntimeError, "can't allocate read lock");
+        return -1;
+    }
+    /* Find out whether buffer_size is a power of 2 */
+    /* XXX is this optimization useful? */
+    for (n = self->buffer_size - 1; n & 1; n >>= 1)
+        ;
+    if (n == 0)
+        self->buffer_mask = self->buffer_size - 1;
+    else
+        self->buffer_mask = 0;
+    if (_Buffered_raw_tell(self) == -1)
+        PyErr_Clear();
+    return 0;
+}
+
+/*
+ * Shared methods and wrappers
+ */
+
+static PyObject *
+Buffered_flush(BufferedObject *self, PyObject *args)
+{
+    PyObject *res;
+
+    CHECK_INITIALIZED(self)
+    if (BufferedIOMixin_closed(self)) {
+        PyErr_SetString(PyExc_ValueError, "flush of closed file");
+        return NULL;
+    }
+
+    ENTER_BUFFERED(self)
+    res = _BufferedWriter_flush_unlocked(self, 0);
+    if (res != NULL && self->readable) {
+        /* Rewind the raw stream so that its position corresponds to
+           the current logical position. */
+        Py_off_t n;
+        n = _Buffered_raw_seek(self, -RAW_OFFSET(self), 1);
+        if (n == -1)
+            Py_CLEAR(res);
+        _BufferedReader_reset_buf(self);
+    }
+    LEAVE_BUFFERED(self)
+
+    return res;
+}
+
+static PyObject *
+Buffered_peek(BufferedObject *self, PyObject *args)
+{
+    Py_ssize_t n = 0;
+    PyObject *res = NULL;
+
+    CHECK_INITIALIZED(self)
+    if (!PyArg_ParseTuple(args, "|n:peek", &n)) {
+        return NULL;
+    }
+
+    ENTER_BUFFERED(self)
+
+    if (self->writable) {
+        res = _BufferedWriter_flush_unlocked(self, 1);
+        if (res == NULL)
+            goto end;
+        Py_CLEAR(res);
+    }
+    res = _BufferedReader_peek_unlocked(self, n);
+
+end:
+    LEAVE_BUFFERED(self)
+    return res;
+}
+
+static PyObject *
+Buffered_read(BufferedObject *self, PyObject *args)
+{
+    Py_ssize_t n = -1;
+    PyObject *res;
+
+    CHECK_INITIALIZED(self)
+    if (!PyArg_ParseTuple(args, "|n:read", &n)) {
+        return NULL;
+    }
+    if (n < -1) {
+        PyErr_SetString(PyExc_ValueError,
+                        "read length must be positive or -1");
+        return NULL;
+    }
+
+    if (BufferedIOMixin_closed(self)) {
+        PyErr_SetString(PyExc_ValueError, "read of closed file");
+        return NULL;
+    }
+
+    ENTER_BUFFERED(self)
+    res = _BufferedReader_read_unlocked(self, n);
+    LEAVE_BUFFERED(self)
+
+    return res;
+}
+
+static PyObject *
+Buffered_read1(BufferedObject *self, PyObject *args)
+{
+    Py_ssize_t n, have, r;
+    PyObject *res = NULL;
+
+    CHECK_INITIALIZED(self)
+    if (!PyArg_ParseTuple(args, "n:read1", &n)) {
+        return NULL;
+    }
+
+    if (n < 0) {
+        PyErr_SetString(PyExc_ValueError,
+                        "read length must be positive");
+        return NULL;
+    }
+    if (n == 0)
+        return PyBytes_FromStringAndSize(NULL, 0);
+
+    ENTER_BUFFERED(self)
+    
+    if (self->writable) {
+        res = _BufferedWriter_flush_unlocked(self, 1);
+        if (res == NULL)
+            goto end;
+        Py_CLEAR(res);
+    }
+
+    /* Return up to n bytes.  If at least one byte is buffered, we
+       only return buffered bytes.  Otherwise, we do one raw read. */
+
+    /* XXX: this mimicks the io.py implementation but is probably wrong.
+       If we need to read from the raw stream, then we could actually read
+       all `n` bytes asked by the caller (and possibly more, so as to fill
+       our buffer for the next reads). */
+
+    have = Py_SAFE_DOWNCAST(READAHEAD(self), Py_off_t, Py_ssize_t);
+    if (have > 0) {
+        if (n > have)
+            n = have;
+        res = PyBytes_FromStringAndSize(self->buffer + self->pos, n);
+        if (res == NULL)
+            goto end;
+        self->pos += n;
+        goto end;
+    }
+
+    /* Fill the buffer from the raw stream, and copy it to the result. */
+    _BufferedReader_reset_buf(self);
+    r = _BufferedReader_fill_buffer(self);
+    if (r == -1)
+        goto end;
+    if (r == -2)
+        r = 0;
+    if (n > r)
+        n = r;
+    res = PyBytes_FromStringAndSize(self->buffer, n);
+    if (res == NULL)
+        goto end;
+    self->pos = n;
+
+end:
+    LEAVE_BUFFERED(self)
+    return res;
+}
+
+static PyObject *
+Buffered_readinto(BufferedObject *self, PyObject *args)
+{
+    PyObject *res = NULL;
+
+    CHECK_INITIALIZED(self)
+    
+    /* TODO: use raw.readinto() instead! */
+    if (self->writable) {
+        ENTER_BUFFERED(self)
+        res = _BufferedWriter_flush_unlocked(self, 0);
+        LEAVE_BUFFERED(self)
+        if (res == NULL)
+            goto end;
+        Py_DECREF(res);
+    }
+    res = BufferedIOBase_readinto((PyObject *)self, args);
+
+end:
+    return res;
+}
+
+static PyObject *
+_Buffered_readline(BufferedObject *self, Py_ssize_t limit)
+{
+    PyObject *res = NULL;
+    PyObject *chunks = NULL;
+    Py_ssize_t n, written = 0;
+    const char *start, *s, *end;
+
+    if (BufferedIOMixin_closed(self)) {
+        PyErr_SetString(PyExc_ValueError, "readline of closed file");
+        return NULL;
+    }
+
+    ENTER_BUFFERED(self)
+
+    /* First, try to find a line in the buffer */
+    n = Py_SAFE_DOWNCAST(READAHEAD(self), Py_off_t, Py_ssize_t);
+    if (limit >= 0 && n > limit)
+        n = limit;
+    start = self->buffer + self->pos;
+    end = start + n;
+    s = start;
+    while (s < end) {
+        if (*s++ == '\n') {
+            res = PyBytes_FromStringAndSize(start, s - start);
+            if (res != NULL)
+                self->pos += s - start;
+            goto end;
+        }
+    }
+    if (n == limit) {
+        res = PyBytes_FromStringAndSize(start, n);
+        if (res != NULL)
+            self->pos += n;
+        goto end;
+    }
+
+    /* Now we try to get some more from the raw stream */
+    if (self->writable) {
+        res = _BufferedWriter_flush_unlocked(self, 1);
+        if (res == NULL)
+            goto end;
+        Py_CLEAR(res);
+    }
+    chunks = PyList_New(0);
+    if (chunks == NULL)
+        goto end;
+    if (n > 0) {
+        res = PyBytes_FromStringAndSize(start, n);
+        if (res == NULL)
+            goto end;
+        if (PyList_Append(chunks, res) < 0) {
+            Py_CLEAR(res);
+            goto end;
+        }
+        Py_CLEAR(res);
+        written += n;
+        if (limit >= 0)
+            limit -= n;
+    }
+
+    for (;;) {
+        _BufferedReader_reset_buf(self);
+        n = _BufferedReader_fill_buffer(self);
+        if (n == -1)
+            goto end;
+        if (n <= 0)
+            break;
+        if (limit >= 0 && n > limit)
+            n = limit;
+        start = self->buffer;
+        end = start + n;
+        s = start;
+        while (s < end) {
+            if (*s++ == '\n') {
+                res = PyBytes_FromStringAndSize(start, s - start);
+                if (res == NULL)
+                    goto end;
+                self->pos = s - start;
+                goto found;
+            }
+        }
+        res = PyBytes_FromStringAndSize(start, n);
+        if (res == NULL)
+            goto end;
+        if (n == limit) {
+            self->pos = n;
+            break;
+        }
+        if (PyList_Append(chunks, res) < 0) {
+            Py_CLEAR(res);
+            goto end;
+        }
+        Py_CLEAR(res);
+        written += n;
+        if (limit >= 0)
+            limit -= n;
+    }
+found:
+    if (res != NULL && PyList_Append(chunks, res) < 0) {
+        Py_CLEAR(res);
+        goto end;
+    }
+    Py_CLEAR(res);
+    res = _PyBytes_Join(_PyIO_empty_bytes, chunks);
+
+end:
+    LEAVE_BUFFERED(self)
+    Py_XDECREF(chunks);
+    return res;
+}
+
+static PyObject *
+Buffered_readline(BufferedObject *self, PyObject *args)
+{
+    Py_ssize_t limit = -1;
+
+    CHECK_INITIALIZED(self)
+
+    if (!PyArg_ParseTuple(args, "|n:readline", &limit)) {
+        return NULL;
+    }
+    return _Buffered_readline(self, limit);
+}
+
+
+static PyObject *
+Buffered_tell(BufferedObject *self, PyObject *args)
+{
+    Py_off_t pos;
+
+    CHECK_INITIALIZED(self)
+    pos = _Buffered_raw_tell(self);
+    if (pos == -1)
+        return NULL;
+    pos -= RAW_OFFSET(self);
+    /* TODO: sanity check (pos >= 0) */
+    return PyLong_FromOff_t(pos);
+}
+
+static PyObject *
+Buffered_seek(BufferedObject *self, PyObject *args)
+{
+    Py_off_t target, n;
+    int whence = 0;
+    PyObject *targetobj, *res = NULL;
+
+    CHECK_INITIALIZED(self)
+    if (!PyArg_ParseTuple(args, "O|i:seek", &targetobj, &whence)) {
+        return NULL;
+    }
+    
+    if (whence < 0 || whence > 2) {
+        PyErr_Format(PyExc_ValueError,
+                     "whence must be between 0 and 2, not %d", whence);
+        return NULL;
+    }
+    target = PyNumber_AsOff_t(targetobj, PyExc_ValueError);
+    if (target == -1 && PyErr_Occurred())
+        return NULL;
+
+    ENTER_BUFFERED(self)
+
+    if (whence != 2 && self->readable) {
+        Py_off_t current, avail;
+        /* Check if seeking leaves us inside the current buffer,
+           so as to return quickly if possible.
+           Don't know how to do that when whence == 2, though. */
+        current = RAW_TELL(self);
+        avail = READAHEAD(self);
+        if (avail > 0) {
+            Py_off_t offset;
+            if (whence == 0)
+                offset = target - (current - RAW_OFFSET(self));
+            else
+                offset = target;
+            if (offset >= -self->pos && offset <= avail) {
+                self->pos += offset;
+                res = PyLong_FromOff_t(current - avail + offset);
+                goto end;
+            }
+        }
+    }
+
+    /* Fallback: invoke raw seek() method and clear buffer */
+    if (self->writable) {
+        res = _BufferedWriter_flush_unlocked(self, 0);
+        if (res == NULL)
+            goto end;
+        Py_CLEAR(res);
+        _BufferedWriter_reset_buf(self);
+    }
+
+    /* TODO: align on block boundary and read buffer if needed? */
+    if (whence == 1)
+        target -= RAW_OFFSET(self);
+    n = _Buffered_raw_seek(self, target, whence);
+    if (n == -1)
+        goto end;
+    self->raw_pos = -1;
+    res = PyLong_FromOff_t(n);
+    if (res != NULL && self->readable)
+        _BufferedReader_reset_buf(self);
+
+end:
+    LEAVE_BUFFERED(self)
+    return res;
+}
+
+static PyObject *
+Buffered_truncate(BufferedObject *self, PyObject *args)
+{
+    PyObject *pos = Py_None;
+    PyObject *res = NULL;
+
+    CHECK_INITIALIZED(self)
+    if (!PyArg_ParseTuple(args, "|O:truncate", &pos)) {
+        return NULL;
+    }
+
+    ENTER_BUFFERED(self)
+
+    if (self->writable) {
+        res = _BufferedWriter_flush_unlocked(self, 0);
+        if (res == NULL)
+            goto end;
+        Py_CLEAR(res);
+    }
+    if (self->readable) {
+        if (pos == Py_None) {
+            /* Rewind the raw stream so that its position corresponds to
+               the current logical position. */
+            if (_Buffered_raw_seek(self, -RAW_OFFSET(self), 1) == -1)
+                goto end;
+        }
+        _BufferedReader_reset_buf(self);
+    }
+    res = PyObject_CallMethodObjArgs(self->raw, _PyIO_str_truncate, pos, NULL);
+    if (res == NULL)
+        goto end;
+    /* Reset cached position */
+    if (_Buffered_raw_tell(self) == -1)
+        PyErr_Clear();
+
+end:
+    LEAVE_BUFFERED(self)
+    return res;
+}
+
+static PyObject *
+Buffered_iternext(BufferedObject *self)
+{
+    PyObject *line;
+    PyTypeObject *tp;
+
+    CHECK_INITIALIZED(self);
+
+    tp = Py_TYPE(self);
+    if (tp == &PyBufferedReader_Type ||
+        tp == &PyBufferedRandom_Type) {
+        /* Skip method call overhead for speed */
+        line = _Buffered_readline(self, -1);
+    }
+    else {
+        line = PyObject_CallMethodObjArgs((PyObject *)self,
+                                           _PyIO_str_readline, NULL);
+        if (line && !PyBytes_Check(line)) {
+            PyErr_Format(PyExc_IOError,
+                         "readline() should have returned a bytes object, "
+                         "not '%.200s'", Py_TYPE(line)->tp_name);
+            Py_DECREF(line);
+            return NULL;
+        }
+    }
+
+    if (line == NULL)
+        return NULL;
+
+    if (PyBytes_GET_SIZE(line) == 0) {
+        /* Reached EOF or would have blocked */
+        Py_DECREF(line);
+        return NULL;
+    }
+
+    return line;
+}
+
+/*
+ * class BufferedReader
+ */
+
+PyDoc_STRVAR(BufferedReader_doc,
+             "Create a new buffered reader using the given readable raw IO object.");
+
+static void _BufferedReader_reset_buf(BufferedObject *self)
+{
+    self->read_end = -1;
+}
+
+static int
+BufferedReader_init(BufferedObject *self, PyObject *args, PyObject *kwds)
+{
+    char *kwlist[] = {"raw", "buffer_size", NULL};
+    Py_ssize_t buffer_size = DEFAULT_BUFFER_SIZE;
+    PyObject *raw;
+
+    self->ok = 0;
+
+    if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|n:BufferedReader", kwlist,
+                                     &raw, &buffer_size)) {
+        return -1;
+    }
+
+    if (_PyIOBase_checkReadable(raw, Py_True) == NULL)
+        return -1;
+
+    Py_CLEAR(self->raw);
+    Py_INCREF(raw);
+    self->raw = raw;
+    self->buffer_size = buffer_size;
+    self->readable = 1;
+    self->writable = 0;
+
+    if (_Buffered_init(self) < 0)
+        return -1;
+    _BufferedReader_reset_buf(self);
+
+    self->ok = 1;
+    return 0;
+}
+
+static Py_ssize_t
+_BufferedReader_raw_read(BufferedObject *self, char *start, Py_ssize_t len)
+{
+    Py_buffer buf;
+    PyObject *memobj, *res;
+    Py_ssize_t n;
+    /* NOTE: the buffer needn't be released as its object is NULL. */
+    if (PyBuffer_FillInfo(&buf, NULL, start, len, 0, PyBUF_CONTIG) == -1)
+        return -1;
+    memobj = PyMemoryView_FromBuffer(&buf);
+    if (memobj == NULL)
+        return -1;
+    res = PyObject_CallMethodObjArgs(self->raw, _PyIO_str_readinto, memobj, NULL);
+    Py_DECREF(memobj);
+    if (res == NULL)
+        return -1;
+    if (res == Py_None) {
+        /* Non-blocking stream would have blocked. Special return code! */
+        Py_DECREF(res);
+        return -2;
+    }
+    n = PyNumber_AsSsize_t(res, PyExc_ValueError);
+    Py_DECREF(res);
+    if (n < 0 || n > len) {
+        PyErr_Format(PyExc_IOError,
+                     "raw readinto() returned invalid length %zd "
+                     "(should have been between 0 and %zd)", n, len);
+        return -1;
+    }
+    if (n > 0 && self->abs_pos != -1)
+        self->abs_pos += n;
+    return n;
+}
+
+static Py_ssize_t
+_BufferedReader_fill_buffer(BufferedObject *self)
+{
+    Py_ssize_t start, len, n;
+    if (VALID_READ_BUFFER(self))
+        start = Py_SAFE_DOWNCAST(self->read_end, Py_off_t, Py_ssize_t);
+    else
+        start = 0;
+    len = self->buffer_size - start;
+    n = _BufferedReader_raw_read(self, self->buffer + start, len);
+    if (n <= 0)
+        return n;
+    self->read_end = start + n;
+    self->raw_pos = start + n;
+    return n;
+}
+
+static PyObject *
+_BufferedReader_read_unlocked(BufferedObject *self, Py_ssize_t n)
+{
+    PyObject *data, *res = NULL;
+    Py_ssize_t current_size, remaining, written;
+    char *out;
+    static PyObject *sep = NULL;
+
+    /* Special case for when the number of bytes to read is unspecified. */
+    if (n == -1) {
+        PyObject *chunks = PyList_New(0);
+        if (chunks == NULL)
+            return NULL;
+
+        /* First copy what we have in the current buffer. */
+        current_size = Py_SAFE_DOWNCAST(READAHEAD(self), Py_off_t, Py_ssize_t);
+        data = NULL;
+        if (current_size) {
+            data = PyBytes_FromStringAndSize(
+                self->buffer + self->pos, current_size);
+            if (data == NULL) {
+                Py_DECREF(chunks);
+                return NULL;
+            }
+        }
+        _BufferedReader_reset_buf(self);
+        /* We're going past the buffer's bounds, flush it */
+        if (self->writable) {
+            res = _BufferedWriter_flush_unlocked(self, 1);
+            if (res == NULL) {
+                Py_DECREF(chunks);
+                return NULL;
+            }
+            Py_CLEAR(res);
+        }
+        while (1) {
+            if (data) {
+                if (PyList_Append(chunks, data) < 0) {
+                    Py_DECREF(data);
+                    Py_DECREF(chunks);
+                    return NULL;
+                }
+                Py_DECREF(data);
+            }
+
+            /* Read until EOF or until read() would block. */
+            data = PyObject_CallMethodObjArgs(self->raw, _PyIO_str_read, NULL);
+            if (data == NULL) {
+                Py_DECREF(chunks);
+                return NULL;
+            }
+            if (data != Py_None && !PyBytes_Check(data)) {
+                Py_DECREF(data);
+                Py_DECREF(chunks);
+                PyErr_SetString(PyExc_TypeError, "read() should return bytes");
+                return NULL;
+            }
+            if (data == Py_None || PyBytes_GET_SIZE(data) == 0) {
+                if (current_size == 0) {
+                    Py_DECREF(chunks);
+                    return data;
+                }
+                else {
+                    if (sep == NULL) {
+                        sep = PyBytes_FromStringAndSize(NULL, 0);
+                        if (sep == NULL) {
+                            Py_DECREF(data);
+                            Py_DECREF(chunks);
+                            return NULL;
+                        }
+                    }
+                    res =_PyBytes_Join(sep, chunks);
+                    Py_DECREF(data);
+                    Py_DECREF(chunks);
+                    return res;
+                }
+            }
+            current_size += PyBytes_GET_SIZE(data);
+            if (self->abs_pos != -1)
+                self->abs_pos += PyBytes_GET_SIZE(data);
+        }
+    }
+
+    /* The number of bytes to read is specified, return at most n bytes. */
+    current_size = Py_SAFE_DOWNCAST(READAHEAD(self), Py_off_t, Py_ssize_t);
+    if (n <= current_size) {
+        /* Fast path: the data to read is fully buffered. */
+        res = PyBytes_FromStringAndSize(self->buffer + self->pos, n);
+        if (res == NULL)
+            goto error;
+        self->pos += n;
+        return res;
+    }
+
+    /* Slow path: read from the stream until enough bytes are read,
+     * or until an EOF occurs or until read() would block.
+     */
+    res = PyBytes_FromStringAndSize(NULL, n);
+    if (res == NULL)
+        goto error;
+    out = PyBytes_AS_STRING(res);
+    remaining = n;
+    written = 0;
+    if (current_size > 0) {
+        memcpy(out, self->buffer + self->pos, current_size);
+        remaining -= current_size;
+        written += current_size;
+    }
+    _BufferedReader_reset_buf(self);
+    while (remaining > 0) {
+        /* We want to read a whole block at the end into buffer.
+           If we had readv() we could do this in one pass. */
+        Py_ssize_t r = MINUS_LAST_BLOCK(self, remaining);
+        if (r == 0)
+            break;
+        r = _BufferedReader_raw_read(self, out + written, r);
+        if (r == -1)
+            goto error;
+        if (r == 0 || r == -2) {
+            /* EOF occurred or read() would block. */
+            if (r == 0 || written > 0) {
+                if (_PyBytes_Resize(&res, written))
+                    goto error;
+                return res;
+            }
+            Py_DECREF(res);
+            Py_INCREF(Py_None);
+            return Py_None;
+        }
+        remaining -= r;
+        written += r;
+    }
+    assert(remaining <= self->buffer_size);
+    self->pos = 0;
+    self->raw_pos = 0;
+    self->read_end = 0;
+    while (self->read_end < self->buffer_size) {
+        Py_ssize_t r = _BufferedReader_fill_buffer(self);
+        if (r == -1)
+            goto error;
+        if (r == 0 || r == -2) {
+            /* EOF occurred or read() would block. */
+            if (r == 0 || written > 0) {
+                if (_PyBytes_Resize(&res, written))
+                    goto error;
+                return res;
+            }
+            Py_DECREF(res);
+            Py_INCREF(Py_None);
+            return Py_None;
+        }
+        if (remaining > r) {
+            memcpy(out + written, self->buffer + self->pos, r);
+            written += r;
+            self->pos += r;
+            remaining -= r;
+        }
+        else if (remaining > 0) {
+            memcpy(out + written, self->buffer + self->pos, remaining);
+            written += remaining;
+            self->pos += remaining;
+            remaining = 0;
+        }
+        if (remaining == 0)
+            break;
+    }
+
+    return res;
+
+error:
+    Py_XDECREF(res);
+    return NULL;
+}
+
+static PyObject *
+_BufferedReader_peek_unlocked(BufferedObject *self, Py_ssize_t n)
+{
+    Py_ssize_t have, r;
+
+    have = Py_SAFE_DOWNCAST(READAHEAD(self), Py_off_t, Py_ssize_t);
+    /* Constraints:
+       1. we don't want to advance the file position.
+       2. we don't want to lose block alignment, so we can't shift the buffer
+          to make some place.
+       Therefore, we either return `have` bytes (if > 0), or a full buffer.
+    */
+    if (have > 0) {
+        return PyBytes_FromStringAndSize(self->buffer + self->pos, have);
+    }
+
+    /* Fill the buffer from the raw stream, and copy it to the result. */
+    _BufferedReader_reset_buf(self);
+    r = _BufferedReader_fill_buffer(self);
+    if (r == -1)
+        return NULL;
+    if (r == -2)
+        r = 0;
+    self->pos = 0;
+    return PyBytes_FromStringAndSize(self->buffer, r);
+}
+
+static PyMethodDef BufferedReader_methods[] = {
+    /* BufferedIOMixin methods */
+    {"flush", (PyCFunction)BufferedIOMixin_flush, METH_NOARGS},
+    {"close", (PyCFunction)BufferedIOMixin_close, METH_NOARGS},
+    {"seekable", (PyCFunction)BufferedIOMixin_seekable, METH_NOARGS},
+    {"readable", (PyCFunction)BufferedIOMixin_readable, METH_NOARGS},
+    {"writable", (PyCFunction)BufferedIOMixin_writable, METH_NOARGS},
+    {"fileno", (PyCFunction)BufferedIOMixin_fileno, METH_NOARGS},
+    {"isatty", (PyCFunction)BufferedIOMixin_isatty, METH_NOARGS},
+
+    {"read", (PyCFunction)Buffered_read, METH_VARARGS},
+    {"peek", (PyCFunction)Buffered_peek, METH_VARARGS},
+    {"read1", (PyCFunction)Buffered_read1, METH_VARARGS},
+    {"readline", (PyCFunction)Buffered_readline, METH_VARARGS},
+    {"seek", (PyCFunction)Buffered_seek, METH_VARARGS},
+    {"tell", (PyCFunction)Buffered_tell, METH_NOARGS},
+    {"truncate", (PyCFunction)Buffered_truncate, METH_VARARGS},
+    {NULL, NULL}
+};
+
+static PyMemberDef BufferedReader_members[] = {
+    {"raw", T_OBJECT, offsetof(BufferedObject, raw), 0},
+    {NULL}
+};
+
+static PyGetSetDef BufferedReader_getset[] = {
+    {"closed", (getter)BufferedIOMixin_closed_get, NULL, NULL},
+    {"name", (getter)BufferedIOMixin_name_get, NULL, NULL},
+    {"mode", (getter)BufferedIOMixin_mode_get, NULL, NULL},
+    {0}
+};
+
+
+PyTypeObject PyBufferedReader_Type = {
+    PyVarObject_HEAD_INIT(NULL, 0)
+    "_io.BufferedReader",       /*tp_name*/
+    sizeof(BufferedObject),     /*tp_basicsize*/
+    0,                          /*tp_itemsize*/
+    (destructor)BufferedObject_dealloc,     /*tp_dealloc*/
+    0,                          /*tp_print*/
+    0,                          /*tp_getattr*/
+    0,                          /*tp_setattr*/
+    0,                          /*tp_compare */
+    0,                          /*tp_repr*/
+    0,                          /*tp_as_number*/
+    0,                          /*tp_as_sequence*/
+    0,                          /*tp_as_mapping*/
+    0,                          /*tp_hash */
+    0,                          /*tp_call*/
+    0,                          /*tp_str*/
+    0,                          /*tp_getattro*/
+    0,                          /*tp_setattro*/
+    0,                          /*tp_as_buffer*/
+    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE
+            | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
+    BufferedReader_doc,         /* tp_doc */
+    (traverseproc)Buffered_traverse, /* tp_traverse */
+    (inquiry)Buffered_clear,    /* tp_clear */
+    0,                          /* tp_richcompare */
+    offsetof(BufferedObject, weakreflist), /*tp_weaklistoffset*/
+    0,                          /* tp_iter */
+    (iternextfunc)Buffered_iternext, /* tp_iternext */
+    BufferedReader_methods,     /* tp_methods */
+    BufferedReader_members,     /* tp_members */
+    BufferedReader_getset,      /* tp_getset */
+    0,                          /* tp_base */
+    0,                          /* tp_dict */
+    0,                          /* tp_descr_get */
+    0,                          /* tp_descr_set */
+    offsetof(BufferedObject, dict), /* tp_dictoffset */
+    (initproc)BufferedReader_init, /* tp_init */
+    0,                          /* tp_alloc */
+    PyType_GenericNew,          /* tp_new */
+};
+
+
+/*
+ * class BufferedWriter
+ */
+PyDoc_STRVAR(BufferedWriter_doc,
+    "A buffer for a writeable sequential RawIO object.\n"
+    "\n"
+    "The constructor creates a BufferedWriter for the given writeable raw\n"
+    "stream. If the buffer_size is not given, it defaults to\n"
+    "DEFAULT_BUFFER_SIZE. max_buffer_size isn't used anymore.\n"
+    );
+
+static void
+_BufferedWriter_reset_buf(BufferedObject *self)
+{
+    self->write_pos = 0;
+    self->write_end = -1;
+}
+
+static int
+BufferedWriter_init(BufferedObject *self, PyObject *args, PyObject *kwds)
+{
+    /* TODO: properly deprecate max_buffer_size */
+    char *kwlist[] = {"raw", "buffer_size", "max_buffer_size", NULL};
+    Py_ssize_t buffer_size = DEFAULT_BUFFER_SIZE;
+    Py_ssize_t max_buffer_size = -1;
+    PyObject *raw;
+
+    self->ok = 0;
+
+    if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|nn:BufferedReader", kwlist,
+                                     &raw, &buffer_size, &max_buffer_size)) {
+        return -1;
+    }
+
+    if (_PyIOBase_checkWritable(raw, Py_True) == NULL)
+        return -1;
+
+    Py_CLEAR(self->raw);
+    Py_INCREF(raw);
+    self->raw = raw;
+    self->readable = 0;
+    self->writable = 1;
+
+    self->buffer_size = buffer_size;
+    if (_Buffered_init(self) < 0)
+        return -1;
+    _BufferedWriter_reset_buf(self);
+    self->pos = 0;
+
+    self->ok = 1;
+    return 0;
+}
+
+static Py_ssize_t
+_BufferedWriter_raw_write(BufferedObject *self, char *start, Py_ssize_t len)
+{
+    Py_buffer buf;
+    PyObject *memobj, *res;
+    Py_ssize_t n;
+    /* NOTE: the buffer needn't be released as its object is NULL. */
+    if (PyBuffer_FillInfo(&buf, NULL, start, len, 1, PyBUF_CONTIG_RO) == -1)
+        return -1;
+    memobj = PyMemoryView_FromBuffer(&buf);
+    if (memobj == NULL)
+        return -1;
+    res = PyObject_CallMethodObjArgs(self->raw, _PyIO_str_write, memobj, NULL);
+    Py_DECREF(memobj);
+    if (res == NULL)
+        return -1;
+    n = PyNumber_AsSsize_t(res, PyExc_ValueError);
+    Py_DECREF(res);
+    if (n < 0 || n > len) {
+        PyErr_Format(PyExc_IOError,
+                     "raw write() returned invalid length %zd "
+                     "(should have been between 0 and %zd)", n, len);
+        return -1;
+    }
+    if (n > 0 && self->abs_pos != -1)
+        self->abs_pos += n;
+    return n;
+}
+
+/* `restore_pos` is 1 if we need to restore the raw stream position at
+   the end, 0 otherwise. */
+static PyObject *
+_BufferedWriter_flush_unlocked(BufferedObject *self, int restore_pos)
+{
+    Py_ssize_t written = 0;
+    Py_off_t n, rewind;
+
+    if (!VALID_WRITE_BUFFER(self) || self->write_pos == self->write_end)
+        goto end;
+    /* First, rewind */
+    rewind = RAW_OFFSET(self) + (self->pos - self->write_pos);
+    if (rewind != 0) {
+        n = _Buffered_raw_seek(self, -rewind, 1);
+        if (n < 0) {
+            goto error;
+        }
+        self->raw_pos -= rewind;
+    }
+    while (self->write_pos < self->write_end) {
+        n = _BufferedWriter_raw_write(self,
+            self->buffer + self->write_pos,
+            Py_SAFE_DOWNCAST(self->write_end - self->write_pos,
+                             Py_off_t, Py_ssize_t));
+        if (n == -1) {
+            Py_ssize_t *w = _Buffered_check_blocking_error();
+            if (w == NULL)
+                goto error;
+            self->write_pos += *w;
+            self->raw_pos = self->write_pos;
+            written += *w;
+            *w = written;
+            /* Already re-raised */
+            goto error;
+        }
+        self->write_pos += n;
+        self->raw_pos = self->write_pos;
+        written += Py_SAFE_DOWNCAST(n, Py_off_t, Py_ssize_t);
+    }
+
+    if (restore_pos) {
+        Py_off_t forward = rewind - written;
+        if (forward != 0) {
+            n = _Buffered_raw_seek(self, forward, 1);
+            if (n < 0) {
+                goto error;
+            }
+            self->raw_pos += forward;
+        }
+    }
+    _BufferedWriter_reset_buf(self);
+
+end:
+    Py_RETURN_NONE;
+
+error:
+    return NULL;
+}
+
+static PyObject *
+BufferedWriter_write(BufferedObject *self, PyObject *args)
+{
+    PyObject *res = NULL;
+    Py_buffer buf;
+    Py_ssize_t written, avail, remaining, n;
+
+    CHECK_INITIALIZED(self)
+    if (!PyArg_ParseTuple(args, "y*:write", &buf)) {
+        return NULL;
+    }
+
+    if (BufferedIOMixin_closed(self)) {
+        PyErr_SetString(PyExc_ValueError, "write to closed file");
+        PyBuffer_Release(&buf);
+        return NULL;
+    }
+
+    ENTER_BUFFERED(self)
+
+    /* Fast path: the data to write can be fully buffered. */
+    if (!VALID_READ_BUFFER(self) && !VALID_WRITE_BUFFER(self)) {
+        self->pos = 0;
+        self->raw_pos = 0;
+    }
+    avail = Py_SAFE_DOWNCAST(self->buffer_size - self->pos, Py_off_t, Py_ssize_t);
+    if (buf.len <= avail) {
+        memcpy(self->buffer + self->pos, buf.buf, buf.len);
+        if (!VALID_WRITE_BUFFER(self)) {
+            self->write_pos = self->pos;
+        }
+        ADJUST_POSITION(self, self->pos + buf.len);
+        if (self->pos > self->write_end)
+            self->write_end = self->pos;
+        written = buf.len;
+        goto end;
+    }
+
+    /* First write the current buffer */
+    res = _BufferedWriter_flush_unlocked(self, 0);
+    if (res == NULL) {
+        Py_ssize_t *w = _Buffered_check_blocking_error();
+        if (w == NULL)
+            goto error;
+        if (self->readable)
+            _BufferedReader_reset_buf(self);
+        /* Make some place by shifting the buffer. */
+        assert(VALID_WRITE_BUFFER(self));
+        memmove(self->buffer, self->buffer + self->write_pos,
+                Py_SAFE_DOWNCAST(self->write_end - self->write_pos,
+                                 Py_off_t, Py_ssize_t));
+        self->write_end -= self->write_pos;
+        self->raw_pos -= self->write_pos;
+        self->pos -= self->write_pos;
+        self->write_pos = 0;
+        avail = Py_SAFE_DOWNCAST(self->buffer_size - self->write_end,
+                                 Py_off_t, Py_ssize_t);
+        if (buf.len <= avail) {
+            /* Everything can be buffered */
+            PyErr_Clear();
+            memcpy(self->buffer + self->write_end, buf.buf, buf.len);
+            self->write_end += buf.len;
+            written = buf.len;
+            goto end;
+        }
+        /* Buffer as much as possible. */
+        memcpy(self->buffer + self->write_end, buf.buf, avail);
+        self->write_end += avail;
+        /* Already re-raised */
+        *w = avail;
+        goto error;
+    }
+    Py_CLEAR(res);
+
+    /* Then write buf itself. At this point the buffer has been emptied. */
+    remaining = buf.len;
+    written = 0;
+    while (remaining > self->buffer_size) {
+        n = _BufferedWriter_raw_write(
+            self, (char *) buf.buf + written, buf.len - written);
+        if (n == -1) {
+            Py_ssize_t *w = _Buffered_check_blocking_error();
+            if (w == NULL)
+                goto error;
+            written += *w;
+            remaining -= *w;
+            if (remaining > self->buffer_size) {
+                /* Can't buffer everything, still buffer as much as possible */
+                memcpy(self->buffer,
+                       (char *) buf.buf + written, self->buffer_size);
+                self->raw_pos = 0;
+                ADJUST_POSITION(self, self->buffer_size);
+                self->write_end = self->buffer_size;
+                *w = written + self->buffer_size;
+                /* Already re-raised */
+                goto error;
+            }
+            PyErr_Clear();
+            break;
+        }
+        written += n;
+        remaining -= n;
+    }
+    if (self->readable)
+        _BufferedReader_reset_buf(self);
+    if (remaining > 0) {
+        memcpy(self->buffer, (char *) buf.buf + written, remaining);
+        written += remaining;
+    }
+    self->write_pos = 0;
+    /* TODO: sanity check (remaining >= 0) */
+    self->write_end = remaining;
+    ADJUST_POSITION(self, remaining);
+    self->raw_pos = 0;
+
+end:
+    res = PyLong_FromSsize_t(written);
+
+error:
+    LEAVE_BUFFERED(self)
+    PyBuffer_Release(&buf);
+    return res;
+}
+
+static PyMethodDef BufferedWriter_methods[] = {
+    /* BufferedIOMixin methods */
+    {"close", (PyCFunction)BufferedIOMixin_close, METH_NOARGS},
+    {"seekable", (PyCFunction)BufferedIOMixin_seekable, METH_NOARGS},
+    {"readable", (PyCFunction)BufferedIOMixin_readable, METH_NOARGS},
+    {"writable", (PyCFunction)BufferedIOMixin_writable, METH_NOARGS},
+    {"fileno", (PyCFunction)BufferedIOMixin_fileno, METH_NOARGS},
+    {"isatty", (PyCFunction)BufferedIOMixin_isatty, METH_NOARGS},
+
+    {"write", (PyCFunction)BufferedWriter_write, METH_VARARGS},
+    {"truncate", (PyCFunction)Buffered_truncate, METH_VARARGS},
+    {"flush", (PyCFunction)Buffered_flush, METH_NOARGS},
+    {"seek", (PyCFunction)Buffered_seek, METH_VARARGS},
+    {"tell", (PyCFunction)Buffered_tell, METH_NOARGS},
+    {NULL, NULL}
+};
+
+static PyMemberDef BufferedWriter_members[] = {
+    {"raw", T_OBJECT, offsetof(BufferedObject, raw), 0},
+    {NULL}
+};
+
+static PyGetSetDef BufferedWriter_getset[] = {
+    {"closed", (getter)BufferedIOMixin_closed_get, NULL, NULL},
+    {"name", (getter)BufferedIOMixin_name_get, NULL, NULL},
+    {"mode", (getter)BufferedIOMixin_mode_get, NULL, NULL},
+    {0}
+};
+
+
+PyTypeObject PyBufferedWriter_Type = {
+    PyVarObject_HEAD_INIT(NULL, 0)
+    "_io.BufferedWriter",       /*tp_name*/
+    sizeof(BufferedObject),     /*tp_basicsize*/
+    0,                          /*tp_itemsize*/
+    (destructor)BufferedObject_dealloc,     /*tp_dealloc*/
+    0,                          /*tp_print*/
+    0,                          /*tp_getattr*/
+    0,                          /*tp_setattr*/
+    0,                          /*tp_compare */
+    0,                          /*tp_repr*/
+    0,                          /*tp_as_number*/
+    0,                          /*tp_as_sequence*/
+    0,                          /*tp_as_mapping*/
+    0,                          /*tp_hash */
+    0,                          /*tp_call*/
+    0,                          /*tp_str*/
+    0,                          /*tp_getattro*/
+    0,                          /*tp_setattro*/
+    0,                          /*tp_as_buffer*/
+    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE
+        | Py_TPFLAGS_HAVE_GC,   /*tp_flags*/
+    BufferedWriter_doc,         /* tp_doc */
+    (traverseproc)Buffered_traverse, /* tp_traverse */
+    (inquiry)Buffered_clear,    /* tp_clear */
+    0,                          /* tp_richcompare */
+    offsetof(BufferedObject, weakreflist), /*tp_weaklistoffset*/
+    0,                          /* tp_iter */
+    0,                          /* tp_iternext */
+    BufferedWriter_methods,     /* tp_methods */
+    BufferedWriter_members,     /* tp_members */
+    BufferedWriter_getset,      /* tp_getset */
+    0,                          /* tp_base */
+    0,                          /* tp_dict */
+    0,                          /* tp_descr_get */
+    0,                          /* tp_descr_set */
+    offsetof(BufferedObject, dict), /* tp_dictoffset */
+    (initproc)BufferedWriter_init, /* tp_init */
+    0,                          /* tp_alloc */
+    PyType_GenericNew,          /* tp_new */
+};
+
+
+
+/*
+ * BufferedRWPair
+ */
+
+PyDoc_STRVAR(BufferedRWPair_doc,
+    "A buffered reader and writer object together.\n"
+    "\n"
+    "A buffered reader object and buffered writer object put together to\n"
+    "form a sequential IO object that can read and write. This is typically\n"
+    "used with a socket or two-way pipe.\n"
+    "\n"
+    "reader and writer are RawIOBase objects that are readable and\n"
+    "writeable respectively. If the buffer_size is omitted it defaults to\n"
+    "DEFAULT_BUFFER_SIZE. The max_buffer_size (for the buffered writer)\n"
+    "defaults to twice the buffer size.\n"
+    );
+
+/* XXX The usefulness of this (compared to having two separate IO objects) is
+ * questionable.
+ */
+
+typedef struct {
+    PyObject_HEAD
+    BufferedObject *reader;
+    BufferedObject *writer;
+    PyObject *dict;
+    PyObject *weakreflist;
+} BufferedRWPairObject;
+
+static int
+BufferedRWPair_init(BufferedRWPairObject *self, PyObject *args,
+                     PyObject *kwds)
+{
+    PyObject *reader, *writer;
+    Py_ssize_t buffer_size = DEFAULT_BUFFER_SIZE;
+    Py_ssize_t max_buffer_size = -1;
+
+    if (!PyArg_ParseTuple(args, "OO|nn:BufferedRWPair", &reader, &writer,
+                          &buffer_size, &max_buffer_size)) {
+        return -1;
+    }
+
+    if (_PyIOBase_checkReadable(reader, Py_True) == NULL)
+        return -1;
+    if (_PyIOBase_checkWritable(writer, Py_True) == NULL)
+        return -1;
+
+    args = Py_BuildValue("(n)", buffer_size);
+    if (args == NULL) {
+        Py_CLEAR(self->reader);
+        return -1;
+    }
+    self->reader = (BufferedObject *)PyType_GenericNew(
+            &PyBufferedReader_Type, args, NULL);
+    Py_DECREF(args);
+    if (self->reader == NULL)
+        return -1;
+
+    args = Py_BuildValue("(nn)", buffer_size, max_buffer_size);
+    if (args == NULL) {
+        Py_CLEAR(self->reader);
+        return -1;
+    }
+    self->writer = (BufferedObject *)PyType_GenericNew(
+            &PyBufferedWriter_Type, args, NULL);
+    Py_DECREF(args);
+    if (self->writer == NULL) {
+        Py_CLEAR(self->reader);
+        return -1;
+    }
+    return 0;
+}
+
+static int
+BufferedRWPair_traverse(BufferedRWPairObject *self, visitproc visit, void *arg)
+{
+    Py_VISIT(self->dict);
+    return 0;
+}
+
+static int
+BufferedRWPair_clear(BufferedRWPairObject *self)
+{
+    Py_CLEAR(self->reader);
+    Py_CLEAR(self->writer);
+    Py_CLEAR(self->dict);
+    return 0;
+}
+
+static void
+BufferedRWPair_dealloc(BufferedRWPairObject *self)
+{
+    _PyObject_GC_UNTRACK(self);
+    Py_CLEAR(self->reader);
+    Py_CLEAR(self->writer);
+    Py_CLEAR(self->dict);
+    Py_TYPE(self)->tp_free((PyObject *) self);
+}
+
+static PyObject *
+_forward_call(BufferedObject *self, const char *name, PyObject *args)
+{
+    PyObject *func = PyObject_GetAttrString((PyObject *)self, name);
+    PyObject *ret;
+
+    if (func == NULL) {
+        PyErr_SetString(PyExc_AttributeError, name);
+        return NULL;
+    }
+
+    ret = PyObject_CallObject(func, args);
+    Py_DECREF(func);
+    return ret;
+}
+
+static PyObject *
+BufferedRWPair_read(BufferedRWPairObject *self, PyObject *args)
+{
+    return _forward_call(self->reader, "read", args);
+}
+
+static PyObject *
+BufferedRWPair_peek(BufferedRWPairObject *self, PyObject *args)
+{
+    return _forward_call(self->reader, "peek", args);
+}
+
+static PyObject *
+BufferedRWPair_read1(BufferedRWPairObject *self, PyObject *args)
+{
+    return _forward_call(self->reader, "read1", args);
+}
+
+static PyObject *
+BufferedRWPair_write(BufferedRWPairObject *self, PyObject *args)
+{
+    return _forward_call(self->writer, "write", args);
+}
+
+static PyObject *
+BufferedRWPair_flush(BufferedRWPairObject *self, PyObject *args)
+{
+    return _forward_call(self->writer, "flush", args);
+}
+
+static PyObject *
+BufferedRWPair_readable(BufferedRWPairObject *self, PyObject *args)
+{
+    return _forward_call(self->reader, "readable", args);
+}
+
+static PyObject *
+BufferedRWPair_writable(BufferedRWPairObject *self, PyObject *args)
+{
+    return _forward_call(self->writer, "writable", args);
+}
+
+static PyObject *
+BufferedRWPair_close(BufferedRWPairObject *self, PyObject *args)
+{
+    PyObject *ret = _forward_call(self->writer, "close", args);
+    if (ret == NULL)
+        return NULL;
+    Py_DECREF(ret);
+
+    return _forward_call(self->reader, "close", args);
+}
+
+static PyObject *
+BufferedRWPair_isatty(BufferedRWPairObject *self, PyObject *args)
+{
+    PyObject *ret = _forward_call(self->writer, "isatty", args);
+
+    if (ret != Py_False) {
+        /* either True or exception */
+        return ret;
+    }
+    Py_DECREF(ret);
+
+    return _forward_call(self->reader, "isatty", args);
+}
+
+
+static PyMethodDef BufferedRWPair_methods[] = {
+    {"read", (PyCFunction)BufferedRWPair_read, METH_VARARGS},
+    {"peek", (PyCFunction)BufferedRWPair_peek, METH_VARARGS},
+    {"read1", (PyCFunction)BufferedRWPair_read1, METH_VARARGS},
+    {"readinto", (PyCFunction)Buffered_readinto, METH_VARARGS},
+
+    {"write", (PyCFunction)BufferedRWPair_write, METH_VARARGS},
+    {"flush", (PyCFunction)BufferedRWPair_flush, METH_NOARGS},
+
+    {"readable", (PyCFunction)BufferedRWPair_readable, METH_NOARGS},
+    {"writable", (PyCFunction)BufferedRWPair_writable, METH_NOARGS},
+
+    {"close", (PyCFunction)BufferedRWPair_close, METH_NOARGS},
+    {"isatty", (PyCFunction)BufferedRWPair_isatty, METH_NOARGS},
+
+    {NULL, NULL}
+};
+
+PyTypeObject PyBufferedRWPair_Type = {
+    PyVarObject_HEAD_INIT(NULL, 0)
+    "_io.BufferedRWPair",       /*tp_name*/
+    sizeof(BufferedRWPairObject), /*tp_basicsize*/
+    0,                          /*tp_itemsize*/
+    (destructor)BufferedRWPair_dealloc,     /*tp_dealloc*/
+    0,                          /*tp_print*/
+    0,                          /*tp_getattr*/
+    0,                          /*tp_setattr*/
+    0,                          /*tp_compare */
+    0,                          /*tp_repr*/
+    0,                          /*tp_as_number*/
+    0,                          /*tp_as_sequence*/
+    0,                          /*tp_as_mapping*/
+    0,                          /*tp_hash */
+    0,                          /*tp_call*/
+    0,                          /*tp_str*/
+    0,                          /*tp_getattro*/
+    0,                          /*tp_setattro*/
+    0,                          /*tp_as_buffer*/
+    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE
+        | Py_TPFLAGS_HAVE_GC,   /* tp_flags */
+    BufferedRWPair_doc,         /* tp_doc */
+    (traverseproc)BufferedRWPair_traverse, /* tp_traverse */
+    (inquiry)BufferedRWPair_clear, /* tp_clear */
+    0,                          /* tp_richcompare */
+    offsetof(BufferedRWPairObject, weakreflist), /*tp_weaklistoffset*/
+    0,                          /* tp_iter */
+    0,                          /* tp_iternext */
+    BufferedRWPair_methods,     /* tp_methods */
+    0,                          /* tp_members */
+    0,                          /* tp_getset */
+    0,                          /* tp_base */
+    0,                          /* tp_dict */
+    0,                          /* tp_descr_get */
+    0,                          /* tp_descr_set */
+    offsetof(BufferedRWPairObject, dict), /* tp_dictoffset */
+    (initproc)BufferedRWPair_init, /* tp_init */
+    0,                          /* tp_alloc */
+    PyType_GenericNew,          /* tp_new */
+};
+
+
+
+/*
+ * BufferedRandom
+ */
+
+PyDoc_STRVAR(BufferedRandom_doc,
+    "A buffered interface to random access streams.\n"
+    "\n"
+    "The constructor creates a reader and writer for a seekable stream,\n"
+    "raw, given in the first argument. If the buffer_size is omitted it\n"
+    "defaults to DEFAULT_BUFFER_SIZE. max_buffer_size isn't used anymore.\n"
+    );
+
+static int
+BufferedRandom_init(BufferedObject *self, PyObject *args, PyObject *kwds)
+{
+    char *kwlist[] = {"raw", "buffer_size", "max_buffer_size", NULL};
+    Py_ssize_t buffer_size = DEFAULT_BUFFER_SIZE;
+    Py_ssize_t max_buffer_size = -1;
+    PyObject *raw;
+
+    self->ok = 0;
+
+    if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|nn:BufferedReader", kwlist,
+                                     &raw, &buffer_size, &max_buffer_size)) {
+        return -1;
+    }
+
+    if (_PyIOBase_checkSeekable(raw, Py_True) == NULL)
+        return -1;
+    if (_PyIOBase_checkReadable(raw, Py_True) == NULL)
+        return -1;
+    if (_PyIOBase_checkWritable(raw, Py_True) == NULL)
+        return -1;
+
+    Py_CLEAR(self->raw);
+    Py_INCREF(raw);
+    self->raw = raw;
+    self->buffer_size = buffer_size;
+    self->readable = 1;
+    self->writable = 1;
+
+    if (_Buffered_init(self) < 0)
+        return -1;
+    _BufferedReader_reset_buf(self);
+    _BufferedWriter_reset_buf(self);
+    self->pos = 0;
+
+    self->ok = 1;
+    return 0;
+}
+
+static PyMethodDef BufferedRandom_methods[] = {
+    /* BufferedIOMixin methods */
+    {"close", (PyCFunction)BufferedIOMixin_close, METH_NOARGS},
+    {"seekable", (PyCFunction)BufferedIOMixin_seekable, METH_NOARGS},
+    {"readable", (PyCFunction)BufferedIOMixin_readable, METH_NOARGS},
+    {"writable", (PyCFunction)BufferedIOMixin_writable, METH_NOARGS},
+    {"fileno", (PyCFunction)BufferedIOMixin_fileno, METH_NOARGS},
+    {"isatty", (PyCFunction)BufferedIOMixin_isatty, METH_NOARGS},
+
+    {"flush", (PyCFunction)Buffered_flush, METH_NOARGS},
+
+    {"seek", (PyCFunction)Buffered_seek, METH_VARARGS},
+    {"tell", (PyCFunction)Buffered_tell, METH_NOARGS},
+    {"truncate", (PyCFunction)Buffered_truncate, METH_VARARGS},
+    {"read", (PyCFunction)Buffered_read, METH_VARARGS},
+    {"read1", (PyCFunction)Buffered_read1, METH_VARARGS},
+    {"readinto", (PyCFunction)Buffered_readinto, METH_VARARGS},
+    {"readline", (PyCFunction)Buffered_readline, METH_VARARGS},
+    {"peek", (PyCFunction)Buffered_peek, METH_VARARGS},
+    {"write", (PyCFunction)BufferedWriter_write, METH_VARARGS},
+    {NULL, NULL}
+};
+
+static PyMemberDef BufferedRandom_members[] = {
+    {"raw", T_OBJECT, offsetof(BufferedObject, raw), 0},
+    {NULL}
+};
+
+static PyGetSetDef BufferedRandom_getset[] = {
+    {"closed", (getter)BufferedIOMixin_closed_get, NULL, NULL},
+    {"name", (getter)BufferedIOMixin_name_get, NULL, NULL},
+    {"mode", (getter)BufferedIOMixin_mode_get, NULL, NULL},
+    {0}
+};
+
+
+PyTypeObject PyBufferedRandom_Type = {
+    PyVarObject_HEAD_INIT(NULL, 0)
+    "_io.BufferedRandom",       /*tp_name*/
+    sizeof(BufferedObject),     /*tp_basicsize*/
+    0,                          /*tp_itemsize*/
+    (destructor)BufferedObject_dealloc,     /*tp_dealloc*/
+    0,                          /*tp_print*/
+    0,                          /*tp_getattr*/
+    0,                          /*tp_setattr*/
+    0,                          /*tp_compare */
+    0,                          /*tp_repr*/
+    0,                          /*tp_as_number*/
+    0,                          /*tp_as_sequence*/
+    0,                          /*tp_as_mapping*/
+    0,                          /*tp_hash */
+    0,                          /*tp_call*/
+    0,                          /*tp_str*/
+    0,                          /*tp_getattro*/
+    0,                          /*tp_setattro*/
+    0,                          /*tp_as_buffer*/
+    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE
+        | Py_TPFLAGS_HAVE_GC,   /*tp_flags*/
+    BufferedRandom_doc,         /* tp_doc */
+    (traverseproc)Buffered_traverse, /* tp_traverse */
+    (inquiry)Buffered_clear,    /* tp_clear */
+    0,                          /* tp_richcompare */
+    offsetof(BufferedObject, weakreflist), /*tp_weaklistoffset*/
+    0,                          /* tp_iter */
+    (iternextfunc)Buffered_iternext, /* tp_iternext */
+    BufferedRandom_methods,     /* tp_methods */
+    BufferedRandom_members,     /* tp_members */
+    BufferedRandom_getset,      /* tp_getset */
+    0,                          /* tp_base */
+    0,                          /*tp_dict*/
+    0,                          /* tp_descr_get */
+    0,                          /* tp_descr_set */
+    offsetof(BufferedObject, dict), /*tp_dictoffset*/
+    (initproc)BufferedRandom_init, /* tp_init */
+    0,                          /* tp_alloc */
+    PyType_GenericNew,          /* tp_new */
+};
+