<div dir="ltr">Just to update this thread, Serhiy beat me to a patch and committed it. Through the change he found a few latent bugs in the stdlib itself so this idea has already proven/paid for itself.<br></div><br><div class="gmail_quote">On Mon, Mar 16, 2015 at 1:25 PM Brett Cannon <<a href="mailto:bcannon@gmail.com">bcannon@gmail.com</a>> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div class="gmail_quote">On Mon, Mar 16, 2015 at 11:22 AM Victor Stinner <<a href="mailto:victor.stinner@gmail.com" target="_blank">victor.stinner@gmail.com</a>> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi,<br>
<br>
2015-03-16 16:11 GMT+01:00 Brett Cannon <<a href="mailto:bcannon@gmail.com" target="_blank">bcannon@gmail.com</a>>:<br>
> One of the rather subtle issues with writing Python 2/3 code is that<br>
> indexing on bytes in Python 2 returns a length-1 bytes object while in<br>
> Python 3 it returns an int. Because ==/!= always returns True/False it can<br>
> be a very subtle failure and tough to track down.<br>
<br>
I worked on such patch in the past, but I lost it :-) I can help to<br>
rewrite it if needed.<br></blockquote><div><br></div></div></div><div dir="ltr"><div class="gmail_quote"><div>I filed <a href="http://bugs.python.org/issue23681" target="_blank">http://bugs.python.org/issue23681</a> if you want to help (although I don't expect the patch to be complicated; I might ask you for a code review though =).</div></div></div><div dir="ltr"><div class="gmail_quote"><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
So yes, it *is* very useful to port a large Python 2 project to Python 3.<br>
<br>
For example, you may not be able to run your application with Python 3<br>
because a third party library cannot be imported on Python 3, so it<br>
blocks the whole work on porting an application to Python 3. Until the<br>
module is ported, you may want to prepare the port. Checking<br>
bytes==str just by reading the source code is difficult.<br>
<br>
Other issues which can only be "seen" at runtime when running an<br>
application on Python 3 :<br>
<br>
- "x > 0" with x=None => TypeError is raised in Python 3<br></blockquote><div><br></div></div></div><div dir="ltr"><div class="gmail_quote"><div>Testing can easily catch that <b>and</b> have a traceback to trace down where the problem originated from, so I'm not worried.</div></div></div><div dir="ltr"><div class="gmail_quote"><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
- x / 8 where x is an int => becomes a float in Python 3, it's hard to<br>
detect this issue in Python 2 just by reading the source code :-/<br></blockquote><div><br></div></div></div><div dir="ltr"><div class="gmail_quote"><div>-Q in Python 2 and/or adding the division __future__ statement solves this one.</div><div><br></div><div>Basically I'm just trying to plug the last few holes that can't be automated by tools like Modernize or Futurize <b>and</b> won't surface where the problem is easily during testing under Python 2 or 3 (e.g., a traceback during testing makes it easy to find unless you swallow the exception, in which case a warning won't help you either if you use -Werror).</div></div></div><div dir="ltr"><div class="gmail_quote"><div><br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
> What do people think of extending -b/-bb in Python 3 to warn when performing<br>
> equality between an int and a bytes object of any length? I don't want to<br>
> restrict to length-1 bytes objects because people may be doing comparisons<br>
> where the result can be length-1 or any other length and thus would still<br>
> have a subtle bug to pick up. Do people think this would raise a ton of<br>
> false-positives? Would people find it useful?<br>
<br>
First ensure that the stdlib doesn't raise any BytesWarning exception.<br></blockquote><div><br></div></div></div><div dir="ltr"><div class="gmail_quote"><div>Yep, I always test with -Werror whenever I muck with something that can raise a warning (side-effect from helping to write _warnings.c).</div></div></div><div dir="ltr"><div class="gmail_quote"><div><br></div><div>-Brett</div></div></div><div dir="ltr"><div class="gmail_quote"><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
For example, os.get_exec_path() has to modify warnings filters<br>
temporary in Python 3 :-/<br>
<br>
# {b'PATH': ...}.get('PATH') and {'PATH': ...}.get(b'PATH') emit a<br>
# BytesWarning when using python -b or python -bb: ignore the warning<br>
<br>
Victor<br>
</blockquote></div></div></blockquote></div>