16.1.1 RExec Objects

RExec instances support the following methods:

r_eval(code)
code 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_exec(code)
code 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(filename)
Execute the Python code contained in the file filename in the restricted environment's __main__ module.

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

s_eval(code)
code must be a string containing a Python expression, which will be evaluated in the restricted environment.

s_exec(code)
code must be a string containing one or more lines of Python code, which will be executed in the restricted environment.

s_execfile(code)
Execute the Python code contained in the file filename in the restricted environment.

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.

r_import(modulename[, globals[, locals[, fromlist]]])
Import the module modulename, raising an ImportError exception if the module is considered unsafe.

r_open(filename[, mode[, bufsize]])
Method called when open() is called in the restricted environment. The arguments are identical to those of open(), 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. See the example below for an implementation of a less restrictive r_open().

r_reload(module)
Reload the module object module, re-parsing and re-initializing it.

r_unload(module)
Unload the module object module (i.e., remove it from the restricted environment's sys.modules dictionary).

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

s_import(modulename[, globals[, locals[, fromlist]]])
Import the module modulename, raising an ImportError exception if the module is considered unsafe.

s_reload(module)
Reload the module object module, re-parsing and re-initializing it.

s_unload(module)
Unload the module object module.

See About this document... for information on suggesting changes.