#99 LanguageValidator fails with pycountry 0.11 (with patch)

closed-fixed
nobody
None
5
2010-05-16
2010-04-22
Anonymous
No

Using formencode with pylons (with pycountry 0.11) LanguageValidator always raises an exception because not every language has an alpha2/iso_639_1_code attribute defined in ISO639.xml (actually iso_639_1_code is an implied attribute and not a required one).

This exception happens for existing languages as well as fictional ones
Examples as follow:
>>> from formencode.national import LanguageValidator
>>> l = LanguageValidator()
>>> l.to_python('German')
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/home/mariano/Sandbox/insite/lib/python2.6/site-packages/FormEncode-1.2.2-py2.6.egg/formencode/api.py", line 413, in to_python
value = tp(value, state)
File "/home/mariano/Sandbox/insite/lib/python2.6/site-packages/FormEncode-1.2.2-py2.6.egg/formencode/national.py", line 729, in _to_python
for k, v in get_languages():
File "/home/mariano/Sandbox/insite/lib/python2.6/site-packages/FormEncode-1.2.2-py2.6.egg/formencode/national.py", line 88, in get_languages
return [(e.alpha2, _l(e.name)) for e in pycountry.languages]
AttributeError: 'Language' object has no attribute 'alpha2'
>>> l.to_python('Klingonian')
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/home/mariano/Sandbox/insite/lib/python2.6/site-packages/FormEncode-1.2.2-py2.6.egg/formencode/api.py", line 413, in to_python
value = tp(value, state)
File "/home/mariano/Sandbox/insite/lib/python2.6/site-packages/FormEncode-1.2.2-py2.6.egg/formencode/national.py", line 729, in _to_python
for k, v in get_languages():
File "/home/mariano/Sandbox/insite/lib/python2.6/site-packages/FormEncode-1.2.2-py2.6.egg/formencode/national.py", line 88, in get_languages
return [(e.alpha2, _l(e.name)) for e in pycountry.languages]
AttributeError: 'Language' object has no attribute 'alpha2'

My fix in get_languages verifies in the list comprehension that each instance of languages has an alpha2 attribute and only returns the ones that comply with
this constraint. So:

>>> from formencode.national import LanguageValidator
>>> l = LanguageValidator()
>>> l.to_python('German')
'de'
>>> l.to_python('Klingonian')
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/home/mariano/Sandbox/insite/lib/python2.6/site-packages/FormEncode-1.2.2-py2.6.egg/formencode/api.py", line 413, in to_python
value = tp(value, state)
File "/home/mariano/Sandbox/insite/lib/python2.6/site-packages/FormEncode-1.2.2-py2.6.egg/formencode/national.py", line 736, in _to_python
raise Invalid(self.message('valueNotFound', state), value, state)
Invalid: That language is not listed in ISO 639

Of course, the validation when using the ISO code continues to work:
>>> l.from_python('de')
'German'
>>> l.from_python('zh')
'Chinese'
>>>

Discussion

  • Noticed the same problem yesterday in my clean-up and fixed it in changeset 838e979f61f2 in a similar way.

     
    • status: open --> closed-fixed