Skip to content

Commit 7cb9204

Browse files
authored
bpo-37421: urllib.request tests call urlcleanup() (GH-14529)
urllib.request tests now call urlcleanup() to remove temporary files created by urlretrieve() tests and to clear the _opener global variable set by urlopen() and functions calling indirectly urlopen(). regrtest now checks if urllib.request._url_tempfiles and urllib.request._opener are changed by tests.
1 parent 039fb49 commit 7cb9204

File tree

9 files changed

+62
-0
lines changed

9 files changed

+62
-0
lines changed

Lib/test/libregrtest/save_env.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import sys
88
import sysconfig
99
import threading
10+
import urllib.request
1011
import warnings
1112
from test import support
1213
from test.libregrtest.utils import print_warning
@@ -68,8 +69,20 @@ def __init__(self, testname, verbose=0, quiet=False, *, pgo=False):
6869
'files', 'locale', 'warnings.showwarning',
6970
'shutil_archive_formats', 'shutil_unpack_formats',
7071
'asyncio.events._event_loop_policy',
72+
'urllib.requests._url_tempfiles', 'urllib.requests._opener',
7173
)
7274

75+
def get_urllib_requests__url_tempfiles(self):
76+
return list(urllib.request._url_tempfiles)
77+
def restore_urllib_requests__url_tempfiles(self, tempfiles):
78+
for filename in tempfiles:
79+
support.unlink(filename)
80+
81+
def get_urllib_requests__opener(self):
82+
return urllib.request._opener
83+
def restore_urllib_requests__opener(self, opener):
84+
urllib.request._opener = opener
85+
7386
def get_asyncio_events__event_loop_policy(self):
7487
return support.maybe_get_event_loop_policy()
7588
def restore_asyncio_events__event_loop_policy(self, policy):

Lib/test/test_robotparser.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,9 @@ def log_message(self, format, *args):
309309
class PasswordProtectedSiteTestCase(unittest.TestCase):
310310

311311
def setUp(self):
312+
# clear _opener global variable
313+
self.addCleanup(urllib.request.urlcleanup)
314+
312315
self.server = HTTPServer((support.HOST, 0), RobotHandler)
313316

314317
self.t = threading.Thread(

Lib/test/test_sax.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import os.path
2121
import shutil
2222
from urllib.error import URLError
23+
import urllib.request
2324
from test import support
2425
from test.support import findfile, run_unittest, FakePath, TESTFN
2526

@@ -979,6 +980,9 @@ def test_expat_dtdhandler(self):
979980
self.assertEqual(handler._entities, [("img", None, "expat.gif", "GIF")])
980981

981982
def test_expat_external_dtd_enabled(self):
983+
# clear _opener global variable
984+
self.addCleanup(urllib.request.urlcleanup)
985+
982986
parser = create_parser()
983987
parser.setFeature(feature_external_ges, True)
984988
resolver = self.TestEntityRecorder()

Lib/test/test_urllib.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,9 @@ class urlopen_DataTests(unittest.TestCase):
545545
"""Test urlopen() opening a data URL."""
546546

547547
def setUp(self):
548+
# clear _opener global variable
549+
self.addCleanup(urllib.request.urlcleanup)
550+
548551
# text containing URL special- and unicode-characters
549552
self.text = "test data URLs :;,%=& \u00f6 \u00c4 "
550553
# 2x1 pixel RGB PNG image with one black and one white pixel
@@ -619,6 +622,9 @@ class urlretrieve_FileTests(unittest.TestCase):
619622
"""Test urllib.urlretrieve() on local files"""
620623

621624
def setUp(self):
625+
# clear _opener global variable
626+
self.addCleanup(urllib.request.urlcleanup)
627+
622628
# Create a list of temporary files. Each item in the list is a file
623629
# name (absolute path or relative to the current working directory).
624630
# All files in this list will be deleted in the tearDown method. Note,
@@ -759,6 +765,8 @@ class urlretrieve_HttpTests(unittest.TestCase, FakeHTTPMixin):
759765
"""Test urllib.urlretrieve() using fake http connections"""
760766

761767
def test_short_content_raises_ContentTooShortError(self):
768+
self.addCleanup(urllib.request.urlcleanup)
769+
762770
self.fakehttp(b'''HTTP/1.1 200 OK
763771
Date: Wed, 02 Jan 2008 03:03:54 GMT
764772
Server: Apache/1.3.33 (Debian GNU/Linux) mod_ssl/2.8.22 OpenSSL/0.9.7e
@@ -780,6 +788,8 @@ def _reporthook(par1, par2, par3):
780788
self.unfakehttp()
781789

782790
def test_short_content_raises_ContentTooShortError_without_reporthook(self):
791+
self.addCleanup(urllib.request.urlcleanup)
792+
783793
self.fakehttp(b'''HTTP/1.1 200 OK
784794
Date: Wed, 02 Jan 2008 03:03:54 GMT
785795
Server: Apache/1.3.33 (Debian GNU/Linux) mod_ssl/2.8.22 OpenSSL/0.9.7e

Lib/test/test_urllib2.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ def test___all__(self):
4747
def test_trivial(self):
4848
# A couple trivial tests
4949

50+
# clear _opener global variable
51+
self.addCleanup(urllib.request.urlcleanup)
52+
5053
self.assertRaises(ValueError, urllib.request.urlopen, 'bogus url')
5154

5255
# XXX Name hacking to get this to work on Windows.
@@ -1290,6 +1293,10 @@ def test_redirect_fragment(self):
12901293

12911294
def test_redirect_no_path(self):
12921295
# Issue 14132: Relative redirect strips original path
1296+
1297+
# clear _opener global variable
1298+
self.addCleanup(urllib.request.urlcleanup)
1299+
12931300
real_class = http.client.HTTPConnection
12941301
response1 = b"HTTP/1.1 302 Found\r\nLocation: ?query\r\n\r\n"
12951302
http.client.HTTPConnection = test_urllib.fakehttp(response1)

Lib/test/test_urllib2_localnet.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,9 @@ class TestUrlopen(unittest.TestCase):
447447
def setUp(self):
448448
super(TestUrlopen, self).setUp()
449449

450+
# clear _opener global variable
451+
self.addCleanup(urllib.request.urlcleanup)
452+
450453
# Ignore proxies for localhost tests.
451454
def restore_environ(old_environ):
452455
os.environ.clear()

Lib/test/test_urllib2net.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ class AuthTests(unittest.TestCase):
8282
class CloseSocketTest(unittest.TestCase):
8383

8484
def test_close(self):
85+
# clear _opener global variable
86+
self.addCleanup(urllib.request.urlcleanup)
87+
8588
# calling .close() on urllib2's response objects should close the
8689
# underlying socket
8790
url = support.TEST_HTTP_URL
@@ -257,6 +260,10 @@ def _extra_handlers(self):
257260

258261

259262
class TimeoutTest(unittest.TestCase):
263+
def setUp(self):
264+
# clear _opener global variable
265+
self.addCleanup(urllib.request.urlcleanup)
266+
260267
def test_http_basic(self):
261268
self.assertIsNone(socket.getdefaulttimeout())
262269
url = support.TEST_HTTP_URL

Lib/test/test_urllibnet.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ def tearDown(self):
2525
socket.setdefaulttimeout(None)
2626

2727
def testURLread(self):
28+
# clear _opener global variable
29+
self.addCleanup(urllib.request.urlcleanup)
30+
2831
domain = urllib.parse.urlparse(support.TEST_HTTP_URL).netloc
2932
with support.transient_internet(domain):
3033
f = urllib.request.urlopen(support.TEST_HTTP_URL)
@@ -48,6 +51,10 @@ class urlopenNetworkTests(unittest.TestCase):
4851

4952
url = 'http://www.pythontest.net/'
5053

54+
def setUp(self):
55+
# clear _opener global variable
56+
self.addCleanup(urllib.request.urlcleanup)
57+
5158
@contextlib.contextmanager
5259
def urlopen(self, *args, **kwargs):
5360
resource = args[0]
@@ -144,6 +151,10 @@ def test_bad_address(self):
144151
class urlretrieveNetworkTests(unittest.TestCase):
145152
"""Tests urllib.request.urlretrieve using the network."""
146153

154+
def setUp(self):
155+
# remove temporary files created by urlretrieve()
156+
self.addCleanup(urllib.request.urlcleanup)
157+
147158
@contextlib.contextmanager
148159
def urlretrieve(self, *args, **kwargs):
149160
resource = args[0]
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
urllib.request tests now call :func:`~urllib.request.urlcleanup` to remove
2+
temporary files created by ``urlretrieve()`` tests and to clear the ``_opener``
3+
global variable set by ``urlopen()`` and functions calling indirectly
4+
``urlopen()``.

0 commit comments

Comments
 (0)