Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
gh-124098: Fix incorrect inclusion of handler methods without protoco…
…l prefix in OpenerDirector (GH-136873)
  • Loading branch information
MonadChains authored and cocolato committed Dec 18, 2025
commit 8dafad7956d0acf451f53452c803bc66dc3b39a0
17 changes: 17 additions & 0 deletions Lib/test/test_urllib2.py
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,23 @@ class NonHandler(object):
self.assertRaises(TypeError,
OpenerDirector().add_handler, NonHandler())

def test_no_protocol_methods(self):
# test the case that methods starts with handler type without the protocol
# like open*() or _open*().
# These methods should be ignored

o = OpenerDirector()
meth_spec = [
["open"],
["_open"],
["error"]
]

add_ordered_mock_handlers(o, meth_spec)

self.assertEqual(len(o.handle_open), 0)
self.assertEqual(len(o.handle_error), 0)

def test_badly_named_methods(self):
# test work-around for three methods that accidentally follow the
# naming conventions for handler methods
Expand Down
2 changes: 2 additions & 0 deletions Lib/urllib/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,8 @@ def add_handler(self, handler):
continue

i = meth.find("_")
if i < 1:
continue
protocol = meth[:i]
condition = meth[i+1:]

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