<s> from __future__ import with_statement <EOL> from sandbox import Sandbox , SandboxError , HAVE_CSANDBOX , HAVE_PYPY <EOL> from sandbox . test import ( createSandbox , createSandboxConfig , <EOL> SkipTest , TestException , skipIf ) <EOL> from sandbox . test . tools import read_first_line , READ_FILENAME <EOL> from sys import version_info <EOL> import os <EOL> def _get_file_type ( obj ) : <EOL> if version_info >= ( <NUM_LIT:3> , <NUM_LIT:0> ) : <EOL> if hasattr ( obj , "<STR_LIT>" ) : <EOL> obj = obj . buffer <EOL> if hasattr ( obj , "<STR_LIT>" ) : <EOL> obj = obj . raw <EOL> return type ( obj ) <EOL> def test_open_denied ( ) : <EOL> from errno import EACCES <EOL> def access_denied ( ) : <EOL> try : <EOL> read_first_line ( open ) <EOL> except IOError , err : <EOL> if err . errno == EACCES : <EOL> assert err . args [ <NUM_LIT:1> ] . startswith ( '<STR_LIT>' ) <EOL> else : <EOL> assert str ( err ) == '<STR_LIT>' <EOL> else : <EOL> assert False <EOL> createSandbox ( ) . call ( access_denied ) <EOL> read_first_line ( open ) <EOL> def test_open_whitelist ( ) : <EOL> config = createSandboxConfig ( ) <EOL> if config . cpython_restricted : <EOL> raise SkipTest ( "<STR_LIT>" ) <EOL> config . allowPath ( READ_FILENAME ) <EOL> Sandbox ( config ) . call ( read_first_line , open ) <EOL> def test_write_file ( ) : <EOL> from tempfile import mktemp <EOL> def write_file ( filename ) : <EOL> with open ( filename , "<STR_LIT:w>" ) as fp : <EOL> fp . write ( "<STR_LIT:test>" ) <EOL> filename = mktemp ( ) <EOL> def write_denied ( filename ) : <EOL> try : <EOL> write_file ( filename ) <EOL> except ValueError , err : <EOL> assert str ( err ) == "<STR_LIT>" <EOL> except IOError , err : <EOL> assert str ( err ) == "<STR_LIT>" <EOL> else : <EOL> assert False , "<STR_LIT>" <EOL> createSandbox ( ) . call ( write_denied , filename ) <EOL> filename = mktemp ( ) <EOL> write_file ( filename ) <EOL> os . unlink ( filename ) <EOL> def test_filetype_from_sys_stdout ( ) : <EOL> def get_file_type_from_stdout ( ) : <EOL> import sys <EOL> return _get_file_type ( sys . stdout ) <EOL> config = createSandboxConfig ( '<STR_LIT>' ) <EOL> def get_file_type_object ( ) : <EOL> file_type = get_file_type_from_stdout ( ) <EOL> try : <EOL> read_first_line ( file_type ) <EOL> except TypeError , err : <EOL> assert str ( err ) in ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> else : <EOL> assert False <EOL> Sandbox ( config ) . call ( get_file_type_object ) <EOL> file_type = get_file_type_from_stdout ( ) <EOL> read_first_line ( file_type ) <EOL> def test_filetype_from_open_file ( ) : <EOL> def get_file_type_from_open_file ( filename ) : <EOL> try : <EOL> with open ( filename , '<STR_LIT:rb>' ) as fp : <EOL> return _get_file_type ( fp ) <EOL> except SandboxError : <EOL> pass <EOL> try : <EOL> with open ( filename , '<STR_LIT:rb>' ) as fp : <EOL> return type ( fp ) <EOL> except SandboxError : <EOL> pass <EOL> raise TestException ( "<STR_LIT>" ) <EOL> filename = READ_FILENAME <EOL> config = createSandboxConfig ( ) <EOL> if config . cpython_restricted : <EOL> raise SkipTest ( "<STR_LIT>" ) <EOL> config . allowPath ( filename ) <EOL> def get_file_type_object ( ) : <EOL> file_type = get_file_type_from_open_file ( filename ) <EOL> try : <EOL> read_first_line ( file_type ) <EOL> except TypeError , err : <EOL> assert str ( err ) in ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> else : <EOL> assert False <EOL> Sandbox ( config ) . call ( get_file_type_object ) <EOL> file_type = get_file_type_from_open_file ( filename ) <EOL> read_first_line ( file_type ) <EOL> def test_method_proxy ( ) : <EOL> def get_file_type_from_stdout_method ( ) : <EOL> import sys <EOL> return _get_file_type ( sys . stdout . __enter__ ( ) ) <EOL> config = createSandboxConfig ( '<STR_LIT>' ) <EOL> def get_file_type_object ( ) : <EOL> file_type = get_file_type_from_stdout_method ( ) <EOL> try : <EOL> read_first_line ( file_type ) <EOL> except TypeError , err : <EOL> assert str ( err ) in ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> else : <EOL> assert False <EOL> Sandbox ( config ) . call ( get_file_type_object ) <EOL> file_type = get_file_type_from_stdout_method ( ) <EOL> read_first_line ( file_type ) <EOL> @ skipIf ( HAVE_PYPY , "<STR_LIT>" ) <EOL> def test_subclasses ( ) : <EOL> if version_info >= ( <NUM_LIT:3> , <NUM_LIT:0> ) : <EOL> raise SkipTest ( "<STR_LIT>" ) <EOL> def get_file_type_from_subclasses ( ) : <EOL> for subtype in object . __subclasses__ ( ) : <EOL> if subtype . __name__ == "<STR_LIT:file>" : <EOL> return subtype <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> def subclasses_denied ( ) : <EOL> try : <EOL> get_file_type_from_subclasses ( ) <EOL> except AttributeError , err : <EOL> assert str ( err ) == "<STR_LIT>" <EOL> else : <EOL> assert False <EOL> createSandbox ( ) . call ( subclasses_denied ) <EOL> file_type = get_file_type_from_subclasses ( ) <EOL> read_first_line ( file_type ) </s>
