On Thu, Dec 09, 2004 at 06:34:08AM -0500, David Binger wrote:
> >Looking at the source (1.x) I don't see a way to subclass HTTPResponse
> >or
> >HTTPRequest.
>
> Because we are python programmers, we have some options besides
> subclassing.
> These options feel a little like cheating, since they are not
> available in most other languages.
> If there is a method on a class that you want to replace, you can just
> use assignment
> to replace it. This could get the job done for HTTPRequest,
> HTTPResponse, or,
> for that matter, for CGIHTTPServer.
In my demo I hacked the Publisher to subclass HTTPRequest, then reverted
it because I found I can set instance variables in Session.start_request()
and I didn't need methods. Then I wanted to add methods after all, so I
made a mixin class and grafted it in:
HTTPRequest.__bases__ = (HTTPRequestMixin,) + HTTPRequest.__bases__
However, although you *can* set methods and base classes this way or by
function assignment, I'm not sure we should encourage it. I don't see
what the holdup is regarding making HTTPRequest subclassable. It's only
a tiny change in the Publisher that lifts an artificial restriction to
Quixote's flexibility.
class Publisher: # Quixote 1.
HTTPRequestClass = HTTPRequest
HTTPUploadRequestClass = HTTPUploadRequest
def create_request(self, stdin, env):
ctype = get_content_type(env)
if ctype == "multipart/form-data":
req = self.HTTPUploadRequestClass(stdin, env, content_type=ctype)
req.set_upload_dir(self.config.upload_dir,
self.config.upload_dir_mode)
return req
else:
return self.HTTPRequestClass(stdin, env, content_type=ctype)
--
-Mike Orr (aka. Sluggo), mso@oz.net (iron@sense-sea-MegaSub-1-465.oz.net)
http://sluggo.kicks-ass.org/ Cxu vi parolas Esperante?