question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to patch urllib2 after install_opener has been used.

See original GitHub issue

We’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:closed
  • Created 9 years ago
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
IvanMalisoncommented, Jan 8, 2015

@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:

from vcr.stubs import VCRHTTPSConnection
my_vcr = config.VCR(custom_patches=((wherever_it_lives, 'CustomHTTPSConnection', VCRHTTPSConnection),))

Can you test this and let me know if I can close this issue?

0reactions
IvanMalisoncommented, Jan 22, 2015

cool. I’m going to close this issue for now. Let me know if there are any more problems.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found