Unable to patch urllib2 after install_opener has been used.
See original GitHub issueWe’re using a custom opener with urllib2 (adding some additional sec features), looks something like so:
urllib2.install_opener(urllib2.build_opener(CustomHTTPSOpener))
The opener overwrites the https_open
method so that a custom connection object can be used. Something like the following:
class CustomHTTPSOpener(urllib2.HTTPSHandler):
def __init__(self, **kwargs):
urllib2.HTTPSHandler.__init__(self)
self._connect_args = kwargs
def https_open(self, req):
def connection_wrapper(host, **kwargs):
all_kwargs = dict(self._connect_args)
all_kwargs.update(kwargs)
return CustomHTTPSConnection(host, **all_kwargs)
return self.do_open(connection_wrapper, req)
Then when I use vcrpy, I get the following:
TypeError: unbound method __init__() must be called with VCRHTTPConnection myapp/tests/cassette.yaml instance as first argument (got CustomHTTPSConnection instance instead)
Any ideas? Thanks
Issue Analytics
- State:
- Created 9 years ago
- Comments:7 (1 by maintainers)
Top Results From Across the Web
python - Import error: No module name urllib2 - Stack Overflow
The urllib2 module has been split across several modules in Python 3 named urllib.request and urllib.error . The 2to3 tool will automatically adapt...
Read more >How to Fix “Import error: No module named urllib2” in Python?
Solution 1: Use import urllib.<module>. The error occurred in this case because the urllib2 module has been split across several modules in Python...
Read more >Fix the module not found import error for urllib2 and urllib3
In today's post we would like to show how to easily fix error messages when importing the urllib2 library. The solution is pretty...
Read more >Failed to mock the python urlopen function · Issue #313 - GitHub
I tested both python 2 and 3. mock in python 2.7.10. from mock import patch from urllib2 import urlopen import urllib2 @patch('urllib2.urlopen') ...
Read more >urllib.request — Extensible library for opening URLs — Python ...
Source code: Lib/urllib/request.py The urllib.request module defines functions and classes which help in opening URLs (mostly HTTP) in a complex world ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@tiffanycheng @breerly https://github.com/kevin1024/vcrpy/pull/133 should enable the behavior that you want.
All that you really need to do is use a custom vcr for patching that looks like:
Can you test this and let me know if I can close this issue?
cool. I’m going to close this issue for now. Let me know if there are any more problems.