Ticket #2062 (closed defect: invalid)
[PATCH] subtle gettext issue when tg is partially loaded
| Reported by: | cdevienne | Owned by: | faide |
|---|---|---|---|
| Priority: | normal | Milestone: | 1.0.x bugfix |
| Component: | TurboGears | Version: | 1.0.7 |
| Severity: | normal | Keywords: | |
| Cc: |
Description
Since I had fun tracking this one down, I thought I would share this joy :
The issue arose when I was documenting my application with Sphinx using autodoc.
- At one moment, my controller modules get loaded, but at this moment, for some reasons, i18n.install() has _not_ been called yet.
- In one of the module I have a ToscaWidgets? form, with input widgets in it, which is instantiated as a global variable.
- When the form is instantiated, the children widgets gets copied.
- When the input widgets get copied, its attributes get copied
- One of the attributes is a property called "is_required", which tries to validate an empty string with the widget validator.
- Because one of my fields is indeed required, the validator wants to raise the Invalid exception, and start by formatting its message.
- The message formatting consist in the first place to translate the message template using the builtin gettext, ie __builtin__._.
Reminding that i18n.install was not yet called ?
File "/home/cdevienne/prog/tw.forms/tw/forms/core.py", line 56, in __new__
obj = super(InputWidget, cls).__new__(cls, id, parent, children, **kw)
File "/home/cdevienne/ws/starform/sf_env/lib/python2.5/site-packages/ToscaWidgets-0.9.3-py2.5.egg/tw/core/base.py", line 288, in __new__
attr = getattr(obj, name, None)
File "/home/cdevienne/prog/tw.forms/tw/forms/fields.py", line 104, in is_required
self.validate('', use_request_local=False)
File "/home/cdevienne/prog/tw.forms/tw/forms/core.py", line 130, in validate
value = self.validator.to_python(value, state)
File "/home/cdevienne/ws/starform/sf_env/lib/python2.5/site-packages/FormEncode-1.2-py2.5.egg/formencode/api.py", line 395, in to_python
raise Invalid(self.message('empty', state), value, state)
File "/home/cdevienne/ws/starform/sf_env/lib/python2.5/site-packages/FormEncode-1.2-py2.5.egg/formencode/api.py", line 229, in message
return trans(self._messages[msgName], **self.gettextargs) % kw
TypeError: ugettext() got an unexpected keyword argument 'domain'
And indeed, Validator.gettextargs contains a "domain" key, which is set to "FormEncode". Who did that ? turbogears.validators.
Explanation
In the end the problem is that Validator.gettextargs setting made by turbogears.validators assumes that __builtin__._ is indeed turbogears.i18n.gettext. But it may not be true in some conditions.
Workaround
Put Validator.gettextargs = {} just after loading turbogears.validators
Solution
Put Validator.gettextargs['domain'] = 'FormEncode' in i18n.install(). Patch attached.
Attachments
Change History
Changed 4 years ago by cdevienne
-
attachment
fix_i18n_and_validators_tg1.0.patch
added
comment:1 Changed 4 years ago by cdevienne
Argl !!! It does not solve my issue...
It is even worse actually :(
The '_' seems to have been overridden by Sphinx... In the end my particular issue may not be fixable in TG.
But I guess other people may have it.
I will post my final solution when it is tested.
Christophe
comment:2 Changed 4 years ago by cdevienne
Here it is :
Put these lines into conf.py :
import turbogears.validators turbogears.validators.Validator.gettextargs = {}
Patch for TG 1.0