Commit strict str/bytes distinction.
From now on, trying to write str to a binary stream
is an error (I'm still working on the reverse).
There are still (at least) two failing tests:
- test_asynchat
- test_urllib2_localnet
but I'm sure these will be fixed by someone.
diff --git a/Lib/io.py b/Lib/io.py
index 3ebf5ae..7aa79ce 100644
--- a/Lib/io.py
+++ b/Lib/io.py
@@ -659,12 +659,14 @@
     def write(self, b):
         if self.closed:
             raise ValueError("write to closed file")
+        if isinstance(b, str):
+            raise TypeError("can't write str to binary stream")
         n = len(b)
         newpos = self._pos + n
         if newpos > len(self._buffer):
             # Inserts null bytes between the current end of the file
             # and the new write position.
-            padding = '\x00' * (newpos - len(self._buffer) - n)
+            padding = b'\x00' * (newpos - len(self._buffer) - n)
             self._buffer[self._pos:newpos - n] = padding
         self._buffer[self._pos:newpos] = b
         self._pos = newpos
@@ -801,11 +803,8 @@
     def write(self, b):
         if self.closed:
             raise ValueError("write to closed file")
-        if not isinstance(b, bytes):
-            if hasattr(b, "__index__"):
-                raise TypeError("Can't write object of type %s" %
-                                type(b).__name__)
-            b = bytes(b)
+        if isinstance(b, str):
+            raise TypeError("can't write str to binary stream")
         # XXX we can implement some more tricks to try and avoid partial writes
         if len(self._write_buf) > self.buffer_size:
             # We're full, so let's pre-flush the buffer
@@ -1099,8 +1098,6 @@
             s = s.replace("\n", self._writenl)
         # XXX What if we were just reading?
         b = s.encode(self._encoding)
-        if isinstance(b, str):
-            b = bytes(b)
         self.buffer.write(b)
         if haslf and self.isatty():
             self.flush()