summaryrefslogtreecommitdiff
path: root/Lib/test
diff options
context:
space:
mode:
authorChristian Heimes <christian@cheimes.de>2008-01-25 11:23:10 +0000
committerChristian Heimes <christian@cheimes.de>2008-01-25 11:23:10 +0000
commit969fe57baa0eb80332990f9cda936a33e13fabef (patch)
tree050188e2c28aa859b072b1a2e860f268a2534a42 /Lib/test
parentbad17e7b176334a3b1dc92a4e496e1f8f4ff34a7 (diff)
downloadcpython-git-969fe57baa0eb80332990f9cda936a33e13fabef.tar.gz
Merged revisions 60245-60277 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk ........ r60246 | guido.van.rossum | 2008-01-24 18:58:05 +0100 (Thu, 24 Jan 2008) | 2 lines Fix test67.py from issue #1303614. ........ r60248 | raymond.hettinger | 2008-01-24 19:05:54 +0100 (Thu, 24 Jan 2008) | 1 line Clean-up and speed-up code by accessing numerator/denominator directly. There's no reason to enforce readonliness ........ r60249 | raymond.hettinger | 2008-01-24 19:12:23 +0100 (Thu, 24 Jan 2008) | 1 line Revert 60189 and restore performance. ........ r60250 | guido.van.rossum | 2008-01-24 19:21:02 +0100 (Thu, 24 Jan 2008) | 5 lines News about recently fixed crashers: - A few crashers fixed: weakref_in_del.py (issue #1377858); loosing_dict_ref.py (issue #1303614, test67.py); borrowed_ref_[34].py (not in tracker). ........ r60252 | thomas.heller | 2008-01-24 19:36:27 +0100 (Thu, 24 Jan 2008) | 7 lines Use a PyDictObject again for the array type cache; retrieving items from the WeakValueDictionary was slower by nearly a factor of 3. To avoid leaks, weakref proxies for the array types are put into the cache dict, with weakref callbacks that removes the entries when the type goes away. ........ r60253 | thomas.heller | 2008-01-24 19:54:12 +0100 (Thu, 24 Jan 2008) | 2 lines Replace Py_BuildValue with PyTuple_Pack because it is faster. Also add a missing DECREF. ........ r60254 | raymond.hettinger | 2008-01-24 20:05:29 +0100 (Thu, 24 Jan 2008) | 1 line Add support for trunc(). ........ r60255 | thomas.heller | 2008-01-24 20:15:02 +0100 (Thu, 24 Jan 2008) | 5 lines Invert the checks in get_[u]long and get_[u]longlong. The intent was to not accept float types; the result was that integer-like objects were not accepted. Ported from release25-maint. ........ r60256 | raymond.hettinger | 2008-01-24 20:30:19 +0100 (Thu, 24 Jan 2008) | 1 line Add support for int(r) just like the other numeric classes. ........ r60263 | raymond.hettinger | 2008-01-24 22:23:58 +0100 (Thu, 24 Jan 2008) | 1 line Expand tests to include nested graph structures. ........ r60264 | raymond.hettinger | 2008-01-24 22:47:56 +0100 (Thu, 24 Jan 2008) | 1 line Shorter pprint's for empty sets and frozensets. Fix indentation of frozensets. Add tests including two complex data structures. ........ r60265 | amaury.forgeotdarc | 2008-01-24 23:51:18 +0100 (Thu, 24 Jan 2008) | 14 lines #1920: when considering a block starting by "while 0", the compiler optimized the whole construct away, even when an 'else' clause is present:: while 0: print("no") else: print("yes") did not generate any code at all. Now the compiler emits the 'else' block, like it already does for 'if' statements. Will backport. ........ r60266 | amaury.forgeotdarc | 2008-01-24 23:59:25 +0100 (Thu, 24 Jan 2008) | 2 lines News entry for r60265 (Issue 1920). ........ r60269 | raymond.hettinger | 2008-01-25 00:50:26 +0100 (Fri, 25 Jan 2008) | 1 line More code cleanup. Remove unnecessary indirection to useless class methods. ........ r60270 | raymond.hettinger | 2008-01-25 01:21:54 +0100 (Fri, 25 Jan 2008) | 1 line Add support for copy, deepcopy, and pickle. ........ r60271 | raymond.hettinger | 2008-01-25 01:33:45 +0100 (Fri, 25 Jan 2008) | 1 line Mark todos and review comments. ........ r60272 | raymond.hettinger | 2008-01-25 02:13:12 +0100 (Fri, 25 Jan 2008) | 1 line Add one other review comment. ........ r60273 | raymond.hettinger | 2008-01-25 02:23:38 +0100 (Fri, 25 Jan 2008) | 1 line Fix-up signature for approximation. ........ r60274 | raymond.hettinger | 2008-01-25 02:46:33 +0100 (Fri, 25 Jan 2008) | 1 line More design notes ........ r60276 | neal.norwitz | 2008-01-25 07:37:23 +0100 (Fri, 25 Jan 2008) | 6 lines Make the test more robust by trying to reconnect up to 3 times in case there were transient failures. This will hopefully silence the buildbots for this test. As we find other tests that have a problem, we can fix with a similar strategy assuming it is successful. It worked on my box in a loop for 10+ runs where it would have an exception otherwise. ........ r60277 | neal.norwitz | 2008-01-25 09:04:16 +0100 (Fri, 25 Jan 2008) | 4 lines Add prototypes to get the mathmodule.c to compile on OSF1 5.1 (Tru64) and eliminate a compiler warning in floatobject.c. There might be a better way to go about this, but it should be good enough for now. ........
Diffstat (limited to 'Lib/test')
-rw-r--r--Lib/test/crashers/loosing_dict_ref.py21
-rw-r--r--Lib/test/test_decimal.py11
-rw-r--r--Lib/test/test_descr.py24
-rw-r--r--Lib/test/test_grammar.py9
-rw-r--r--Lib/test/test_pprint.py192
-rw-r--r--Lib/test/test_rational.py17
-rw-r--r--Lib/test/test_set.py106
-rw-r--r--Lib/test/test_urllib2net.py60
8 files changed, 392 insertions, 48 deletions
diff --git a/Lib/test/crashers/loosing_dict_ref.py b/Lib/test/crashers/loosing_dict_ref.py
deleted file mode 100644
index f44370b9a5..0000000000
--- a/Lib/test/crashers/loosing_dict_ref.py
+++ /dev/null
@@ -1,21 +0,0 @@
-
-# http://python.org/sf/1303614
-
-class Strange(object):
- def __hash__(self):
- return hash('hello')
-
- def __eq__(self, other):
- x.__dict__ = {} # the old x.__dict__ is deallocated
- return False
-
-
-class X(object):
- pass
-
-if __name__ == '__main__':
- v = 123
- x = X()
- x.__dict__ = {Strange(): 42,
- 'hello': v+456}
- x.hello # segfault: the above dict is accessed after it's deallocated
diff --git a/Lib/test/test_decimal.py b/Lib/test/test_decimal.py
index 83fb337268..1d3765ab05 100644
--- a/Lib/test/test_decimal.py
+++ b/Lib/test/test_decimal.py
@@ -1141,6 +1141,7 @@ class DecimalUsabilityTest(unittest.TestCase):
checkSameDec("__floordiv__", True)
checkSameDec("__hash__")
checkSameDec("__int__")
+ checkSameDec("__trunc__")
checkSameDec("__mod__", True)
checkSameDec("__mul__", True)
checkSameDec("__neg__")
@@ -1204,6 +1205,16 @@ class DecimalPythonAPItests(unittest.TestCase):
r = d.to_integral(ROUND_DOWN)
self.assertEqual(Decimal(int(d)), r)
+ def test_trunc(self):
+ for x in range(-250, 250):
+ s = '%0.2f' % (x / 100.0)
+ # should work the same as for floats
+ self.assertEqual(int(Decimal(s)), int(float(s)))
+ # should work the same as to_integral in the ROUND_DOWN mode
+ d = Decimal(s)
+ r = d.to_integral(ROUND_DOWN)
+ self.assertEqual(Decimal(trunc(d)), r)
+
class ContextAPItests(unittest.TestCase):
def test_pickle(self):
diff --git a/Lib/test/test_descr.py b/Lib/test/test_descr.py
index 028be3dbc1..8670ff9ccf 100644
--- a/Lib/test/test_descr.py
+++ b/Lib/test/test_descr.py
@@ -4248,6 +4248,29 @@ def test_borrowed_ref_4_segfault():
finally:
builtins.__import__ = orig_import
+def test_losing_dict_ref_segfault():
+ # This used to segfault;
+ # derived from issue #1303614, test67.py
+ if verbose:
+ print("Testing losing dict ref segfault...")
+
+ class Strange(object):
+ def __hash__(self):
+ return hash('hello')
+
+ def __eq__(self, other):
+ x.__dict__ = {} # the old x.__dict__ is deallocated
+ return False
+
+ class X(object):
+ pass
+
+ v = 123
+ x = X()
+ x.__dict__ = {Strange(): 42, 'hello': v+456}
+ x.hello
+
+
def test_main():
weakref_segfault() # Must be first, somehow
wrapper_segfault() # NB This one is slow
@@ -4348,6 +4371,7 @@ def test_main():
test_weakref_in_del_segfault()
test_borrowed_ref_3_segfault()
test_borrowed_ref_4_segfault()
+ test_losing_dict_ref_segfault()
if verbose: print("All OK")
diff --git a/Lib/test/test_grammar.py b/Lib/test/test_grammar.py
index 0777307b9f..04aedd536e 100644
--- a/Lib/test/test_grammar.py
+++ b/Lib/test/test_grammar.py
@@ -500,6 +500,15 @@ class GrammarTests(unittest.TestCase):
while 0: pass
else: pass
+ # Issue1920: "while 0" is optimized away,
+ # ensure that the "else" clause is still present.
+ x = 0
+ while 0:
+ x = 1
+ else:
+ x = 2
+ self.assertEquals(x, 2)
+
def testFor(self):
# 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite]
for i in 1, 2, 3: pass
diff --git a/Lib/test/test_pprint.py b/Lib/test/test_pprint.py
index 869890714d..8d75d58fe6 100644
--- a/Lib/test/test_pprint.py
+++ b/Lib/test/test_pprint.py
@@ -1,6 +1,7 @@
import pprint
import test.test_support
import unittest
+import test.test_set
# list, tuple and dict subclasses that do or don't overwrite __repr__
class list2(list):
@@ -189,6 +190,197 @@ class QueryTestCase(unittest.TestCase):
others.should.not.be: like.this}"""
self.assertEqual(DottedPrettyPrinter().pformat(o), exp)
+ def test_set_reprs(self):
+ self.assertEqual(pprint.pformat(set()), 'set()')
+ self.assertEqual(pprint.pformat(set(range(3))), '{0, 1, 2}')
+ self.assertEqual(pprint.pformat(frozenset()), 'frozenset()')
+ self.assertEqual(pprint.pformat(frozenset(range(3))), 'frozenset({0, 1, 2})')
+ cube_repr_tgt = """\
+{frozenset(): frozenset({frozenset({2}), frozenset({0}), frozenset({1})}),
+ frozenset({0}): frozenset([frozenset(),
+ frozenset({0, 2}),
+ frozenset({0, 1})]),
+ frozenset({1}): frozenset([frozenset(),
+ frozenset({1, 2}),
+ frozenset({0, 1})]),
+ frozenset({2}): frozenset([frozenset(),
+ frozenset({1, 2}),
+ frozenset({0, 2})]),
+ frozenset({1, 2}): frozenset([frozenset({2}),
+ frozenset({1}),
+ frozenset({0, 1, 2})]),
+ frozenset({0, 2}): frozenset([frozenset({2}),
+ frozenset({0}),
+ frozenset({0, 1, 2})]),
+ frozenset({0, 1}): frozenset([frozenset({0}),
+ frozenset({1}),
+ frozenset({0, 1, 2})]),
+ frozenset({0, 1, 2}): frozenset([frozenset({1, 2}),
+ frozenset({0, 2}),
+ frozenset({0, 1})])}"""
+ cube = test.test_set.cube(3)
+ self.assertEqual(pprint.pformat(cube), cube_repr_tgt)
+ cubo_repr_tgt = """\
+{frozenset({frozenset({0, 2}), frozenset({0})}): frozenset([frozenset([frozenset([0,
+ 2]),
+ frozenset([0,
+ 1,
+ 2])]),
+ frozenset([frozenset([0]),
+ frozenset([0,
+ 1])]),
+ frozenset([frozenset(),
+ frozenset([0])]),
+ frozenset([frozenset([2]),
+ frozenset([0,
+ 2])])]),
+ frozenset({frozenset({0, 1}), frozenset({1})}): frozenset([frozenset([frozenset([0,
+ 1]),
+ frozenset([0,
+ 1,
+ 2])]),
+ frozenset([frozenset([0]),
+ frozenset([0,
+ 1])]),
+ frozenset([frozenset([1]),
+ frozenset([1,
+ 2])]),
+ frozenset([frozenset(),
+ frozenset([1])])]),
+ frozenset({frozenset({1, 2}), frozenset({1})}): frozenset([frozenset([frozenset([1,
+ 2]),
+ frozenset([0,
+ 1,
+ 2])]),
+ frozenset([frozenset([2]),
+ frozenset([1,
+ 2])]),
+ frozenset([frozenset(),
+ frozenset([1])]),
+ frozenset([frozenset([1]),
+ frozenset([0,
+ 1])])]),
+ frozenset({frozenset({1, 2}), frozenset({2})}): frozenset([frozenset([frozenset([1,
+ 2]),
+ frozenset([0,
+ 1,
+ 2])]),
+ frozenset([frozenset([1]),
+ frozenset([1,
+ 2])]),
+ frozenset([frozenset([2]),
+ frozenset([0,
+ 2])]),
+ frozenset([frozenset(),
+ frozenset([2])])]),
+ frozenset({frozenset(), frozenset({0})}): frozenset([frozenset([frozenset([0]),
+ frozenset([0,
+ 1])]),
+ frozenset([frozenset([0]),
+ frozenset([0,
+ 2])]),
+ frozenset([frozenset(),
+ frozenset([1])]),
+ frozenset([frozenset(),
+ frozenset([2])])]),
+ frozenset({frozenset(), frozenset({1})}): frozenset([frozenset([frozenset(),
+ frozenset([0])]),
+ frozenset([frozenset([1]),
+ frozenset([1,
+ 2])]),
+ frozenset([frozenset(),
+ frozenset([2])]),
+ frozenset([frozenset([1]),
+ frozenset([0,
+ 1])])]),
+ frozenset({frozenset({2}), frozenset()}): frozenset([frozenset([frozenset([2]),
+ frozenset([1,
+ 2])]),
+ frozenset([frozenset(),
+ frozenset([0])]),
+ frozenset([frozenset(),
+ frozenset([1])]),
+ frozenset([frozenset([2]),
+ frozenset([0,
+ 2])])]),
+ frozenset({frozenset({0, 1, 2}), frozenset({0, 1})}): frozenset([frozenset([frozenset([1,
+ 2]),
+ frozenset([0,
+ 1,
+ 2])]),
+ frozenset([frozenset([0,
+ 2]),
+ frozenset([0,
+ 1,
+ 2])]),
+ frozenset([frozenset([0]),
+ frozenset([0,
+ 1])]),
+ frozenset([frozenset([1]),
+ frozenset([0,
+ 1])])]),
+ frozenset({frozenset({0}), frozenset({0, 1})}): frozenset([frozenset([frozenset(),
+ frozenset([0])]),
+ frozenset([frozenset([0,
+ 1]),
+ frozenset([0,
+ 1,
+ 2])]),
+ frozenset([frozenset([0]),
+ frozenset([0,
+ 2])]),
+ frozenset([frozenset([1]),
+ frozenset([0,
+ 1])])]),
+ frozenset({frozenset({2}), frozenset({0, 2})}): frozenset([frozenset([frozenset([0,
+ 2]),
+ frozenset([0,
+ 1,
+ 2])]),
+ frozenset([frozenset([2]),
+ frozenset([1,
+ 2])]),
+ frozenset([frozenset([0]),
+ frozenset([0,
+ 2])]),
+ frozenset([frozenset(),
+ frozenset([2])])]),
+ frozenset({frozenset({0, 1, 2}), frozenset({0, 2})}): frozenset([frozenset([frozenset([1,
+ 2]),
+ frozenset([0,
+ 1,
+ 2])]),
+ frozenset([frozenset([0,
+ 1]),
+ frozenset([0,
+ 1,
+ 2])]),
+ frozenset([frozenset([0]),
+ frozenset([0,
+ 2])]),
+ frozenset([frozenset([2]),
+ frozenset([0,
+ 2])])]),
+ frozenset({frozenset({1, 2}), frozenset({0, 1, 2})}): frozenset([frozenset([frozenset([0,
+ 2]),
+ frozenset([0,
+ 1,
+ 2])]),
+ frozenset([frozenset([0,
+ 1]),
+ frozenset([0,
+ 1,
+ 2])]),
+ frozenset([frozenset([2]),
+ frozenset([1,
+ 2])]),
+ frozenset([frozenset([1]),
+ frozenset([1,
+ 2])])])}"""
+
+ cubo = test.test_set.linegraph(cube)
+ self.assertEqual(pprint.pformat(cubo), cubo_repr_tgt)
+
class DottedPrettyPrinter(pprint.PrettyPrinter):
diff --git a/Lib/test/test_rational.py b/Lib/test/test_rational.py
index 1bd18142c6..49868dd2cc 100644
--- a/Lib/test/test_rational.py
+++ b/Lib/test/test_rational.py
@@ -6,6 +6,8 @@ import math
import operator
import rational
import unittest
+from copy import copy, deepcopy
+from cPickle import dumps, loads
R = rational.Rational
def _components(r):
@@ -153,16 +155,17 @@ class RationalTest(unittest.TestCase):
[-4, 1, 6, 15, 1, 292, 1, 1, 1, 2, 1, 3, 1, 14, 3, 3])
self.assertEqual(R(0).as_continued_fraction(), [0])
- def testApproximateFromFloat(self):
- self.assertEqual(R.approximate_from_float(math.pi, 10000), R(355, 113))
- self.assertEqual(R.approximate_from_float(-math.pi, 10000), R(-355, 113))
- self.assertEqual(R.approximate_from_float(0.0, 10000), R(0))
+ def testApproximateFrom(self):
+ self.assertEqual(R.from_float(math.pi).approximate(10000), R(355, 113))
+ self.assertEqual(R.from_float(-math.pi).approximate(10000), R(-355, 113))
+ self.assertEqual(R.from_float(0.0).approximate(10000), R(0))
def testConversions(self):
self.assertTypedEquals(-1, trunc(R(-11, 10)))
self.assertTypedEquals(-2, math.floor(R(-11, 10)))
self.assertTypedEquals(-1, math.ceil(R(-11, 10)))
self.assertTypedEquals(-1, math.ceil(R(-10, 10)))
+ self.assertTypedEquals(-1, int(R(-11, 10)))
self.assertTypedEquals(0, round(R(-1, 10)))
self.assertTypedEquals(0, round(R(-5, 10)))
@@ -360,6 +363,12 @@ class RationalTest(unittest.TestCase):
s += num / fact * sign
self.assertAlmostEquals(math.cos(1), s)
+ def test_copy_deepcopy_pickle(self):
+ r = R(13, 7)
+ self.assertEqual(r, loads(dumps(r)))
+ self.assertEqual(id(r), id(copy(r)))
+ self.assertEqual(id(r), id(deepcopy(r)))
+
def test_main():
run_unittest(RationalTest)
diff --git a/Lib/test/test_set.py b/Lib/test/test_set.py
index e761f19c1e..9e3d64ff23 100644
--- a/Lib/test/test_set.py
+++ b/Lib/test/test_set.py
@@ -8,6 +8,7 @@ import os
from random import randrange, shuffle
import sys
import warnings
+import collections
class PassThru(Exception):
pass
@@ -1605,6 +1606,110 @@ class TestVariousIteratorArgs(unittest.TestCase):
self.assertRaises(TypeError, getattr(set('january'), methname), N(data))
self.assertRaises(ZeroDivisionError, getattr(set('january'), methname), E(data))
+# Application tests (based on David Eppstein's graph recipes ====================================
+
+def powerset(U):
+ """Generates all subsets of a set or sequence U."""
+ U = iter(U)
+ try:
+ x = frozenset([next(U)])
+ for S in powerset(U):
+ yield S
+ yield S | x
+ except StopIteration:
+ yield frozenset()
+
+def cube(n):
+ """Graph of n-dimensional hypercube."""
+ singletons = [frozenset([x]) for x in range(n)]
+ return dict([(x, frozenset([x^s for s in singletons]))
+ for x in powerset(range(n))])
+
+def linegraph(G):
+ """Graph, the vertices of which are edges of G,
+ with two vertices being adjacent iff the corresponding
+ edges share a vertex."""
+ L = {}
+ for x in G:
+ for y in G[x]:
+ nx = [frozenset([x,z]) for z in G[x] if z != y]
+ ny = [frozenset([y,z]) for z in G[y] if z != x]
+ L[frozenset([x,y])] = frozenset(nx+ny)
+ return L
+
+def faces(G):
+ 'Return a set of faces in G. Where a face is a set of vertices on that face'
+ # currently limited to triangles,squares, and pentagons
+ f = set()
+ for v1, edges in G.items():
+ for v2 in edges:
+ for v3 in G[v2]:
+ if v1 == v3:
+ continue
+ if v1 in G[v3]:
+ f.add(frozenset([v1, v2, v3]))
+ else:
+ for v4 in G[v3]:
+ if v4 == v2:
+ continue
+ if v1 in G[v4]:
+ f.add(frozenset([v1, v2, v3, v4]))
+ else:
+ for v5 in G[v4]:
+ if v5 == v3 or v5 == v2:
+ continue
+ if v1 in G[v5]:
+ f.add(frozenset([v1, v2, v3, v4, v5]))
+ return f
+
+
+class TestGraphs(unittest.TestCase):
+
+ def test_cube(self):
+
+ g = cube(3) # vert --> {v1, v2, v3}
+ vertices1 = set(g)
+ self.assertEqual(len(vertices1), 8) # eight vertices
+ for edge in g.values():
+ self.assertEqual(len(edge), 3) # each vertex connects to three edges
+ vertices2 = set(v for edges in g.values() for v in edges)
+ self.assertEqual(vertices1, vertices2) # edge vertices in original set
+
+ cubefaces = faces(g)
+ self.assertEqual(len(cubefaces), 6) # six faces
+ for face in cubefaces:
+ self.assertEqual(len(face), 4) # each face is a square
+
+ def test_cuboctahedron(self):
+
+ # http://en.wikipedia.org/wiki/Cuboctahedron
+ # 8 triangular faces and 6 square faces
+ # 12 indentical vertices each connecting a triangle and square
+
+ g = cube(3)
+ cuboctahedron = linegraph(g) # V( --> {V1, V2, V3, V4}
+ self.assertEqual(len(cuboctahedron), 12)# twelve vertices
+
+ vertices = set(cuboctahedron)
+ for edges in cuboctahedron.values():
+ self.assertEqual(len(edges), 4) # each vertex connects to four other vertices
+ othervertices = set(edge for edges in cuboctahedron.values() for edge in edges)
+ self.assertEqual(vertices, othervertices) # edge vertices in original set
+
+ cubofaces = faces(cuboctahedron)
+ facesizes = collections.defaultdict(int)
+ for face in cubofaces:
+ facesizes[len(face)] += 1
+ self.assertEqual(facesizes[3], 8) # eight triangular faces
+ self.assertEqual(facesizes[4], 6) # six square faces
+
+ for vertex in cuboctahedron:
+ edge = vertex # Cuboctahedron vertices are edges in Cube
+ self.assertEqual(len(edge), 2) # Two cube vertices define an edge
+ for cubevert in edge:
+ self.assert_(cubevert in g)
+
+
#==============================================================================
def test_main(verbose=None):
@@ -1644,6 +1749,7 @@ def test_main(verbose=None):
TestCopyingNested,
TestIdentities,
TestVariousIteratorArgs,
+ TestGraphs,
)
test_support.run_unittest(*test_classes)
diff --git a/Lib/test/test_urllib2net.py b/Lib/test/test_urllib2net.py
index fae7e4d2bf..109dd1b9c5 100644
--- a/Lib/test/test_urllib2net.py
+++ b/Lib/test/test_urllib2net.py
@@ -10,6 +10,20 @@ import sys
import os
import mimetools
+
+def _urlopen_with_retry(host, *args, **kwargs):
+ # Connecting to remote hosts is flaky. Make it more robust
+ # by retrying the connection several times.
+ for i in range(3):
+ try:
+ return urllib2.urlopen(host, *args, **kwargs)
+ except urllib2.URLError as last_exc:
+ continue
+ except:
+ raise
+ raise last_exc
+
+
class URLTimeoutTest(unittest.TestCase):
TIMEOUT = 10.0
@@ -21,7 +35,7 @@ class URLTimeoutTest(unittest.TestCase):
socket.setdefaulttimeout(None)
def testURLread(self):
- f = urllib2.urlopen("http://www.python.org/")
+ f = _urlopen_with_retry("http://www.python.org/")
x = f.read()
@@ -42,7 +56,7 @@ class AuthTests(unittest.TestCase):
#
# # failure
# try:
-# urllib2.urlopen(test_url)
+# _urlopen_with_retry(test_url)
# except urllib2.HTTPError, exc:
# self.assertEqual(exc.code, 401)
# else:
@@ -54,7 +68,7 @@ class AuthTests(unittest.TestCase):
# test_user, test_password)
# opener = urllib2.build_opener(auth_handler)
# f = opener.open('http://localhost/')
-# response = urllib2.urlopen("http://www.python.org/")
+# response = _urlopen_with_retry("http://www.python.org/")
#
# # The 'userinfo' URL component is deprecated by RFC 3986 for security
# # reasons, let's not implement it! (it's already implemented for proxy
@@ -73,7 +87,7 @@ class CloseSocketTest(unittest.TestCase):
# underlying socket
# delve deep into response to fetch socket._socketobject
- response = urllib2.urlopen("http://www.python.org/")
+ response = _urlopen_with_retry("http://www.python.org/")
abused_fileobject = response.fp
httpresponse = abused_fileobject.raw
self.assert_(httpresponse.__class__ is httplib.HTTPResponse)
@@ -100,7 +114,7 @@ class urlopenNetworkTests(unittest.TestCase):
def test_basic(self):
# Simple test expected to pass.
- open_url = urllib2.urlopen("http://www.python.org/")
+ open_url = _urlopen_with_retry("http://www.python.org/")
for attr in ("read", "close", "info", "geturl"):
self.assert_(hasattr(open_url, attr), "object returned from "
"urlopen lacks the %s attribute" % attr)
@@ -111,7 +125,7 @@ class urlopenNetworkTests(unittest.TestCase):
def test_info(self):
# Test 'info'.
- open_url = urllib2.urlopen("http://www.python.org/")
+ open_url = _urlopen_with_retry("http://www.python.org/")
try:
info_obj = open_url.info()
finally:
@@ -124,7 +138,7 @@ class urlopenNetworkTests(unittest.TestCase):
def test_geturl(self):
# Make sure same URL as opened is returned by geturl.
URL = "http://www.python.org/"
- open_url = urllib2.urlopen(URL)
+ open_url = _urlopen_with_retry(URL)
try:
gotten_url = open_url.geturl()
finally:
@@ -155,7 +169,7 @@ class OtherNetworkTests(unittest.TestCase):
def test_range (self):
req = urllib2.Request("http://www.python.org",
headers={'Range': 'bytes=20-39'})
- result = urllib2.urlopen(req)
+ result = _urlopen_with_retry(req)
data = result.read()
self.assertEqual(len(data), 20)
@@ -182,7 +196,7 @@ class OtherNetworkTests(unittest.TestCase):
'file:'+sanepathname2url(os.path.abspath(TESTFN)),
('file:///nonsensename/etc/passwd', None, urllib2.URLError),
]
- self._test_urls(urls, self._extra_handlers())
+ self._test_urls(urls, self._extra_handlers(), urllib2.urlopen)
finally:
os.remove(TESTFN)
@@ -224,7 +238,7 @@ class OtherNetworkTests(unittest.TestCase):
## self._test_urls(urls, self._extra_handlers()+[bauth, dauth])
- def _test_urls(self, urls, handlers):
+ def _test_urls(self, urls, handlers, urlopen=_urlopen_with_retry):
import socket
import time
import logging
@@ -239,7 +253,7 @@ class OtherNetworkTests(unittest.TestCase):
req = expected_err = None
debug(url)
try:
- f = urllib2.urlopen(url, req)
+ f = urlopen(url, req)
except EnvironmentError as err:
debug(err)
if expected_err:
@@ -265,47 +279,47 @@ class OtherNetworkTests(unittest.TestCase):
class TimeoutTest(unittest.TestCase):
def test_http_basic(self):
- u = urllib2.urlopen("http://www.python.org")
+ u = _urlopen_with_retry("http://www.python.org")
self.assertTrue(u.fp.raw.fp._sock.gettimeout() is None)
def test_http_NoneWithdefault(self):
prev = socket.getdefaulttimeout()
socket.setdefaulttimeout(60)
try:
- u = urllib2.urlopen("http://www.python.org", timeout=None)
+ u = _urlopen_with_retry("http://www.python.org", timeout=None)
self.assertTrue(u.fp.raw.fp._sock.gettimeout(), 60)
finally:
socket.setdefaulttimeout(prev)
def test_http_Value(self):
- u = urllib2.urlopen("http://www.python.org", timeout=120)
+ u = _urlopen_with_retry("http://www.python.org", timeout=120)
self.assertEqual(u.fp.raw.fp._sock.gettimeout(), 120)
def test_http_NoneNodefault(self):
- u = urllib2.urlopen("http://www.python.org", timeout=None)
+ u = _urlopen_with_retry("http://www.python.org", timeout=None)
self.assertTrue(u.fp.raw.fp._sock.gettimeout() is None)
+ FTP_HOST = "ftp://ftp.mirror.nl/pub/mirror/gnu/"
+
def test_ftp_basic(self):
- u = urllib2.urlopen("ftp://ftp.mirror.nl/pub/mirror/gnu/")
+ u = _urlopen_with_retry(self.FTP_HOST)
self.assertTrue(u.fp.fp.raw._sock.gettimeout() is None)
def test_ftp_NoneWithdefault(self):
prev = socket.getdefaulttimeout()
socket.setdefaulttimeout(60)
try:
- u = urllib2.urlopen("ftp://ftp.mirror.nl/pub/mirror/gnu/",
- timeout=None)
- self.assertEqual(u.fp.fp.raw._sock.gettimeout(), 60)
+ u = _urlopen_with_retry(self.FTP_HOST, timeout=None)
+ self.assertEqual(u.fp.fp._sock.gettimeout(), 60)
finally:
socket.setdefaulttimeout(prev)
def test_ftp_NoneNodefault(self):
- u = urllib2.urlopen("ftp://ftp.mirror.nl/pub/mirror/gnu/",
- timeout=None)
- self.assertTrue(u.fp.fp.raw._sock.gettimeout() is None)
+ u = _urlopen_with_retry(self.FTP_HOST, timeout=None)
+ self.assertTrue(u.fp.fp._sock.gettimeout() is None)
def test_ftp_Value(self):
- u = urllib2.urlopen("ftp://ftp.mirror.nl/pub/mirror/gnu/", timeout=60)
+ u = _urlopen_with_retry(self.FTP_HOST, timeout=60)
self.assertEqual(u.fp.fp.raw._sock.gettimeout(), 60)