summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorGeorg Brandl <georg@python.org>2008-06-10 19:20:26 (GMT)
committerGeorg Brandl <georg@python.org>2008-06-10 19:20:26 (GMT)
commitc28e1fa71f61278256887d257e4e7e24b0e7e7ce (patch)
tree5cea84b5d34c9a4ed7a84ca0ae990a24c102a465 /Lib
parente932c5c81322637a2cb8bfbcf42af7bd3a8f3418 (diff)
downloadcpython-c28e1fa71f61278256887d257e4e7e24b0e7e7ce.zip
cpython-c28e1fa71f61278256887d257e4e7e24b0e7e7ce.tar.gz
cpython-c28e1fa71f61278256887d257e4e7e24b0e7e7ce.tar.bz2
Merged revisions 64002-64003,64012,64036-64037,64047,64050-64052,64054-64055,64066,64071 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk ........ r64002 | travis.oliphant | 2008-06-07 00:33:21 +0200 (Sat, 07 Jun 2008) | 1 line Add long double check support to configure test. ........ r64003 | travis.oliphant | 2008-06-07 00:39:47 +0200 (Sat, 07 Jun 2008) | 1 line Remove locking part of new buffer protocol. ........ r64012 | facundo.batista | 2008-06-07 15:36:36 +0200 (Sat, 07 Jun 2008) | 4 lines Finished bug #2451. Fixed the retrying part to make it more robust. ........ r64036 | georg.brandl | 2008-06-08 10:54:40 +0200 (Sun, 08 Jun 2008) | 2 lines #3028: tokenize passes the physical line. ........ r64037 | georg.brandl | 2008-06-08 10:59:38 +0200 (Sun, 08 Jun 2008) | 2 lines Argh, I read it wrong. Reverted 64036 and added a clarifying remark. ........ r64047 | raymond.hettinger | 2008-06-09 03:28:30 +0200 (Mon, 09 Jun 2008) | 1 line Issue3065: Fixed pickling of named tuples. Added tests. ........ r64050 | raymond.hettinger | 2008-06-09 08:54:45 +0200 (Mon, 09 Jun 2008) | 1 line Issue #2138: Add math.factorial(). ........ r64051 | raymond.hettinger | 2008-06-09 10:33:37 +0200 (Mon, 09 Jun 2008) | 1 line Let set.union() and set.update() accept multiple inputs. ........ r64052 | raymond.hettinger | 2008-06-09 11:29:17 +0200 (Mon, 09 Jun 2008) | 1 line Address double-rounding scenarios by setting all variables to long doubles. ........ r64054 | raymond.hettinger | 2008-06-09 13:24:47 +0200 (Mon, 09 Jun 2008) | 1 line Unhappy buildbots. Revert 64052. Long doubles have unexpected effects on some builds. ........ r64055 | raymond.hettinger | 2008-06-09 15:07:27 +0200 (Mon, 09 Jun 2008) | 1 line Let set.intersection() and set.intersection_update() take multiple input arguments. ........ r64066 | robert.schuppenies | 2008-06-10 12:10:31 +0200 (Tue, 10 Jun 2008) | 2 lines Issue 3048: Fixed sys.getsizeof for unicode objects. ........ r64071 | thomas.heller | 2008-06-10 16:07:12 +0200 (Tue, 10 Jun 2008) | 3 lines NEWS entry for: Add an optional 'offset' parameter to byref, defaulting to zero. ........
Diffstat (limited to 'Lib')
-rw-r--r--Lib/collections.py4
-rw-r--r--Lib/test/test_collections.py21
-rw-r--r--Lib/test/test_math.py15
-rw-r--r--Lib/test/test_set.py13
-rw-r--r--Lib/test/test_sys.py32
-rw-r--r--Lib/test/test_urllib2net.py24
6 files changed, 92 insertions, 17 deletions
diff --git a/Lib/collections.py b/Lib/collections.py
index 73fc150..6d439e5 100644
--- a/Lib/collections.py
+++ b/Lib/collections.py
@@ -87,7 +87,9 @@ def namedtuple(typename, field_names, verbose=False):
result = self._make(map(kwds.pop, %(field_names)r, self))
if kwds:
raise ValueError('Got unexpected field names: %%r' %% kwds.keys())
- return result \n\n''' % locals()
+ return result \n
+ def __getnewargs__(self):
+ return tuple(self) \n\n''' % locals()
for i, name in enumerate(field_names):
template += ' %s = property(itemgetter(%d))\n' % (name, i)
if verbose:
diff --git a/Lib/test/test_collections.py b/Lib/test/test_collections.py
index 4ed0e24..9289afe 100644
--- a/Lib/test/test_collections.py
+++ b/Lib/test/test_collections.py
@@ -3,6 +3,7 @@
import unittest, doctest
from test import support
from collections import namedtuple
+import pickle, copy
from collections import Hashable, Iterable, Iterator
from collections import Sized, Container, Callable
from collections import Set, MutableSet
@@ -10,6 +11,7 @@ from collections import Mapping, MutableMapping
from collections import Sequence, MutableSequence
from collections import ByteString
+TestNT = namedtuple('TestNT', 'x y z') # type used for pickle tests
class TestNamedTuple(unittest.TestCase):
@@ -111,7 +113,7 @@ class TestNamedTuple(unittest.TestCase):
self.assertEqual(Dot(1)._replace(d=999), (999,))
self.assertEqual(Dot(1)._fields, ('d',))
- # n = 10000
+ # n = 5000
n = 254 # SyntaxError: more than 255 arguments:
import string, random
names = list(set(''.join([random.choice(string.ascii_letters)
@@ -134,6 +136,23 @@ class TestNamedTuple(unittest.TestCase):
self.assertEqual(b2, tuple(b2_expected))
self.assertEqual(b._fields, tuple(names))
+ def test_pickle(self):
+ p = TestNT(x=10, y=20, z=30)
+ for module in (pickle,):
+ loads = getattr(module, 'loads')
+ dumps = getattr(module, 'dumps')
+ for protocol in -1, 0, 1, 2:
+ q = loads(dumps(p, protocol))
+ self.assertEqual(p, q)
+ self.assertEqual(p._fields, q._fields)
+
+ def test_copy(self):
+ p = TestNT(x=10, y=20, z=30)
+ for copier in copy.copy, copy.deepcopy:
+ q = copier(p)
+ self.assertEqual(p, q)
+ self.assertEqual(p._fields, q._fields)
+
class TestOneTrickPonyABCs(unittest.TestCase):
def test_Hashable(self):
diff --git a/Lib/test/test_math.py b/Lib/test/test_math.py
index f4c27bb..1eafeba 100644
--- a/Lib/test/test_math.py
+++ b/Lib/test/test_math.py
@@ -6,6 +6,7 @@ import unittest
import math
import os
import sys
+import random
eps = 1E-05
NAN = float('nan')
@@ -274,6 +275,20 @@ class MathTests(unittest.TestCase):
self.ftest('fabs(0)', math.fabs(0), 0)
self.ftest('fabs(1)', math.fabs(1), 1)
+ def testFactorial(self):
+ def fact(n):
+ result = 1
+ for i in range(1, int(n)+1):
+ result *= i
+ return result
+ values = list(range(10)) + [50, 100, 500]
+ random.shuffle(values)
+ for x in range(10):
+ for cast in (int, float):
+ self.assertEqual(math.factorial(cast(x)), fact(x), (x, fact(x), math.factorial(x)))
+ self.assertRaises(ValueError, math.factorial, -1)
+ self.assertRaises(ValueError, math.factorial, math.pi)
+
def testFloor(self):
self.assertRaises(TypeError, math.floor)
self.assertEquals(int, type(math.floor(0.5)))
diff --git a/Lib/test/test_set.py b/Lib/test/test_set.py
index d05df3f..ed2db18 100644
--- a/Lib/test/test_set.py
+++ b/Lib/test/test_set.py
@@ -79,6 +79,7 @@ class TestJointOps(unittest.TestCase):
self.assertEqual(self.thetype('abcba').union(C('efgfe')), set('abcefg'))
self.assertEqual(self.thetype('abcba').union(C('ccb')), set('abc'))
self.assertEqual(self.thetype('abcba').union(C('ef')), set('abcef'))
+ self.assertEqual(self.thetype('abcba').union(C('ef'), C('fg')), set('abcefg'))
def test_or(self):
i = self.s.union(self.otherword)
@@ -103,6 +104,7 @@ class TestJointOps(unittest.TestCase):
self.assertEqual(self.thetype('abcba').intersection(C('efgfe')), set(''))
self.assertEqual(self.thetype('abcba').intersection(C('ccb')), set('bc'))
self.assertEqual(self.thetype('abcba').intersection(C('ef')), set(''))
+ self.assertEqual(self.thetype('abcba').intersection(C('cbcf'), C('bag')), set('b'))
def test_isdisjoint(self):
def f(s1, s2):
@@ -410,6 +412,12 @@ class TestSet(TestJointOps):
s = self.thetype('abcba')
self.assertEqual(s.update(C(p)), None)
self.assertEqual(s, set(q))
+ for p in ('cdc', 'efgfe', 'ccb', 'ef', 'abcda'):
+ q = 'ahi'
+ for C in set, frozenset, dict.fromkeys, str, list, tuple:
+ s = self.thetype('abcba')
+ self.assertEqual(s.update(C(p), C(q)), None)
+ self.assertEqual(s, set(s) | set(p) | set(q))
def test_ior(self):
self.s |= set(self.otherword)
@@ -431,6 +439,11 @@ class TestSet(TestJointOps):
s = self.thetype('abcba')
self.assertEqual(s.intersection_update(C(p)), None)
self.assertEqual(s, set(q))
+ ss = 'abcba'
+ s = self.thetype(ss)
+ t = 'cbc'
+ self.assertEqual(s.intersection_update(C(p), C(t)), None)
+ self.assertEqual(s, set('abcba')&set(p)&set(t))
def test_iand(self):
self.s &= set(self.otherword)
diff --git a/Lib/test/test_sys.py b/Lib/test/test_sys.py
index e9570e9..39ed0ad 100644
--- a/Lib/test/test_sys.py
+++ b/Lib/test/test_sys.py
@@ -386,11 +386,14 @@ class SizeofTest(unittest.TestCase):
self.file.close()
test.support.unlink(test.support.TESTFN)
- def check_sizeof(self, o, size):
+ def check_sizeof(self, o, size, size2=None):
+ """Check size of o. Possible are size and optionally size2)."""
result = sys.getsizeof(o)
- msg = 'wrong size for %s: got %d, expected %d' \
- % (type(o), result, size)
- self.assertEqual(result, size, msg)
+ msg = 'wrong size for %s: got %d, expected ' % (type(o), result)
+ if (size2 != None) and (result != size):
+ self.assertEqual(result, size2, msg + str(size2))
+ else:
+ self.assertEqual(result, size, msg + str(size))
def align(self, value):
mod = value % self.p
@@ -486,6 +489,24 @@ class SizeofTest(unittest.TestCase):
# list
self.check_sizeof([], h + l + p + l)
self.check_sizeof([1, 2, 3], h + l + p + l + 3*l)
+ # unicode
+ import math
+ usize = math.log(sys.maxunicode + 1, 2) / 8
+ samples = ['', '1'*100]
+ # we need to test for both sizes, because we don't know if the string
+ # has been cached
+ for s in samples:
+ basicsize = h + l + p + l + l + p + usize * (len(s) + 1)
+ defenc = bytes(s, 'ascii')
+ self.check_sizeof(s, basicsize,
+ size2=basicsize + sys.getsizeof(defenc))
+ # trigger caching encoded version as bytes object
+ try:
+ getattr(sys, s)
+ except AttributeError:
+ pass
+ finally:
+ self.check_sizeof(s, basicsize + sys.getsizeof(defenc))
h += l
# long
@@ -495,9 +516,6 @@ class SizeofTest(unittest.TestCase):
self.check_sizeof(32768, h + self.align(2) + 2)
self.check_sizeof(32768*32768-1, h + self.align(2) + 2)
self.check_sizeof(32768*32768, h + self.align(2) + 4)
- # XXX add Unicode support
- # self.check_sizeof('', h + l + self.align(i + 1))
- # self.check_sizeof('abc', h + l + self.align(i + 1) + 3)
def test_main():
diff --git a/Lib/test/test_urllib2net.py b/Lib/test/test_urllib2net.py
index 752406c..1c4af18 100644
--- a/Lib/test/test_urllib2net.py
+++ b/Lib/test/test_urllib2net.py
@@ -11,19 +11,25 @@ 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.
+def _retry_thrice(func, exc, *args, **kwargs):
for i in range(3):
try:
- return urllib2.urlopen(host, *args, **kwargs)
- except urllib2.URLError as e:
+ return func(*args, **kwargs)
+ except exc as e:
last_exc = e
continue
except:
raise
raise last_exc
+def _wrap_with_retry_thrice(func, exc):
+ def wrapped(*args, **kwargs):
+ return _retry_thrice(func, exc, *args, **kwargs)
+ return wrapped
+
+# Connecting to remote hosts is flaky. Make it more robust by retrying
+# the connection several times.
+_urlopen_with_retry = _wrap_with_retry_thrice(urllib2.urlopen, urllib2.URLError)
class AuthTests(unittest.TestCase):
@@ -114,7 +120,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(), urllib2.urlopen)
+ self._test_urls(urls, self._extra_handlers(), retry=True)
finally:
os.remove(TESTFN)
@@ -146,13 +152,15 @@ class OtherNetworkTests(unittest.TestCase):
## self._test_urls(urls, self._extra_handlers()+[bauth, dauth])
- def _test_urls(self, urls, handlers, urlopen=_urlopen_with_retry):
+ def _test_urls(self, urls, handlers, retry=True):
import socket
import time
import logging
debug = logging.getLogger("test_urllib2").debug
- urllib2.install_opener(urllib2.build_opener(*handlers))
+ urlopen = urllib2.build_opener(*handlers).open
+ if retry:
+ urlopen = _wrap_with_retry_thrice(urlopen, urllib2.URLError)
for url in urls:
if isinstance(url, tuple):