| 1 |
From: "Miss Islington (bot)" |
| 2 |
<[email protected]> |
| 3 |
Date: Sat, 4 Apr 2026 00:53:49 +0200 |
| 4 |
Subject: gh-143930: Tweak the exception message and increase test coverage |
| 5 |
(GH-146476) (GH-148045) (GH-148051) (GH-148052) |
| 6 |
MIME-Version: 1.0 |
| 7 |
Content-Type: text/plain; charset="utf-8" |
| 8 |
Content-Transfer-Encoding: 8bit |
| 9 |
|
| 10 |
(cherry picked from commit cc023511238ad93ecc8796157c6f9139a2bb2932) |
| 11 |
(cherry picked from commit 89bfb8e5ed3c7caa241028f1a4eac5f6275a46a4) |
| 12 |
(cherry picked from commit 3681d47a440865aead912a054d4599087b4270dd) |
| 13 |
|
| 14 |
Co-authored-by: Ćukasz Langa <[email protected]> |
| 15 |
|
| 16 |
Origin: cpython, https://github.com/python/cpython/commit/96fc5048605863c7b6fd6289643feb0e97edd96c |
| 17 |
--- |
| 18 |
lib-python/3/test/test_webbrowser.py | 81 +++++++++++++++++++++++++++++++++--- |
| 19 |
lib-python/3/webbrowser.py | 2 +- |
| 20 |
2 files changed, 76 insertions(+), 7 deletions(-) |
| 21 |
|
| 22 |
diff --git a/lib-python/3/test/test_webbrowser.py b/lib-python/3/test/test_webbrowser.py |
| 23 |
index 0ac985f..522219b 100644 |
| 24 |
--- a/lib-python/3/test/test_webbrowser.py |
| 25 |
+++ b/lib-python/3/test/test_webbrowser.py |
| 26 |
@@ -1,6 +1,7 @@ |
| 27 |
+import io |
| 28 |
+import os |
| 29 |
import webbrowser |
| 30 |
import unittest |
| 31 |
-import os |
| 32 |
import sys |
| 33 |
import subprocess |
| 34 |
from unittest import mock |
| 35 |
@@ -49,6 +50,14 @@ class CommandTestMixin: |
| 36 |
popen_args.pop(popen_args.index(option)) |
| 37 |
self.assertEqual(popen_args, arguments) |
| 38 |
|
| 39 |
+ def test_reject_dash_prefixes(self): |
| 40 |
+ browser = self.browser_class(name=CMD_NAME) |
| 41 |
+ with self.assertRaisesRegex( |
| 42 |
+ ValueError, |
| 43 |
+ r"^Invalid URL \(leading dash disallowed\): '--key=val http.*'$" |
| 44 |
+ ): |
| 45 |
+ browser.open(f"--key=val {URL}") |
| 46 |
+ |
| 47 |
|
| 48 |
class GenericBrowserCommandTest(CommandTestMixin, unittest.TestCase): |
| 49 |
|
| 50 |
@@ -59,11 +68,6 @@ class GenericBrowserCommandTest(CommandTestMixin, unittest.TestCase): |
| 51 |
options=[], |
| 52 |
arguments=[URL]) |
| 53 |
|
| 54 |
- def test_reject_dash_prefixes(self): |
| 55 |
- browser = self.browser_class(name=CMD_NAME) |
| 56 |
- with self.assertRaises(ValueError): |
| 57 |
- browser.open(f"--key=val {URL}") |
| 58 |
- |
| 59 |
|
| 60 |
class BackgroundBrowserCommandTest(CommandTestMixin, unittest.TestCase): |
| 61 |
|
| 62 |
@@ -224,6 +228,71 @@ class ELinksCommandTest(CommandTestMixin, unittest.TestCase): |
| 63 |
arguments=['openURL({},new-tab)'.format(URL)]) |
| 64 |
|
| 65 |
|
| 66 |
+class MockPopenPipe: |
| 67 |
+ def __init__(self, cmd, mode): |
| 68 |
+ self.cmd = cmd |
| 69 |
+ self.mode = mode |
| 70 |
+ self.pipe = io.StringIO() |
| 71 |
+ self._closed = False |
| 72 |
+ |
| 73 |
+ def write(self, buf): |
| 74 |
+ self.pipe.write(buf) |
| 75 |
+ |
| 76 |
+ def close(self): |
| 77 |
+ self._closed = True |
| 78 |
+ return None |
| 79 |
+ |
| 80 |
+ |
| 81 |
[email protected](sys.platform == "darwin", "macOS specific test") |
| 82 |
+class MacOSXOSAScriptTest(unittest.TestCase): |
| 83 |
+ def setUp(self): |
| 84 |
+ # Ensure that 'BROWSER' is not set to 'open' or something else. |
| 85 |
+ # See: https://github.com/python/cpython/issues/131254. |
| 86 |
+ env = self.enterContext(os_helper.EnvironmentVarGuard()) |
| 87 |
+ env.unset("BROWSER") |
| 88 |
+ |
| 89 |
+ support.patch(self, os, "popen", self.mock_popen) |
| 90 |
+ self.browser = webbrowser.MacOSXOSAScript("default") |
| 91 |
+ |
| 92 |
+ def mock_popen(self, cmd, mode): |
| 93 |
+ self.popen_pipe = MockPopenPipe(cmd, mode) |
| 94 |
+ return self.popen_pipe |
| 95 |
+ |
| 96 |
+ def test_default(self): |
| 97 |
+ browser = webbrowser.get() |
| 98 |
+ assert isinstance(browser, webbrowser.MacOSXOSAScript) |
| 99 |
+ self.assertEqual(browser.name, "default") |
| 100 |
+ |
| 101 |
+ def test_default_open(self): |
| 102 |
+ url = "https://python.org" |
| 103 |
+ self.browser.open(url) |
| 104 |
+ self.assertTrue(self.popen_pipe._closed) |
| 105 |
+ self.assertEqual(self.popen_pipe.cmd, "osascript") |
| 106 |
+ script = self.popen_pipe.pipe.getvalue() |
| 107 |
+ self.assertEqual(script.strip(), f'open location "{url}"') |
| 108 |
+ |
| 109 |
+ def test_url_quote(self): |
| 110 |
+ self.browser.open('https://python.org/"quote"') |
| 111 |
+ script = self.popen_pipe.pipe.getvalue() |
| 112 |
+ self.assertEqual( |
| 113 |
+ script.strip(), 'open location "https://python.org/%22quote%22"' |
| 114 |
+ ) |
| 115 |
+ |
| 116 |
+ def test_explicit_browser(self): |
| 117 |
+ browser = webbrowser.MacOSXOSAScript("safari") |
| 118 |
+ browser.open("https://python.org") |
| 119 |
+ script = self.popen_pipe.pipe.getvalue() |
| 120 |
+ self.assertIn('tell application "safari"', script) |
| 121 |
+ self.assertIn('open location "https://python.org"', script) |
| 122 |
+ |
| 123 |
+ def test_reject_dash_prefixes(self): |
| 124 |
+ with self.assertRaisesRegex( |
| 125 |
+ ValueError, |
| 126 |
+ r"^Invalid URL \(leading dash disallowed\): '--key=val http.*'$" |
| 127 |
+ ): |
| 128 |
+ self.browser.open(f"--key=val {URL}") |
| 129 |
+ |
| 130 |
+ |
| 131 |
class BrowserRegistrationTest(unittest.TestCase): |
| 132 |
|
| 133 |
def setUp(self): |
| 134 |
diff --git a/lib-python/3/webbrowser.py b/lib-python/3/webbrowser.py |
| 135 |
index 0fd0aeb..52ad205 100755 |
| 136 |
--- a/lib-python/3/webbrowser.py |
| 137 |
+++ b/lib-python/3/webbrowser.py |
| 138 |
@@ -159,7 +159,7 @@ class BaseBrowser(object): |
| 139 |
def _check_url(url): |
| 140 |
"""Ensures that the URL is safe to pass to subprocesses as a parameter""" |
| 141 |
if url and url.lstrip().startswith("-"): |
| 142 |
- raise ValueError(f"Invalid URL: {url}") |
| 143 |
+ raise ValueError(f"Invalid URL (leading dash disallowed): {url!r}") |
| 144 |
|
| 145 |
|
| 146 |
class GenericBrowser(BaseBrowser): |