[stringtemplate-interest] PyST Zero-valued attributes patch (was Problem in multivalued attribute handling?)

Colin Bean ccbean at gmail.com
Mon Jul 17 14:40:39 PDT 2006


Hi all,

I've finally got a more complete patch to make PyST correctly handle
an attribute with a numeric value of zero.
In my previous post I mentioned that in my patched version, setting an
attribute property to zero causes an error.  Upon further inspection,
it seems like this also causes an error in the standard 2.2 release (I
tested this on a clean build).  In order to duplicate it, do something
like:

import stringtemplate
test = stringtemplate.StringTemplate('$foo.a$')
test['foo'] = {'a':0}
print test

PyST should fail on the print statement.  This is not a problem in
2.1, it seems newly introduced in 2.2.

The patch below fixes this bug, and causes zero values to render
correctly in single value attributes, multivalued attributes, lists,
and as object properties.  I still haven't tested zero values
inherited in nested templates, so ymmv.  I'll post if I make a more
complete patch, but this should fix basic zero attribute problems;
code changes are below.

Regards,
-Colin

--- StringTemplateOrig.py       2006-07-10 15:53:44.000000000 -0700
+++ StringTemplate.py   2006-07-10 23:14:10.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-11 07:13:38.000000000 -0700
@@ -177,7 +177,7 @@
                     i += 1
             else:
                 for ithValue in attributeValue:
-                    if not ithValue:
+                    if ithValue is None:
                         # weird...a None value in the list; ignore
                         continue

@@ -270,7 +270,7 @@
                 value = o[propertyName]
             except:
                 value = None
-            if not value:
+            if value is None:
                 # no property defined; if a map in this group
                 # then there may be a default value
                 value = o[ASTExpr.DEFAULT_MAP_VALUE_NAME]
@@ -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:


More information about the stringtemplate-interest mailing list