<s> from __future__ import absolute_import <EOL> import __builtin__ as BUILTINS_MODULE <EOL> from types import FrameType <EOL> from sys import version_info <EOL> import sys <EOL> from sandbox import SandboxError , HAVE_CSANDBOX <EOL> from . safe_open import _safe_open <EOL> from . safe_import import _safe_import <EOL> from . restorable_dict import RestorableDict <EOL> from . proxy import createReadOnlyObject <EOL> from . blacklist_proxy import createReadOnlyBuiltins <EOL> if HAVE_CSANDBOX : <EOL> from _sandbox import set_frame_builtins , set_interp_builtins <EOL> class CleanupBuiltins : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . get_frame_builtins = FrameType . f_builtins . __get__ <EOL> self . builtin_dict = RestorableDict ( BUILTINS_MODULE . __dict__ ) <EOL> def enable ( self , sandbox ) : <EOL> config = sandbox . config <EOL> whitelist = config . builtins_whitelist <EOL> keys = set ( self . builtin_dict . dict . iterkeys ( ) ) <EOL> for key in keys - whitelist : <EOL> del self . builtin_dict [ key ] <EOL> self . frame = sandbox . frame <EOL> self . builtins_dict = self . get_frame_builtins ( self . frame ) <EOL> self . modules_dict = [ ] <EOL> for name , module in sys . modules . iteritems ( ) : <EOL> if module is None : <EOL> continue <EOL> if '<STR_LIT>' not in module . __dict__ : <EOL> continue <EOL> if name == "<STR_LIT:__main__>" : <EOL> continue <EOL> self . modules_dict . append ( module . __dict__ ) <EOL> self . main_module = sys . modules [ '<STR_LIT:__main__>' ] <EOL> if not config . cpython_restricted : <EOL> open_whitelist = config . open_whitelist <EOL> safe_open = _safe_open ( open_whitelist ) <EOL> self . builtin_dict [ '<STR_LIT>' ] = safe_open <EOL> if version_info < ( <NUM_LIT:3> , <NUM_LIT:0> ) : <EOL> self . builtin_dict [ '<STR_LIT:file>' ] = safe_open <EOL> import_whitelist = config . import_whitelist <EOL> self . builtin_dict [ '<STR_LIT>' ] = _safe_import ( __import__ , import_whitelist ) <EOL> if '<STR_LIT>' not in config . features : <EOL> def safe_exit ( code = <NUM_LIT:0> ) : <EOL> raise SandboxError ( "<STR_LIT>" ) <EOL> self . builtin_dict [ '<STR_LIT>' ] = safe_exit <EOL> help_func = self . builtin_dict . dict . get ( '<STR_LIT>' ) <EOL> if help_func : <EOL> if '<STR_LIT>' in config . features : <EOL> self . builtin_dict [ '<STR_LIT>' ] = createReadOnlyObject ( help_func ) <EOL> else : <EOL> del self . builtin_dict [ '<STR_LIT>' ] <EOL> safe_builtins = createReadOnlyBuiltins ( self . builtin_dict . dict ) <EOL> if HAVE_CSANDBOX : <EOL> set_frame_builtins ( self . frame , safe_builtins ) <EOL> if not config . cpython_restricted : <EOL> set_interp_builtins ( safe_builtins ) <EOL> for module_dict in self . modules_dict : <EOL> module_dict [ '<STR_LIT>' ] = safe_builtins <EOL> self . main_module . __dict__ [ '<STR_LIT>' ] = safe_builtins <EOL> def disable ( self , sandbox ) : <EOL> self . builtin_dict . restore ( ) <EOL> if HAVE_CSANDBOX : <EOL> set_frame_builtins ( self . frame , self . builtins_dict ) <EOL> if not sandbox . config . cpython_restricted : <EOL> set_interp_builtins ( self . builtins_dict ) <EOL> for module_dict in self . modules_dict : <EOL> module_dict [ '<STR_LIT>' ] = self . builtins_dict <EOL> self . main_module . __dict__ [ '<STR_LIT>' ] = BUILTINS_MODULE <EOL> self . frame = None <EOL> self . builtins_dict = None <EOL> self . main_module = None <EOL> self . modules_dict = None </s>
