Skip to content

Commit 1c544ac

Browse files
authored
gh-124098: Fix incorrect inclusion of handler methods without protocol prefix in OpenerDirector (GH-136873)
1 parent 71a7cb8 commit 1c544ac

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

Lib/test/test_urllib2.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,23 @@ class NonHandler(object):
577577
self.assertRaises(TypeError,
578578
OpenerDirector().add_handler, NonHandler())
579579

580+
def test_no_protocol_methods(self):
581+
# test the case that methods starts with handler type without the protocol
582+
# like open*() or _open*().
583+
# These methods should be ignored
584+
585+
o = OpenerDirector()
586+
meth_spec = [
587+
["open"],
588+
["_open"],
589+
["error"]
590+
]
591+
592+
add_ordered_mock_handlers(o, meth_spec)
593+
594+
self.assertEqual(len(o.handle_open), 0)
595+
self.assertEqual(len(o.handle_error), 0)
596+
580597
def test_badly_named_methods(self):
581598
# test work-around for three methods that accidentally follow the
582599
# naming conventions for handler methods

Lib/urllib/request.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,8 @@ def add_handler(self, handler):
415415
continue
416416

417417
i = meth.find("_")
418+
if i < 1:
419+
continue
418420
protocol = meth[:i]
419421
condition = meth[i+1:]
420422

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Fix issue where methods in handlers that lacked the protocol name but
2+
matched a valid base handler method (e.g., ``_open()`` or ``error()``)
3+
were incorrectly added to :class:`urllib.request.OpenerDirector`'s
4+
handlers. Contributed by Andrea Mattei.

0 commit comments

Comments
 (0)