Module rexec :: Class RExec
[hide private]
[frames] | no frames]

_ClassType RExec

ihooks._Verbose --+
                  |
                 RExec

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.

Instance Methods [hide private]
 
__init__(self, hooks=None, verbose=0)
Returns an instance of the RExec class.
 
set_trusted_path(self)
 
load_dynamic(self, name, filename, file)
 
make_initial_modules(self)
 
get_suffixes(self)
 
is_builtin(self, mname)
 
make_builtin(self)
 
make_main(self)
 
make_osname(self)
 
make_sys(self)
 
copy_except(self, src, exceptions)
 
copy_only(self, src, names)
 
copy_none(self, src)
 
add_module(self, mname)
 
r_exec(self, code)
Execute code within a restricted environment.
 
r_eval(self, code)
Evaluate code within a restricted environment.
 
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.
 
r_reload(self, m)
Reload the module object, re-parsing and re-initializing it.
 
r_unload(self, m)
Unload the module.
 
make_delegate_files(self)
 
set_files(self)
 
reset_files(self)
 
save_files(self)
 
restore_files(self)
 
s_apply(self, func, args=(), kw={})
 
s_exec(self, *args)
Execute code within a restricted environment.
 
s_eval(self, *args)
Evaluate code within a restricted environment.
 
s_execfile(self, *args)
Execute the Python code in the file in the restricted environment's __main__ module.
 
s_import(self, *args)
Import a module, raising an ImportError exception if the module is considered unsafe.
 
s_reload(self, *args)
Reload the module object, re-parsing and re-initializing it.
 
s_unload(self, *args)
Unload the module.
 
r_open(self, file, mode='r', buf=-1)
Method called when open() is called in the restricted environment.
 
r_exc_info(self)

Inherited from ihooks._Verbose: get_verbose, message, note, set_verbose

Class Variables [hide private]
  ok_path = ('/usr/lib/python2.5', '/home/edloper/newdata/projec...
  ok_builtin_modules = ('audioop', 'array', 'binascii', 'cmath',...
  ok_posix_names = ('error', 'fstat', 'listdir', 'lstat', 'readl...
  ok_sys_names = ('byteorder', 'copyright', 'exit', 'getdefaulte...
  nok_builtin_names = ('open', 'file', 'reload', '__import__')
  ok_file_types = (3, 1)
Method Details [hide private]

__init__(self, hooks=None, verbose=0)
(Constructor)

 

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.

Overrides: ihooks._Verbose.__init__

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_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_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_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.

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_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_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.

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.


Class Variable Details [hide private]

ok_path

Value:
('/usr/lib/python2.5',
 '/home/edloper/newdata/projects/epydoc/src/epydoc',
 '/home/edloper/newdata/projects/epydoc/src',
 '/usr/lib/python25.zip',
 '/usr/lib/python2.5',
 '/usr/lib/python2.5/plat-linux2',
 '/usr/lib/python2.5/lib-tk',
 '/usr/lib/python2.5/lib-dynload',
...

ok_builtin_modules

Value:
('audioop',
 'array',
 'binascii',
 'cmath',
 'errno',
 'imageop',
 'marshal',
 'math',
...

ok_posix_names

Value:
('error',
 'fstat',
 'listdir',
 'lstat',
 'readlink',
 'stat',
 'times',
 'uname',
...

ok_sys_names

Value:
('byteorder',
 'copyright',
 'exit',
 'getdefaultencoding',
 'getrefcount',
 'hexversion',
 'maxint',
 'maxunicode',
...