rexec
index
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/rexec.py
Module Docs

Restricted execution facilities.
 
The class RExec exports methods r_exec(), r_eval(), r_execfile(), and
r_import(), which correspond roughly to the built-in operations
exec, eval(), execfile() and import, but executing the code in an
environment that only exposes those built-in operations that are
deemed safe.  To this end, a modest collection of 'fake' modules is
created which mimics the standard modules by the same names.  It is a
policy decision which built-in modules and operations are made
available; this module provides a reasonable default, but derived
classes can change the policies e.g. by overriding or extending class
variables like ok_builtin_modules or methods like make_sys().
 
XXX To do:
- r_open should allow writing tmp dir
- r_exec etc. with explicit globals/locals? (Use rexec("exec ... in ...")?)

 
Modules
       
__builtin__
ihooks
imp
os
sys

 
Classes
       
ihooks._Verbose
RExec

 
class RExec(ihooks._Verbose)
    Basic restricted execution framework.
 
Code executed in this restricted environment will only have access to
modules and functions that are deemed safe; you can subclass RExec to
add or remove capabilities as desired.
 
The RExec class can prevent code from performing unsafe operations like
reading or writing disk files, or using TCP/IP sockets.  However, it does
not protect against code using extremely large amounts of memory or
processor time.
 
  Methods defined here:
__init__(self, hooks=None, verbose=0)
Returns an instance of the RExec class.
 
The hooks parameter is an instance of the RHooks class or a subclass
of it.  If it is omitted or None, the default RHooks class is
instantiated.
 
Whenever the RExec module searches for a module (even a built-in one)
or reads a module's code, it doesn't actually go out to the file
system itself.  Rather, it calls methods of an RHooks instance that
was passed to or created by its constructor.  (Actually, the RExec
object doesn't make these calls --- they are made by a module loader
object that's part of the RExec object.  This allows another level of
flexibility, which can be useful when changing the mechanics of
import within the restricted environment.)
 
By providing an alternate RHooks object, we can control the file
system accesses made to import a module, without changing the
actual algorithm that controls the order in which those accesses are
made.  For instance, we could substitute an RHooks object that
passes all filesystem requests to a file server elsewhere, via some
RPC mechanism such as ILU.  Grail's applet loader uses this to support
importing applets from a URL for a directory.
 
If the verbose parameter is true, additional debugging output may be
sent to standard output.
add_module(self, mname)
copy_except(self, src, exceptions)
copy_none(self, src)
copy_only(self, src, names)
get_suffixes(self)
is_builtin(self, mname)
load_dynamic(self, name, filename, file)
make_builtin(self)
make_delegate_files(self)
make_initial_modules(self)
make_main(self)
make_osname(self)
make_sys(self)
r_eval(self, code)
Evaluate code within a restricted environment.
 
The code parameter must either be a string containing a Python
expression, or a compiled code object, which will be evaluated in
the restricted environment's __main__ module.  The value of the
expression or code object will be returned.
r_exc_info(self)
r_exec(self, code)
Execute code within a restricted environment.
 
The code parameter must either be a string containing one or more
lines of Python code, or a compiled code object, which will be
executed in the restricted environment's __main__ module.
r_execfile(self, file)
Execute the Python code in the file in the restricted
environment's __main__ module.
r_import(self, mname, globals={}, locals={}, fromlist=[])
Import a module, raising an ImportError exception if the module
is considered unsafe.
 
This method is implicitly called by code executing in the
restricted environment.  Overriding this method in a subclass is
used to change the policies enforced by a restricted environment.
r_open(self, file, mode='r', buf=-1)
Method called when open() is called in the restricted environment.
 
The arguments are identical to those of the open() function, and a
file object (or a class instance compatible with file objects)
should be returned.  RExec's default behaviour is allow opening
any file for reading, but forbidding any attempt to write a file.
 
This method is implicitly called by code executing in the
restricted environment.  Overriding this method in a subclass is
used to change the policies enforced by a restricted environment.
r_reload(self, m)
Reload the module object, re-parsing and re-initializing it.
 
This method is implicitly called by code executing in the
restricted environment.  Overriding this method in a subclass is
used to change the policies enforced by a restricted environment.
r_unload(self, m)
Unload the module.
 
Removes it from the restricted environment's sys.modules dictionary.
 
This method is implicitly called by code executing in the
restricted environment.  Overriding this method in a subclass is
used to change the policies enforced by a restricted environment.
reset_files(self)
restore_files(self)
s_apply(self, func, args=(), kw={})
s_eval(self, *args)
Evaluate code within a restricted environment.
 
Similar to the r_eval() method, but the code will be granted access
to restricted versions of the standard I/O streams sys.stdin,
sys.stderr, and sys.stdout.
 
The code parameter must either be a string containing a Python
expression, or a compiled code object, which will be evaluated in
the restricted environment's __main__ module.  The value of the
expression or code object will be returned.
s_exec(self, *args)
Execute code within a restricted environment.
 
Similar to the r_exec() method, but the code will be granted access
to restricted versions of the standard I/O streams sys.stdin,
sys.stderr, and sys.stdout.
 
The code parameter must either be a string containing one or more
lines of Python code, or a compiled code object, which will be
executed in the restricted environment's __main__ module.
s_execfile(self, *args)
Execute the Python code in the file in the restricted
environment's __main__ module.
 
Similar to the r_execfile() method, but the code will be granted
access to restricted versions of the standard I/O streams sys.stdin,
sys.stderr, and sys.stdout.
s_import(self, *args)
Import a module, raising an ImportError exception if the module
is considered unsafe.
 
This method is implicitly called by code executing in the
restricted environment.  Overriding this method in a subclass is
used to change the policies enforced by a restricted environment.
 
Similar to the r_import() method, but has access to restricted
versions of the standard I/O streams sys.stdin, sys.stderr, and
sys.stdout.
s_reload(self, *args)
Reload the module object, re-parsing and re-initializing it.
 
This method is implicitly called by code executing in the
restricted environment.  Overriding this method in a subclass is
used to change the policies enforced by a restricted environment.
 
Similar to the r_reload() method, but has access to restricted
versions of the standard I/O streams sys.stdin, sys.stderr, and
sys.stdout.
s_unload(self, *args)
Unload the module.
 
Removes it from the restricted environment's sys.modules dictionary.
 
This method is implicitly called by code executing in the
restricted environment.  Overriding this method in a subclass is
used to change the policies enforced by a restricted environment.
 
Similar to the r_unload() method, but has access to restricted
versions of the standard I/O streams sys.stdin, sys.stderr, and
sys.stdout.
save_files(self)
set_files(self)
set_trusted_path(self)

Data and other attributes defined here:
nok_builtin_names = ('open', 'file', 'reload', '__import__')
ok_builtin_modules = ('audioop', 'array', 'binascii', 'cmath', 'errno', 'imageop', 'marshal', 'math', 'md5', 'operator', 'parser', 'select', 'sha', '_sre', 'strop', 'struct', 'time', '_weakref')
ok_file_types = (3, 1)
ok_path = ('.', '/System/Library/Frameworks/Python.framework/Versions/2.7/bin', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload', '/Library/Python/2.7/site-packages', '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python', '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC', '/Library/Python/2.7/site-packages/PIL')
ok_posix_names = ('error', 'fstat', 'listdir', 'lstat', 'readlink', 'stat', 'times', 'uname', 'getpid', 'getppid', 'getcwd', 'getuid', 'getgid', 'geteuid', 'getegid')
ok_sys_names = ('byteorder', 'copyright', 'exit', 'getdefaultencoding', 'getrefcount', 'hexversion', 'maxint', 'maxunicode', 'platform', 'ps1', 'ps2', 'version', 'version_info')

Methods inherited from ihooks._Verbose:
get_verbose(self)
message(self, format, *args)
note(self, *args)
set_verbose(self, verbose)

 
Data
        __all__ = ['RExec']