From 0cc4bbd237144a06e127d9e713adae3ac4a86d0a Mon Sep 17 00:00:00 2001 From: SendaoYan Date: Thu, 23 Apr 2026 23:17:00 +0800 Subject: [PATCH 1/2] gh-148919: Make test_redirect_no_path independent of proxy environment variables --- Lib/test/test_urllib2.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Lib/test/test_urllib2.py b/Lib/test/test_urllib2.py index 3a77b9e5ab7928..84f38d9065d1b9 100644 --- a/Lib/test/test_urllib2.py +++ b/Lib/test/test_urllib2.py @@ -1383,6 +1383,12 @@ def test_redirect_fragment(self): def test_redirect_no_path(self): # Issue 14132: Relative redirect strips original path + # Keep this test independent from proxy-related environment settings. + env = self.enterContext(os_helper.EnvironmentVarGuard()) + for var in ("http_proxy", "https_proxy", "all_proxy", "no_proxy", + "HTTP_PROXY", "HTTPS_PROXY", "ALL_PROXY", "NO_PROXY"): + env.pop(var, None) + # clear _opener global variable self.addCleanup(urllib.request.urlcleanup) From 31ceb9af29c1f0b53b5f8067c1f7112355b7dda2 Mon Sep 17 00:00:00 2001 From: Senthil Kumaran Date: Wed, 28 Oct 2026 12:02:21 -0700 Subject: [PATCH 2/2] 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 --- Lib/test/test_urllib2.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/Lib/test/test_urllib2.py b/Lib/test/test_urllib2.py index b184efedf6b9ea..c4c124a15ce22c 100644 --- a/Lib/test/test_urllib2.py +++ b/Lib/test/test_urllib2.py @@ -1453,14 +1453,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 - - # Keep this test independent from proxy-related environment settings. - env = self.enterContext(os_helper.EnvironmentVarGuard()) - for var in ("http_proxy", "https_proxy", "all_proxy", "no_proxy", - "HTTP_PROXY", "HTTPS_PROXY", "ALL_PROXY", "NO_PROXY"): - env.pop(var, None) + self.unset_proxy_env() # clear _opener global variable self.addCleanup(urllib.request.urlcleanup) @@ -1494,6 +1495,7 @@ def http_open(self, req): b'123' ) return result + self.unset_proxy_env() handler = Handler() opener = urllib.request.build_opener(handler) tests = (