diff options
author | Guido van Rossum <guido@python.org> | 2007-08-29 04:05:57 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 2007-08-29 04:05:57 (GMT) |
commit | a74184eb1d1ed8c1c139ea692b6037a7563d5540 (patch) | |
tree | ffa099c584ab7857a1ac61d6cd8b8f872c49ffb0 /Lib | |
parent | 245b42ec4b07682dd44bb92dbde328c7ce78d90b (diff) | |
download | cpython-a74184eb1d1ed8c1c139ea692b6037a7563d5540.zip cpython-a74184eb1d1ed8c1c139ea692b6037a7563d5540.tar.gz cpython-a74184eb1d1ed8c1c139ea692b6037a7563d5540.tar.bz2 |
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.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/io.py | 13 |
1 files changed, 5 insertions, 8 deletions
@@ -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() |