@@ -54,32 +54,41 @@ def threadtest(q):
5454 assert False
5555 runtest ()
5656
57+ # various parts of unpythonic use dynvars, so get what's there before we insert anything for testing
58+ implicits = [k for k in dyn ]
59+ def noimplicits (kvs ):
60+ return tuple (sorted ((k , v ) for k , v in kvs if k not in implicits ))
5761 D = {"a" : 1 , "b" : 2 }
58- # various parts of unpythonic use dynvars, so get what's there before we insert anything
59- implicits = [k for k , v in dyn .items ()] # TODO: add a .keys() method?
60- def noimplicits (dic ):
61- return {k : dic [k ] for k in dic if k not in implicits }
6262 with dyn .let (** D ):
6363 # membership test
6464 assert "a" in dyn
6565 assert "c" not in dyn
66- # subscript syntax as an alternative way to refer to items
66+
67+ # subscript syntax as an alternative notation to refer to dynamic vars
6768 assert dyn .a is dyn ["a" ]
6869
69- # iteration works like in a dictionary
70- assert tuple ( sorted ( k for k in noimplicits (dyn ))) == ("a" , "b" )
70+ assert noimplicits ( dyn . items ()) == (( "a" , 1 ), ( "b" , 2 ))
71+ assert noimplicits (dyn . items ()) == ()
7172
72- # items() gives a snapshot, with values read at the time it was called
73- assert tuple (sorted (noimplicits (dyn ).items ())) == (("a" , 1 ), ("b" , 2 ))
73+ make_dynvar (im_always_there = True )
74+ with dyn .let (a = 1 , b = 2 ):
75+ assert noimplicits (dyn .items ()) == (("a" , 1 ), ("b" , 2 ),
76+ ("im_always_there" , True ))
77+ assert noimplicits (dyn .items ()) == (("im_always_there" , True ),)
7478
75- # safer (TOCTTOU) in complex situations to iterate over keys and retrieve the current dyn[k]
76- assert tuple (sorted ({k : dyn [k ] for k in noimplicits (dyn )}.items ())) == (("a" , 1 ), ("b" , 2 ))
79+ # dyn.asdict() returns a live view, which is essentially a collections.ChainMap
80+ view = dyn .asdict ()
81+ assert noimplicits (view .items ()) == (("im_always_there" , True ),)
82+ with dyn .let (a = 1 , b = 2 ):
83+ assert noimplicits (view .items ()) == (("a" , 1 ), ("b" , 2 ),
84+ ("im_always_there" , True ))
7785
78- make_dynvar (im_always_there = True )
86+ # as does dyn.items() (it's an abbreviation for dyn.asdict().items())
87+ items = dyn .items ()
88+ assert noimplicits (items ) == (("im_always_there" , True ),)
7989 with dyn .let (a = 1 , b = 2 ):
80- assert tuple (sorted (noimplicits (dyn ).items ())) == (("a" , 1 ), ("b" , 2 ),
81- ("im_always_there" , True ))
82- assert tuple (sorted (noimplicits (dyn ).items ())) == (("im_always_there" , True ),)
90+ assert noimplicits (items ) == (("a" , 1 ), ("b" , 2 ),
91+ ("im_always_there" , True ))
8392
8493 print ("All tests PASSED" )
8594
0 commit comments