Upgrading to Python3: minor things which caught me out

I’ve already made one module Python3-compatible. I decided to bite the bullet and make my not-really-released winsys module completely Python3. I’d tried the 2to3.py approach but it left just enough false positives & negatives (sorry, don’t remember the details) that I decided to go by hand, including translating the unit tests from nose format to the updated unittest format.

Although I was reasonably clued-in on the changes from 2 to 3, I was caught out by a few things, which I list here in case they help anyone else:

  • __nonzero__ is now __bool__: I knew this perfectly well, but it took me a while to find out why my tests were failing. 2to3 would have picked this up straightaway.
  • __hash__ isn’t inherited if __eq__ is defined: this one surprised me and took a bit of research. (All right; I actually read the docs for __hash__).
  • bytes vs str: well, obviously you knew this, but it’s bitten me in some slightly unexpected places when interfacing via pywin32. The error message speaks of expecting a buffer object, which threw me slightly.
  • assertTrue doesn’t bool its param: UPDATE - of course this isn’t true at all; I was confusing this and the __bool__/__nonzero__ issue above. Thanks to Marius Gedminas for pointing this out.
  • pywin32 generates tz-aware datetime objects: for a long time, pywin32 rolled its own datetime type (as Python had none). Last year, Mark H canvassed the python-win32 list about committing to the Python datetime type. And now it’s there. But it’s tz-aware. And the core Python type isn’t. Fortunately, pywin32 has for a while shipped with the win32timezone module which solves the issue.

I’m sure there’ll be more which I’ll add here as they come along.

2 Comments so far »

  1. OpenIDhttp://gedmin.as/ said,

    Wrote on July 5, 2010 @ 6:35 pm

    Could you clarify the assertTrue thing? In what circumstances can the difference be perceived?

    Especially given that Nose’s assert_true _is_ precisely the same as unittest.TestCase.assertTrue, only under a different name; see here: http://code.google.com/p/python-nose/source/browse/nose/tools.py#172

  2. tim said,

    Wrote on July 5, 2010 @ 7:37 pm

    @gedmin.as Umm… now you mention it, I remember that that *wasn’t* the issue (although I originally thought it was): it was the other point I made about __bool__ / __nonzero__. Thanks for going the lengths of identifying the code path. I’ll add an update to the entry.

Comment RSS · TrackBack URI

Leave a Comment

OpenID

Sign in with your OpenID ?

Anonymous

Name: (Required)

E-mail: (Required)

Website:

Comment: