Hi there,

I just tracked down a thorny problem I was having with Storage.getlist(), 
and think it may be useful to folks.

The particular situation was a strange edge-case, where I'm wanting a 
request.vars value as a list, so am calling:
my_ids = request.vars.getlist('my_id')

The problem was that in this case request.vars.my_id = "" (empty string)
And, unfortunately request.vars.getlist('my_id') was returning "" (and not 
[""] as was hoped)

Looking at the source-code for getlist:
def getlist(self, key): 
    value = 
self.get<http://www.web2py.com/examples/static/epydoc/web2py.gluon.storage-pysrc.html#>(key,
 
[]) 
    return value if not value else \
         value if isinstance(value, (list, tuple)) else [value]

It seems that the "return value if not value..." is confusing things, since 
"" (empty string) will evaluate as False.

I've redefined Storage.getlist() in my code as:
def getlist(self, key):
    value = self.get(key, [])
    if value is None or isinstance(value, (list, tuple)):
        return value
    else:
        return [value]

and it seems to work for me now.

Thoughts?

-- 



Reply via email to