Does class patching actually work

J

John Nagle

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
 
J

James Stroud

John said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top