From 7451c21fc99cf6c9047ed3ff05bfa690f787ed41 Mon Sep 17 00:00:00 2001 From: sendaoYan Date: Thu, 29 Oct 2026 03:38:18 +0800 Subject: [PATCH] gh-148919: Make test_redirect_no_path independent of proxy environment variables (GH-148920) * gh-148919: Make test_redirect_no_path independent of proxy environment variables * gh-148919: Also make test_redirect_encoding independent of proxy env test_redirect_encoding uses build_opener(), which installs the default ProxyHandler, so it failed the same way when http_proxy was set. Move the environment cleanup into a helper and use EnvironmentVarGuard.unset(). Co-Authored-By: Claude Opus 5.5 --------- (cherry picked from commit d85438045f6fce7579f964d82cf8529e338b9d82) Co-authored-by: sendaoYan Co-authored-by: Senthil Kumaran Co-authored-by: Claude Opus 5.5 --- Lib/test/test_urllib2.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Lib/test/test_urllib2.py b/Lib/test/test_urllib2.py index 94998145b40c7c1..f15baab4519416a 100644 --- a/Lib/test/test_urllib2.py +++ b/Lib/test/test_urllib2.py @@ -1436,8 +1436,15 @@ def test_redirect_fragment(self): fp = o.open('http://www.example.com') self.assertEqual(fp.geturl(), redirected_url.strip()) + def unset_proxy_env(self): + # Keep the test independent from proxy-related environment settings. + env = self.enterContext(os_helper.EnvironmentVarGuard()) + env.unset("http_proxy", "https_proxy", "all_proxy", "no_proxy", + "HTTP_PROXY", "HTTPS_PROXY", "ALL_PROXY", "NO_PROXY") + def test_redirect_no_path(self): # Issue 14132: Relative redirect strips original path + self.unset_proxy_env() # clear _opener global variable self.addCleanup(urllib.request.urlcleanup) @@ -1471,6 +1478,7 @@ def http_open(self, req): b'123' ) return result + self.unset_proxy_env() handler = Handler() opener = urllib.request.build_opener(handler) tests = (