[stringtemplate-interest] Problem in multivalued attribute handling?

Colin Bean ccbean at gmail.com
Tue Jul 11 13:38:11 PDT 2006


Sounds good!  However, after experimenting further, I realize there's
a couple of problems with my changes after I tried some more complex
templates.  A value of zero passed as an element in a list (to a
template that iterates over it) is not displayed -- not surprising
since I didn't examine that part of the code.  Also, passing a zero in
as an attribute property throws an error :(  I'll look at it further
tonight and get a complete set of changes together.
I wanted to mention that the unit tests don't seem to pass in a zero
at any point; this might be worth including in the future (although
Python is one of the few languages where this would matter).
Lastly, I want to say thanks to Marq for the new Python versions (and
Terrence for ST -- goes without saying ;)  It's a great tool and very
much appreciated.

-Colin

On 7/11/06, Terence Parr <parrt at cs.usfca.edu> wrote:

>
> 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
>
> _______________________________________________
> 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