Hacker News new | ask | show | jobs
by kstrauser 804 days ago
A few years ago, I got tired of my company talking about plans to upgrade our main app from Python 2 to Python 3, until one weekend I just did it. The tests caught a million little changes that I worked through until everything passed. Come Monday, we were on Python 3 and I took a couple workdays off to play video games.

I wouldn’t have dared even start if I didn’t have confidence in our test suite.

1 comments

I was not so lucky! I had a good test suite, but there were a lot of places where I mixed up strings and bytes. I had to add new parameters to specify in Unicode encoding/decoding options, new tests to handle those failures, and new APIs so I could have one function return bytes and another return strings.

Plus, I had C extensions, which had to be updated to handle changes in the Python/C API, including places where Python 2.7 could handle both Unicode and bytes in the ASCII subset:

  >>> u"bbcf".decode("hex")
  '\xbb\xcf'
  >>> "bbcf".decode("hex")
  '\xbb\xcf'
  >>> b"bbcf".decode("hex")
  '\xbb\xcf'
but under Python 3 required more work:

  >>> bytes.fromhex("BBCF")
  b'\xbb\xcf'
  >>> bytes.fromhex(b"BBCF")
  Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
  TypeError: fromhex() argument must be str, not bytes
AND, I needed to support both Python 2.7 and Python 3.5+ on the same code base.

AND, I needed to support various third-party tools that had their own different migration paths for how to handle the transition.