Accidentally shooting yourself in the foot in PythonRecently, I stumbled over a small issue in Python's cgi module that is a good illustration of how unintended consequences in Python can wind up shooting you in the foot. The cgi module's main purpose is to create a dictionary-like
object that contains all of the parameters passed to your CGI program
in the form = cgi.FieldStorage() for k in form.keys(): ... stuff ... Then one day a cracker tried an XML-RPC based exploit against DWiki and
this code blew up, getting a No problem; I could just guard the This surprising behavior of the cgi module happens through three steps. First, Python decides whether objects are True or False like this:
As a dictionary-like thing, FieldStorage defines a def __len__(self): return len(self.keys()) Finally, FieldStorage decided to let instances represent several
different things and that calling Apart from a practical illustration of unintended consequences and complex interactions, what I've taken away from this is to remember than __len__ on objects is used for more than just the len() function. (Other special methods also have multiple uses.) Sidebar: so how did I solve this?My solution was lame: try: form.keys() except TypeError: return I suspect that the correct solution is to check |
These are my WanderingThoughts GettingAround This is part of CSpace, and is written by ChrisSiebenmann. * * * Atom feeds are available; see the bottom of most pages. Categories: links, linux, programming, python, snark, solaris, spam, sysadmin, tech, unix, web |