This module contains the RExec class, which supports r_eval(), r_execfile(), r_exec(), and r_import() methods, which are restricted versions of the standard Python functions eval(), execfile() and the exec and import statements. 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.
Note: 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 CPU time.
hooks is an instance of the RHooks class or a subclass of it.
If it is omitted or
None, the default RHooks class is
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, e.g. using packages.)
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 verbose is true, additional debugging output may be sent to standard output.
The RExec class has the following class attributes, which are used by the __init__() method. Changing them on an existing instance won't have any effect; instead, create a subclass of RExec and assign them new values in the class definition. Instances of the new class will then use those new values. All these attributes are tuples of strings.
'__import__'). (This gives the exceptions, because by far the majority of built-in functions are harmless. A subclass that wants to override this variable should probably start with the value from the base class and concatenate additional forbidden functions -- when new dangerous built-in functions are added to Python, they will also be added to this module.)
'time'). A similar remark about overriding this variable applies -- use the value from the base class as a starting point.
sys.path(at the time the module is loaded) for unrestricted code.
RExec instances support the following methods:
Methods whose names begin with "s_" are similar to the functions
beginning with "r_", but the code will be granted access to
restricted versions of the standard I/O streams
RExec objects must also support various methods which will be implicitly called by code executing in the restricted environment. Overriding these methods in a subclass is used to change the policies enforced by a restricted environment.
And their equivalents with access to restricted standard I/O streams: