By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,713 Members | 764 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,713 IT Pros & Developers. It's quick & easy.

Does class patching actually work

P: n/a
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
Jan 16 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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.

pyfrom urllib import *
pyURLopener.open_https = None
pyURLopener.open_https
pytype(URLopener.open_https)
<type 'NoneType'>
pydef doit(self, url, data=None, ssl_context=None):
.... print 'self is %s, url is %s' % (self, url)
....
....
pyURLopener.open_https = doit
pyu = URLopener()
pyu = u.open_https('http://wherever.com')
self is <urllib.URLopener instance at 0x4066070c>, url is
http://wherever.com
Jan 16 '07 #2

P: n/a
James Stroud wrote:
John Nagle wrote:
>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.
My mistake. Confusion between

import a.b
c = foo

and

import a
b.c = foo

which result in different namespaces.

John Nagle
Jan 17 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.