[Checkins] SVN: grok/trunk/src/grok/ "if obj" tests are evil if obj's type is completely unknown. Ssomehow there's the

Philipp von Weitershausen philikon at philikon.de
Wed Jan 17 17:31:33 EST 2007


Log message for revision 72077:
  "if obj" tests are evil if obj's type is completely unknown. Ssomehow there's the
  belief out there that PEP8 discourages 'if something is not None', while in fact
  it's the opposite:
  
        Also, beware of writing "if x" when you really mean "if x is not None"
        -- e.g. when testing whether a variable or argument that defaults to
        None was set to some other value.  The other value might have a type
        (such as a container) that could be false in a boolean context!
  
  In this case traversal failed for URLs like /foo/bar where 'foo' was a container
  and 'bar' was an object that evaluated to False, e.g. an empty container.
  

Changed:
  U   grok/trunk/src/grok/components.py
  U   grok/trunk/src/grok/ftests/traversal/containertraverse.py

-=-
Modified: grok/trunk/src/grok/components.py
===================================================================
--- grok/trunk/src/grok/components.py	2007-01-17 21:47:01 UTC (rev 72076)
+++ grok/trunk/src/grok/components.py	2007-01-17 22:31:33 UTC (rev 72077)
@@ -287,7 +287,7 @@
 
     def publishTraverse(self, request, name):
         subob = self.traverse(name)
-        if subob:
+        if subob is not None:
             return subob
 
         # XXX special logic here to deal with views and containers.

Modified: grok/trunk/src/grok/ftests/traversal/containertraverse.py
===================================================================
--- grok/trunk/src/grok/ftests/traversal/containertraverse.py	2007-01-17 21:47:01 UTC (rev 72076)
+++ grok/trunk/src/grok/ftests/traversal/containertraverse.py	2007-01-17 22:31:33 UTC (rev 72077)
@@ -51,6 +51,12 @@
   </body>
   </html>
 
+Also try (an empty) container as a subitem a container:
+
+  >>> herd['subherd'] = Herd()
+  >>> browser.open("http://localhost/herd/subherd")
+  >>> print browser.contents
+  A herd
 """
 import grok
 
@@ -61,6 +67,13 @@
             return Special()
         return None
     
+class HerdIndex(grok.View):
+    grok.context(Herd)
+    grok.name('index')
+
+    def render(self):
+        return 'A herd'
+
 class Mammoth(grok.Model):
 
     def __init__(self, name):



More information about the Checkins mailing list