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.
This commit is contained in:
Guido van Rossum 2007-08-29 04:05:57 +00:00
parent 245b42ec4b
commit a74184eb1d
3 changed files with 59 additions and 119 deletions

View file

@ -659,12 +659,14 @@ class BytesIO(BufferedIOBase):
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 @@ class BufferedWriter(_BufferedIOMixin):
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 @@ class TextIOWrapper(TextIOBase):
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()