NameError propagation / Undefined

1 view
Skip to first unread message

James Harris

unread,
Feb 19, 2007, 10:21:34 PM2/19/07
to Genshi
Hi,

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.

Christian Boos

unread,
Feb 20, 2007, 7:59:49 AM2/20/07
to gen...@googlegroups.com

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

Simon Cross

unread,
Feb 20, 2007, 8:03:25 AM2/20/07
to gen...@googlegroups.com
On 2/20/07, Christian Boos <cb...@neuf.fr> wrote:
> 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.

I quote python rule #2:
"Explicit is better than implicit."

Schiavo
Simon Cross

Shannon -jj Behrens

unread,
Feb 20, 2007, 7:17:20 PM2/20/07
to gen...@googlegroups.com

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

--
http://jjinux.blogspot.com/

James Harris

unread,
Feb 20, 2007, 7:47:31 PM2/20/07
to Genshi

> 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
>
> --http://jjinux.blogspot.com/

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

Shannon -jj Behrens

unread,
Feb 20, 2007, 8:28:06 PM2/20/07
to gen...@googlegroups.com
On 2/20/07, James Harris <sto...@gmail.com> wrote:
>
>
> > 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
> >
> > --http://jjinux.blogspot.com/
>
> I'm not sure if you are saying you prefer the use of Undefined or if
> you'd prefer the exception to propagate :P

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".

James Harris

unread,
Feb 20, 2007, 8:53:51 PM2/20/07
to Genshi

> 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

Christopher Lenz

unread,
Feb 21, 2007, 10:26:32 AM2/21/07
to gen...@googlegroups.com

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/

Shannon -jj Behrens

unread,
Feb 21, 2007, 3:38:11 PM2/21/07
to gen...@googlegroups.com

I have PyCon this week, but I've scheduled some time to try it out
next week. Thanks a lot!

James Harris

unread,
Feb 22, 2007, 4:41:12 AM2/22/07
to Genshi
On Feb 22, 1:26 am, Christopher Lenz <cml...@gmx.de> wrote:
> 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.

I tried the patch out earlier today, seems to work a treat :)

Thanks!
James.

Reply all
Reply to author
Forward
0 new messages