Wednesday, November 12, 2008

Remember when I said "things that make me unhappy"? Well, that applies to coding, sometimes...

So, I have some potential projects that will use server-side Python for various things. Naturally, I want to be able to use cgi with them. Easy enough, right? It's Python, surely the cgi module will be well-documented? About the only useful thing I could derive is that 'you don't have to worry about whether it's a get request or a post request, the same class handles it all!'. Sounds a little bit sketchy, and it gets me horribly confused, trying to figure out whether some method is for extracting name-value pairs or uploading files...

Point is, on the advice of a friend, I open up the source code. And see why the documentation is so patchy and confused: the cgi module is a baroque, elaborate, obsolete MESS. The FieldStorage class that was supposed to take care of every request for me? It's a dict-style class, that much I knew. However, it's dict-style due to emulation, rather than subclassing. Why, Max, you must be asking, gentle readers, all statistically .00001 of you, was the FieldStorage class not subclassed from dict? It's deceptively simple, dear readers, and I will no more prolong the artificial suspense: it wasn't subclassed from dict because it wasn't subclassed from ANYTHING. That's right. An old-style class in a 2.5.2 module. (My target platform hasn't yet added 2.6, and it seems I haven't either.) And what an old-style class. The initialization parameters were passed around like state variables with disassociative identity disorder. The determination of which request type is being handled occurs on the fly, and conceptually remaps the meaning of variables in blatant violation of their documented purpose. I know this isn't obvious unless you look at the code like I did, but this... thing... looks nigh-unmaintainable. Which might explain the old-style classes. Perhaps all that's truly changed is the opening comments...

In any case, I cannot let this stand. As of now, Massively Moaning Online and Fermat's Margins are on hold until I gin up a sane replacement for this dinosaur.

No comments: