From: Georg Brandl Date: Thu, 12 Jul 2007 08:05:48 +0000 (+0000) Subject: Patch #1752270, #1750931: complain if urllib2 add_handler called X-Git-Tag: v2.5.2c1~247 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=40df67f7043b6953175a9c748ed9f8bc76349857;p=thirdparty%2FPython%2Fcpython.git Patch #1752270, #1750931: complain if urllib2 add_handler called without handler. (backport from rev. 56293) --- diff --git a/Lib/test/test_urllib2.py b/Lib/test/test_urllib2.py index 5ca760ef4bf6..9934d3778bf4 100644 --- a/Lib/test/test_urllib2.py +++ b/Lib/test/test_urllib2.py @@ -381,6 +381,12 @@ class MockPasswordManager: 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 diff --git a/Lib/urllib2.py b/Lib/urllib2.py index 09d7f9c423e8..3578e7a0b97b 100644 --- a/Lib/urllib2.py +++ b/Lib/urllib2.py @@ -298,6 +298,10 @@ class OpenerDirector: self.process_request = {} def add_handler(self, handler): + if not hasattr(handler, "add_parent"): + raise TypeError("expected BaseHandler instance, got %r" % + type(handler)) + added = False for meth in dir(handler): if meth in ["redirect_request", "do_open", "proxy_open"]: