[stringtemplate-interest] Problem in multivalued attribute
handling?
Terence Parr
parrt at cs.usfca.edu
Tue Jul 11 12:58:24 PDT 2006
On Jul 11, 2006, at 9:24 AM, Colin Bean wrote:
> Okay, I've got the beginnings of a fix. The problem is indeed in
> checking for the existence of an attribute. As Praki mentioned
> earlier, using a conditional like "if x:" looks like it should simply
> check for the existence of x, but in fact evaluates to false not only
> if x is None, but also if x is an empty container or a numeric zero.
Empty container is correct, but 0 is incorrect ;)
> It's a weird little python feature that has bitten me a couple of
> times; the solution is to explicitly check if a value is None.
> I'm sure that there are many other places in the code that could use a
> review for this, the following changes made PyST correctly evaluate a
> (single or multi-valued) attribute with the integer value of zero. I
> changed one line in StringTemplate.py, where it checks for the
> attribute's existence before setting it, and one in
> language/ASTExpr.py, where it again checks existence before writing an
> attribute out. I ran the code through the unit tests and it was fine,
> but other than that and my "zero" test I haven't run this code
> anywhere else -- ymmv. Here's a diff of the changes:
Sweet! Thanks, Colin :) I'm CC'ing Marq in case he misses this.
Ter
> --- StringTemplateOrig.py 2006-07-10 15:53:44.000000000 -0700
> +++ StringTemplate.py 2006-07-10 15:59:49.000000000 -0700
> @@ -677,7 +677,7 @@
> # a normal call to setAttribute with unknown attribute
> raise KeyError("no such attribute: " + name +
> " in template context " +
> self.getEnclosingInstanceStackString())
> - if value:
> + if value is not None:
> attributes[name] = value
> elif isinstance(value, list) or \
> isinstance(value, dict) or \
> --- language/ASTExprOrig.py 2006-07-10 15:57:35.000000000 -0700
> +++ language/ASTExpr.py 2006-07-10 15:11:35.000000000 -0700
> @@ -390,7 +390,7 @@
> return self._write(this, o, out, separator)
>
> def _write(self, this, o, out, separator):
> - if not o:
> + if o is None:
> return 0
> n = 0
> try:
>
>
> If I find anything else relevant to this I'll post it.
> Regards,
> -Colin
> _______________________________________________
> stringtemplate-interest mailing list
> stringtemplate-interest at antlr.org
> http://www.antlr.org:8080/mailman/listinfo/stringtemplate-interest
More information about the stringtemplate-interest
mailing list