<s> from __future__ import with_statement <EOL> from sys import version_info <EOL> from sandbox import Sandbox , HAVE_PYPY <EOL> from sandbox . test import createSandbox , createSandboxConfig , SkipTest <EOL> from sandbox . test . tools import capture_stdout <EOL> import os <EOL> def test_execute ( ) : <EOL> config = createSandboxConfig ( ) <EOL> if HAVE_PYPY : <EOL> config . _builtins_whitelist . add ( '<STR_LIT>' ) <EOL> if config . use_subprocess : <EOL> globals_builtins = set ( ) <EOL> else : <EOL> globals_builtins = set ( ( '<STR_LIT>' , ) ) <EOL> def test ( * lines , ** kw ) : <EOL> code = "<STR_LIT>" . join ( lines ) <EOL> Sandbox ( config ) . execute ( code , ** kw ) <EOL> test ( <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" ) <EOL> namespace = { '<STR_LIT:a>' : <NUM_LIT:1> } <EOL> test ( <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> globals = namespace ) <EOL> assert set ( namespace . keys ( ) ) == ( set ( ( '<STR_LIT:a>' , '<STR_LIT:x>' ) ) | globals_builtins ) <EOL> assert namespace [ '<STR_LIT:a>' ] == <NUM_LIT:10> <EOL> assert namespace [ '<STR_LIT:x>' ] == <NUM_LIT> <EOL> namespace = { '<STR_LIT:b>' : <NUM_LIT:2> } <EOL> test ( <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> locals = namespace ) <EOL> assert namespace == { '<STR_LIT:b>' : <NUM_LIT:20> , '<STR_LIT:x>' : <NUM_LIT> } <EOL> my_globals = { '<STR_LIT:a>' : <NUM_LIT:1> } <EOL> namespace = { '<STR_LIT:b>' : <NUM_LIT:2> } <EOL> test ( <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> globals = my_globals , <EOL> locals = namespace ) <EOL> assert set ( my_globals . keys ( ) ) == ( set ( ( '<STR_LIT:a>' , ) ) | globals_builtins ) <EOL> assert my_globals [ '<STR_LIT:a>' ] == <NUM_LIT:1> <EOL> assert namespace == { '<STR_LIT:a>' : <NUM_LIT:10> , '<STR_LIT:b>' : <NUM_LIT:20> , '<STR_LIT:x>' : <NUM_LIT> } <EOL> namespace = { } <EOL> test ( '<STR_LIT>' , locals = namespace ) <EOL> assert namespace == { '<STR_LIT:a>' : <NUM_LIT:1> } , namespace <EOL> def test_execfile ( ) : <EOL> import sys <EOL> if version_info >= ( <NUM_LIT:3> , <NUM_LIT:0> ) : <EOL> raise SkipTest ( "<STR_LIT>" ) <EOL> def execfile_test ( filename ) : <EOL> execfile ( filename ) <EOL> from tempfile import mktemp <EOL> filename = mktemp ( ) <EOL> with open ( filename , "<STR_LIT:w>" ) as script : <EOL> print >> script , "<STR_LIT>" <EOL> script . flush ( ) <EOL> try : <EOL> config = createSandboxConfig ( '<STR_LIT>' ) <EOL> try : <EOL> Sandbox ( config ) . call ( execfile_test , filename ) <EOL> except NameError , err : <EOL> assert str ( err ) == "<STR_LIT>" <EOL> else : <EOL> assert False <EOL> with capture_stdout ( ) as stdout : <EOL> execfile_test ( filename ) <EOL> sys . stdout . flush ( ) <EOL> stdout . seek ( <NUM_LIT:0> ) <EOL> output = stdout . read ( ) <EOL> assert output . startswith ( '<STR_LIT>' ) <EOL> finally : <EOL> os . unlink ( filename ) <EOL> def test_compile ( ) : <EOL> import sys <EOL> orig_displayhook = sys . displayhook <EOL> try : <EOL> results = [ ] <EOL> def displayhook ( value ) : <EOL> results . append ( value ) <EOL> sys . displayhook = displayhook <EOL> def _test_compile ( ) : <EOL> exec compile ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) in { } <EOL> assert results == [ <NUM_LIT:2> ] <EOL> config = createSandboxConfig ( ) <EOL> Sandbox ( config ) . call ( _test_compile ) <EOL> del results [ : ] <EOL> _test_compile ( ) <EOL> finally : <EOL> sys . displayhook = orig_displayhook </s>
