Index: Lib/urllib2.py =================================================================== --- Lib/urllib2.py (revision 56224) +++ Lib/urllib2.py (working copy) @@ -339,6 +339,8 @@ bisect.insort(self.handlers, handler) handler.add_parent(self) + return added + def close(self): # Only exists for backwards compatibility. pass @@ -459,7 +461,8 @@ for h in handlers: if isclass(h): h = h() - opener.add_handler(h) + if not opener.add_handler(h): + raise ValueError, "%s is not a handler" % type(h) return opener class BaseHandler: Index: Lib/test/test_urllib2.py =================================================================== --- Lib/test/test_urllib2.py (revision 56224) +++ Lib/test/test_urllib2.py (working copy) @@ -1019,6 +1019,8 @@ def foo_open(self): pass class BarHandler(urllib2.BaseHandler): def bar_open(self): pass + class NotActuallyAHandler: + pass build_opener = urllib2.build_opener @@ -1044,6 +1046,8 @@ o = build_opener(urllib2.HTTPHandler()) self.opener_has_handler(o, urllib2.HTTPHandler) + self.assertRaises(ValueError, build_opener, NotActuallyAHandler) + def opener_has_handler(self, opener, handler_class): for h in opener.handlers: if h.__class__ == handler_class: