Leo's Technical Blog

The Devil is in the Details

Introduction

user

Leo Soto


python, jython

The Devil is in the Details

Posted by Leo Soto on .
Featured

python, jython

The Devil is in the Details

Posted by Leo Soto on .

It's a cliché, but really, look at this commit message I just wrote. It's for a supposedly simple change I made to Jython to get '%d' % foo and '%f' % bar working, on some corner cases[1]:

StringFormatter: '%d' and '%f' support for the __int__ and __float__ protocol respectively.

The implementation is more convulted than it should be, because we have PyString implementing float and int at the "java level" but not at the "python level". For string formatting, only "python level" float and int must be
supported.

Also, now that int can return a PyLong, this case needs special care. Basically formatInteger now can call formatLong if a PyLong is found as the result of int. Then, as formatLong can also be called from formatInteger, hex, oct and str conversions were moved inside formatLong.

Finally, test_format_jy was changed to stop checking that we don't support big floats on '%d' (CPython doesn't, but that seems a limitation of the specific implementation and I can't imagine a program that could break on Jython because we support it).”

Python is wonderful, but there are a lot of details which make it tricky when implementing it. Nice to see that, when we play the role of Python users, we aren't exposed too much to this subtleties. In fact, I'd say that it is one of the languages with the better "user interface" I've seen.

[1] Naturally, I find this corner cases when running and testing the Django codebase. Well, that's one of the points of my SoC project: Test how CPython-compliant Jython is, and fix it when it isn't :).