Index: urllib2.py =================================================================== --- urllib2.py (revision 56286) +++ urllib2.py (working copy) @@ -295,6 +295,9 @@ self.process_request = {} def add_handler(self, handler): + if not hasattr(handler, "add_parent"): + raise TypeError("expected BaseHandler instance, got %r" % handler) + added = False for meth in dir(handler): if meth in ["redirect_request", "do_open", "proxy_open"]: Index: test/test_urllib2.py =================================================================== --- test/test_urllib2.py (revision 56286) +++ test/test_urllib2.py (working copy) @@ -381,6 +381,12 @@ class OpenerDirectorTests(unittest.TestCase): + def test_add_non_handler(self): + class NonHandler(object): + pass + self.assertRaises(TypeError, + OpenerDirector().add_handler, NonHandler()) + def test_badly_named_methods(self): # test work-around for three methods that accidentally follow the # naming conventions for handler methods