Skip to content

gh-124098: Fix incorrect inclusion of handler methods without protocol prefix in OpenerDirector #136873

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
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.
Loading