I would propose using "hasattr" to check for the existence of awake,
respond, and sleep, and removing the assert statement.  The RuntimeError
that results could have a much better error message than the assert would
give us, anyhow.

But I'm curious on Chuck's take on removing the assert statement.  I somehow
have a feeling that he has an opinion on this, maybe because it was
discussed before?

- Geoff

> -----Original Message-----
> From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]]
> Sent: Monday, April 08, 2002 3:31 PM
> To: [EMAIL PROTECTED]
> Subject: Re: [Webware-discuss] Allowing delegation in servlets
> 
> 
> On Mon, Apr 08, 2002 at 02:43:37PM -0400, Geoffrey Talvola wrote:
> > > My first attempt failed because line 183 of ServletFactory is:
> > > 
> > >     assert issubclass(theClass, Servlet)
> > > 
> > > Anyway, I'd like to argue for the removal of the assertion in
> > > ServletFactory, since it is denying use of a very powerful, 
> > > and arguably
> > > better technique than inheritance. In any case, assertions 
> > > can be optimized
> > > away in .pyo files, so that the presence of the assertion 
> > > statement is of
> > > questionable value...
> > 
> > I think the assertion is there to help out the Webware user 
> who forgets to
> > have their servlet inherit from Servlet.  I assume that 
> mistake happens
> > relatively frequently.  I know I've done it once or twice.  
> Without the
> > assertion (which pinpoints the problem pretty clearly), 
> presumably you would
> > wind up with a traceback somewhere else in the guts of 
> Appliction.py that
> > would be less obvious to track down, especially for a casual user.
> > 
> > Now you've identified a legitimate use for a servlet not 
> derived from
> > Servlet.  So you want to remove the assertion, but that 
> would hurt casual
> > Webware users hwo might forget the base class.  So maybe 
> there should be an
> > option "AllowServletsNotDerivedFromServlet" that defaults 
> to 0, but you can
> > just flip the switch and it skips the assertion?  I would 
> imagine this to be
> > documented as "for experts".
> 
> If a servlet must always subclass Servlet, or at least we pretend to
> require that for the default case, the exception should be 
> changed from
> AssertionError to something else.  AssertionError mean "I" made an
> error.  Other exceptions mean "you" made an error.
> 
> But perhaps we need to rethink what are the requirements for servlets.
> At minimum, awake/respond/sleep methods.  This sounds like an 
> interface.
> Since Python doesn't have interfaces (yet), we've been using  
> "must be a
> subclass of Servlet" as a substitute.
> 
> The simplest way to avoid the issubclass test and yet not introduce an
> esoteric option like AllowServletsNotDerivedFromServlet would be to
> test explicitly for the required features:
> 
>       if not hasattr(theClass, 'respond'): raise RuntimeError(...)
> 
>       try:  
>               theClass.respond
>       except AttributeError:
>               raise RuntimeError(...)
> 
>       if not hasattr(theClass, 'respond') or \
>               type(theClass.respond) != types.MethodType:
>               raise RuntimeError(...)
> 
>       requiredMethods = 'awake', 'respond', 'sleep'
>       for methMame in requiredMethods:
>               try:
>                       meth = getattr(theClass, methName)
>                       if type(meth) != types.MethodType:
>                               raise RuntimeError("not a method...")
>               except AttributeError:
>                       raise RuntimeError("method doesn't exist...")
>       
> 
> -- 
> -Mike (Iron) Orr, [EMAIL PROTECTED]  (if mail problems: [EMAIL PROTECTED])
>    http://iron.cx/     English * Esperanto * Russkiy * 
> Deutsch * Espan~ol
> 
> _______________________________________________
> Webware-discuss mailing list
> [EMAIL PROTECTED]
> https://lists.sourceforge.net/lists/listinfo/webware-discuss
> 

_______________________________________________
Webware-discuss mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/webware-discuss

Reply via email to