Index: Lib/io.py =================================================================== --- Lib/io.py (revision 56394) +++ Lib/io.py (working copy) @@ -1056,15 +1056,19 @@ def isatty(self): return self.buffer.isatty() - def write(self, s: str): + + def write(self, s: (str, bytes)): if self.closed: raise ValueError("write to closed file") # XXX What if we were just reading? - b = s.encode(self._encoding) - if isinstance(b, str): + if isinstance(s, basestring): + b = s.encode(self._encoding) + elif isinstance(s, bytes): + b = s + else: b = bytes(b) n = self.buffer.write(b) - if "\n" in s: + if b"\n" in b: # XXX only if isatty self.flush() self._snapshot = self._decoder = None Index: Lib/test/test_io.py =================================================================== --- Lib/test/test_io.py (revision 56394) +++ Lib/test/test_io.py (working copy) @@ -660,6 +660,20 @@ print("Reading using readline(): %6.3f seconds" % (t3-t2)) print("Using readline()+tell(): %6.3f seconds" % (t4-t3)) + def testWriteBytes(self): + encs = ["utf-8", "latin-1"] + data_u = '\xc3\xbcber' + for enc in encs: + data_b = data_u.encode(enc) + self.failUnless(isinstance(data_b, bytes)) + f = io.open(test_support.TESTFN, "w", encoding=enc) + self.failUnless(isinstance(f, io.TextIOWrapper)) + f.write(data_b) + f.close() + f = io.open(test_support.TESTFN, "r", encoding=enc) + self.failUnless(isinstance(f, io.TextIOWrapper)) + self.assertEqual(f.read(), data_u) + f.close() # XXX Tests for open()