mirror of
https://github.com/python/cpython.git
synced 2025-01-21 07:55:16 +08:00
b186d0084c
svn+ssh://pythondev@svn.python.org/python/trunk ........ r61431 | vinay.sajip | 2008-03-16 22:35:58 +0100 (So, 16 Mär 2008) | 1 line Clarified documentation on use of shutdown(). ........ r61433 | mark.summerfield | 2008-03-17 09:28:15 +0100 (Mo, 17 Mär 2008) | 5 lines Added a footnote to each pointing out that for XML output if an encoding string is given it should conform to the appropriate XML standards---for example, "UTF-8" is okay, but "UTF8" is not. ........ r61434 | eric.smith | 2008-03-17 12:01:01 +0100 (Mo, 17 Mär 2008) | 7 lines Issue 2264: empty float presentation type needs to have at least one digit past the decimal point. Added "Z" format_char to PyOS_ascii_formatd to support empty float presentation type. Renamed buf_size in PyOS_ascii_formatd to more accurately reflect it's meaning. Modified format.__float__ to use the new "Z" format as the default. Added test cases. ........ r61435 | eric.smith | 2008-03-17 13:14:29 +0100 (Mo, 17 Mär 2008) | 2 lines Reformated lines > 79 chars. Deleted unused macro ISXDIGIT. ........ r61436 | jeffrey.yasskin | 2008-03-17 15:40:53 +0100 (Mo, 17 Mär 2008) | 13 lines Allow Gnu gcc's to build python on OSX by removing -Wno-long-double, -no-cpp-precomp, and -mno-fused-madd from configure. * r22183 added -no-cpp-precomp, which http://gcc.gnu.org/ml/gcc/2005-12/msg00368.html claims hasn't been needed since gcc-3.1. * r25607 added -Wno-long-double to avoid a warning in Include/objimpl.h (issue 525481). The long double is still there, but OSX 10.4's gcc no longer warns about it. * r33666 fixed issue 775892 on OSX 10.3 by adding -mno-fused-madd, which changed the sign of some float 0s. Tim Peters said it wasn't a real issue anyway, and it no longer causes test failures. Fixes issue #1779871. ........ r61439 | martin.v.loewis | 2008-03-17 17:31:57 +0100 (Mo, 17 Mär 2008) | 2 lines Add Trent Nelson. ........ r61444 | travis.oliphant | 2008-03-17 18:36:12 +0100 (Mo, 17 Mär 2008) | 1 line Add necessary headers to back-port new buffer protocol to Python 2.6 ........ r61449 | gregory.p.smith | 2008-03-17 19:48:05 +0100 (Mo, 17 Mär 2008) | 8 lines Force zlib.crc32 and zlib.adler32 to return a signed integer on all platforms regardless of the native sizeof(long) used in the integer object. This somewhat odd behavior of returning a signed is maintained in 2.x for compatibility reasons of always returning an integer rather than a long object. Fixes Issue1202 for Python 2.6 ........ r61450 | neal.norwitz | 2008-03-17 20:02:45 +0100 (Mo, 17 Mär 2008) | 3 lines Use a buffer large enough to ensure we don't overrun, even if the value is outside the range we expect. ........ r61453 | steven.bethard | 2008-03-17 20:33:11 +0100 (Mo, 17 Mär 2008) | 1 line Document unicode.isnumeric() and unicode.isdecimal() (issue2326) ........ r61458 | neal.norwitz | 2008-03-17 21:22:43 +0100 (Mo, 17 Mär 2008) | 5 lines Issue 2321: reduce memory usage (increase the memory that is returned to the system) by using pymalloc for the data of unicode objects. Will backport. ........ r61465 | martin.v.loewis | 2008-03-17 22:55:30 +0100 (Mo, 17 Mär 2008) | 2 lines Add David Wolever. ........ r61468 | gregory.p.smith | 2008-03-18 01:20:01 +0100 (Di, 18 Mär 2008) | 3 lines Fix the IOError message text when opening a file with an invalid filename. Error reported by Ilan Schnell. ........ r61471 | brett.cannon | 2008-03-18 02:00:07 +0100 (Di, 18 Mär 2008) | 2 lines Convert test_strftime, test_getargs, and test_pep247 to use unittest. ........ r61472 | jeffrey.yasskin | 2008-03-18 02:09:59 +0100 (Di, 18 Mär 2008) | 2 lines Fix build on platforms that don't have intptr_t. Patch by Joseph Armbruster. ........ r61473 | brett.cannon | 2008-03-18 02:50:25 +0100 (Di, 18 Mär 2008) | 2 lines Convert test_dummy_threading and test_dbm to unittest. ........ r61474 | brett.cannon | 2008-03-18 02:58:56 +0100 (Di, 18 Mär 2008) | 2 lines Move test_extcall to doctest. ........ r61480 | brett.cannon | 2008-03-18 04:46:22 +0100 (Di, 18 Mär 2008) | 2 lines test_errno was a no-op test; now it actually tests things and uses unittest. ........ r61483 | brett.cannon | 2008-03-18 05:09:00 +0100 (Di, 18 Mär 2008) | 3 lines Remove our implementation of memmove() and strerror(); both are in the C89 standard library. ........ r61484 | brett.cannon | 2008-03-18 05:16:06 +0100 (Di, 18 Mär 2008) | 2 lines The output directory for tests that compare against stdout is now gone! ........ r61488 | jeffrey.yasskin | 2008-03-18 05:29:35 +0100 (Di, 18 Mär 2008) | 2 lines Block the "socket.ssl() is deprecated" warning from test_socket_ssl. ........ r61495 | jeffrey.yasskin | 2008-03-18 05:56:06 +0100 (Di, 18 Mär 2008) | 4 lines Speed test_thread up from 51.328s to 0.081s by reducing its sleep times. We still sleep at all to make it likely that all threads are active at the same time. ........ r61496 | jeffrey.yasskin | 2008-03-18 06:12:41 +0100 (Di, 18 Mär 2008) | 4 lines Speed up test_dict by about 10x by only checking selected dict literal sizes, instead of every integer from 0 to 400. Exhaustive testing wastes time without providing enough more assurance that the code is correct. ........ r61498 | neal.norwitz | 2008-03-18 06:20:29 +0100 (Di, 18 Mär 2008) | 1 line Try increasing the timeout to reduce the flakiness of this test. ........ r61503 | brett.cannon | 2008-03-18 06:43:04 +0100 (Di, 18 Mär 2008) | 2 lines Improve the error message for a test that failed on the S-390 Debian buildbot. ........ r61504 | jeffrey.yasskin | 2008-03-18 06:45:40 +0100 (Di, 18 Mär 2008) | 3 lines Add a -S/--slow flag to regrtest to have it print the 10 slowest tests with their times. ........ r61507 | neal.norwitz | 2008-03-18 07:03:46 +0100 (Di, 18 Mär 2008) | 1 line Add some info to the failure messages ........ r61509 | trent.nelson | 2008-03-18 08:02:12 +0100 (Di, 18 Mär 2008) | 1 line Issue 2286: bump up the stack size of the 64-bit debug python_d.exe to 2100000. The default value of 200000 causes a stack overflow at 1965 iterations of r_object() in marshal.c, 35 iterations before the 2000 limit enforced by MAX_MARSHAL_STACK_DEPTH. ........ r61510 | trent.nelson | 2008-03-18 08:32:47 +0100 (Di, 18 Mär 2008) | 5 lines The behaviour of winsound.Beep() seems to differ between different versions of Windows when there's either: a) no sound card entirely b) legacy beep driver has been disabled c) the legacy beep driver has been uninstalled Sometimes RuntimeErrors are raised, sometimes they're not. If _have_soundcard() returns False, don't expect winsound.Beep() to raise a RuntimeError, as this clearly isn't the case, as demonstrated by the various Win32 XP buildbots. ........ r61515 | martin.v.loewis | 2008-03-18 13:20:15 +0100 (Di, 18 Mär 2008) | 2 lines norwitz-amd64 (gentoo) has EREMOTEIO. ........ r61516 | martin.v.loewis | 2008-03-18 13:45:37 +0100 (Di, 18 Mär 2008) | 2 lines Add more Linux error codes. ........ r61517 | martin.v.loewis | 2008-03-18 14:05:03 +0100 (Di, 18 Mär 2008) | 2 lines Add WSA errors. ........ r61518 | martin.v.loewis | 2008-03-18 14:16:05 +0100 (Di, 18 Mär 2008) | 2 lines Note that the stderr output of the test is intentional. ........
662 lines
20 KiB
Python
662 lines
20 KiB
Python
import unittest
|
|
from test import test_support
|
|
|
|
import sys, collections, random, string
|
|
|
|
|
|
class DictTest(unittest.TestCase):
|
|
|
|
def test_constructor(self):
|
|
# calling built-in types without argument must return empty
|
|
self.assertEqual(dict(), {})
|
|
self.assert_(dict() is not {})
|
|
|
|
def test_literal_constructor(self):
|
|
# check literal constructor for different sized dicts (to exercise the BUILD_MAP oparg
|
|
for n in (0, 1, 6, 256, 400):
|
|
items = [(''.join([random.choice(string.ascii_letters)
|
|
for j in range(8)]),
|
|
i)
|
|
for i in range(n)]
|
|
random.shuffle(items)
|
|
dictliteral = '{' + ', '.join('%r: %d' % item for item in items) + '}'
|
|
self.assertEqual(eval(dictliteral), dict(items))
|
|
|
|
def test_bool(self):
|
|
self.assert_(not {})
|
|
self.assert_({1: 2})
|
|
self.assert_(bool({}) is False)
|
|
self.assert_(bool({1: 2}) is True)
|
|
|
|
def test_keys(self):
|
|
d = {}
|
|
self.assertEqual(set(d.keys()), set())
|
|
d = {'a': 1, 'b': 2}
|
|
k = d.keys()
|
|
self.assert_('a' in d)
|
|
self.assert_('b' in d)
|
|
|
|
self.assertRaises(TypeError, d.keys, None)
|
|
|
|
def test_values(self):
|
|
d = {}
|
|
self.assertEqual(set(d.values()), set())
|
|
d = {1:2}
|
|
self.assertEqual(set(d.values()), {2})
|
|
|
|
self.assertRaises(TypeError, d.values, None)
|
|
|
|
def test_items(self):
|
|
d = {}
|
|
self.assertEqual(set(d.items()), set())
|
|
|
|
d = {1:2}
|
|
self.assertEqual(set(d.items()), {(1, 2)})
|
|
|
|
self.assertRaises(TypeError, d.items, None)
|
|
|
|
def test_contains(self):
|
|
d = {}
|
|
self.assert_(not ('a' in d))
|
|
self.assert_('a' not in d)
|
|
d = {'a': 1, 'b': 2}
|
|
self.assert_('a' in d)
|
|
self.assert_('b' in d)
|
|
self.assert_('c' not in d)
|
|
|
|
self.assertRaises(TypeError, d.__contains__)
|
|
|
|
def test_len(self):
|
|
d = {}
|
|
self.assertEqual(len(d), 0)
|
|
d = {'a': 1, 'b': 2}
|
|
self.assertEqual(len(d), 2)
|
|
|
|
def test_getitem(self):
|
|
d = {'a': 1, 'b': 2}
|
|
self.assertEqual(d['a'], 1)
|
|
self.assertEqual(d['b'], 2)
|
|
d['c'] = 3
|
|
d['a'] = 4
|
|
self.assertEqual(d['c'], 3)
|
|
self.assertEqual(d['a'], 4)
|
|
del d['b']
|
|
self.assertEqual(d, {'a': 4, 'c': 3})
|
|
|
|
self.assertRaises(TypeError, d.__getitem__)
|
|
|
|
class BadEq(object):
|
|
def __eq__(self, other):
|
|
raise Exc()
|
|
def __hash__(self):
|
|
return 24
|
|
|
|
d = {}
|
|
d[BadEq()] = 42
|
|
self.assertRaises(KeyError, d.__getitem__, 23)
|
|
|
|
class Exc(Exception): pass
|
|
|
|
class BadHash(object):
|
|
fail = False
|
|
def __hash__(self):
|
|
if self.fail:
|
|
raise Exc()
|
|
else:
|
|
return 42
|
|
|
|
x = BadHash()
|
|
d[x] = 42
|
|
x.fail = True
|
|
self.assertRaises(Exc, d.__getitem__, x)
|
|
|
|
def test_clear(self):
|
|
d = {1:1, 2:2, 3:3}
|
|
d.clear()
|
|
self.assertEqual(d, {})
|
|
|
|
self.assertRaises(TypeError, d.clear, None)
|
|
|
|
def test_update(self):
|
|
d = {}
|
|
d.update({1:100})
|
|
d.update({2:20})
|
|
d.update({1:1, 2:2, 3:3})
|
|
self.assertEqual(d, {1:1, 2:2, 3:3})
|
|
|
|
d.update()
|
|
self.assertEqual(d, {1:1, 2:2, 3:3})
|
|
|
|
self.assertRaises((TypeError, AttributeError), d.update, None)
|
|
|
|
class SimpleUserDict:
|
|
def __init__(self):
|
|
self.d = {1:1, 2:2, 3:3}
|
|
def keys(self):
|
|
return self.d.keys()
|
|
def __getitem__(self, i):
|
|
return self.d[i]
|
|
d.clear()
|
|
d.update(SimpleUserDict())
|
|
self.assertEqual(d, {1:1, 2:2, 3:3})
|
|
|
|
class Exc(Exception): pass
|
|
|
|
d.clear()
|
|
class FailingUserDict:
|
|
def keys(self):
|
|
raise Exc
|
|
self.assertRaises(Exc, d.update, FailingUserDict())
|
|
|
|
class FailingUserDict:
|
|
def keys(self):
|
|
class BogonIter:
|
|
def __init__(self):
|
|
self.i = 1
|
|
def __iter__(self):
|
|
return self
|
|
def __next__(self):
|
|
if self.i:
|
|
self.i = 0
|
|
return 'a'
|
|
raise Exc
|
|
return BogonIter()
|
|
def __getitem__(self, key):
|
|
return key
|
|
self.assertRaises(Exc, d.update, FailingUserDict())
|
|
|
|
class FailingUserDict:
|
|
def keys(self):
|
|
class BogonIter:
|
|
def __init__(self):
|
|
self.i = ord('a')
|
|
def __iter__(self):
|
|
return self
|
|
def __next__(self):
|
|
if self.i <= ord('z'):
|
|
rtn = chr(self.i)
|
|
self.i += 1
|
|
return rtn
|
|
raise StopIteration
|
|
return BogonIter()
|
|
def __getitem__(self, key):
|
|
raise Exc
|
|
self.assertRaises(Exc, d.update, FailingUserDict())
|
|
|
|
class badseq(object):
|
|
def __iter__(self):
|
|
return self
|
|
def __next__(self):
|
|
raise Exc()
|
|
|
|
self.assertRaises(Exc, {}.update, badseq())
|
|
|
|
self.assertRaises(ValueError, {}.update, [(1, 2, 3)])
|
|
|
|
def test_fromkeys(self):
|
|
self.assertEqual(dict.fromkeys('abc'), {'a':None, 'b':None, 'c':None})
|
|
d = {}
|
|
self.assert_(not(d.fromkeys('abc') is d))
|
|
self.assertEqual(d.fromkeys('abc'), {'a':None, 'b':None, 'c':None})
|
|
self.assertEqual(d.fromkeys((4,5),0), {4:0, 5:0})
|
|
self.assertEqual(d.fromkeys([]), {})
|
|
def g():
|
|
yield 1
|
|
self.assertEqual(d.fromkeys(g()), {1:None})
|
|
self.assertRaises(TypeError, {}.fromkeys, 3)
|
|
class dictlike(dict): pass
|
|
self.assertEqual(dictlike.fromkeys('a'), {'a':None})
|
|
self.assertEqual(dictlike().fromkeys('a'), {'a':None})
|
|
self.assert_(type(dictlike.fromkeys('a')) is dictlike)
|
|
self.assert_(type(dictlike().fromkeys('a')) is dictlike)
|
|
class mydict(dict):
|
|
def __new__(cls):
|
|
return collections.UserDict()
|
|
ud = mydict.fromkeys('ab')
|
|
self.assertEqual(ud, {'a':None, 'b':None})
|
|
self.assert_(isinstance(ud, collections.UserDict))
|
|
self.assertRaises(TypeError, dict.fromkeys)
|
|
|
|
class Exc(Exception): pass
|
|
|
|
class baddict1(dict):
|
|
def __init__(self):
|
|
raise Exc()
|
|
|
|
self.assertRaises(Exc, baddict1.fromkeys, [1])
|
|
|
|
class BadSeq(object):
|
|
def __iter__(self):
|
|
return self
|
|
def __next__(self):
|
|
raise Exc()
|
|
|
|
self.assertRaises(Exc, dict.fromkeys, BadSeq())
|
|
|
|
class baddict2(dict):
|
|
def __setitem__(self, key, value):
|
|
raise Exc()
|
|
|
|
self.assertRaises(Exc, baddict2.fromkeys, [1])
|
|
|
|
# test fast path for dictionary inputs
|
|
d = dict(zip(range(6), range(6)))
|
|
self.assertEqual(dict.fromkeys(d, 0), dict(zip(range(6), [0]*6)))
|
|
|
|
def test_copy(self):
|
|
d = {1:1, 2:2, 3:3}
|
|
self.assertEqual(d.copy(), {1:1, 2:2, 3:3})
|
|
self.assertEqual({}.copy(), {})
|
|
self.assertRaises(TypeError, d.copy, None)
|
|
|
|
def test_get(self):
|
|
d = {}
|
|
self.assert_(d.get('c') is None)
|
|
self.assertEqual(d.get('c', 3), 3)
|
|
d = {'a' : 1, 'b' : 2}
|
|
self.assert_(d.get('c') is None)
|
|
self.assertEqual(d.get('c', 3), 3)
|
|
self.assertEqual(d.get('a'), 1)
|
|
self.assertEqual(d.get('a', 3), 1)
|
|
self.assertRaises(TypeError, d.get)
|
|
self.assertRaises(TypeError, d.get, None, None, None)
|
|
|
|
def test_setdefault(self):
|
|
# dict.setdefault()
|
|
d = {}
|
|
self.assert_(d.setdefault('key0') is None)
|
|
d.setdefault('key0', [])
|
|
self.assert_(d.setdefault('key0') is None)
|
|
d.setdefault('key', []).append(3)
|
|
self.assertEqual(d['key'][0], 3)
|
|
d.setdefault('key', []).append(4)
|
|
self.assertEqual(len(d['key']), 2)
|
|
self.assertRaises(TypeError, d.setdefault)
|
|
|
|
class Exc(Exception): pass
|
|
|
|
class BadHash(object):
|
|
fail = False
|
|
def __hash__(self):
|
|
if self.fail:
|
|
raise Exc()
|
|
else:
|
|
return 42
|
|
|
|
x = BadHash()
|
|
d[x] = 42
|
|
x.fail = True
|
|
self.assertRaises(Exc, d.setdefault, x, [])
|
|
|
|
def test_popitem(self):
|
|
# dict.popitem()
|
|
for copymode in -1, +1:
|
|
# -1: b has same structure as a
|
|
# +1: b is a.copy()
|
|
for log2size in range(12):
|
|
size = 2**log2size
|
|
a = {}
|
|
b = {}
|
|
for i in range(size):
|
|
a[repr(i)] = i
|
|
if copymode < 0:
|
|
b[repr(i)] = i
|
|
if copymode > 0:
|
|
b = a.copy()
|
|
for i in range(size):
|
|
ka, va = ta = a.popitem()
|
|
self.assertEqual(va, int(ka))
|
|
kb, vb = tb = b.popitem()
|
|
self.assertEqual(vb, int(kb))
|
|
self.assert_(not(copymode < 0 and ta != tb))
|
|
self.assert_(not a)
|
|
self.assert_(not b)
|
|
|
|
d = {}
|
|
self.assertRaises(KeyError, d.popitem)
|
|
|
|
def test_pop(self):
|
|
# Tests for pop with specified key
|
|
d = {}
|
|
k, v = 'abc', 'def'
|
|
d[k] = v
|
|
self.assertRaises(KeyError, d.pop, 'ghi')
|
|
|
|
self.assertEqual(d.pop(k), v)
|
|
self.assertEqual(len(d), 0)
|
|
|
|
self.assertRaises(KeyError, d.pop, k)
|
|
|
|
# verify longs/ints get same value when key > 32 bits (for 64-bit archs)
|
|
# see SF bug #689659
|
|
x = 4503599627370496
|
|
y = 4503599627370496
|
|
h = {x: 'anything', y: 'something else'}
|
|
self.assertEqual(h[x], h[y])
|
|
|
|
self.assertEqual(d.pop(k, v), v)
|
|
d[k] = v
|
|
self.assertEqual(d.pop(k, 1), v)
|
|
|
|
self.assertRaises(TypeError, d.pop)
|
|
|
|
class Exc(Exception): pass
|
|
|
|
class BadHash(object):
|
|
fail = False
|
|
def __hash__(self):
|
|
if self.fail:
|
|
raise Exc()
|
|
else:
|
|
return 42
|
|
|
|
x = BadHash()
|
|
d[x] = 42
|
|
x.fail = True
|
|
self.assertRaises(Exc, d.pop, x)
|
|
|
|
def test_mutatingiteration(self):
|
|
d = {}
|
|
d[1] = 1
|
|
try:
|
|
for i in d:
|
|
d[i+1] = 1
|
|
except RuntimeError:
|
|
pass
|
|
else:
|
|
self.fail("changing dict size during iteration doesn't raise Error")
|
|
|
|
def test_repr(self):
|
|
d = {}
|
|
self.assertEqual(repr(d), '{}')
|
|
d[1] = 2
|
|
self.assertEqual(repr(d), '{1: 2}')
|
|
d = {}
|
|
d[1] = d
|
|
self.assertEqual(repr(d), '{1: {...}}')
|
|
|
|
class Exc(Exception): pass
|
|
|
|
class BadRepr(object):
|
|
def __repr__(self):
|
|
raise Exc()
|
|
|
|
d = {1: BadRepr()}
|
|
self.assertRaises(Exc, repr, d)
|
|
|
|
def test_eq(self):
|
|
self.assertEqual({}, {})
|
|
self.assertEqual({1: 2}, {1: 2})
|
|
|
|
class Exc(Exception): pass
|
|
|
|
class BadCmp(object):
|
|
def __eq__(self, other):
|
|
raise Exc()
|
|
def __hash__(self):
|
|
return 1
|
|
|
|
d1 = {BadCmp(): 1}
|
|
d2 = {1: 1}
|
|
try:
|
|
d1 == d2
|
|
except Exc:
|
|
pass
|
|
else:
|
|
self.fail("< didn't raise Exc")
|
|
|
|
def test_keys_contained(self):
|
|
self.helper_keys_contained(lambda x: x.keys())
|
|
self.helper_keys_contained(lambda x: x.items())
|
|
|
|
def helper_keys_contained(self, fn):
|
|
# Test rich comparisons against dict key views, which should behave the
|
|
# same as sets.
|
|
empty = fn(dict())
|
|
empty2 = fn(dict())
|
|
smaller = fn({1:1, 2:2})
|
|
larger = fn({1:1, 2:2, 3:3})
|
|
larger2 = fn({1:1, 2:2, 3:3})
|
|
larger3 = fn({4:1, 2:2, 3:3})
|
|
|
|
self.assertTrue(smaller < larger)
|
|
self.assertTrue(smaller <= larger)
|
|
self.assertTrue(larger > smaller)
|
|
self.assertTrue(larger >= smaller)
|
|
|
|
self.assertFalse(smaller >= larger)
|
|
self.assertFalse(smaller > larger)
|
|
self.assertFalse(larger <= smaller)
|
|
self.assertFalse(larger < smaller)
|
|
|
|
self.assertFalse(smaller < larger3)
|
|
self.assertFalse(smaller <= larger3)
|
|
self.assertFalse(larger3 > smaller)
|
|
self.assertFalse(larger3 >= smaller)
|
|
|
|
# Inequality strictness
|
|
self.assertTrue(larger2 >= larger)
|
|
self.assertTrue(larger2 <= larger)
|
|
self.assertFalse(larger2 > larger)
|
|
self.assertFalse(larger2 < larger)
|
|
|
|
self.assertTrue(larger == larger2)
|
|
self.assertTrue(smaller != larger)
|
|
|
|
# There is an optimization on the zero-element case.
|
|
self.assertTrue(empty == empty2)
|
|
self.assertFalse(empty != empty2)
|
|
self.assertFalse(empty == smaller)
|
|
self.assertTrue(empty != smaller)
|
|
|
|
# With the same size, an elementwise compare happens
|
|
self.assertTrue(larger != larger3)
|
|
self.assertFalse(larger == larger3)
|
|
|
|
def test_errors_in_view_containment_check(self):
|
|
class C:
|
|
def __eq__(self, other):
|
|
raise RuntimeError
|
|
d1 = {1: C()}
|
|
d2 = {1: C()}
|
|
self.assertRaises(RuntimeError, lambda: d1.items() == d2.items())
|
|
self.assertRaises(RuntimeError, lambda: d1.items() != d2.items())
|
|
self.assertRaises(RuntimeError, lambda: d1.items() <= d2.items())
|
|
self.assertRaises(RuntimeError, lambda: d1.items() >= d2.items())
|
|
d3 = {1: C(), 2: C()}
|
|
self.assertRaises(RuntimeError, lambda: d2.items() < d3.items())
|
|
self.assertRaises(RuntimeError, lambda: d3.items() > d2.items())
|
|
|
|
def test_dictview_set_operations_on_keys(self):
|
|
k1 = {1:1, 2:2}.keys()
|
|
k2 = {1:1, 2:2, 3:3}.keys()
|
|
k3 = {4:4}.keys()
|
|
|
|
self.assertEquals(k1 - k2, set())
|
|
self.assertEquals(k1 - k3, {1,2})
|
|
self.assertEquals(k2 - k1, {3})
|
|
self.assertEquals(k3 - k1, {4})
|
|
self.assertEquals(k1 & k2, {1,2})
|
|
self.assertEquals(k1 & k3, set())
|
|
self.assertEquals(k1 | k2, {1,2,3})
|
|
self.assertEquals(k1 ^ k2, {3})
|
|
self.assertEquals(k1 ^ k3, {1,2,4})
|
|
|
|
def test_dictview_set_operations_on_items(self):
|
|
k1 = {1:1, 2:2}.items()
|
|
k2 = {1:1, 2:2, 3:3}.items()
|
|
k3 = {4:4}.items()
|
|
|
|
self.assertEquals(k1 - k2, set())
|
|
self.assertEquals(k1 - k3, {(1,1), (2,2)})
|
|
self.assertEquals(k2 - k1, {(3,3)})
|
|
self.assertEquals(k3 - k1, {(4,4)})
|
|
self.assertEquals(k1 & k2, {(1,1), (2,2)})
|
|
self.assertEquals(k1 & k3, set())
|
|
self.assertEquals(k1 | k2, {(1,1), (2,2), (3,3)})
|
|
self.assertEquals(k1 ^ k2, {(3,3)})
|
|
self.assertEquals(k1 ^ k3, {(1,1), (2,2), (4,4)})
|
|
|
|
def test_dictview_mixed_set_operations(self):
|
|
# Just a few for .keys()
|
|
self.assertTrue({1:1}.keys() == {1})
|
|
self.assertTrue({1} == {1:1}.keys())
|
|
self.assertEquals({1:1}.keys() | {2}, {1, 2})
|
|
self.assertEquals({2} | {1:1}.keys(), {1, 2})
|
|
# And a few for .items()
|
|
self.assertTrue({1:1}.items() == {(1,1)})
|
|
self.assertTrue({(1,1)} == {1:1}.items())
|
|
self.assertEquals({1:1}.items() | {2}, {(1,1), 2})
|
|
self.assertEquals({2} | {1:1}.items(), {(1,1), 2})
|
|
|
|
def test_missing(self):
|
|
# Make sure dict doesn't have a __missing__ method
|
|
self.assertEqual(hasattr(dict, "__missing__"), False)
|
|
self.assertEqual(hasattr({}, "__missing__"), False)
|
|
# Test several cases:
|
|
# (D) subclass defines __missing__ method returning a value
|
|
# (E) subclass defines __missing__ method raising RuntimeError
|
|
# (F) subclass sets __missing__ instance variable (no effect)
|
|
# (G) subclass doesn't define __missing__ at a all
|
|
class D(dict):
|
|
def __missing__(self, key):
|
|
return 42
|
|
d = D({1: 2, 3: 4})
|
|
self.assertEqual(d[1], 2)
|
|
self.assertEqual(d[3], 4)
|
|
self.assert_(2 not in d)
|
|
self.assert_(2 not in d.keys())
|
|
self.assertEqual(d[2], 42)
|
|
class E(dict):
|
|
def __missing__(self, key):
|
|
raise RuntimeError(key)
|
|
e = E()
|
|
try:
|
|
e[42]
|
|
except RuntimeError as err:
|
|
self.assertEqual(err.args, (42,))
|
|
else:
|
|
self.fail("e[42] didn't raise RuntimeError")
|
|
class F(dict):
|
|
def __init__(self):
|
|
# An instance variable __missing__ should have no effect
|
|
self.__missing__ = lambda key: None
|
|
f = F()
|
|
try:
|
|
f[42]
|
|
except KeyError as err:
|
|
self.assertEqual(err.args, (42,))
|
|
else:
|
|
self.fail("f[42] didn't raise KeyError")
|
|
class G(dict):
|
|
pass
|
|
g = G()
|
|
try:
|
|
g[42]
|
|
except KeyError as err:
|
|
self.assertEqual(err.args, (42,))
|
|
else:
|
|
self.fail("g[42] didn't raise KeyError")
|
|
|
|
def test_tuple_keyerror(self):
|
|
# SF #1576657
|
|
d = {}
|
|
try:
|
|
d[(1,)]
|
|
except KeyError as e:
|
|
self.assertEqual(e.args, ((1,),))
|
|
else:
|
|
self.fail("missing KeyError")
|
|
|
|
def test_bad_key(self):
|
|
# Dictionary lookups should fail if __cmp__() raises an exception.
|
|
class CustomException(Exception):
|
|
pass
|
|
|
|
class BadDictKey:
|
|
def __hash__(self):
|
|
return hash(self.__class__)
|
|
|
|
def __eq__(self, other):
|
|
if isinstance(other, self.__class__):
|
|
raise CustomException
|
|
return other
|
|
|
|
d = {}
|
|
x1 = BadDictKey()
|
|
x2 = BadDictKey()
|
|
d[x1] = 1
|
|
for stmt in ['d[x2] = 2',
|
|
'z = d[x2]',
|
|
'x2 in d',
|
|
'd.get(x2)',
|
|
'd.setdefault(x2, 42)',
|
|
'd.pop(x2)',
|
|
'd.update({x2: 2})']:
|
|
try:
|
|
exec(stmt, locals())
|
|
except CustomException:
|
|
pass
|
|
else:
|
|
self.fail("Statement %r didn't raise exception" % stmt)
|
|
|
|
def test_resize1(self):
|
|
# Dict resizing bug, found by Jack Jansen in 2.2 CVS development.
|
|
# This version got an assert failure in debug build, infinite loop in
|
|
# release build. Unfortunately, provoking this kind of stuff requires
|
|
# a mix of inserts and deletes hitting exactly the right hash codes in
|
|
# exactly the right order, and I can't think of a randomized approach
|
|
# that would be *likely* to hit a failing case in reasonable time.
|
|
|
|
d = {}
|
|
for i in range(5):
|
|
d[i] = i
|
|
for i in range(5):
|
|
del d[i]
|
|
for i in range(5, 9): # i==8 was the problem
|
|
d[i] = i
|
|
|
|
def test_resize2(self):
|
|
# Another dict resizing bug (SF bug #1456209).
|
|
# This caused Segmentation faults or Illegal instructions.
|
|
|
|
class X(object):
|
|
def __hash__(self):
|
|
return 5
|
|
def __eq__(self, other):
|
|
if resizing:
|
|
d.clear()
|
|
return False
|
|
d = {}
|
|
resizing = False
|
|
d[X()] = 1
|
|
d[X()] = 2
|
|
d[X()] = 3
|
|
d[X()] = 4
|
|
d[X()] = 5
|
|
# now trigger a resize
|
|
resizing = True
|
|
d[9] = 6
|
|
|
|
|
|
from test import mapping_tests
|
|
|
|
class GeneralMappingTests(mapping_tests.BasicTestMappingProtocol):
|
|
type2test = dict
|
|
|
|
class Dict(dict):
|
|
pass
|
|
|
|
class SubclassMappingTests(mapping_tests.BasicTestMappingProtocol):
|
|
type2test = Dict
|
|
|
|
def test_main():
|
|
test_support.run_unittest(
|
|
DictTest,
|
|
GeneralMappingTests,
|
|
SubclassMappingTests,
|
|
)
|
|
|
|
if __name__ == "__main__":
|
|
test_main()
|