I've just begun using Genshi and have found the suppression of
NameError exceptions to be bit of a hindrance. Is there any known /
standard / elegant way to prevent the use of Undefined within a
template (therefore allowing the NameError exceptions to propagate as
usual)?
I'm not sure exactly how this feature is useful, if anybody can give
me an example that would be great... perhaps I'm missing something :P
TIA,
J.
This is also discussed in this ticket: http://genshi.edgewall.org/ticket/88
Personally, I like the current behavior, as an Undefined value behaves
like a boolean "false" value.
Therefore, you can easily test in your template whether you can use the
value or not. In the corresponding Python code, you can simply omit that
value from the dictionary, instead of having to set it explicitly to None.
-- Christian
I quote python rule #2:
"Explicit is better than implicit."
Schiavo
Simon Cross
I'm going to have to agree with Christian on this one. That behavior
drives me nuts. As much as I love Genshi, it's the thing I like
least. I have a bug submitted against myself reminding me to figure
out how to turn it off. ;)
Best Regards,
-jj
I'm not sure if you are saying you prefer the use of Undefined or if
you'd prefer the exception to propagate :P At any rate, I have what I
hope might be a solution that can still preserve the current behavior.
Just looking at the code responsible for constructing the Undefined
instances now, I wonder if instead of referencing the Undefined class
explicitly, it could be referenced as BUILTINS['Undefined'], then if
the BUILTINS dict were to be exposed to the library user, they could
provide a custom Undefined class, or even remove it from the
dictionary completely. The ability to modify the BUILTINS dict might
be beneficial even on it's own.
- James
Sorry, I'm saying I want the exception to propagate. If I misspelled
a variable name, I want a NameError! ;)
> At any rate, I have what I
> hope might be a solution that can still preserve the current behavior.
>
> Just looking at the code responsible for constructing the Undefined
> instances now, I wonder if instead of referencing the Undefined class
> explicitly, it could be referenced as BUILTINS['Undefined'], then if
> the BUILTINS dict were to be exposed to the library user, they could
> provide a custom Undefined class, or even remove it from the
> dictionary completely. The ability to modify the BUILTINS dict might
> be beneficial even on it's own.
There should probably just be a configuration variable to tell Genshi
whether or not to be "forgiving".
That would be great ;)
For now, I just do the following before parsing any templates, seems
to do the trick, but it sure is hideous.
import genshi.template.eval as ge
del ge.BUILTINS['Undefined']
def _raise(name): raise NameError(name)
ge.Undefined = _raise
- James
In retrospect, I think I agree that supressing errors on undefined
variables was a bad decision. Similar behavior in other template
engines such as Django's (which is a *lot* more forgiving, even) has
frustrated me very much lately.
I've added a patch to the ticket to change the behavior:
<http://genshi.edgewall.org/ticket/88#comment:6>
Please try it and let me know how it works.
Thanks,
Chris
--
Christopher Lenz
cmlenz at gmx.de
http://www.cmlenz.net/
I have PyCon this week, but I've scheduled some time to try it out
next week. Thanks a lot!
I tried the patch out earlier today, seems to work a treat :)
Thanks!
James.