Writting consistent tests
Category: Sysadmin.
I recently encountered a somewhat funny problem when porting Labyrinth to FreeBSD, python complaining about some locale.bindtextdomain
that does not exist.
Having a look at the source code, I could read:
if hasattr(gettext, 'bind_textdomain_codeset'): gettext.bind_textdomain_codeset('labyrinth','UTF-8') gettext.textdomain('labyrinth') if not os.name == 'nt': locale.bindtextdomain('labyrinth', localedir) if hasattr(locale, 'bind_textdomain_codeset'): locale.bind_textdomain_codeset('labyrinth','UTF-8') locale.textdomain('labyrinth') gtk.glade.bindtextdomain('labyrinth') gtk.glade.textdomain('labyrinth')
Okay, as far as I know my operating system is still FreeBSD and thus not nt, so the developer of the program was not aware that there was at least two operating systems that where not compatible with this.
But is this really operating-system-dependant? I don't think so... So why test the operating system and not use reflection to determine if the desired method is available or not?
My patch:
--- src/labyrinth.py.orig +++ src/labyrinth.py @@ -56,11 +56,12 @@ if hasattr(gettext, 'bind_textdomain_codeset'): gettext.bind_textdomain_codeset('labyrinth','UTF-8') gettext.textdomain('labyrinth') -if not os.name == 'nt': - locale.bindtextdomain('labyrinth', localedir) - if hasattr(locale, 'bind_textdomain_codeset'): - locale.bind_textdomain_codeset('labyrinth','UTF-8') - locale.textdomain('labyrinth') +if hasattr(locale, 'bindtextdomain'): + if not os.name == 'nt': + locale.bindtextdomain('labyrinth', localedir) + if hasattr(locale, 'bind_textdomain_codeset'): + locale.bind_textdomain_codeset('labyrinth','UTF-8') + locale.textdomain('labyrinth') gtk.glade.bindtextdomain('labyrinth') gtk.glade.textdomain('labyrinth')
It's not the first time I see (and fix) such an error, and I think is actually works on a GNU/Linux machine. As I am not a GNU/Linux user, I can't figure out what this is supposed to do, but what I see is that locale's documentation does not have any reference to some bindtextdomain()
or textdomain()
functions.
Anyway, Labyrinth is a cool piece of code, you should have a look at it :-) !