[stringtemplate-interest] Problem in multivalued attribute
handling?
Colin Bean
ccbean at gmail.com
Tue Jul 11 09:24:07 PDT 2006
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.
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:
--- 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
More information about the stringtemplate-interest
mailing list