From f2e08aa4ea8653a82a080190088584c07ce6a51a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Sat, 15 Mar 2025 11:33:28 +0100 Subject: [PATCH 1/3] ensure that BROWSER is not set for `test_webbrowser` on macOS. --- Lib/test/test_webbrowser.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Lib/test/test_webbrowser.py b/Lib/test/test_webbrowser.py index 870ddd7349f494..4c3ea1cd8df13e 100644 --- a/Lib/test/test_webbrowser.py +++ b/Lib/test/test_webbrowser.py @@ -321,7 +321,13 @@ def close(self): @unittest.skipUnless(sys.platform == "darwin", "macOS specific test") @requires_subprocess() class MacOSXOSAScriptTest(unittest.TestCase): + def setUp(self): + # Ensure that 'BROWSER' is not set to 'open' or something else. + # See: https://github.com/python/cpython/issues/131254. + env = self.enterContext(os_helper.EnvironmentVarGuard()) + env.unset("BROWSER") + support.patch(self, os, "popen", self.mock_popen) self.browser = webbrowser.MacOSXOSAScript("default") From 431d8278dff7f5cf711859f770fd348383acb071 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Sat, 15 Mar 2025 11:43:10 +0100 Subject: [PATCH 2/3] fix possible KeyError --- Lib/test/test_webbrowser.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_webbrowser.py b/Lib/test/test_webbrowser.py index 4c3ea1cd8df13e..59b2877078e257 100644 --- a/Lib/test/test_webbrowser.py +++ b/Lib/test/test_webbrowser.py @@ -326,7 +326,8 @@ def setUp(self): # Ensure that 'BROWSER' is not set to 'open' or something else. # See: https://github.com/python/cpython/issues/131254. env = self.enterContext(os_helper.EnvironmentVarGuard()) - env.unset("BROWSER") + if "BROWSER" in env: + env.unset("BROWSER") support.patch(self, os, "popen", self.mock_popen) self.browser = webbrowser.MacOSXOSAScript("default") From a32c280bf097fdb3bd816d7ef5d64a97084f7073 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Sun, 16 Mar 2025 14:07:31 +0100 Subject: [PATCH 3/3] Actually, `unset` checks for the existence of the key. --- Lib/test/test_webbrowser.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Lib/test/test_webbrowser.py b/Lib/test/test_webbrowser.py index 59b2877078e257..4c3ea1cd8df13e 100644 --- a/Lib/test/test_webbrowser.py +++ b/Lib/test/test_webbrowser.py @@ -326,8 +326,7 @@ def setUp(self): # Ensure that 'BROWSER' is not set to 'open' or something else. # See: https://github.com/python/cpython/issues/131254. env = self.enterContext(os_helper.EnvironmentVarGuard()) - if "BROWSER" in env: - env.unset("BROWSER") + env.unset("BROWSER") support.patch(self, os, "popen", self.mock_popen) self.browser = webbrowser.MacOSXOSAScript("default")