Lessons learned in porting to PyPy

I’m running the ncolony tests with pypy so I can add PyPy support. I expected it to be a no-op — but turned out I have ingrained expectations that are no longer true: moreover, PyPy is right, and I’m wrong.

  • “hello” is “hello” –> not necessarily true. PyPy does not intern strings like CPython does.
  • file(name, ‘w’).write(“string”) is bad and you should not do that. PyPy is garbage collected, not ref counted, so the file might not be closed for a while.

That was it! It was actually a pleasant experience 🙂

Advertisements

2 Responses to Lessons learned in porting to PyPy

  1. glyph says:

    A couple of additional notes:

    – file() is going away, you should use open().
    – There are two solutions to the “.write” conundrum. One, if you’re using Twisted, is twisted.python.filepath.FilePath.setContent, which has the added benefit of being an atomic update. The other is to use a context manager, i.e.:

    with open(name, ‘w’) as f:
    f.write(“string”)

    which is *almost* as convenient as the lazy-and-wrong pattern, so easy to update to.

  2. moshez says:

    @glyph — do you remember who first implemented setContent? 😛

    But yes, the “with” thing was what I went with…
    The only two cases was in the test suite, I use setContent for production code.
    https://github.com/moshez/ncolony/commit/cf39369f90c9c985b51f1a45c5f76b466c423aa5 is the relevant fix.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: