git » pyweave » commit 4aacb1f

Fix fromform() when no parameters are passed

author Alberto Bertogli
2010-04-07 23:04:58 UTC
committer Alberto Bertogli
2010-04-07 23:08:28 UTC
parent 42b49d4bb624be459bad4f1fba9801e781c606cf

Fix fromform() when no parameters are passed

cgi.FieldStorage() raises an exception when trying to access a parameter and
no parameters were passed:

	Traceback (most recent call last):
	  File "pyweave.cgi", line 531, in <module>
	    handle_cgi()
	  File "pyweave.cgi", line 519, in handle_cgi
	    do_delete(path, storage)
	  File "pyweave.cgi", line 467, in do_delete
	    ids = fromform(form, "ids")
	  File "pyweave.cgi", line 353, in fromform
	    v = form.getfirst(name, None)
	  File "/usr/lib/python2.6/cgi.py", line 560, in getfirst
	    if key in self:
	  File "/usr/lib/python2.6/cgi.py", line 595, in __contains__
	    raise TypeError, "not indexable"
	TypeError: not indexable

That happens because FieldStorage() doesn't know where to get the data. It may
be a bug in the implementation, or maybe desired behaviour.

Either way, we must avoid trying to access it when that occurs. Fortunately,
that situation can be easily detected by checking if the instance is true.

This patch fixes this bug by using that trick to check if the form is valid,
and only requesting it if so.

Signed-off-by: Alberto Bertogli <albertito@blitiri.com.ar>

pyweave.cgi +6 -1

diff --git a/pyweave.cgi b/pyweave.cgi
index 7f7d895..d73d2e4 100755
--- a/pyweave.cgi
+++ b/pyweave.cgi
@@ -350,7 +350,12 @@ def output(obj, timestamp = None):
 		print json.dumps(obj)
 
 def fromform(form, name, convert = None):
-	v = form.getfirst(name, None)
+	# if no form is sent, form.getfirst() raises an exception, so handle
+	# this by avoiding the call
+	if form:
+		v = form.getfirst(name, None)
+	else:
+		v = None
 	if convert and v is not None:
 		return convert(v)
 	return v