<br><br><div><span class="gmail_quote">On 9/29/07, <b class="gmail_sendername">Jeffrey Yasskin</b> &lt;<a href="mailto:jyasskin@gmail.com">jyasskin@gmail.com</a>&gt; wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
On 9/29/07, Phillip J. Eby &lt;<a href="mailto:pje@telecommunity.com">pje@telecommunity.com</a>&gt; wrote:<br>&gt; At 07:33 AM 9/29/2007 -0700, Guido van Rossum wrote:<br>&gt; &gt;Until just before 3.0a1, they were unequal. We decided to raise
<br>&gt; &gt;TypeError because we noticed many bugs in code that was doing things<br>&gt; &gt;like<br>&gt; &gt;<br>&gt; &gt;&nbsp;&nbsp; data = f.read(4096)<br>&gt; &gt;&nbsp;&nbsp; if data == &quot;&quot;: break<br>&gt;<br>&gt; Thought experiment: what if read() always returned strings, and to
<br>&gt; read bytes, you had to use something like &#39;f.readinto(ob, 4096)&#39;,<br>&gt; where &#39;ob&#39; is a mutable bytes instance or memory view?<br></blockquote><div><br>Using what encoding?&nbsp; read() should raise an exception on a file opened as binary in that case.&nbsp; And instead of readinto() how about readbytes() that just returns bytes and raises an exception on non-binary mode files.&nbsp; (readinto for buffers is a good idea and i think we should have it but that idea could be taken further to allow for even more scattered IO into a mutable buffer; thats another discussion and should be a PEP of its own)
<br></div><br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">&gt; But as the above example makes clear, in 3.x you simply *can&#39;t* write
<br>&gt; code that works correctly with an arbitrary file that might be binary<br>&gt; or text, at least not without typechecking the return value from<br>&gt; read().&nbsp;&nbsp;(In which case, you might as well inspect the file<br>
&gt; object.)&nbsp;&nbsp;So, the above problem could be fixed by having .read()<br>&gt; raise an error (or simply not exist) on a binary file object.<br><br>Perhaps write<br>&nbsp;&nbsp;if len(data) == 0: break<br>since that&#39;s what you really mean.
</blockquote><div><br>data = f.read()<br>if not data: break<br></div><br>Is the preferred way to write that.&nbsp; Regardless, I agree.&nbsp; read() returning a different type based on the file open mode is going to cause problems.&nbsp; I do -NOT- like the idea of bytes vs string comparison raising an exception.&nbsp; read() and readbytes() methods that raise exceptions when used on the wrong mode of file would &quot;solve&quot; the problem in a more obvious way.
<br><br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">Any other code that compares the result of read() to either a bytes or<br>a str really is taking a text or binary file object specifically and
<br>not working on an arbitrary file.<br><br>&gt; In this way, the problem is fixed at the point where it really<br>&gt; occurs: i.e., at the point of not having decided whether the stream<br>&gt; is bytes or text.<br>&gt;
<br>&gt; This also seems to fit better (IMO) with the best practice of<br>&gt; enforcing str/unicode/encoding distinctions at the point where data<br>&gt; enters the program, rather than delaying the error to later.<br>&gt;
<br>&gt;<br>&gt; &gt;I thought about&nbsp;&nbsp;using warning too, but since nobody wants warnings,<br>&gt; &gt;that would be pretty much the same as raising TypeError except for the<br>&gt; &gt;most dedicated individuals (and if I were really dedicated I&#39;d just
<br>&gt; &gt;write my own eq() function anyway).<br>&gt;<br>&gt; The use case I&#39;m concerned about is code that&#39;s not type-specific<br>&gt; getting a TypeError by comparing arbitrary objects.&nbsp;&nbsp;For example, if<br>&gt; you write Python code to create a Python code object (
e.g. the<br>&gt; compiler package or my own BytecodeAssembler), you need to create a<br>&gt; list of constants as you generate the code, and you need to be able<br>&gt; to search the list for an equal constant.&nbsp;&nbsp;Since strings and bytes
<br>&gt; can both be constants, a simple list.index() test could now raise a<br>&gt; TypeError, as could &quot;item in list&quot;.<br>&gt;<br>&gt; So raising an error to make bad code fail sooner, will also take down<br>&gt; unsuspecting code that isn&#39;t really broken, and *force* the writing
<br>&gt; of special comparison code -- which won&#39;t be usable with things like<br>&gt; list.remove and the &quot;in&quot; operator.<br>&gt;<br>&gt; In comparison, forcing code to be bytes vs. text aware at the point<br>
&gt; of I/O directs attention to the place where you can best decide what<br>&gt; to do about it.&nbsp;&nbsp;(After all, the comparison that raises the TypeError<br>&gt; might occur deep in a library that&#39;s expecting to work with text.)
<br>&gt;<br>&gt;<br>&gt; &gt;And the warning would do nothing<br>&gt; &gt;about the issue brought up by Jim Jewett, the unpredictable behavior<br>&gt; &gt;of a dict with both bytes and strings as keys.<br>&gt;<br>&gt; I&#39;ve looked at all of Jim&#39;s messages for September, but I don&#39;t see
<br>&gt; this.&nbsp;&nbsp;I do see where raising TypeError for comparisons causes a<br>&gt; problem with dictionaries, but I don&#39;t see how an unequal comparison<br>&gt; creates &quot;unpredictable&quot; behavior (as opposed to predictable failure to match).
<br>&gt;<br>&gt; _______________________________________________<br>&gt; Python-3000 mailing list<br>&gt; <a href="mailto:Python-3000@python.org">Python-3000@python.org</a><br>&gt; <a href="http://mail.python.org/mailman/listinfo/python-3000">
http://mail.python.org/mailman/listinfo/python-3000</a><br>&gt; Unsubscribe: <a href="http://mail.python.org/mailman/options/python-3000/jyasskin%40gmail.com">http://mail.python.org/mailman/options/python-3000/jyasskin%40gmail.com
</a><br>&gt;<br><br><br>--<br>Namasté,<br>Jeffrey Yasskin<br><a href="http://jeffrey.yasskin.info/">http://jeffrey.yasskin.info/</a><br><br>&quot;Religion is an improper response to the Divine.&quot; — &quot;Skinny Legs and
<br>All&quot;, by Tom Robbins<br>_______________________________________________<br>Python-3000 mailing list<br><a href="mailto:Python-3000@python.org">Python-3000@python.org</a><br><a href="http://mail.python.org/mailman/listinfo/python-3000">
http://mail.python.org/mailman/listinfo/python-3000</a><br>Unsubscribe: <a href="http://mail.python.org/mailman/options/python-3000/greg%40krypto.org">http://mail.python.org/mailman/options/python-3000/greg%40krypto.org</a>
<br></blockquote></div><br>