[twill] wsgi_intercept, https and twill

Kumar McMillan kumar.mcmillan at gmail.com
Wed Jan 9 12:13:02 PST 2008


On Jan 9, 2008 3:43 AM, Titus Brown <titus at caltech.edu> wrote:
> On Mon, Jan 07, 2008 at 08:17:45PM +0000, John J Lee wrote:
> -> On Tue, 8 Jan 2008, Robert Leftwich wrote:
> -> [...]
> -> > class PatchedMechanizeBrowser(MechanizeBrowser):
> -> >    """
> -> >    A patched version of the mechanize browser class.  Currently
> -> >    installs the WSGI intercept handler & fixes a problem with
> -> >    mechanize/urllib2 Basic Authentication.
> -> >    """
> -> >    def __init__(self, *args, **kwargs):
> -> >        # install WSGI intercept handler.
> -> >        self.handler_classes['http'] = build_http_handler()
> -> >        self.handler_classes['https'] = build_http_handler()
> -> [...]
> ->
> -> You're mutating a class attribute here -- not a good idea.
>
> Understood.
>
> It's a private copy of mechanize.Browser at this point, though, so it's
> unlikely to matter.  I suppose I could copy this:
>
> class Browser(UserAgentBase):
>    handler_classes = copy.copy(UserAgentBase.handler_classes)
>
> Is that the right way to go about it?

that sounds like a better route to me.  I went ahead and changed this
in standalone  wsgi_intercept too (thanks for pointing that out,
John).  Titus, the dict instance has its own copy() method for
handiness.  The standalone code seems to have strayed pretty far from
twill :/ so my changes more or less look like :

class Browser(MechanizeBrowser):
    """
    A version of the mechanize browser class that
    installs the WSGI intercept handler
    """
    handler_classes = MechanizeBrowser.handler_classes.copy()
    handler_classes['http'] = WSGI_HTTPHandler
    handler_classes['https'] = WSGI_HTTPSHandler
    def __init__(self, *args, **kwargs):
        # install WSGI intercept handler.
        install(self)
        MechanizeBrowser.__init__(self, *args, **kwargs)


...and all tests are passing with that change (in trunk).


K



More information about the twill mailing list