Standard Module rexec

rexec

This module contains the RExec class, which supports r_exec(), r_eval(), r_execfile(), and r_import() methods, which are restricted versions of the standard Python functions exec(), eval(), execfile(), and the import statement. 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.


\begin{funcdesc}{RExec}{\optional{hooks\optional{\, verbose}}}
Returns an instan...
...true, additional debugging output may be sent to
standard output.
\end{funcdesc}

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.


\begin{datadesc}{nok_builtin_names}
Contains the names of built-in functions whi...
...ons are added to Python, they will also be
added to this module.)
\end{datadesc}


\begin{datadesc}{ok_builtin_modules}
Contains the names of built-in modules whic...
...pplies --- use the value from the base
class as a starting point.
\end{datadesc}


\begin{datadesc}{ok_path}
Contains the directories which will be searched when a...
...s.path} (at the time
the module is loaded) for unrestricted code.
\end{datadesc}


\begin{datadesc}{ok_posix_names}
Contains the names of the functions in the \cod...
...{'getuid',} \code{'getgid',} \code{'geteuid',}
\code{'getegid')}.
\end{datadesc}


\begin{datadesc}{ok_sys_names}
Contains the names of the functions and variables...
...e{'version',} \code{'platform',}
\code{'exit',} \code{'maxint')}.
\end{datadesc}

RExec instances support the following methods:


\begin{funcdesc}{r_eval}{code}
\var{code} must either be a string containing a P...
...ule. The value of the expression or
code object will be returned.
\end{funcdesc}


\begin{funcdesc}{r_exec}{code}
\var{code} must either be a string containing one...
... executed in the
restricted environment's \code{__main__} module.
\end{funcdesc}


\begin{funcdesc}{r_execfile}{filename}
Execute the Python code contained in the ...
...filename} in the
restricted environment's \code{__main__} module.
\end{funcdesc}

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 streans sys.stdin, sys.stderr, and sys.stdout.


\begin{funcdesc}{s_eval}{code}
\var{code} must be a string containing a Python expression, which will
be evaluated in the restricted environment.
\end{funcdesc}


\begin{funcdesc}{s_exec}{code}
\var{code} must be a string containing one or mor...
...thon code,
which will be executed in the restricted environment.
\end{funcdesc}


\begin{funcdesc}{s_execfile}{code}
Execute the Python code contained in the file \var{filename} in the
restricted environment.
\end{funcdesc}

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.


\begin{funcdesc}{r_import}{modulename\optional{\, globals\, locals\, fromlist}}
...
... \code{ImportError}
exception if the module is considered unsafe.
\end{funcdesc}


\begin{funcdesc}{r_open}{filename\optional{\, mode\optional{\, bufsize}}}
Method...
...elow for an implementation of a less restrictive
\code{r_open()}.
\end{funcdesc}


\begin{funcdesc}{r_reload}{module}
Reload the module object \var{module}, re-parsing and re-initializing it.
\end{funcdesc}


\begin{funcdesc}{r_unload}{module}
Unload the module object \var{module} (i.e., ...
...from the
restricted environment's \code{sys.modules} dictionary).
\end{funcdesc}

And their equivalents with access to restricted standard I/O streams:


\begin{funcdesc}{s_import}{modulename\optional{\, globals, locals, fromlist}}
Im...
... \code{ImportError}
exception if the module is considered unsafe.
\end{funcdesc}


\begin{funcdesc}{s_reload}{module}
Reload the module object \var{module}, re-parsing and re-initializing it.
\end{funcdesc}


\begin{funcdesc}{s_unload}{module}
Unload the module object \var{module}.
\end{funcdesc}



Subsections