summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Lib/test/test_urllib2.py6
-rw-r--r--Lib/urllib2.py4
2 files changed, 10 insertions, 0 deletions
diff --git a/Lib/test/test_urllib2.py b/Lib/test/test_urllib2.py
index c9dcf5ef7b..90e1771733 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 415c991ad3..b2cec727fa 100644
--- a/Lib/urllib2.py
+++ b/Lib/urllib2.py
@@ -295,6 +295,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"]: