Does class patching actually work

James Stroud jstroud at mbi.ucla.edu
Mon Jan 15 22:59:26 EST 2007


John Nagle wrote:
> In M2Crypto/m2urllib there is this:
> 
>     import string, sys, urllib
>     from urllib import *
> 
>     def open_https(self, url, data=None, ssl_context=None):
>      ...
> 
>     # Minor brain surgery.
>     URLopener.open_https = open_https
> 
> The intent of this is to replace method open_https of class URLopener
> with a local, patched version.
> 
> Does that actually work?  When I've tried to do that in other code, it
> seems to have no effect.  In fact, I can write
> 
>     URLopener.open_https = None
> 
> and nothing changes.
> 
>                 John Nagle

I'm not able to reproduce your results with python 2.5.

py> from urllib import *
py> URLopener.open_https = None
py> URLopener.open_https
py> type(URLopener.open_https)
<type 'NoneType'>
py> def doit(self, url, data=None, ssl_context=None):
...   print 'self is %s, url is %s' % (self, url)
...
...
py> URLopener.open_https = doit
py> u = URLopener()
py> u = u.open_https('http://wherever.com')
self is <urllib.URLopener instance at 0x4066070c>, url is 
http://wherever.com



More information about the Python-list mailing list