<s> import unittest <EOL> from test import support <EOL> import collections , random , string <EOL> import collections . abc <EOL> import gc , weakref <EOL> import pickle <EOL> class DictTest ( unittest . TestCase ) : <EOL> def test_invalid_keyword_arguments ( self ) : <EOL> class Custom ( dict ) : <EOL> pass <EOL> for invalid in { <NUM_LIT:1> : <NUM_LIT:2> } , Custom ( { <NUM_LIT:1> : <NUM_LIT:2> } ) : <EOL> with self . assertRaises ( TypeError ) : <EOL> dict ( ** invalid ) <EOL> with self . assertRaises ( TypeError ) : <EOL> { } . update ( ** invalid ) <EOL> def test_constructor ( self ) : <EOL> self . assertEqual ( dict ( ) , { } ) <EOL> self . assertIsNot ( dict ( ) , { } ) <EOL> def test_literal_constructor ( self ) : <EOL> for n in ( <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:6> , <NUM_LIT> , <NUM_LIT> ) : <EOL> items = [ ( '<STR_LIT>' . join ( random . sample ( string . ascii_letters , <NUM_LIT:8> ) ) , i ) <EOL> for i in range ( n ) ] <EOL> random . shuffle ( items ) <EOL> formatted_items = ( '<STR_LIT>' . format ( k , v ) for k , v in items ) <EOL> dictliteral = '<STR_LIT:{>' + '<STR_LIT:U+002CU+0020>' . join ( formatted_items ) + '<STR_LIT:}>' <EOL> self . assertEqual ( eval ( dictliteral ) , dict ( items ) ) <EOL> def test_bool ( self ) : <EOL> self . assertIs ( not { } , True ) <EOL> self . assertTrue ( { <NUM_LIT:1> : <NUM_LIT:2> } ) <EOL> self . assertIs ( bool ( { } ) , False ) <EOL> self . assertIs ( bool ( { <NUM_LIT:1> : <NUM_LIT:2> } ) , True ) <EOL> def test_keys ( self ) : <EOL> d = { } <EOL> self . assertEqual ( set ( d . keys ( ) ) , set ( ) ) <EOL> d = { '<STR_LIT:a>' : <NUM_LIT:1> , '<STR_LIT:b>' : <NUM_LIT:2> } <EOL> k = d . keys ( ) <EOL> self . assertEqual ( set ( k ) , { '<STR_LIT:a>' , '<STR_LIT:b>' } ) <EOL> self . assertIn ( '<STR_LIT:a>' , k ) <EOL> self . assertIn ( '<STR_LIT:b>' , k ) <EOL> self . assertIn ( '<STR_LIT:a>' , d ) <EOL> self . assertIn ( '<STR_LIT:b>' , d ) <EOL> self . assertRaises ( TypeError , d . keys , None ) <EOL> self . assertEqual ( repr ( dict ( a = <NUM_LIT:1> ) . keys ( ) ) , "<STR_LIT>" ) <EOL> def test_values ( self ) : <EOL> d = { } <EOL> self . assertEqual ( set ( d . values ( ) ) , set ( ) ) <EOL> d = { <NUM_LIT:1> : <NUM_LIT:2> } <EOL> self . assertEqual ( set ( d . values ( ) ) , { <NUM_LIT:2> } ) <EOL> self . assertRaises ( TypeError , d . values , None ) <EOL> self . assertEqual ( repr ( dict ( a = <NUM_LIT:1> ) . values ( ) ) , "<STR_LIT>" ) <EOL> def test_items ( self ) : <EOL> d = { } <EOL> self . assertEqual ( set ( d . items ( ) ) , set ( ) ) <EOL> d = { <NUM_LIT:1> : <NUM_LIT:2> } <EOL> self . assertEqual ( set ( d . items ( ) ) , { ( <NUM_LIT:1> , <NUM_LIT:2> ) } ) <EOL> self . assertRaises ( TypeError , d . items , None ) <EOL> self . assertEqual ( repr ( dict ( a = <NUM_LIT:1> ) . items ( ) ) , "<STR_LIT>" ) <EOL> def test_contains ( self ) : <EOL> d = { } <EOL> self . assertNotIn ( '<STR_LIT:a>' , d ) <EOL> self . assertFalse ( '<STR_LIT:a>' in d ) <EOL> self . assertTrue ( '<STR_LIT:a>' not in d ) <EOL> d = { '<STR_LIT:a>' : <NUM_LIT:1> , '<STR_LIT:b>' : <NUM_LIT:2> } <EOL> self . assertIn ( '<STR_LIT:a>' , d ) <EOL> self . assertIn ( '<STR_LIT:b>' , d ) <EOL> self . assertNotIn ( '<STR_LIT:c>' , d ) <EOL> self . assertRaises ( TypeError , d . __contains__ ) <EOL> def test_len ( self ) : <EOL> d = { } <EOL> self . assertEqual ( len ( d ) , <NUM_LIT:0> ) <EOL> d = { '<STR_LIT:a>' : <NUM_LIT:1> , '<STR_LIT:b>' : <NUM_LIT:2> } <EOL> self . assertEqual ( len ( d ) , <NUM_LIT:2> ) <EOL> def test_getitem ( self ) : <EOL> d = { '<STR_LIT:a>' : <NUM_LIT:1> , '<STR_LIT:b>' : <NUM_LIT:2> } <EOL> self . assertEqual ( d [ '<STR_LIT:a>' ] , <NUM_LIT:1> ) <EOL> self . assertEqual ( d [ '<STR_LIT:b>' ] , <NUM_LIT:2> ) <EOL> d [ '<STR_LIT:c>' ] = <NUM_LIT:3> <EOL> d [ '<STR_LIT:a>' ] = <NUM_LIT:4> <EOL> self . assertEqual ( d [ '<STR_LIT:c>' ] , <NUM_LIT:3> ) <EOL> self . assertEqual ( d [ '<STR_LIT:a>' ] , <NUM_LIT:4> ) <EOL> del d [ '<STR_LIT:b>' ] <EOL> self . assertEqual ( d , { '<STR_LIT:a>' : <NUM_LIT:4> , '<STR_LIT:c>' : <NUM_LIT:3> } ) <EOL> self . assertRaises ( TypeError , d . __getitem__ ) <EOL> class BadEq ( object ) : <EOL> def __eq__ ( self , other ) : <EOL> raise Exc ( ) <EOL> def __hash__ ( self ) : <EOL> return <NUM_LIT> <EOL> d = { } <EOL> d [ BadEq ( ) ] = <NUM_LIT> <EOL> self . assertRaises ( KeyError , d . __getitem__ , <NUM_LIT> ) <EOL> class Exc ( Exception ) : pass <EOL> class BadHash ( object ) : <EOL> fail = False <EOL> def __hash__ ( self ) : <EOL> if self . fail : <EOL> raise Exc ( ) <EOL> else : <EOL> return <NUM_LIT> <EOL> x = BadHash ( ) <EOL> d [ x ] = <NUM_LIT> <EOL> x . fail = True <EOL> self . assertRaises ( Exc , d . __getitem__ , x ) <EOL> def test_clear ( self ) : <EOL> d = { <NUM_LIT:1> : <NUM_LIT:1> , <NUM_LIT:2> : <NUM_LIT:2> , <NUM_LIT:3> : <NUM_LIT:3> } <EOL> d . clear ( ) <EOL> self . assertEqual ( d , { } ) <EOL> self . assertRaises ( TypeError , d . clear , None ) <EOL> def test_update ( self ) : <EOL> d = { } <EOL> d . update ( { <NUM_LIT:1> : <NUM_LIT:100> } ) <EOL> d . update ( { <NUM_LIT:2> : <NUM_LIT:20> } ) <EOL> d . update ( { <NUM_LIT:1> : <NUM_LIT:1> , <NUM_LIT:2> : <NUM_LIT:2> , <NUM_LIT:3> : <NUM_LIT:3> } ) <EOL> self . assertEqual ( d , { <NUM_LIT:1> : <NUM_LIT:1> , <NUM_LIT:2> : <NUM_LIT:2> , <NUM_LIT:3> : <NUM_LIT:3> } ) <EOL> d . update ( ) <EOL> self . assertEqual ( d , { <NUM_LIT:1> : <NUM_LIT:1> , <NUM_LIT:2> : <NUM_LIT:2> , <NUM_LIT:3> : <NUM_LIT:3> } ) <EOL> self . assertRaises ( ( TypeError , AttributeError ) , d . update , None ) <EOL> class SimpleUserDict : <EOL> def __init__ ( self ) : <EOL> self . d = { <NUM_LIT:1> : <NUM_LIT:1> , <NUM_LIT:2> : <NUM_LIT:2> , <NUM_LIT:3> : <NUM_LIT:3> } <EOL> def keys ( self ) : <EOL> return self . d . keys ( ) <EOL> def __getitem__ ( self , i ) : <EOL> return self . d [ i ] <EOL> d . clear ( ) <EOL> d . update ( SimpleUserDict ( ) ) <EOL> self . assertEqual ( d , { <NUM_LIT:1> : <NUM_LIT:1> , <NUM_LIT:2> : <NUM_LIT:2> , <NUM_LIT:3> : <NUM_LIT:3> } ) <EOL> class Exc ( Exception ) : pass <EOL> d . clear ( ) <EOL> class FailingUserDict : <EOL> def keys ( self ) : <EOL> raise Exc <EOL> self . assertRaises ( Exc , d . update , FailingUserDict ( ) ) <EOL> class FailingUserDict : <EOL> def keys ( self ) : <EOL> class BogonIter : <EOL> def __init__ ( self ) : <EOL> self . i = <NUM_LIT:1> <EOL> def __iter__ ( self ) : <EOL> return self <EOL> def __next__ ( self ) : <EOL> if self . i : <EOL> self . i = <NUM_LIT:0> <EOL> return '<STR_LIT:a>' <EOL> raise Exc <EOL> return BogonIter ( ) <EOL> def __getitem__ ( self , key ) : <EOL> return key <EOL> self . assertRaises ( Exc , d . update , FailingUserDict ( ) ) <EOL> class FailingUserDict : <EOL> def keys ( self ) : <EOL> class BogonIter : <EOL> def __init__ ( self ) : <EOL> self . i = ord ( '<STR_LIT:a>' ) <EOL> def __iter__ ( self ) : <EOL> return self <EOL> def __next__ ( self ) : <EOL> if self . i <= ord ( '<STR_LIT:z>' ) : <EOL> rtn = chr ( self . i ) <EOL> self . i += <NUM_LIT:1> <EOL> return rtn <EOL> raise StopIteration <EOL> return BogonIter ( ) <EOL> def __getitem__ ( self , key ) : <EOL> raise Exc <EOL> self . assertRaises ( Exc , d . update , FailingUserDict ( ) ) <EOL> class badseq ( object ) : <EOL> def __iter__ ( self ) : <EOL> return self <EOL> def __next__ ( self ) : <EOL> raise Exc ( ) <EOL> self . assertRaises ( Exc , { } . update , badseq ( ) ) <EOL> self . assertRaises ( ValueError , { } . update , [ ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ) ] ) <EOL> def test_fromkeys ( self ) : <EOL> self . assertEqual ( dict . fromkeys ( '<STR_LIT:abc>' ) , { '<STR_LIT:a>' : None , '<STR_LIT:b>' : None , '<STR_LIT:c>' : None } ) <EOL> d = { } <EOL> self . assertIsNot ( d . fromkeys ( '<STR_LIT:abc>' ) , d ) <EOL> self . assertEqual ( d . fromkeys ( '<STR_LIT:abc>' ) , { '<STR_LIT:a>' : None , '<STR_LIT:b>' : None , '<STR_LIT:c>' : None } ) <EOL> self . assertEqual ( d . fromkeys ( ( <NUM_LIT:4> , <NUM_LIT:5> ) , <NUM_LIT:0> ) , { <NUM_LIT:4> : <NUM_LIT:0> , <NUM_LIT:5> : <NUM_LIT:0> } ) <EOL> self . assertEqual ( d . fromkeys ( [ ] ) , { } ) <EOL> def g ( ) : <EOL> yield <NUM_LIT:1> <EOL> self . assertEqual ( d . fromkeys ( g ( ) ) , { <NUM_LIT:1> : None } ) <EOL> self . assertRaises ( TypeError , { } . fromkeys , <NUM_LIT:3> ) <EOL> class dictlike ( dict ) : pass <EOL> self . assertEqual ( dictlike . fromkeys ( '<STR_LIT:a>' ) , { '<STR_LIT:a>' : None } ) <EOL> self . assertEqual ( dictlike ( ) . fromkeys ( '<STR_LIT:a>' ) , { '<STR_LIT:a>' : None } ) <EOL> self . assertIsInstance ( dictlike . fromkeys ( '<STR_LIT:a>' ) , dictlike ) <EOL> self . assertIsInstance ( dictlike ( ) . fromkeys ( '<STR_LIT:a>' ) , dictlike ) <EOL> class mydict ( dict ) : <EOL> def __new__ ( cls ) : <EOL> return collections . UserDict ( ) <EOL> ud = mydict . fromkeys ( '<STR_LIT>' ) <EOL> self . assertEqual ( ud , { '<STR_LIT:a>' : None , '<STR_LIT:b>' : None } ) <EOL> self . assertIsInstance ( ud , collections . UserDict ) <EOL> self . assertRaises ( TypeError , dict . fromkeys ) <EOL> class Exc ( Exception ) : pass <EOL> class baddict1 ( dict ) : <EOL> def __init__ ( self ) : <EOL> raise Exc ( ) <EOL> self . assertRaises ( Exc , baddict1 . fromkeys , [ <NUM_LIT:1> ] ) <EOL> class BadSeq ( object ) : <EOL> def __iter__ ( self ) : <EOL> return self <EOL> def __next__ ( self ) : <EOL> raise Exc ( ) <EOL> self . assertRaises ( Exc , dict . fromkeys , BadSeq ( ) ) <EOL> class baddict2 ( dict ) : <EOL> def __setitem__ ( self , key , value ) : <EOL> raise Exc ( ) <EOL> self . assertRaises ( Exc , baddict2 . fromkeys , [ <NUM_LIT:1> ] ) <EOL> d = dict ( zip ( range ( <NUM_LIT:6> ) , range ( <NUM_LIT:6> ) ) ) <EOL> self . assertEqual ( dict . fromkeys ( d , <NUM_LIT:0> ) , dict ( zip ( range ( <NUM_LIT:6> ) , [ <NUM_LIT:0> ] * <NUM_LIT:6> ) ) ) <EOL> class baddict3 ( dict ) : <EOL> def __new__ ( cls ) : <EOL> return d <EOL> d = { i : i for i in range ( <NUM_LIT:10> ) } <EOL> res = d . copy ( ) <EOL> res . update ( a = None , b = None , c = None ) <EOL> self . assertEqual ( baddict3 . fromkeys ( { "<STR_LIT:a>" , "<STR_LIT:b>" , "<STR_LIT:c>" } ) , res ) <EOL> def test_copy ( self ) : <EOL> d = { <NUM_LIT:1> : <NUM_LIT:1> , <NUM_LIT:2> : <NUM_LIT:2> , <NUM_LIT:3> : <NUM_LIT:3> } <EOL> self . assertEqual ( d . copy ( ) , { <NUM_LIT:1> : <NUM_LIT:1> , <NUM_LIT:2> : <NUM_LIT:2> , <NUM_LIT:3> : <NUM_LIT:3> } ) <EOL> self . assertEqual ( { } . copy ( ) , { } ) <EOL> self . assertRaises ( TypeError , d . copy , None ) <EOL> def test_get ( self ) : <EOL> d = { } <EOL> self . assertIs ( d . get ( '<STR_LIT:c>' ) , None ) <EOL> self . assertEqual ( d . get ( '<STR_LIT:c>' , <NUM_LIT:3> ) , <NUM_LIT:3> ) <EOL> d = { '<STR_LIT:a>' : <NUM_LIT:1> , '<STR_LIT:b>' : <NUM_LIT:2> } <EOL> self . assertIs ( d . get ( '<STR_LIT:c>' ) , None ) <EOL> self . assertEqual ( d . get ( '<STR_LIT:c>' , <NUM_LIT:3> ) , <NUM_LIT:3> ) <EOL> self . assertEqual ( d . get ( '<STR_LIT:a>' ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( d . get ( '<STR_LIT:a>' , <NUM_LIT:3> ) , <NUM_LIT:1> ) <EOL> self . assertRaises ( TypeError , d . get ) <EOL> self . assertRaises ( TypeError , d . get , None , None , None ) <EOL> def test_setdefault ( self ) : <EOL> d = { } <EOL> self . assertIs ( d . setdefault ( '<STR_LIT>' ) , None ) <EOL> d . setdefault ( '<STR_LIT>' , [ ] ) <EOL> self . assertIs ( d . setdefault ( '<STR_LIT>' ) , None ) <EOL> d . setdefault ( '<STR_LIT:key>' , [ ] ) . append ( <NUM_LIT:3> ) <EOL> self . assertEqual ( d [ '<STR_LIT:key>' ] [ <NUM_LIT:0> ] , <NUM_LIT:3> ) <EOL> d . setdefault ( '<STR_LIT:key>' , [ ] ) . append ( <NUM_LIT:4> ) <EOL> self . assertEqual ( len ( d [ '<STR_LIT:key>' ] ) , <NUM_LIT:2> ) <EOL> self . assertRaises ( TypeError , d . setdefault ) <EOL> class Exc ( Exception ) : pass <EOL> class BadHash ( object ) : <EOL> fail = False <EOL> def __hash__ ( self ) : <EOL> if self . fail : <EOL> raise Exc ( ) <EOL> else : <EOL> return <NUM_LIT> <EOL> x = BadHash ( ) <EOL> d [ x ] = <NUM_LIT> <EOL> x . fail = True <EOL> self . assertRaises ( Exc , d . setdefault , x , [ ] ) <EOL> def test_setdefault_atomic ( self ) : <EOL> class Hashed ( object ) : <EOL> def __init__ ( self ) : <EOL> self . hash_count = <NUM_LIT:0> <EOL> self . eq_count = <NUM_LIT:0> <EOL> def __hash__ ( self ) : <EOL> self . hash_count += <NUM_LIT:1> <EOL> return <NUM_LIT> <EOL> def __eq__ ( self , other ) : <EOL> self . eq_count += <NUM_LIT:1> <EOL> return id ( self ) == id ( other ) <EOL> hashed1 = Hashed ( ) <EOL> y = { hashed1 : <NUM_LIT:5> } <EOL> hashed2 = Hashed ( ) <EOL> y . setdefault ( hashed2 , [ ] ) <EOL> self . assertEqual ( hashed1 . hash_count , <NUM_LIT:1> ) <EOL> self . assertEqual ( hashed2 . hash_count , <NUM_LIT:1> ) <EOL> self . assertEqual ( hashed1 . eq_count + hashed2 . eq_count , <NUM_LIT:1> ) <EOL> def test_setitem_atomic_at_resize ( self ) : <EOL> class Hashed ( object ) : <EOL> def __init__ ( self ) : <EOL> self . hash_count = <NUM_LIT:0> <EOL> self . eq_count = <NUM_LIT:0> <EOL> def __hash__ ( self ) : <EOL> self . hash_count += <NUM_LIT:1> <EOL> return <NUM_LIT> <EOL> def __eq__ ( self , other ) : <EOL> self . eq_count += <NUM_LIT:1> <EOL> return id ( self ) == id ( other ) <EOL> hashed1 = Hashed ( ) <EOL> y = { hashed1 : <NUM_LIT:5> , <NUM_LIT:0> : <NUM_LIT:0> , <NUM_LIT:1> : <NUM_LIT:1> , <NUM_LIT:2> : <NUM_LIT:2> , <NUM_LIT:3> : <NUM_LIT:3> } <EOL> hashed2 = Hashed ( ) <EOL> y [ hashed2 ] = [ ] <EOL> self . assertEqual ( hashed1 . hash_count , <NUM_LIT:1> ) <EOL> self . assertEqual ( hashed2 . hash_count , <NUM_LIT:1> ) <EOL> self . assertEqual ( hashed1 . eq_count + hashed2 . eq_count , <NUM_LIT:1> ) <EOL> def test_popitem ( self ) : <EOL> for copymode in - <NUM_LIT:1> , + <NUM_LIT:1> : <EOL> for log2size in range ( <NUM_LIT:12> ) : <EOL> size = <NUM_LIT:2> ** log2size <EOL> a = { } <EOL> b = { } <EOL> for i in range ( size ) : <EOL> a [ repr ( i ) ] = i <EOL> if copymode < <NUM_LIT:0> : <EOL> b [ repr ( i ) ] = i <EOL> if copymode > <NUM_LIT:0> : <EOL> b = a . copy ( ) <EOL> for i in range ( size ) : <EOL> ka , va = ta = a . popitem ( ) <EOL> self . assertEqual ( va , int ( ka ) ) <EOL> kb , vb = tb = b . popitem ( ) <EOL> self . assertEqual ( vb , int ( kb ) ) <EOL> self . assertFalse ( copymode < <NUM_LIT:0> and ta != tb ) <EOL> self . assertFalse ( a ) <EOL> self . assertFalse ( b ) <EOL> d = { } <EOL> self . assertRaises ( KeyError , d . popitem ) <EOL> def test_pop ( self ) : <EOL> d = { } <EOL> k , v = '<STR_LIT:abc>' , '<STR_LIT>' <EOL> d [ k ] = v <EOL> self . assertRaises ( KeyError , d . pop , '<STR_LIT>' ) <EOL> self . assertEqual ( d . pop ( k ) , v ) <EOL> self . assertEqual ( len ( d ) , <NUM_LIT:0> ) <EOL> self . assertRaises ( KeyError , d . pop , k ) <EOL> self . assertEqual ( d . pop ( k , v ) , v ) <EOL> d [ k ] = v <EOL> self . assertEqual ( d . pop ( k , <NUM_LIT:1> ) , v ) <EOL> self . assertRaises ( TypeError , d . pop ) <EOL> class Exc ( Exception ) : pass <EOL> class BadHash ( object ) : <EOL> fail = False <EOL> def __hash__ ( self ) : <EOL> if self . fail : <EOL> raise Exc ( ) <EOL> else : <EOL> return <NUM_LIT> <EOL> x = BadHash ( ) <EOL> d [ x ] = <NUM_LIT> <EOL> x . fail = True <EOL> self . assertRaises ( Exc , d . pop , x ) <EOL> def test_mutating_iteration ( self ) : <EOL> d = { } <EOL> d [ <NUM_LIT:1> ] = <NUM_LIT:1> <EOL> with self . assertRaises ( RuntimeError ) : <EOL> for i in d : <EOL> d [ i + <NUM_LIT:1> ] = <NUM_LIT:1> <EOL> def test_mutating_lookup ( self ) : <EOL> class NastyKey : <EOL> mutate_dict = None <EOL> def __init__ ( self , value ) : <EOL> self . value = value <EOL> def __hash__ ( self ) : <EOL> return <NUM_LIT:1> <EOL> def __eq__ ( self , other ) : <EOL> if NastyKey . mutate_dict : <EOL> mydict , key = NastyKey . mutate_dict <EOL> NastyKey . mutate_dict = None <EOL> del mydict [ key ] <EOL> return self . value == other . value <EOL> key1 = NastyKey ( <NUM_LIT:1> ) <EOL> key2 = NastyKey ( <NUM_LIT:2> ) <EOL> d = { key1 : <NUM_LIT:1> } <EOL> NastyKey . mutate_dict = ( d , key1 ) <EOL> d [ key2 ] = <NUM_LIT:2> <EOL> self . assertEqual ( d , { key2 : <NUM_LIT:2> } ) <EOL> def test_repr ( self ) : <EOL> d = { } <EOL> self . assertEqual ( repr ( d ) , '<STR_LIT:{}>' ) <EOL> d [ <NUM_LIT:1> ] = <NUM_LIT:2> <EOL> self . assertEqual ( repr ( d ) , '<STR_LIT>' ) <EOL> d = { } <EOL> d [ <NUM_LIT:1> ] = d <EOL> self . assertEqual ( repr ( d ) , '<STR_LIT>' ) <EOL> class Exc ( Exception ) : pass <EOL> class BadRepr ( object ) : <EOL> def __repr__ ( self ) : <EOL> raise Exc ( ) <EOL> d = { <NUM_LIT:1> : BadRepr ( ) } <EOL> self . assertRaises ( Exc , repr , d ) <EOL> def test_eq ( self ) : <EOL> self . assertEqual ( { } , { } ) <EOL> self . assertEqual ( { <NUM_LIT:1> : <NUM_LIT:2> } , { <NUM_LIT:1> : <NUM_LIT:2> } ) <EOL> class Exc ( Exception ) : pass <EOL> class BadCmp ( object ) : <EOL> def __eq__ ( self , other ) : <EOL> raise Exc ( ) <EOL> def __hash__ ( self ) : <EOL> return <NUM_LIT:1> <EOL> d1 = { BadCmp ( ) : <NUM_LIT:1> } <EOL> d2 = { <NUM_LIT:1> : <NUM_LIT:1> } <EOL> with self . assertRaises ( Exc ) : <EOL> d1 == d2 <EOL> def test_keys_contained ( self ) : <EOL> self . helper_keys_contained ( lambda x : x . keys ( ) ) <EOL> self . helper_keys_contained ( lambda x : x . items ( ) ) <EOL> def helper_keys_contained ( self , fn ) : <EOL> empty = fn ( dict ( ) ) <EOL> empty2 = fn ( dict ( ) ) <EOL> smaller = fn ( { <NUM_LIT:1> : <NUM_LIT:1> , <NUM_LIT:2> : <NUM_LIT:2> } ) <EOL> larger = fn ( { <NUM_LIT:1> : <NUM_LIT:1> , <NUM_LIT:2> : <NUM_LIT:2> , <NUM_LIT:3> : <NUM_LIT:3> } ) <EOL> larger2 = fn ( { <NUM_LIT:1> : <NUM_LIT:1> , <NUM_LIT:2> : <NUM_LIT:2> , <NUM_LIT:3> : <NUM_LIT:3> } ) <EOL> larger3 = fn ( { <NUM_LIT:4> : <NUM_LIT:1> , <NUM_LIT:2> : <NUM_LIT:2> , <NUM_LIT:3> : <NUM_LIT:3> } ) <EOL> self . assertTrue ( smaller < larger ) <EOL> self . assertTrue ( smaller <= larger ) <EOL> self . assertTrue ( larger > smaller ) <EOL> self . assertTrue ( larger >= smaller ) <EOL> self . assertFalse ( smaller >= larger ) <EOL> self . assertFalse ( smaller > larger ) <EOL> self . assertFalse ( larger <= smaller ) <EOL> self . assertFalse ( larger < smaller ) <EOL> self . assertFalse ( smaller < larger3 ) <EOL> self . assertFalse ( smaller <= larger3 ) <EOL> self . assertFalse ( larger3 > smaller ) <EOL> self . assertFalse ( larger3 >= smaller ) <EOL> self . assertTrue ( larger2 >= larger ) <EOL> self . assertTrue ( larger2 <= larger ) <EOL> self . assertFalse ( larger2 > larger ) <EOL> self . assertFalse ( larger2 < larger ) <EOL> self . assertTrue ( larger == larger2 ) <EOL> self . assertTrue ( smaller != larger ) <EOL> self . assertTrue ( empty == empty2 ) <EOL> self . assertFalse ( empty != empty2 ) <EOL> self . assertFalse ( empty == smaller ) <EOL> self . assertTrue ( empty != smaller ) <EOL> self . assertTrue ( larger != larger3 ) <EOL> self . assertFalse ( larger == larger3 ) <EOL> def test_errors_in_view_containment_check ( self ) : <EOL> class C : <EOL> def __eq__ ( self , other ) : <EOL> raise RuntimeError <EOL> d1 = { <NUM_LIT:1> : C ( ) } <EOL> d2 = { <NUM_LIT:1> : C ( ) } <EOL> with self . assertRaises ( RuntimeError ) : <EOL> d1 . items ( ) == d2 . items ( ) <EOL> with self . assertRaises ( RuntimeError ) : <EOL> d1 . items ( ) != d2 . items ( ) <EOL> with self . assertRaises ( RuntimeError ) : <EOL> d1 . items ( ) <= d2 . items ( ) <EOL> with self . assertRaises ( RuntimeError ) : <EOL> d1 . items ( ) >= d2 . items ( ) <EOL> d3 = { <NUM_LIT:1> : C ( ) , <NUM_LIT:2> : C ( ) } <EOL> with self . assertRaises ( RuntimeError ) : <EOL> d2 . items ( ) < d3 . items ( ) <EOL> with self . assertRaises ( RuntimeError ) : <EOL> d3 . items ( ) > d2 . items ( ) <EOL> def test_dictview_set_operations_on_keys ( self ) : <EOL> k1 = { <NUM_LIT:1> : <NUM_LIT:1> , <NUM_LIT:2> : <NUM_LIT:2> } . keys ( ) <EOL> k2 = { <NUM_LIT:1> : <NUM_LIT:1> , <NUM_LIT:2> : <NUM_LIT:2> , <NUM_LIT:3> : <NUM_LIT:3> } . keys ( ) <EOL> k3 = { <NUM_LIT:4> : <NUM_LIT:4> } . keys ( ) <EOL> self . assertEqual ( k1 - k2 , set ( ) ) <EOL> self . assertEqual ( k1 - k3 , { <NUM_LIT:1> , <NUM_LIT:2> } ) <EOL> self . assertEqual ( k2 - k1 , { <NUM_LIT:3> } ) <EOL> self . assertEqual ( k3 - k1 , { <NUM_LIT:4> } ) <EOL> self . assertEqual ( k1 & k2 , { <NUM_LIT:1> , <NUM_LIT:2> } ) <EOL> self . assertEqual ( k1 & k3 , set ( ) ) <EOL> self . assertEqual ( k1 | k2 , { <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> } ) <EOL> self . assertEqual ( k1 ^ k2 , { <NUM_LIT:3> } ) <EOL> self . assertEqual ( k1 ^ k3 , { <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:4> } ) <EOL> def test_dictview_set_operations_on_items ( self ) : <EOL> k1 = { <NUM_LIT:1> : <NUM_LIT:1> , <NUM_LIT:2> : <NUM_LIT:2> } . items ( ) <EOL> k2 = { <NUM_LIT:1> : <NUM_LIT:1> , <NUM_LIT:2> : <NUM_LIT:2> , <NUM_LIT:3> : <NUM_LIT:3> } . items ( ) <EOL> k3 = { <NUM_LIT:4> : <NUM_LIT:4> } . items ( ) <EOL> self . assertEqual ( k1 - k2 , set ( ) ) <EOL> self . assertEqual ( k1 - k3 , { ( <NUM_LIT:1> , <NUM_LIT:1> ) , ( <NUM_LIT:2> , <NUM_LIT:2> ) } ) <EOL> self . assertEqual ( k2 - k1 , { ( <NUM_LIT:3> , <NUM_LIT:3> ) } ) <EOL> self . assertEqual ( k3 - k1 , { ( <NUM_LIT:4> , <NUM_LIT:4> ) } ) <EOL> self . assertEqual ( k1 & k2 , { ( <NUM_LIT:1> , <NUM_LIT:1> ) , ( <NUM_LIT:2> , <NUM_LIT:2> ) } ) <EOL> self . assertEqual ( k1 & k3 , set ( ) ) <EOL> self . assertEqual ( k1 | k2 , { ( <NUM_LIT:1> , <NUM_LIT:1> ) , ( <NUM_LIT:2> , <NUM_LIT:2> ) , ( <NUM_LIT:3> , <NUM_LIT:3> ) } ) <EOL> self . assertEqual ( k1 ^ k2 , { ( <NUM_LIT:3> , <NUM_LIT:3> ) } ) <EOL> self . assertEqual ( k1 ^ k3 , { ( <NUM_LIT:1> , <NUM_LIT:1> ) , ( <NUM_LIT:2> , <NUM_LIT:2> ) , ( <NUM_LIT:4> , <NUM_LIT:4> ) } ) <EOL> def test_dictview_mixed_set_operations ( self ) : <EOL> self . assertTrue ( { <NUM_LIT:1> : <NUM_LIT:1> } . keys ( ) == { <NUM_LIT:1> } ) <EOL> self . assertTrue ( { <NUM_LIT:1> } == { <NUM_LIT:1> : <NUM_LIT:1> } . keys ( ) ) <EOL> self . assertEqual ( { <NUM_LIT:1> : <NUM_LIT:1> } . keys ( ) | { <NUM_LIT:2> } , { <NUM_LIT:1> , <NUM_LIT:2> } ) <EOL> self . assertEqual ( { <NUM_LIT:2> } | { <NUM_LIT:1> : <NUM_LIT:1> } . keys ( ) , { <NUM_LIT:1> , <NUM_LIT:2> } ) <EOL> self . assertTrue ( { <NUM_LIT:1> : <NUM_LIT:1> } . items ( ) == { ( <NUM_LIT:1> , <NUM_LIT:1> ) } ) <EOL> self . assertTrue ( { ( <NUM_LIT:1> , <NUM_LIT:1> ) } == { <NUM_LIT:1> : <NUM_LIT:1> } . items ( ) ) <EOL> self . assertEqual ( { <NUM_LIT:1> : <NUM_LIT:1> } . items ( ) | { <NUM_LIT:2> } , { ( <NUM_LIT:1> , <NUM_LIT:1> ) , <NUM_LIT:2> } ) <EOL> self . assertEqual ( { <NUM_LIT:2> } | { <NUM_LIT:1> : <NUM_LIT:1> } . items ( ) , { ( <NUM_LIT:1> , <NUM_LIT:1> ) , <NUM_LIT:2> } ) <EOL> def test_missing ( self ) : <EOL> self . assertFalse ( hasattr ( dict , "<STR_LIT>" ) ) <EOL> self . assertFalse ( hasattr ( { } , "<STR_LIT>" ) ) <EOL> class D ( dict ) : <EOL> def __missing__ ( self , key ) : <EOL> return <NUM_LIT> <EOL> d = D ( { <NUM_LIT:1> : <NUM_LIT:2> , <NUM_LIT:3> : <NUM_LIT:4> } ) <EOL> self . assertEqual ( d [ <NUM_LIT:1> ] , <NUM_LIT:2> ) <EOL> self . assertEqual ( d [ <NUM_LIT:3> ] , <NUM_LIT:4> ) <EOL> self . assertNotIn ( <NUM_LIT:2> , d ) <EOL> self . assertNotIn ( <NUM_LIT:2> , d . keys ( ) ) <EOL> self . assertEqual ( d [ <NUM_LIT:2> ] , <NUM_LIT> ) <EOL> class E ( dict ) : <EOL> def __missing__ ( self , key ) : <EOL> raise RuntimeError ( key ) <EOL> e = E ( ) <EOL> with self . assertRaises ( RuntimeError ) as c : <EOL> e [ <NUM_LIT> ] <EOL> self . assertEqual ( c . exception . args , ( <NUM_LIT> , ) ) <EOL> class F ( dict ) : <EOL> def __init__ ( self ) : <EOL> self . __missing__ = lambda key : None <EOL> f = F ( ) <EOL> with self . assertRaises ( KeyError ) as c : <EOL> f [ <NUM_LIT> ] <EOL> self . assertEqual ( c . exception . args , ( <NUM_LIT> , ) ) <EOL> class G ( dict ) : <EOL> pass <EOL> g = G ( ) <EOL> with self . assertRaises ( KeyError ) as c : <EOL> g [ <NUM_LIT> ] <EOL> self . assertEqual ( c . exception . args , ( <NUM_LIT> , ) ) <EOL> def test_tuple_keyerror ( self ) : <EOL> d = { } <EOL> with self . assertRaises ( KeyError ) as c : <EOL> d [ ( <NUM_LIT:1> , ) ] <EOL> self . assertEqual ( c . exception . args , ( ( <NUM_LIT:1> , ) , ) ) <EOL> def test_bad_key ( self ) : <EOL> class CustomException ( Exception ) : <EOL> pass <EOL> class BadDictKey : <EOL> def __hash__ ( self ) : <EOL> return hash ( self . __class__ ) <EOL> def __eq__ ( self , other ) : <EOL> if isinstance ( other , self . __class__ ) : <EOL> raise CustomException <EOL> return other <EOL> d = { } <EOL> x1 = BadDictKey ( ) <EOL> x2 = BadDictKey ( ) <EOL> d [ x1 ] = <NUM_LIT:1> <EOL> for stmt in [ '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ] : <EOL> with self . assertRaises ( CustomException ) : <EOL> exec ( stmt , locals ( ) ) <EOL> def test_resize1 ( self ) : <EOL> d = { } <EOL> for i in range ( <NUM_LIT:5> ) : <EOL> d [ i ] = i <EOL> for i in range ( <NUM_LIT:5> ) : <EOL> del d [ i ] <EOL> for i in range ( <NUM_LIT:5> , <NUM_LIT:9> ) : <EOL> d [ i ] = i <EOL> def test_resize2 ( self ) : <EOL> class X ( object ) : <EOL> def __hash__ ( self ) : <EOL> return <NUM_LIT:5> <EOL> def __eq__ ( self , other ) : <EOL> if resizing : <EOL> d . clear ( ) <EOL> return False <EOL> d = { } <EOL> resizing = False <EOL> d [ X ( ) ] = <NUM_LIT:1> <EOL> d [ X ( ) ] = <NUM_LIT:2> <EOL> d [ X ( ) ] = <NUM_LIT:3> <EOL> d [ X ( ) ] = <NUM_LIT:4> <EOL> d [ X ( ) ] = <NUM_LIT:5> <EOL> resizing = True <EOL> d [ <NUM_LIT:9> ] = <NUM_LIT:6> <EOL> def test_empty_presized_dict_in_freelist ( self ) : <EOL> with self . assertRaises ( ZeroDivisionError ) : <EOL> d = { '<STR_LIT:a>' : <NUM_LIT:1> // <NUM_LIT:0> , '<STR_LIT:b>' : None , '<STR_LIT:c>' : None , '<STR_LIT:d>' : None , '<STR_LIT:e>' : None , <EOL> '<STR_LIT:f>' : None , '<STR_LIT:g>' : None , '<STR_LIT:h>' : None } <EOL> d = { } <EOL> def test_container_iterator ( self ) : <EOL> class C ( object ) : <EOL> pass <EOL> views = ( dict . items , dict . values , dict . keys ) <EOL> for v in views : <EOL> obj = C ( ) <EOL> ref = weakref . ref ( obj ) <EOL> container = { obj : <NUM_LIT:1> } <EOL> obj . v = v ( container ) <EOL> obj . x = iter ( obj . v ) <EOL> del obj , container <EOL> gc . collect ( ) <EOL> self . assertIs ( ref ( ) , None , "<STR_LIT>" ) <EOL> def _not_tracked ( self , t ) : <EOL> gc . collect ( ) <EOL> gc . collect ( ) <EOL> self . assertFalse ( gc . is_tracked ( t ) , t ) <EOL> def _tracked ( self , t ) : <EOL> self . assertTrue ( gc . is_tracked ( t ) , t ) <EOL> gc . collect ( ) <EOL> gc . collect ( ) <EOL> self . assertTrue ( gc . is_tracked ( t ) , t ) <EOL> @ support . cpython_only <EOL> def test_track_literals ( self ) : <EOL> x , y , z , w = <NUM_LIT> , "<STR_LIT:a>" , ( <NUM_LIT:1> , None ) , [ ] <EOL> self . _not_tracked ( { } ) <EOL> self . _not_tracked ( { x : ( ) , y : x , z : <NUM_LIT:1> } ) <EOL> self . _not_tracked ( { <NUM_LIT:1> : "<STR_LIT:a>" , "<STR_LIT:b>" : <NUM_LIT:2> } ) <EOL> self . _not_tracked ( { <NUM_LIT:1> : <NUM_LIT:2> , ( None , True , False , ( ) ) : int } ) <EOL> self . _not_tracked ( { <NUM_LIT:1> : object ( ) } ) <EOL> self . _tracked ( { <NUM_LIT:1> : [ ] } ) <EOL> self . _tracked ( { <NUM_LIT:1> : ( [ ] , ) } ) <EOL> self . _tracked ( { <NUM_LIT:1> : { } } ) <EOL> self . _tracked ( { <NUM_LIT:1> : set ( ) } ) <EOL> @ support . cpython_only <EOL> def test_track_dynamic ( self ) : <EOL> class MyObject ( object ) : <EOL> pass <EOL> x , y , z , w , o = <NUM_LIT> , "<STR_LIT:a>" , ( <NUM_LIT:1> , object ( ) ) , [ ] , MyObject ( ) <EOL> d = dict ( ) <EOL> self . _not_tracked ( d ) <EOL> d [ <NUM_LIT:1> ] = "<STR_LIT:a>" <EOL> self . _not_tracked ( d ) <EOL> d [ y ] = <NUM_LIT:2> <EOL> self . _not_tracked ( d ) <EOL> d [ z ] = <NUM_LIT:3> <EOL> self . _not_tracked ( d ) <EOL> self . _not_tracked ( d . copy ( ) ) <EOL> d [ <NUM_LIT:4> ] = w <EOL> self . _tracked ( d ) <EOL> self . _tracked ( d . copy ( ) ) <EOL> d [ <NUM_LIT:4> ] = None <EOL> self . _not_tracked ( d ) <EOL> self . _not_tracked ( d . copy ( ) ) <EOL> d = dict ( ) <EOL> dd = dict ( ) <EOL> d [ <NUM_LIT:1> ] = dd <EOL> self . _not_tracked ( dd ) <EOL> self . _tracked ( d ) <EOL> dd [ <NUM_LIT:1> ] = d <EOL> self . _tracked ( dd ) <EOL> d = dict . fromkeys ( [ x , y , z ] ) <EOL> self . _not_tracked ( d ) <EOL> dd = dict ( ) <EOL> dd . update ( d ) <EOL> self . _not_tracked ( dd ) <EOL> d = dict . fromkeys ( [ x , y , z , o ] ) <EOL> self . _tracked ( d ) <EOL> dd = dict ( ) <EOL> dd . update ( d ) <EOL> self . _tracked ( dd ) <EOL> d = dict ( x = x , y = y , z = z ) <EOL> self . _not_tracked ( d ) <EOL> d = dict ( x = x , y = y , z = z , w = w ) <EOL> self . _tracked ( d ) <EOL> d = dict ( ) <EOL> d . update ( x = x , y = y , z = z ) <EOL> self . _not_tracked ( d ) <EOL> d . update ( w = w ) <EOL> self . _tracked ( d ) <EOL> d = dict ( [ ( x , y ) , ( z , <NUM_LIT:1> ) ] ) <EOL> self . _not_tracked ( d ) <EOL> d = dict ( [ ( x , y ) , ( z , w ) ] ) <EOL> self . _tracked ( d ) <EOL> d = dict ( ) <EOL> d . update ( [ ( x , y ) , ( z , <NUM_LIT:1> ) ] ) <EOL> self . _not_tracked ( d ) <EOL> d . update ( [ ( x , y ) , ( z , w ) ] ) <EOL> self . _tracked ( d ) <EOL> @ support . cpython_only <EOL> def test_track_subtypes ( self ) : <EOL> class MyDict ( dict ) : <EOL> pass <EOL> self . _tracked ( MyDict ( ) ) <EOL> def test_iterator_pickling ( self ) : <EOL> data = { <NUM_LIT:1> : "<STR_LIT:a>" , <NUM_LIT:2> : "<STR_LIT:b>" , <NUM_LIT:3> : "<STR_LIT:c>" } <EOL> it = iter ( data ) <EOL> d = pickle . dumps ( it ) <EOL> it = pickle . loads ( d ) <EOL> self . assertEqual ( sorted ( it ) , sorted ( data ) ) <EOL> it = pickle . loads ( d ) <EOL> try : <EOL> drop = next ( it ) <EOL> except StopIteration : <EOL> return <EOL> d = pickle . dumps ( it ) <EOL> it = pickle . loads ( d ) <EOL> del data [ drop ] <EOL> self . assertEqual ( sorted ( it ) , sorted ( data ) ) <EOL> def test_itemiterator_pickling ( self ) : <EOL> data = { <NUM_LIT:1> : "<STR_LIT:a>" , <NUM_LIT:2> : "<STR_LIT:b>" , <NUM_LIT:3> : "<STR_LIT:c>" } <EOL> itorg = iter ( data . items ( ) ) <EOL> d = pickle . dumps ( itorg ) <EOL> it = pickle . loads ( d ) <EOL> self . assertEqual ( dict ( it ) , data ) <EOL> it = pickle . loads ( d ) <EOL> drop = next ( it ) <EOL> d = pickle . dumps ( it ) <EOL> it = pickle . loads ( d ) <EOL> del data [ drop [ <NUM_LIT:0> ] ] <EOL> self . assertEqual ( dict ( it ) , data ) <EOL> def test_valuesiterator_pickling ( self ) : <EOL> data = { <NUM_LIT:1> : "<STR_LIT:a>" , <NUM_LIT:2> : "<STR_LIT:b>" , <NUM_LIT:3> : "<STR_LIT:c>" } <EOL> it = iter ( data . values ( ) ) <EOL> d = pickle . dumps ( it ) <EOL> it = pickle . loads ( d ) <EOL> self . assertEqual ( sorted ( list ( it ) ) , sorted ( list ( data . values ( ) ) ) ) <EOL> it = pickle . loads ( d ) <EOL> drop = next ( it ) <EOL> d = pickle . dumps ( it ) <EOL> it = pickle . loads ( d ) <EOL> values = list ( it ) + [ drop ] <EOL> self . assertEqual ( sorted ( values ) , sorted ( list ( data . values ( ) ) ) ) <EOL> def test_instance_dict_getattr_str_subclass ( self ) : <EOL> class Foo : <EOL> def __init__ ( self , msg ) : <EOL> self . msg = msg <EOL> f = Foo ( '<STR_LIT>' ) <EOL> class _str ( str ) : <EOL> pass <EOL> self . assertEqual ( f . msg , getattr ( f , _str ( '<STR_LIT>' ) ) ) <EOL> self . assertEqual ( f . msg , f . __dict__ [ _str ( '<STR_LIT>' ) ] ) <EOL> def test_object_set_item_single_instance_non_str_key ( self ) : <EOL> class Foo : pass <EOL> f = Foo ( ) <EOL> f . __dict__ [ <NUM_LIT:1> ] = <NUM_LIT:1> <EOL> f . a = '<STR_LIT:a>' <EOL> self . assertEqual ( f . __dict__ , { <NUM_LIT:1> : <NUM_LIT:1> , '<STR_LIT:a>' : '<STR_LIT:a>' } ) <EOL> from test import mapping_tests <EOL> class GeneralMappingTests ( mapping_tests . BasicTestMappingProtocol ) : <EOL> type2test = dict <EOL> class Dict ( dict ) : <EOL> pass <EOL> class SubclassMappingTests ( mapping_tests . BasicTestMappingProtocol ) : <EOL> type2test = Dict <EOL> def test_main ( ) : <EOL> support . run_unittest ( <EOL> DictTest , <EOL> GeneralMappingTests , <EOL> SubclassMappingTests , <EOL> ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> test_main ( ) </s>
