From d60ea5ff009e7e48be78c388f642cd64287655f7 Mon Sep 17 00:00:00 2001
From: Martin Panter <vadmium+py@gmail.com>
Date: Sat, 22 Oct 2016 01:28:21 +0000
Subject: Issue #26620: Fix ResourceWarning in test_urllib2_localnet

* Use context manager on urllib objects to ensure that they are closed on error
* Use self.addCleanup() to cleanup resources even if a test is interrupted
  with CTRL+c

This backports a patch by Victor Stinner.
---
 Lib/test/test_urllib2_localnet.py | 39 ++++++++++++++++++---------------------
 1 file changed, 18 insertions(+), 21 deletions(-)

diff --git a/Lib/test/test_urllib2_localnet.py b/Lib/test/test_urllib2_localnet.py
index c8b37ee..e9564fd 100644
--- a/Lib/test/test_urllib2_localnet.py
+++ b/Lib/test/test_urllib2_localnet.py
@@ -289,12 +289,12 @@ class BasicAuthTests(unittest.TestCase):
         def http_server_with_basic_auth_handler(*args, **kwargs):
             return BasicAuthHandler(*args, **kwargs)
         self.server = LoopbackHttpServerThread(http_server_with_basic_auth_handler)
+        self.addCleanup(self.server.stop)
         self.server_url = 'http://127.0.0.1:%s' % self.server.port
         self.server.start()
         self.server.ready.wait()
 
     def tearDown(self):
-        self.server.stop()
         super(BasicAuthTests, self).tearDown()
 
     def test_basic_auth_success(self):
@@ -438,17 +438,13 @@ class TestUrlopen(unittest.TestCase):
 
     def setUp(self):
         super(TestUrlopen, self).setUp()
+
         # Ignore proxies for localhost tests.
-        self.old_environ = os.environ.copy()
+        def restore_environ(old_environ):
+            os.environ.clear()
+            os.environ.update(old_environ)
+        self.addCleanup(restore_environ, os.environ.copy())
         os.environ['NO_PROXY'] = '*'
-        self.server = None
-
-    def tearDown(self):
-        if self.server is not None:
-            self.server.stop()
-        os.environ.clear()
-        os.environ.update(self.old_environ)
-        super(TestUrlopen, self).tearDown()
 
     def urlopen(self, url, data=None, **kwargs):
         l = []
@@ -469,6 +465,7 @@ class TestUrlopen(unittest.TestCase):
         handler = GetRequestHandler(responses)
 
         self.server = LoopbackHttpServerThread(handler)
+        self.addCleanup(self.server.stop)
         self.server.start()
         self.server.ready.wait()
         port = self.server.port
@@ -592,7 +589,8 @@ class TestUrlopen(unittest.TestCase):
         handler = self.start_server()
         req = urllib.request.Request("http://localhost:%s/" % handler.port,
                                      headers={"Range": "bytes=20-39"})
-        urllib.request.urlopen(req)
+        with urllib.request.urlopen(req):
+            pass
         self.assertEqual(handler.headers_received["Range"], "bytes=20-39")
 
     def test_basic(self):
@@ -608,22 +606,21 @@ class TestUrlopen(unittest.TestCase):
 
     def test_info(self):
         handler = self.start_server()
-        try:
-            open_url = urllib.request.urlopen(
-                "http://localhost:%s" % handler.port)
+        open_url = urllib.request.urlopen(
+            "http://localhost:%s" % handler.port)
+        with open_url:
             info_obj = open_url.info()
-            self.assertIsInstance(info_obj, email.message.Message,
-                                  "object returned by 'info' is not an "
-                                  "instance of email.message.Message")
-            self.assertEqual(info_obj.get_content_subtype(), "plain")
-        finally:
-            self.server.stop()
+        self.assertIsInstance(info_obj, email.message.Message,
+                              "object returned by 'info' is not an "
+                              "instance of email.message.Message")
+        self.assertEqual(info_obj.get_content_subtype(), "plain")
 
     def test_geturl(self):
         # Make sure same URL as opened is returned by geturl.
         handler = self.start_server()
         open_url = urllib.request.urlopen("http://localhost:%s" % handler.port)
-        url = open_url.geturl()
+        with open_url:
+            url = open_url.geturl()
         self.assertEqual(url, "http://localhost:%s" % handler.port)
 
     def test_iteration(self):
-- 
cgit v0.12


From 1002a621c1f0c8d5adcdd6462b9bdd0470e34290 Mon Sep 17 00:00:00 2001
From: Martin Panter <vadmium+py@gmail.com>
Date: Sat, 22 Oct 2016 01:42:06 +0000
Subject: Issue #28435: Avoid no_proxy environment variable interfering with
 tests

Patch by Piotr Szczepaniak.
---
 Lib/test/test_urllib2_localnet.py | 9 +++++++++
 Misc/ACKS                         | 1 +
 2 files changed, 10 insertions(+)

diff --git a/Lib/test/test_urllib2_localnet.py b/Lib/test/test_urllib2_localnet.py
index e9564fd..68c523a 100644
--- a/Lib/test/test_urllib2_localnet.py
+++ b/Lib/test/test_urllib2_localnet.py
@@ -323,6 +323,14 @@ class ProxyAuthTests(unittest.TestCase):
 
     def setUp(self):
         super(ProxyAuthTests, self).setUp()
+        # Ignore proxy bypass settings in the environment.
+        def restore_environ(old_environ):
+            os.environ.clear()
+            os.environ.update(old_environ)
+        self.addCleanup(restore_environ, os.environ.copy())
+        os.environ['NO_PROXY'] = ''
+        os.environ['no_proxy'] = ''
+
         self.digest_auth_handler = DigestAuthHandler()
         self.digest_auth_handler.set_users({self.USER: self.PASSWD})
         self.digest_auth_handler.set_realm(self.REALM)
@@ -445,6 +453,7 @@ class TestUrlopen(unittest.TestCase):
             os.environ.update(old_environ)
         self.addCleanup(restore_environ, os.environ.copy())
         os.environ['NO_PROXY'] = '*'
+        os.environ['no_proxy'] = '*'
 
     def urlopen(self, url, data=None, **kwargs):
         l = []
diff --git a/Misc/ACKS b/Misc/ACKS
index d35f1e8..8c5c0c2 100644
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -1450,6 +1450,7 @@ Al Sweigart
 Thenault Sylvain
 Péter Szabó
 John Szakmeister
+Piotr Szczepaniak
 Amir Szekely
 Maciej Szulik
 Arfrever Frehtes Taifersar Arahesis
-- 
cgit v0.12

