From e9695d2695091d188135615127e6ca67abecf184 Mon Sep 17 00:00:00 2001 From: monadchains Date: Sun, 20 Jul 2025 15:30:04 +0200 Subject: [PATCH 1/3] fix + tests --- Lib/test/test_urllib2.py | 17 +++++++++++++++++ Lib/urllib/request.py | 2 ++ 2 files changed, 19 insertions(+) diff --git a/Lib/test/test_urllib2.py b/Lib/test/test_urllib2.py index 7d7f2fa00d35b6..3a77b9e5ab7928 100644 --- a/Lib/test/test_urllib2.py +++ b/Lib/test/test_urllib2.py @@ -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 diff --git a/Lib/urllib/request.py b/Lib/urllib/request.py index 41dc5d7b35dedb..043accc9dad0b3 100644 --- a/Lib/urllib/request.py +++ b/Lib/urllib/request.py @@ -415,6 +415,8 @@ def add_handler(self, handler): continue i = meth.find("_") + if i < 1: + continue protocol = meth[:i] condition = meth[i+1:] From b62a515e43b1071f2f35b9aa56d7f5db0df221db Mon Sep 17 00:00:00 2001 From: monadchains Date: Sun, 20 Jul 2025 15:45:09 +0200 Subject: [PATCH 2/3] add blurb --- .../Library/2025-07-20-15-39-54.gh-issue-124098.znFPIp.rst | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2025-07-20-15-39-54.gh-issue-124098.znFPIp.rst diff --git a/Misc/NEWS.d/next/Library/2025-07-20-15-39-54.gh-issue-124098.znFPIp.rst b/Misc/NEWS.d/next/Library/2025-07-20-15-39-54.gh-issue-124098.znFPIp.rst new file mode 100644 index 00000000000000..ec2b8ce89f71b0 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-07-20-15-39-54.gh-issue-124098.znFPIp.rst @@ -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:`OpenerDirector`'s handlers. +Contributed by Andrea Mattei. From 365ad01950fed2bc1f02212d537d9b8243daf82e Mon Sep 17 00:00:00 2001 From: monadchains Date: Sun, 20 Jul 2025 16:25:12 +0200 Subject: [PATCH 3/3] fix reference --- .../Library/2025-07-20-15-39-54.gh-issue-124098.znFPIp.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Misc/NEWS.d/next/Library/2025-07-20-15-39-54.gh-issue-124098.znFPIp.rst b/Misc/NEWS.d/next/Library/2025-07-20-15-39-54.gh-issue-124098.znFPIp.rst index ec2b8ce89f71b0..236b37d268ef2d 100644 --- a/Misc/NEWS.d/next/Library/2025-07-20-15-39-54.gh-issue-124098.znFPIp.rst +++ b/Misc/NEWS.d/next/Library/2025-07-20-15-39-54.gh-issue-124098.znFPIp.rst @@ -1,4 +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:`OpenerDirector`'s handlers. -Contributed by Andrea Mattei. +were incorrectly added to :class:`urllib.request.OpenerDirector`'s +handlers. Contributed by Andrea Mattei.