]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-124098: Fix incorrect inclusion of handler methods without protocol prefix in...
authorMonadChains <monadchains@gmail.com>
Thu, 18 Dec 2025 12:50:05 +0000 (13:50 +0100)
committerGitHub <noreply@github.com>
Thu, 18 Dec 2025 12:50:05 +0000 (13:50 +0100)
Lib/test/test_urllib2.py
Lib/urllib/request.py
Misc/NEWS.d/next/Library/2025-07-20-15-39-54.gh-issue-124098.znFPIp.rst [new file with mode: 0644]

index 7d7f2fa00d35b62964b4648d1e63d6e2c7af0c38..3a77b9e5ab792890df753d414eab07e87a796577 100644 (file)
@@ -577,6 +577,23 @@ class OpenerDirectorTests(unittest.TestCase):
         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
index 566b8087aec2771e1f5dc3b776bbd68bd67e56e2..f32de189b1353a5b5939ba32c309c79a9774f5d8 100644 (file)
@@ -415,6 +415,8 @@ class OpenerDirector:
                 continue
 
             i = meth.find("_")
+            if i < 1:
+                continue
             protocol = meth[:i]
             condition = meth[i+1:]
 
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 (file)
index 0000000..236b37d
--- /dev/null
@@ -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.