<s> import unittest <EOL> from test import test_support <EOL> class TestSpecifics ( unittest . TestCase ) : <EOL> def test_exec_functional_style ( self ) : <EOL> g = { '<STR_LIT:b>' : <NUM_LIT:2> } <EOL> exec ( "<STR_LIT>" , g ) <EOL> self . assertEqual ( g [ '<STR_LIT:a>' ] , <NUM_LIT:3> ) <EOL> l = { '<STR_LIT:b>' : <NUM_LIT:3> } <EOL> g = { '<STR_LIT:b>' : <NUM_LIT:5> , '<STR_LIT:c>' : <NUM_LIT:7> } <EOL> exec ( "<STR_LIT>" , g , l ) <EOL> self . assertNotIn ( '<STR_LIT:a>' , g ) <EOL> self . assertEqual ( l [ '<STR_LIT:a>' ] , <NUM_LIT:10> ) <EOL> with self . assertRaises ( TypeError ) : <EOL> exec ( "<STR_LIT>" , ) <EOL> with self . assertRaises ( TypeError ) : <EOL> exec ( "<STR_LIT>" , { } , { } , { } ) <EOL> g = { '<STR_LIT:a>' : <NUM_LIT:3> , '<STR_LIT:b>' : <NUM_LIT:4> } <EOL> l = { } <EOL> with self . assertRaises ( TypeError ) : <EOL> exec ( "<STR_LIT>" , g ) in g <EOL> with self . assertRaises ( TypeError ) : <EOL> exec ( "<STR_LIT>" , g , l ) in g , l <EOL> def test_exec_with_general_mapping_for_locals ( self ) : <EOL> class M : <EOL> "<STR_LIT>" <EOL> def __getitem__ ( self , key ) : <EOL> if key == '<STR_LIT:a>' : <EOL> return <NUM_LIT:12> <EOL> raise KeyError <EOL> def __setitem__ ( self , key , value ) : <EOL> self . results = ( key , value ) <EOL> def keys ( self ) : <EOL> return list ( '<STR_LIT>' ) <EOL> m = M ( ) <EOL> g = globals ( ) <EOL> exec '<STR_LIT>' in g , m <EOL> self . assertEqual ( m . results , ( '<STR_LIT:z>' , <NUM_LIT:12> ) ) <EOL> try : <EOL> exec '<STR_LIT>' in g , m <EOL> except NameError : <EOL> pass <EOL> else : <EOL> self . fail ( '<STR_LIT>' ) <EOL> exec '<STR_LIT>' in g , m <EOL> self . assertEqual ( m . results , ( '<STR_LIT:z>' , list ( '<STR_LIT>' ) ) ) <EOL> exec '<STR_LIT>' in g , m <EOL> self . assertEqual ( m . results , ( '<STR_LIT:z>' , g ) ) <EOL> exec '<STR_LIT>' in g , m <EOL> self . assertEqual ( m . results , ( '<STR_LIT:z>' , m ) ) <EOL> try : <EOL> exec '<STR_LIT>' in m <EOL> except TypeError : <EOL> pass <EOL> else : <EOL> self . fail ( '<STR_LIT>' ) <EOL> class A : <EOL> "<STR_LIT>" <EOL> pass <EOL> m = A ( ) <EOL> try : <EOL> exec '<STR_LIT>' in g , m <EOL> except TypeError : <EOL> pass <EOL> else : <EOL> self . fail ( '<STR_LIT>' ) <EOL> class D ( dict ) : <EOL> def __getitem__ ( self , key ) : <EOL> if key == '<STR_LIT:a>' : <EOL> return <NUM_LIT:12> <EOL> return dict . __getitem__ ( self , key ) <EOL> d = D ( ) <EOL> exec '<STR_LIT>' in g , d <EOL> self . assertEqual ( d [ '<STR_LIT:z>' ] , <NUM_LIT:12> ) <EOL> def test_unicode_encoding ( self ) : <EOL> code = u"<STR_LIT>" <EOL> self . assertRaises ( SyntaxError , compile , code , "<STR_LIT>" , "<STR_LIT>" ) <EOL> def test_main ( ) : <EOL> test_support . run_unittest ( TestSpecifics ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> import sys , StringIO , re <EOL> orig_stdout = sys . stdout <EOL> out = StringIO . StringIO ( ) <EOL> sys . stdout = out <EOL> test_main ( ) <EOL> sys . stdout = orig_stdout <EOL> print re . sub ( "<STR_LIT>" , "<STR_LIT>" , out . getvalue ( ) ) </s>
