On 7/14/07, Christian Heimes <[EMAIL PROTECTED]> wrote:
> I'm having some troubles with unit tests in the py3k-struni branch. Some
> test like test_uu are failing because an io.TextIOWrapper instance's
> write() method doesn't handle bytes. The method is defined as:
>
>     def write(self, s: str):
>         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):
>             b = bytes(b)
>         n = self.buffer.write(b)
>         if "\n" in s:
>             # XXX only if isatty
>             self.flush()
>         self._snapshot = self._decoder = None
>         return len(s)
>
> The problematic lines are the lines from s.encode() to b = bytes(b). The
> behavior is more than questionable. A bytes object doesn't have an
> encode() method and str's encode method() always returns bytes. IMO the
> write() method should be changed to:
>
>     def write(self, s: (str, bytes)):
>         if self.closed:
>             raise ValueError("write to closed file")
>         # XXX What if we were just reading?
>         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 b"\n" in b:
>             # XXX only if isatty
>             self.flush()
>         self._snapshot = self._decoder = None
>         return len(s)
>
> Or the write() should explictly raise a TypeError when it is not allowed
> to handle bytes.

I came across this in your SF patch. I disagree with your desire to
let TextIOWrapper.write() handle bytes: it should *only* be passed str
objects. The uu test was failing because it was writing bytes to a
text stream.

Perhaps the error should be better; though I'm not sure I want to add
explicit type checks (as it would defeat duck typing).

-- 
--Guido van Rossum (home page: http://www.python.org/~guido/)
_______________________________________________
Python-3000 mailing list
[email protected]
http://mail.python.org/mailman/listinfo/python-3000
Unsubscribe: 
http://mail.python.org/mailman/options/python-3000/archive%40mail-archive.com

Reply via email to