2003-03-30 00:54:24 -04:00
|
|
|
import unittest
|
2008-05-20 18:35:26 -03:00
|
|
|
from test import support
|
2020-08-06 08:51:29 -03:00
|
|
|
from test.support import os_helper
|
2020-04-29 04:36:20 -03:00
|
|
|
from test.support import socket_helper
|
2003-03-30 00:54:24 -04:00
|
|
|
|
2011-05-22 12:35:17 -03:00
|
|
|
import contextlib
|
2003-03-30 00:54:24 -04:00
|
|
|
import socket
|
2024-11-19 10:01:49 -04:00
|
|
|
import urllib.error
|
2019-02-22 09:45:36 -04:00
|
|
|
import urllib.parse
|
2008-06-18 17:49:58 -03:00
|
|
|
import urllib.request
|
2003-05-13 23:18:31 -03:00
|
|
|
import os
|
2008-06-12 01:06:45 -03:00
|
|
|
import email.message
|
2010-05-01 05:29:18 -03:00
|
|
|
import time
|
2003-03-30 00:54:24 -04:00
|
|
|
|
Merged revisions 60284-60349 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r60286 | christian.heimes | 2008-01-25 15:54:23 +0100 (Fri, 25 Jan 2008) | 1 line
setup.py doesn't pick up changes to a header file
........
r60287 | christian.heimes | 2008-01-25 16:52:11 +0100 (Fri, 25 Jan 2008) | 2 lines
Added the Python core headers Include/*.h and pyconfig.h as dependencies for the extensions in Modules/
It forces a rebuild of all extensions when a header files has been modified
........
r60291 | raymond.hettinger | 2008-01-25 20:24:46 +0100 (Fri, 25 Jan 2008) | 4 lines
Changes 54857 and 54840 broke code and were reverted in Py2.5 just before
it was released, but that reversion never made it to the Py2.6 head.
........
r60296 | guido.van.rossum | 2008-01-25 20:50:26 +0100 (Fri, 25 Jan 2008) | 2 lines
Rewrite the list_inline_repeat overflow check slightly differently.
........
r60301 | thomas.wouters | 2008-01-25 22:09:34 +0100 (Fri, 25 Jan 2008) | 4 lines
Use the right (portable) definition of the max of a Py_ssize_t.
........
r60303 | thomas.wouters | 2008-01-26 02:47:05 +0100 (Sat, 26 Jan 2008) | 5 lines
Make 'testall' work again when building in a separate directory.
test_distutils still fails when doing that.
........
r60305 | neal.norwitz | 2008-01-26 06:54:48 +0100 (Sat, 26 Jan 2008) | 3 lines
Prevent this test from failing if there are transient network problems
by retrying the host for up to 3 times.
........
r60306 | neal.norwitz | 2008-01-26 08:26:12 +0100 (Sat, 26 Jan 2008) | 12 lines
Use a condition variable (threading.Event) rather than sleeps and checking a
global to determine when the server is ready to be used. This slows the test
down, but should make it correct. There was a race condition before where the
server could have assigned a port, yet it wasn't ready to serve requests. If
the client sent a request before the server was completely ready, it would get
an exception. There was machinery to try to handle this condition. All of
that should be unnecessary and removed if this change works. A NOTE was
added as a comment about what needs to be fixed.
The buildbots will tell us if there are more errors or
if this test is now stable.
........
r60307 | neal.norwitz | 2008-01-26 08:38:03 +0100 (Sat, 26 Jan 2008) | 3 lines
Fix exception in tearDown on ppc buildbot. If there's no directory,
that shouldn't cause the test to fail. Just like it setUp.
........
r60308 | raymond.hettinger | 2008-01-26 09:19:06 +0100 (Sat, 26 Jan 2008) | 3 lines
Make PySet_Add() work with frozensets. Works like PyTuple_SetItem() to build-up values in a brand new frozenset.
........
r60309 | neal.norwitz | 2008-01-26 09:26:00 +0100 (Sat, 26 Jan 2008) | 1 line
The OS X buildbot had errors with the unavailable exceptions disabled. Restore it.
........
r60310 | raymond.hettinger | 2008-01-26 09:37:28 +0100 (Sat, 26 Jan 2008) | 4 lines
Let marshal build-up sets and frozensets one element at a time.
Saves the unnecessary creation of a tuple as intermediate container.
........
r60311 | raymond.hettinger | 2008-01-26 09:41:13 +0100 (Sat, 26 Jan 2008) | 1 line
Update test code for change to PySet_Add().
........
r60312 | raymond.hettinger | 2008-01-26 10:31:11 +0100 (Sat, 26 Jan 2008) | 1 line
Revert PySet_Add() changes.
........
r60314 | georg.brandl | 2008-01-26 10:43:35 +0100 (Sat, 26 Jan 2008) | 2 lines
#1934: fix os.path.isabs docs.
........
r60316 | georg.brandl | 2008-01-26 12:00:18 +0100 (Sat, 26 Jan 2008) | 2 lines
Add missing things in re docstring.
........
r60317 | georg.brandl | 2008-01-26 12:02:22 +0100 (Sat, 26 Jan 2008) | 2 lines
Slashes allowed on Windows.
........
r60319 | georg.brandl | 2008-01-26 14:41:21 +0100 (Sat, 26 Jan 2008) | 2 lines
Fix markup again.
........
r60320 | andrew.kuchling | 2008-01-26 14:50:51 +0100 (Sat, 26 Jan 2008) | 1 line
Add some items
........
r60321 | georg.brandl | 2008-01-26 15:02:38 +0100 (Sat, 26 Jan 2008) | 2 lines
Clarify "b" mode under Unix.
........
r60322 | georg.brandl | 2008-01-26 15:03:47 +0100 (Sat, 26 Jan 2008) | 3 lines
#1940: make it possible to use curses.filter() before curses.initscr()
as the documentation says.
........
r60324 | georg.brandl | 2008-01-26 15:14:20 +0100 (Sat, 26 Jan 2008) | 3 lines
#1473257: add generator.gi_code attribute that refers to
the original code object backing the generator. Patch by Collin Winter.
........
r60325 | georg.brandl | 2008-01-26 15:19:22 +0100 (Sat, 26 Jan 2008) | 2 lines
Move C API entries to the corresponding section.
........
r60326 | christian.heimes | 2008-01-26 17:43:35 +0100 (Sat, 26 Jan 2008) | 1 line
Unit test fix from Giampaolo Rodola, #1938
........
r60327 | gregory.p.smith | 2008-01-26 19:51:05 +0100 (Sat, 26 Jan 2008) | 2 lines
Update docs for new callpack params added in r60188
........
r60329 | neal.norwitz | 2008-01-26 21:24:36 +0100 (Sat, 26 Jan 2008) | 3 lines
Cleanup the code a bit. test_rfind is failing on PPC and PPC64 buildbots,
this might fix the problem.
........
r60330 | neal.norwitz | 2008-01-26 22:02:45 +0100 (Sat, 26 Jan 2008) | 1 line
Always try to remove the test file even if close raises an exception
........
r60331 | neal.norwitz | 2008-01-26 22:21:59 +0100 (Sat, 26 Jan 2008) | 3 lines
Reduce the race condition by signalling when the server is ready
and not trying to connect before.
........
r60334 | neal.norwitz | 2008-01-27 00:13:46 +0100 (Sun, 27 Jan 2008) | 5 lines
On some systems (e.g., Ubuntu on hppa) the flush()
doesn't cause the exception, but the close() does.
Will backport.
........
r60335 | neal.norwitz | 2008-01-27 00:14:17 +0100 (Sun, 27 Jan 2008) | 2 lines
Consistently use tempfile.tempdir for the db_home directory.
........
r60338 | neal.norwitz | 2008-01-27 02:44:05 +0100 (Sun, 27 Jan 2008) | 4 lines
Eliminate the sleeps that assume the server will start in .5 seconds.
This should make the test less flaky. It also speeds up the test
by about 75% on my box (20+ seconds -> ~4 seconds).
........
r60342 | neal.norwitz | 2008-01-27 06:02:34 +0100 (Sun, 27 Jan 2008) | 6 lines
Try to prevent this test from being flaky. We might need a sleep in here
which isn't as bad as it sounds. The close() *should* raise an exception,
so if it didn't we should give more time to sync and really raise it.
Will backport.
........
r60344 | jeffrey.yasskin | 2008-01-27 06:40:35 +0100 (Sun, 27 Jan 2008) | 3 lines
Make rational.gcd() public and allow Rational to take decimal strings, per
Raymond's advice.
........
r60345 | neal.norwitz | 2008-01-27 08:36:03 +0100 (Sun, 27 Jan 2008) | 3 lines
Mostly reformat. Also set an error and return NULL if neither MS_WINDOWS
nor UNIX is defined. This may have caused problems on cygwin.
........
r60346 | neal.norwitz | 2008-01-27 08:37:38 +0100 (Sun, 27 Jan 2008) | 3 lines
Use int for the sign rather than a char. char can be signed or unsigned.
It's system dependent. This might fix the problem with test_rfind failing.
........
r60347 | neal.norwitz | 2008-01-27 08:41:33 +0100 (Sun, 27 Jan 2008) | 1 line
Add stdarg include for va_list to get this to compile on cygwin
........
r60348 | raymond.hettinger | 2008-01-27 11:13:57 +0100 (Sun, 27 Jan 2008) | 1 line
Docstring nit
........
r60349 | raymond.hettinger | 2008-01-27 11:47:55 +0100 (Sun, 27 Jan 2008) | 1 line
Removed an unnecessary and confusing paragraph from the namedtuple docs.
........
2008-01-27 11:18:18 -04:00
|
|
|
|
2014-04-14 22:31:41 -03:00
|
|
|
support.requires('network')
|
|
|
|
|
2017-05-16 03:08:07 -03:00
|
|
|
|
2003-03-30 00:54:24 -04:00
|
|
|
class URLTimeoutTest(unittest.TestCase):
|
2011-03-26 14:38:06 -03:00
|
|
|
# XXX this test doesn't seem to test anything useful.
|
2003-03-30 00:54:24 -04:00
|
|
|
|
|
|
|
def setUp(self):
|
2019-12-10 17:09:23 -04:00
|
|
|
socket.setdefaulttimeout(support.INTERNET_TIMEOUT)
|
2003-03-30 00:54:24 -04:00
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
socket.setdefaulttimeout(None)
|
|
|
|
|
|
|
|
def testURLread(self):
|
2019-07-02 09:50:19 -03:00
|
|
|
# clear _opener global variable
|
|
|
|
self.addCleanup(urllib.request.urlcleanup)
|
|
|
|
|
2019-02-22 09:45:36 -04:00
|
|
|
domain = urllib.parse.urlparse(support.TEST_HTTP_URL).netloc
|
2020-04-29 04:36:20 -03:00
|
|
|
with socket_helper.transient_internet(domain):
|
2019-02-22 09:45:36 -04:00
|
|
|
f = urllib.request.urlopen(support.TEST_HTTP_URL)
|
2017-05-16 03:08:07 -03:00
|
|
|
f.read()
|
2003-03-30 00:54:24 -04:00
|
|
|
|
2011-05-22 12:35:17 -03:00
|
|
|
|
2003-05-13 23:18:31 -03:00
|
|
|
class urlopenNetworkTests(unittest.TestCase):
|
2017-04-02 03:47:35 -03:00
|
|
|
"""Tests urllib.request.urlopen using the network.
|
2003-05-16 12:35:10 -03:00
|
|
|
|
2003-05-13 23:18:31 -03:00
|
|
|
These tests are not exhaustive. Assuming that testing using files does a
|
|
|
|
good job overall of some of the basic interface features. There are no
|
|
|
|
tests exercising the optional 'data' and 'proxies' arguments. No tests
|
|
|
|
for transparent redirection have been written.
|
2003-05-16 12:35:10 -03:00
|
|
|
|
2003-05-13 23:18:31 -03:00
|
|
|
setUp is not used for always constructing a connection to
|
2016-05-07 10:37:09 -03:00
|
|
|
http://www.pythontest.net/ since there a few tests that don't use that address
|
2003-05-13 23:18:31 -03:00
|
|
|
and making a connection is expensive enough to warrant minimizing unneeded
|
|
|
|
connections.
|
2003-05-16 12:35:10 -03:00
|
|
|
|
2003-05-13 23:18:31 -03:00
|
|
|
"""
|
|
|
|
|
2016-05-07 10:37:09 -03:00
|
|
|
url = 'http://www.pythontest.net/'
|
|
|
|
|
2019-07-02 09:50:19 -03:00
|
|
|
def setUp(self):
|
|
|
|
# clear _opener global variable
|
|
|
|
self.addCleanup(urllib.request.urlcleanup)
|
|
|
|
|
2011-05-22 12:35:17 -03:00
|
|
|
@contextlib.contextmanager
|
2010-10-17 07:52:12 -03:00
|
|
|
def urlopen(self, *args, **kwargs):
|
|
|
|
resource = args[0]
|
2020-04-29 04:36:20 -03:00
|
|
|
with socket_helper.transient_internet(resource):
|
2011-05-22 12:35:17 -03:00
|
|
|
r = urllib.request.urlopen(*args, **kwargs)
|
|
|
|
try:
|
|
|
|
yield r
|
|
|
|
finally:
|
|
|
|
r.close()
|
Merged revisions 60284-60349 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r60286 | christian.heimes | 2008-01-25 15:54:23 +0100 (Fri, 25 Jan 2008) | 1 line
setup.py doesn't pick up changes to a header file
........
r60287 | christian.heimes | 2008-01-25 16:52:11 +0100 (Fri, 25 Jan 2008) | 2 lines
Added the Python core headers Include/*.h and pyconfig.h as dependencies for the extensions in Modules/
It forces a rebuild of all extensions when a header files has been modified
........
r60291 | raymond.hettinger | 2008-01-25 20:24:46 +0100 (Fri, 25 Jan 2008) | 4 lines
Changes 54857 and 54840 broke code and were reverted in Py2.5 just before
it was released, but that reversion never made it to the Py2.6 head.
........
r60296 | guido.van.rossum | 2008-01-25 20:50:26 +0100 (Fri, 25 Jan 2008) | 2 lines
Rewrite the list_inline_repeat overflow check slightly differently.
........
r60301 | thomas.wouters | 2008-01-25 22:09:34 +0100 (Fri, 25 Jan 2008) | 4 lines
Use the right (portable) definition of the max of a Py_ssize_t.
........
r60303 | thomas.wouters | 2008-01-26 02:47:05 +0100 (Sat, 26 Jan 2008) | 5 lines
Make 'testall' work again when building in a separate directory.
test_distutils still fails when doing that.
........
r60305 | neal.norwitz | 2008-01-26 06:54:48 +0100 (Sat, 26 Jan 2008) | 3 lines
Prevent this test from failing if there are transient network problems
by retrying the host for up to 3 times.
........
r60306 | neal.norwitz | 2008-01-26 08:26:12 +0100 (Sat, 26 Jan 2008) | 12 lines
Use a condition variable (threading.Event) rather than sleeps and checking a
global to determine when the server is ready to be used. This slows the test
down, but should make it correct. There was a race condition before where the
server could have assigned a port, yet it wasn't ready to serve requests. If
the client sent a request before the server was completely ready, it would get
an exception. There was machinery to try to handle this condition. All of
that should be unnecessary and removed if this change works. A NOTE was
added as a comment about what needs to be fixed.
The buildbots will tell us if there are more errors or
if this test is now stable.
........
r60307 | neal.norwitz | 2008-01-26 08:38:03 +0100 (Sat, 26 Jan 2008) | 3 lines
Fix exception in tearDown on ppc buildbot. If there's no directory,
that shouldn't cause the test to fail. Just like it setUp.
........
r60308 | raymond.hettinger | 2008-01-26 09:19:06 +0100 (Sat, 26 Jan 2008) | 3 lines
Make PySet_Add() work with frozensets. Works like PyTuple_SetItem() to build-up values in a brand new frozenset.
........
r60309 | neal.norwitz | 2008-01-26 09:26:00 +0100 (Sat, 26 Jan 2008) | 1 line
The OS X buildbot had errors with the unavailable exceptions disabled. Restore it.
........
r60310 | raymond.hettinger | 2008-01-26 09:37:28 +0100 (Sat, 26 Jan 2008) | 4 lines
Let marshal build-up sets and frozensets one element at a time.
Saves the unnecessary creation of a tuple as intermediate container.
........
r60311 | raymond.hettinger | 2008-01-26 09:41:13 +0100 (Sat, 26 Jan 2008) | 1 line
Update test code for change to PySet_Add().
........
r60312 | raymond.hettinger | 2008-01-26 10:31:11 +0100 (Sat, 26 Jan 2008) | 1 line
Revert PySet_Add() changes.
........
r60314 | georg.brandl | 2008-01-26 10:43:35 +0100 (Sat, 26 Jan 2008) | 2 lines
#1934: fix os.path.isabs docs.
........
r60316 | georg.brandl | 2008-01-26 12:00:18 +0100 (Sat, 26 Jan 2008) | 2 lines
Add missing things in re docstring.
........
r60317 | georg.brandl | 2008-01-26 12:02:22 +0100 (Sat, 26 Jan 2008) | 2 lines
Slashes allowed on Windows.
........
r60319 | georg.brandl | 2008-01-26 14:41:21 +0100 (Sat, 26 Jan 2008) | 2 lines
Fix markup again.
........
r60320 | andrew.kuchling | 2008-01-26 14:50:51 +0100 (Sat, 26 Jan 2008) | 1 line
Add some items
........
r60321 | georg.brandl | 2008-01-26 15:02:38 +0100 (Sat, 26 Jan 2008) | 2 lines
Clarify "b" mode under Unix.
........
r60322 | georg.brandl | 2008-01-26 15:03:47 +0100 (Sat, 26 Jan 2008) | 3 lines
#1940: make it possible to use curses.filter() before curses.initscr()
as the documentation says.
........
r60324 | georg.brandl | 2008-01-26 15:14:20 +0100 (Sat, 26 Jan 2008) | 3 lines
#1473257: add generator.gi_code attribute that refers to
the original code object backing the generator. Patch by Collin Winter.
........
r60325 | georg.brandl | 2008-01-26 15:19:22 +0100 (Sat, 26 Jan 2008) | 2 lines
Move C API entries to the corresponding section.
........
r60326 | christian.heimes | 2008-01-26 17:43:35 +0100 (Sat, 26 Jan 2008) | 1 line
Unit test fix from Giampaolo Rodola, #1938
........
r60327 | gregory.p.smith | 2008-01-26 19:51:05 +0100 (Sat, 26 Jan 2008) | 2 lines
Update docs for new callpack params added in r60188
........
r60329 | neal.norwitz | 2008-01-26 21:24:36 +0100 (Sat, 26 Jan 2008) | 3 lines
Cleanup the code a bit. test_rfind is failing on PPC and PPC64 buildbots,
this might fix the problem.
........
r60330 | neal.norwitz | 2008-01-26 22:02:45 +0100 (Sat, 26 Jan 2008) | 1 line
Always try to remove the test file even if close raises an exception
........
r60331 | neal.norwitz | 2008-01-26 22:21:59 +0100 (Sat, 26 Jan 2008) | 3 lines
Reduce the race condition by signalling when the server is ready
and not trying to connect before.
........
r60334 | neal.norwitz | 2008-01-27 00:13:46 +0100 (Sun, 27 Jan 2008) | 5 lines
On some systems (e.g., Ubuntu on hppa) the flush()
doesn't cause the exception, but the close() does.
Will backport.
........
r60335 | neal.norwitz | 2008-01-27 00:14:17 +0100 (Sun, 27 Jan 2008) | 2 lines
Consistently use tempfile.tempdir for the db_home directory.
........
r60338 | neal.norwitz | 2008-01-27 02:44:05 +0100 (Sun, 27 Jan 2008) | 4 lines
Eliminate the sleeps that assume the server will start in .5 seconds.
This should make the test less flaky. It also speeds up the test
by about 75% on my box (20+ seconds -> ~4 seconds).
........
r60342 | neal.norwitz | 2008-01-27 06:02:34 +0100 (Sun, 27 Jan 2008) | 6 lines
Try to prevent this test from being flaky. We might need a sleep in here
which isn't as bad as it sounds. The close() *should* raise an exception,
so if it didn't we should give more time to sync and really raise it.
Will backport.
........
r60344 | jeffrey.yasskin | 2008-01-27 06:40:35 +0100 (Sun, 27 Jan 2008) | 3 lines
Make rational.gcd() public and allow Rational to take decimal strings, per
Raymond's advice.
........
r60345 | neal.norwitz | 2008-01-27 08:36:03 +0100 (Sun, 27 Jan 2008) | 3 lines
Mostly reformat. Also set an error and return NULL if neither MS_WINDOWS
nor UNIX is defined. This may have caused problems on cygwin.
........
r60346 | neal.norwitz | 2008-01-27 08:37:38 +0100 (Sun, 27 Jan 2008) | 3 lines
Use int for the sign rather than a char. char can be signed or unsigned.
It's system dependent. This might fix the problem with test_rfind failing.
........
r60347 | neal.norwitz | 2008-01-27 08:41:33 +0100 (Sun, 27 Jan 2008) | 1 line
Add stdarg include for va_list to get this to compile on cygwin
........
r60348 | raymond.hettinger | 2008-01-27 11:13:57 +0100 (Sun, 27 Jan 2008) | 1 line
Docstring nit
........
r60349 | raymond.hettinger | 2008-01-27 11:47:55 +0100 (Sun, 27 Jan 2008) | 1 line
Removed an unnecessary and confusing paragraph from the namedtuple docs.
........
2008-01-27 11:18:18 -04:00
|
|
|
|
2003-05-13 23:18:31 -03:00
|
|
|
def test_basic(self):
|
|
|
|
# Simple test expected to pass.
|
2016-05-07 10:37:09 -03:00
|
|
|
with self.urlopen(self.url) as open_url:
|
2011-05-22 12:35:17 -03:00
|
|
|
for attr in ("read", "readline", "readlines", "fileno", "close",
|
|
|
|
"info", "geturl"):
|
|
|
|
self.assertTrue(hasattr(open_url, attr), "object returned from "
|
|
|
|
"urlopen lacks the %s attribute" % attr)
|
2009-06-30 20:06:06 -03:00
|
|
|
self.assertTrue(open_url.read(), "calling 'read' failed")
|
2003-05-13 23:18:31 -03:00
|
|
|
|
|
|
|
def test_readlines(self):
|
|
|
|
# Test both readline and readlines.
|
2016-05-07 10:37:09 -03:00
|
|
|
with self.urlopen(self.url) as open_url:
|
2010-01-24 15:26:24 -04:00
|
|
|
self.assertIsInstance(open_url.readline(), bytes,
|
|
|
|
"readline did not return a string")
|
|
|
|
self.assertIsInstance(open_url.readlines(), list,
|
|
|
|
"readlines did not return a list")
|
2003-05-13 23:18:31 -03:00
|
|
|
|
|
|
|
def test_info(self):
|
|
|
|
# Test 'info'.
|
2016-05-07 10:37:09 -03:00
|
|
|
with self.urlopen(self.url) as open_url:
|
2003-05-13 23:18:31 -03:00
|
|
|
info_obj = open_url.info()
|
2010-01-24 15:26:24 -04:00
|
|
|
self.assertIsInstance(info_obj, email.message.Message,
|
|
|
|
"object returned by 'info' is not an "
|
|
|
|
"instance of email.message.Message")
|
2008-06-12 01:06:45 -03:00
|
|
|
self.assertEqual(info_obj.get_content_subtype(), "html")
|
2003-05-13 23:18:31 -03:00
|
|
|
|
|
|
|
def test_geturl(self):
|
|
|
|
# Make sure same URL as opened is returned by geturl.
|
2016-05-07 10:37:09 -03:00
|
|
|
with self.urlopen(self.url) as open_url:
|
2003-05-13 23:18:31 -03:00
|
|
|
gotten_url = open_url.geturl()
|
2016-05-07 10:37:09 -03:00
|
|
|
self.assertEqual(gotten_url, self.url)
|
2003-05-13 23:18:31 -03:00
|
|
|
|
Merged revisions 60124-60142 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r60131 | georg.brandl | 2008-01-20 12:13:29 +0100 (Sun, 20 Jan 2008) | 3 lines
#1351692: in pprint, always call format() for dict and list items to enable
custom formatting of contents via subclassing PrettyPrinter.
........
r60133 | georg.brandl | 2008-01-20 12:43:03 +0100 (Sun, 20 Jan 2008) | 2 lines
#1178141: add addinfourl.code to get http status code from urllib.
........
r60134 | georg.brandl | 2008-01-20 13:05:43 +0100 (Sun, 20 Jan 2008) | 4 lines
#856047: respect the ``no_proxy`` env var when checking for proxies
in urllib and using the other ``_proxy`` env vars.
Original patch by Donovan Baarda.
........
r60135 | georg.brandl | 2008-01-20 13:18:17 +0100 (Sun, 20 Jan 2008) | 4 lines
#1664522: in urllib, don't read non-existing directories in ftp mode,
returning a 0-byte file -- raise an IOError instead.
Original patch from Phil Knirsch.
........
r60136 | georg.brandl | 2008-01-20 13:57:47 +0100 (Sun, 20 Jan 2008) | 2 lines
#799369: document possible sys.platform values.
........
r60137 | georg.brandl | 2008-01-20 14:08:37 +0100 (Sun, 20 Jan 2008) | 2 lines
#652749: document the constants added to the builtins by site.py.
........
r60138 | georg.brandl | 2008-01-20 14:59:46 +0100 (Sun, 20 Jan 2008) | 2 lines
#1648: add sys.gettrace() and sys.getprofile().
........
r60139 | georg.brandl | 2008-01-20 15:17:42 +0100 (Sun, 20 Jan 2008) | 2 lines
#1669: don't allow shutil.rmtree() to be called on a symlink.
........
r60140 | georg.brandl | 2008-01-20 15:20:02 +0100 (Sun, 20 Jan 2008) | 2 lines
Fix test_pyclbr after urllib change.
........
r60141 | christian.heimes | 2008-01-20 15:28:28 +0100 (Sun, 20 Jan 2008) | 1 line
Fixed a wrong assumption in configure.in and Include/pyport.h. The is finite function is not called isfinite() but finite(). Sorry, my fault. :)
........
r60142 | georg.brandl | 2008-01-20 15:31:27 +0100 (Sun, 20 Jan 2008) | 2 lines
#1876: fix typos in test_operator.
........
2008-01-20 11:14:11 -04:00
|
|
|
def test_getcode(self):
|
|
|
|
# test getcode() with the fancy opener to get 404 error codes
|
2016-05-07 10:37:09 -03:00
|
|
|
URL = self.url + "XXXinvalidXXX"
|
2020-04-29 04:36:20 -03:00
|
|
|
with socket_helper.transient_internet(URL):
|
2024-11-19 10:01:49 -04:00
|
|
|
with self.assertRaises(urllib.error.URLError) as e:
|
|
|
|
with urllib.request.urlopen(URL):
|
|
|
|
pass
|
|
|
|
self.assertEqual(e.exception.code, 404)
|
Merged revisions 60124-60142 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r60131 | georg.brandl | 2008-01-20 12:13:29 +0100 (Sun, 20 Jan 2008) | 3 lines
#1351692: in pprint, always call format() for dict and list items to enable
custom formatting of contents via subclassing PrettyPrinter.
........
r60133 | georg.brandl | 2008-01-20 12:43:03 +0100 (Sun, 20 Jan 2008) | 2 lines
#1178141: add addinfourl.code to get http status code from urllib.
........
r60134 | georg.brandl | 2008-01-20 13:05:43 +0100 (Sun, 20 Jan 2008) | 4 lines
#856047: respect the ``no_proxy`` env var when checking for proxies
in urllib and using the other ``_proxy`` env vars.
Original patch by Donovan Baarda.
........
r60135 | georg.brandl | 2008-01-20 13:18:17 +0100 (Sun, 20 Jan 2008) | 4 lines
#1664522: in urllib, don't read non-existing directories in ftp mode,
returning a 0-byte file -- raise an IOError instead.
Original patch from Phil Knirsch.
........
r60136 | georg.brandl | 2008-01-20 13:57:47 +0100 (Sun, 20 Jan 2008) | 2 lines
#799369: document possible sys.platform values.
........
r60137 | georg.brandl | 2008-01-20 14:08:37 +0100 (Sun, 20 Jan 2008) | 2 lines
#652749: document the constants added to the builtins by site.py.
........
r60138 | georg.brandl | 2008-01-20 14:59:46 +0100 (Sun, 20 Jan 2008) | 2 lines
#1648: add sys.gettrace() and sys.getprofile().
........
r60139 | georg.brandl | 2008-01-20 15:17:42 +0100 (Sun, 20 Jan 2008) | 2 lines
#1669: don't allow shutil.rmtree() to be called on a symlink.
........
r60140 | georg.brandl | 2008-01-20 15:20:02 +0100 (Sun, 20 Jan 2008) | 2 lines
Fix test_pyclbr after urllib change.
........
r60141 | christian.heimes | 2008-01-20 15:28:28 +0100 (Sun, 20 Jan 2008) | 1 line
Fixed a wrong assumption in configure.in and Include/pyport.h. The is finite function is not called isfinite() but finite(). Sorry, my fault. :)
........
r60142 | georg.brandl | 2008-01-20 15:31:27 +0100 (Sun, 20 Jan 2008) | 2 lines
#1876: fix typos in test_operator.
........
2008-01-20 11:14:11 -04:00
|
|
|
|
2023-09-05 11:56:30 -03:00
|
|
|
@support.requires_resource('walltime')
|
2003-05-13 23:18:31 -03:00
|
|
|
def test_bad_address(self):
|
|
|
|
# Make sure proper exception is raised when connecting to a bogus
|
|
|
|
# address.
|
2015-12-16 00:36:20 -04:00
|
|
|
|
|
|
|
# Given that both VeriSign and various ISPs have in
|
|
|
|
# the past or are presently hijacking various invalid
|
|
|
|
# domain name requests in an attempt to boost traffic
|
|
|
|
# to their own sites, finding a domain name to use
|
|
|
|
# for this test is difficult. RFC2606 leads one to
|
|
|
|
# believe that '.invalid' should work, but experience
|
|
|
|
# seemed to indicate otherwise. Single character
|
|
|
|
# TLDs are likely to remain invalid, so this seems to
|
|
|
|
# be the best choice. The trailing '.' prevents a
|
|
|
|
# related problem: The normal DNS resolver appends
|
|
|
|
# the domain names from the search path if there is
|
|
|
|
# no '.' the end and, and if one of those domains
|
|
|
|
# implements a '*' rule a result is returned.
|
|
|
|
# However, none of this will prevent the test from
|
|
|
|
# failing if the ISP hijacks all invalid domain
|
|
|
|
# requests. The real solution would be to be able to
|
|
|
|
# parameterize the framework with a mock resolver.
|
|
|
|
bogus_domain = "sadflkjsasf.i.nvali.d."
|
2011-07-08 14:19:57 -03:00
|
|
|
try:
|
|
|
|
socket.gethostbyname(bogus_domain)
|
2013-05-25 08:08:13 -03:00
|
|
|
except OSError:
|
|
|
|
# socket.gaierror is too narrow, since getaddrinfo() may also
|
|
|
|
# fail with EAI_SYSTEM and ETIMEDOUT (seen on Ubuntu 13.04),
|
|
|
|
# i.e. Python's TimeoutError.
|
2011-07-08 14:19:57 -03:00
|
|
|
pass
|
|
|
|
else:
|
|
|
|
# This happens with some overzealous DNS providers such as OpenDNS
|
|
|
|
self.skipTest("%r should not resolve for test to work" % bogus_domain)
|
2013-01-11 12:17:53 -04:00
|
|
|
failure_explanation = ('opening an invalid URL did not raise OSError; '
|
|
|
|
'can be caused by a broken DNS server '
|
|
|
|
'(e.g. returns 404 or hijacks page)')
|
|
|
|
with self.assertRaises(OSError, msg=failure_explanation):
|
2015-12-16 00:36:20 -04:00
|
|
|
urllib.request.urlopen("http://{}/".format(bogus_domain))
|
2003-05-13 23:18:31 -03:00
|
|
|
|
2011-05-22 12:35:17 -03:00
|
|
|
|
2003-05-13 23:18:31 -03:00
|
|
|
class urlretrieveNetworkTests(unittest.TestCase):
|
2008-06-18 17:49:58 -03:00
|
|
|
"""Tests urllib.request.urlretrieve using the network."""
|
2003-05-13 23:18:31 -03:00
|
|
|
|
2019-07-02 09:50:19 -03:00
|
|
|
def setUp(self):
|
|
|
|
# remove temporary files created by urlretrieve()
|
|
|
|
self.addCleanup(urllib.request.urlcleanup)
|
|
|
|
|
2011-05-22 12:35:17 -03:00
|
|
|
@contextlib.contextmanager
|
2012-11-10 17:43:44 -04:00
|
|
|
def urlretrieve(self, *args, **kwargs):
|
2010-10-17 07:52:12 -03:00
|
|
|
resource = args[0]
|
2020-04-29 04:36:20 -03:00
|
|
|
with socket_helper.transient_internet(resource):
|
2012-11-10 17:43:44 -04:00
|
|
|
file_location, info = urllib.request.urlretrieve(*args, **kwargs)
|
2011-05-22 12:35:17 -03:00
|
|
|
try:
|
|
|
|
yield file_location, info
|
|
|
|
finally:
|
2020-08-06 08:51:29 -03:00
|
|
|
os_helper.unlink(file_location)
|
Merged revisions 60284-60349 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r60286 | christian.heimes | 2008-01-25 15:54:23 +0100 (Fri, 25 Jan 2008) | 1 line
setup.py doesn't pick up changes to a header file
........
r60287 | christian.heimes | 2008-01-25 16:52:11 +0100 (Fri, 25 Jan 2008) | 2 lines
Added the Python core headers Include/*.h and pyconfig.h as dependencies for the extensions in Modules/
It forces a rebuild of all extensions when a header files has been modified
........
r60291 | raymond.hettinger | 2008-01-25 20:24:46 +0100 (Fri, 25 Jan 2008) | 4 lines
Changes 54857 and 54840 broke code and were reverted in Py2.5 just before
it was released, but that reversion never made it to the Py2.6 head.
........
r60296 | guido.van.rossum | 2008-01-25 20:50:26 +0100 (Fri, 25 Jan 2008) | 2 lines
Rewrite the list_inline_repeat overflow check slightly differently.
........
r60301 | thomas.wouters | 2008-01-25 22:09:34 +0100 (Fri, 25 Jan 2008) | 4 lines
Use the right (portable) definition of the max of a Py_ssize_t.
........
r60303 | thomas.wouters | 2008-01-26 02:47:05 +0100 (Sat, 26 Jan 2008) | 5 lines
Make 'testall' work again when building in a separate directory.
test_distutils still fails when doing that.
........
r60305 | neal.norwitz | 2008-01-26 06:54:48 +0100 (Sat, 26 Jan 2008) | 3 lines
Prevent this test from failing if there are transient network problems
by retrying the host for up to 3 times.
........
r60306 | neal.norwitz | 2008-01-26 08:26:12 +0100 (Sat, 26 Jan 2008) | 12 lines
Use a condition variable (threading.Event) rather than sleeps and checking a
global to determine when the server is ready to be used. This slows the test
down, but should make it correct. There was a race condition before where the
server could have assigned a port, yet it wasn't ready to serve requests. If
the client sent a request before the server was completely ready, it would get
an exception. There was machinery to try to handle this condition. All of
that should be unnecessary and removed if this change works. A NOTE was
added as a comment about what needs to be fixed.
The buildbots will tell us if there are more errors or
if this test is now stable.
........
r60307 | neal.norwitz | 2008-01-26 08:38:03 +0100 (Sat, 26 Jan 2008) | 3 lines
Fix exception in tearDown on ppc buildbot. If there's no directory,
that shouldn't cause the test to fail. Just like it setUp.
........
r60308 | raymond.hettinger | 2008-01-26 09:19:06 +0100 (Sat, 26 Jan 2008) | 3 lines
Make PySet_Add() work with frozensets. Works like PyTuple_SetItem() to build-up values in a brand new frozenset.
........
r60309 | neal.norwitz | 2008-01-26 09:26:00 +0100 (Sat, 26 Jan 2008) | 1 line
The OS X buildbot had errors with the unavailable exceptions disabled. Restore it.
........
r60310 | raymond.hettinger | 2008-01-26 09:37:28 +0100 (Sat, 26 Jan 2008) | 4 lines
Let marshal build-up sets and frozensets one element at a time.
Saves the unnecessary creation of a tuple as intermediate container.
........
r60311 | raymond.hettinger | 2008-01-26 09:41:13 +0100 (Sat, 26 Jan 2008) | 1 line
Update test code for change to PySet_Add().
........
r60312 | raymond.hettinger | 2008-01-26 10:31:11 +0100 (Sat, 26 Jan 2008) | 1 line
Revert PySet_Add() changes.
........
r60314 | georg.brandl | 2008-01-26 10:43:35 +0100 (Sat, 26 Jan 2008) | 2 lines
#1934: fix os.path.isabs docs.
........
r60316 | georg.brandl | 2008-01-26 12:00:18 +0100 (Sat, 26 Jan 2008) | 2 lines
Add missing things in re docstring.
........
r60317 | georg.brandl | 2008-01-26 12:02:22 +0100 (Sat, 26 Jan 2008) | 2 lines
Slashes allowed on Windows.
........
r60319 | georg.brandl | 2008-01-26 14:41:21 +0100 (Sat, 26 Jan 2008) | 2 lines
Fix markup again.
........
r60320 | andrew.kuchling | 2008-01-26 14:50:51 +0100 (Sat, 26 Jan 2008) | 1 line
Add some items
........
r60321 | georg.brandl | 2008-01-26 15:02:38 +0100 (Sat, 26 Jan 2008) | 2 lines
Clarify "b" mode under Unix.
........
r60322 | georg.brandl | 2008-01-26 15:03:47 +0100 (Sat, 26 Jan 2008) | 3 lines
#1940: make it possible to use curses.filter() before curses.initscr()
as the documentation says.
........
r60324 | georg.brandl | 2008-01-26 15:14:20 +0100 (Sat, 26 Jan 2008) | 3 lines
#1473257: add generator.gi_code attribute that refers to
the original code object backing the generator. Patch by Collin Winter.
........
r60325 | georg.brandl | 2008-01-26 15:19:22 +0100 (Sat, 26 Jan 2008) | 2 lines
Move C API entries to the corresponding section.
........
r60326 | christian.heimes | 2008-01-26 17:43:35 +0100 (Sat, 26 Jan 2008) | 1 line
Unit test fix from Giampaolo Rodola, #1938
........
r60327 | gregory.p.smith | 2008-01-26 19:51:05 +0100 (Sat, 26 Jan 2008) | 2 lines
Update docs for new callpack params added in r60188
........
r60329 | neal.norwitz | 2008-01-26 21:24:36 +0100 (Sat, 26 Jan 2008) | 3 lines
Cleanup the code a bit. test_rfind is failing on PPC and PPC64 buildbots,
this might fix the problem.
........
r60330 | neal.norwitz | 2008-01-26 22:02:45 +0100 (Sat, 26 Jan 2008) | 1 line
Always try to remove the test file even if close raises an exception
........
r60331 | neal.norwitz | 2008-01-26 22:21:59 +0100 (Sat, 26 Jan 2008) | 3 lines
Reduce the race condition by signalling when the server is ready
and not trying to connect before.
........
r60334 | neal.norwitz | 2008-01-27 00:13:46 +0100 (Sun, 27 Jan 2008) | 5 lines
On some systems (e.g., Ubuntu on hppa) the flush()
doesn't cause the exception, but the close() does.
Will backport.
........
r60335 | neal.norwitz | 2008-01-27 00:14:17 +0100 (Sun, 27 Jan 2008) | 2 lines
Consistently use tempfile.tempdir for the db_home directory.
........
r60338 | neal.norwitz | 2008-01-27 02:44:05 +0100 (Sun, 27 Jan 2008) | 4 lines
Eliminate the sleeps that assume the server will start in .5 seconds.
This should make the test less flaky. It also speeds up the test
by about 75% on my box (20+ seconds -> ~4 seconds).
........
r60342 | neal.norwitz | 2008-01-27 06:02:34 +0100 (Sun, 27 Jan 2008) | 6 lines
Try to prevent this test from being flaky. We might need a sleep in here
which isn't as bad as it sounds. The close() *should* raise an exception,
so if it didn't we should give more time to sync and really raise it.
Will backport.
........
r60344 | jeffrey.yasskin | 2008-01-27 06:40:35 +0100 (Sun, 27 Jan 2008) | 3 lines
Make rational.gcd() public and allow Rational to take decimal strings, per
Raymond's advice.
........
r60345 | neal.norwitz | 2008-01-27 08:36:03 +0100 (Sun, 27 Jan 2008) | 3 lines
Mostly reformat. Also set an error and return NULL if neither MS_WINDOWS
nor UNIX is defined. This may have caused problems on cygwin.
........
r60346 | neal.norwitz | 2008-01-27 08:37:38 +0100 (Sun, 27 Jan 2008) | 3 lines
Use int for the sign rather than a char. char can be signed or unsigned.
It's system dependent. This might fix the problem with test_rfind failing.
........
r60347 | neal.norwitz | 2008-01-27 08:41:33 +0100 (Sun, 27 Jan 2008) | 1 line
Add stdarg include for va_list to get this to compile on cygwin
........
r60348 | raymond.hettinger | 2008-01-27 11:13:57 +0100 (Sun, 27 Jan 2008) | 1 line
Docstring nit
........
r60349 | raymond.hettinger | 2008-01-27 11:47:55 +0100 (Sun, 27 Jan 2008) | 1 line
Removed an unnecessary and confusing paragraph from the namedtuple docs.
........
2008-01-27 11:18:18 -04:00
|
|
|
|
2003-05-13 23:18:31 -03:00
|
|
|
def test_basic(self):
|
|
|
|
# Test basic functionality.
|
2016-05-07 10:37:09 -03:00
|
|
|
with self.urlretrieve(self.logo) as (file_location, info):
|
2011-05-22 12:35:17 -03:00
|
|
|
self.assertTrue(os.path.exists(file_location), "file location returned by"
|
|
|
|
" urlretrieve is not a valid path")
|
2014-02-20 00:06:41 -04:00
|
|
|
with open(file_location, 'rb') as f:
|
2011-05-22 12:35:17 -03:00
|
|
|
self.assertTrue(f.read(), "reading from the file location returned"
|
|
|
|
" by urlretrieve failed")
|
2003-05-13 23:18:31 -03:00
|
|
|
|
|
|
|
def test_specified_path(self):
|
|
|
|
# Make sure that specifying the location of the file to write to works.
|
2016-05-07 10:37:09 -03:00
|
|
|
with self.urlretrieve(self.logo,
|
2020-08-06 08:51:29 -03:00
|
|
|
os_helper.TESTFN) as (file_location, info):
|
|
|
|
self.assertEqual(file_location, os_helper.TESTFN)
|
2011-05-22 12:35:17 -03:00
|
|
|
self.assertTrue(os.path.exists(file_location))
|
2014-02-20 00:06:41 -04:00
|
|
|
with open(file_location, 'rb') as f:
|
2011-05-22 12:35:17 -03:00
|
|
|
self.assertTrue(f.read(), "reading from temporary file failed")
|
2003-05-13 23:18:31 -03:00
|
|
|
|
|
|
|
def test_header(self):
|
|
|
|
# Make sure header returned as 2nd value from urlretrieve is good.
|
2016-05-07 10:37:09 -03:00
|
|
|
with self.urlretrieve(self.logo) as (file_location, info):
|
2011-05-22 12:35:17 -03:00
|
|
|
self.assertIsInstance(info, email.message.Message,
|
|
|
|
"info is not an instance of email.message.Message")
|
2003-05-16 12:35:10 -03:00
|
|
|
|
2016-05-07 10:37:09 -03:00
|
|
|
logo = "http://www.pythontest.net/"
|
2012-11-10 17:43:44 -04:00
|
|
|
|
2023-09-05 11:56:30 -03:00
|
|
|
@support.requires_resource('walltime')
|
2010-05-01 05:29:18 -03:00
|
|
|
def test_data_header(self):
|
2012-11-10 17:43:44 -04:00
|
|
|
with self.urlretrieve(self.logo) as (file_location, fileheaders):
|
2011-05-22 12:35:17 -03:00
|
|
|
datevalue = fileheaders.get('Date')
|
|
|
|
dateformat = '%a, %d %b %Y %H:%M:%S GMT'
|
|
|
|
try:
|
|
|
|
time.strptime(datevalue, dateformat)
|
|
|
|
except ValueError:
|
2016-05-07 10:58:41 -03:00
|
|
|
self.fail('Date value not in %r format' % dateformat)
|
2003-05-13 23:18:31 -03:00
|
|
|
|
2012-11-10 17:43:44 -04:00
|
|
|
def test_reporthook(self):
|
|
|
|
records = []
|
2017-05-16 03:08:07 -03:00
|
|
|
|
2012-11-10 17:43:44 -04:00
|
|
|
def recording_reporthook(blocks, block_size, total_size):
|
|
|
|
records.append((blocks, block_size, total_size))
|
|
|
|
|
|
|
|
with self.urlretrieve(self.logo, reporthook=recording_reporthook) as (
|
|
|
|
file_location, fileheaders):
|
|
|
|
expected_size = int(fileheaders['Content-Length'])
|
|
|
|
|
|
|
|
records_repr = repr(records) # For use in error messages.
|
|
|
|
self.assertGreater(len(records), 1, msg="There should always be two "
|
|
|
|
"calls; the first one before the transfer starts.")
|
|
|
|
self.assertEqual(records[0][0], 0)
|
|
|
|
self.assertGreater(records[0][1], 0,
|
|
|
|
msg="block size can't be 0 in %s" % records_repr)
|
|
|
|
self.assertEqual(records[0][2], expected_size)
|
|
|
|
self.assertEqual(records[-1][2], expected_size)
|
|
|
|
|
|
|
|
block_sizes = {block_size for _, block_size, _ in records}
|
|
|
|
self.assertEqual({records[0][1]}, block_sizes,
|
|
|
|
msg="block sizes in %s must be equal" % records_repr)
|
|
|
|
self.assertGreaterEqual(records[-1][0]*records[0][1], expected_size,
|
|
|
|
msg="number of blocks * block size must be"
|
|
|
|
" >= total size in %s" % records_repr)
|
|
|
|
|
2003-05-13 23:18:31 -03:00
|
|
|
|
2003-03-30 00:54:24 -04:00
|
|
|
if __name__ == "__main__":
|
2014-04-14 22:31:41 -03:00
|
|
|
unittest.main()
|