The test for darwin is needed because other platforms don't support "UTF-8" as a valid LC_CTYPE name, on a recent linux box:
>>> locale.setlocale(locale.LC_CTYPE, "UTF-8")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/opt/python2.7/lib/python2.7/locale.py", line 539, in setlocale
return _setlocale(category, locale)
locale.Error: unsupported locale setting
(And just calling setlocale to check if the value is valid is not an option because that changes process-global state)