Index: urllib2.py =================================================================== --- urllib2.py (revision 46491) +++ urllib2.py (working copy) @@ -297,6 +297,10 @@ def add_handler(self, handler): added = False for meth in dir(handler): + if meth in ["redirect_request", "do_open", "proxy_open"]: + # oops, coincidental match + continue + i = meth.find("_") protocol = meth[:i] condition = meth[i+1:] Index: test/test_urllib2.py =================================================================== --- test/test_urllib2.py (revision 46491) +++ test/test_urllib2.py (working copy) @@ -270,6 +270,27 @@ class OpenerDirectorTests(unittest.TestCase): + def test_badly_named_methods(self): + # test work-around for three methods that accidentally follow the + # naming conventions for handler methods + # (*_open() / *_request() / *_response()) + + # These used to call the accidentally-named methods, causing a + # TypeError in real code; here, returning self from these mock + # methods would either cause no exception, or AttributeError. + + from urllib2 import URLError + + o = OpenerDirector() + meth_spec = [ + [("do_open", "return self"), ("proxy_open", "return self")], + [("redirect_request", "return self")], + ] + handlers = add_ordered_mock_handlers(o, meth_spec) + o.add_handler(urllib2.UnknownHandler()) + for scheme in "do", "proxy", "redirect": + self.assertRaises(URLError, o.open, scheme+"://example.com/") + def test_handled(self): # handler returning non-None means no more handlers will be called o = OpenerDirector()