<!DOCTYPE html PUBLIC
"-//W3C//DTD HTML 4.0 Transitional//EN">
<link rel=
"STYLESHEET" href=
"lib.css" type='text/css'
/>
<link rel=
"SHORTCUT ICON" href=
"../icons/pyfav.png" type=
"image/png" />
<link rel='start' href='../index.html' title='Python Documentation Index'
/>
<link rel=
"first" href=
"lib.html" title='Python Library Reference'
/>
<link rel='contents' href='contents.html'
title=
"Contents" />
<link rel='index' href='genindex.html' title='Index'
/>
<link rel='last' href='about.html' title='About this document...'
/>
<link rel='help' href='about.html' title='About this document...'
/>
<link rel=
"next" href=
"rexec-extension.html" />
<link rel=
"prev" href=
"module-rexec.html" />
<link rel=
"parent" href=
"module-rexec.html" />
<link rel=
"next" href=
"rexec-extension.html" />
<meta name='aesop' content='information'
/>
<title>17.1.1 RExec Objects
</title>
<div id='top-navigation-panel' xml:id='top-navigation-panel'
>
<table align=
"center" width=
"100%" cellpadding=
"0" cellspacing=
"2">
<td class='online-navigation'
><a rel=
"prev" title=
"17.1 rexec "
href=
"module-rexec.html"><img src='../icons/previous.png'
border='
0' height='
32' alt='Previous Page' width='
32'
/></A></td>
<td class='online-navigation'
><a rel=
"parent" title=
"17.1 rexec "
href=
"module-rexec.html"><img src='../icons/up.png'
border='
0' height='
32' alt='Up One Level' width='
32'
/></A></td>
<td class='online-navigation'
><a rel=
"next" title=
"17.1.2 Defining restricted environments"
href=
"rexec-extension.html"><img src='../icons/next.png'
border='
0' height='
32' alt='Next Page' width='
32'
/></A></td>
<td align=
"center" width=
"100%">Python Library Reference
</td>
<td class='online-navigation'
><a rel=
"contents" title=
"Table of Contents"
href=
"contents.html"><img src='../icons/contents.png'
border='
0' height='
32' alt='Contents' width='
32'
/></A></td>
<td class='online-navigation'
><a href=
"modindex.html" title=
"Module Index"><img src='../icons/modules.png'
border='
0' height='
32' alt='Module Index' width='
32'
/></a></td>
<td class='online-navigation'
><a rel=
"index" title=
"Index"
href=
"genindex.html"><img src='../icons/index.png'
border='
0' height='
32' alt='Index' width='
32'
/></A></td>
<div class='online-navigation'
>
<b class=
"navlabel">Previous:
</b>
<a class=
"sectref" rel=
"prev" href=
"module-rexec.html">17.1 rexec
</A>
<b class=
"navlabel">Up:
</b>
<a class=
"sectref" rel=
"parent" href=
"module-rexec.html">17.1 rexec
</A>
<b class=
"navlabel">Next:
</b>
<a class=
"sectref" rel=
"next" href=
"rexec-extension.html">17.1.2 Defining restricted environments
</A>
<!--End of Navigation Panel-->
<H2><A NAME=
"SECTION0019110000000000000000"></A><A NAME=
"rexec-objects"></A>
<tt class=
"class">RExec
</tt> instances support the following methods:
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
4914' xml:id='l2h-
4914'
class=
"method">r_eval
</tt></b>(
</nobr></td>
<td><var>code
</var>)
</td></tr></table></dt>
<var>code
</var> must either be a string containing a Python expression, or
a compiled code object, which will be evaluated in the restricted
environment's
<tt class=
"module">__main__
</tt> module. The value of the expression or
code object will be returned.
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
4915' xml:id='l2h-
4915'
class=
"method">r_exec
</tt></b>(
</nobr></td>
<td><var>code
</var>)
</td></tr></table></dt>
<var>code
</var> 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
<tt class=
"module">__main__
</tt> module.
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
4916' xml:id='l2h-
4916'
class=
"method">r_execfile
</tt></b>(
</nobr></td>
<td><var>filename
</var>)
</td></tr></table></dt>
Execute the Python code contained in the file
<var>filename
</var> in the
restricted environment's
<tt class=
"module">__main__
</tt> module.
Methods whose names begin with
"<tt class="samp
">s_</tt>" are similar to the functions
beginning with
"<tt class="samp
">r_</tt>", but the code will be granted access to
restricted versions of the standard I/O streams
<code>sys.stdin
</code>,
<code>sys.stderr
</code>, and
<code>sys.stdout
</code>.
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
4917' xml:id='l2h-
4917'
class=
"method">s_eval
</tt></b>(
</nobr></td>
<td><var>code
</var>)
</td></tr></table></dt>
<var>code
</var> must be a string containing a Python expression, which will
be evaluated in the restricted environment.
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
4918' xml:id='l2h-
4918'
class=
"method">s_exec
</tt></b>(
</nobr></td>
<td><var>code
</var>)
</td></tr></table></dt>
<var>code
</var> must be a string containing one or more lines of Python code,
which will be executed in the restricted environment.
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
4919' xml:id='l2h-
4919'
class=
"method">s_execfile
</tt></b>(
</nobr></td>
<td><var>code
</var>)
</td></tr></table></dt>
Execute the Python code contained in the file
<var>filename
</var> in the
<tt class=
"class">RExec
</tt> 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.
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
4920' xml:id='l2h-
4920'
class=
"method">r_import
</tt></b>(
</nobr></td>
<td><var>modulename
</var><big>[
</big><var>, globals
</var><big>[
</big><var>,
locals
</var><big>[
</big><var>, fromlist
</var><big>]
</big><var></var><big>]
</big><var></var><big>]
</big><var></var>)
</td></tr></table></dt>
Import the module
<var>modulename
</var>, raising an
<tt class=
"exception">ImportError
</tt>
exception if the module is considered unsafe.
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
4921' xml:id='l2h-
4921'
class=
"method">r_open
</tt></b>(
</nobr></td>
<td><var>filename
</var><big>[
</big><var>, mode
</var><big>[
</big><var>, bufsize
</var><big>]
</big><var></var><big>]
</big><var></var>)
</td></tr></table></dt>
Method called when
<tt class=
"function">open()
</tt> is called in the restricted
environment. The arguments are identical to those of
<tt class=
"function">open()
</tt>,
and a file object (or a class instance compatible with file objects)
should be returned.
<tt class=
"class">RExec
</tt>'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
<tt class=
"method">r_open()
</tt>.
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
4922' xml:id='l2h-
4922'
class=
"method">r_reload
</tt></b>(
</nobr></td>
<td><var>module
</var>)
</td></tr></table></dt>
Reload the module object
<var>module
</var>, re-parsing and re-initializing it.
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
4923' xml:id='l2h-
4923'
class=
"method">r_unload
</tt></b>(
</nobr></td>
<td><var>module
</var>)
</td></tr></table></dt>
Unload the module object
<var>module
</var> (remove it from the
restricted environment's
<code>sys.modules
</code> dictionary).
And their equivalents with access to restricted standard I/O streams:
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
4924' xml:id='l2h-
4924'
class=
"method">s_import
</tt></b>(
</nobr></td>
<td><var>modulename
</var><big>[
</big><var>, globals
</var><big>[
</big><var>,
locals
</var><big>[
</big><var>, fromlist
</var><big>]
</big><var></var><big>]
</big><var></var><big>]
</big><var></var>)
</td></tr></table></dt>
Import the module
<var>modulename
</var>, raising an
<tt class=
"exception">ImportError
</tt>
exception if the module is considered unsafe.
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
4925' xml:id='l2h-
4925'
class=
"method">s_reload
</tt></b>(
</nobr></td>
<td><var>module
</var>)
</td></tr></table></dt>
Reload the module object
<var>module
</var>, re-parsing and re-initializing it.
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
4926' xml:id='l2h-
4926'
class=
"method">s_unload
</tt></b>(
</nobr></td>
<td><var>module
</var>)
</td></tr></table></dt>
Unload the module object
<var>module
</var>.
<div class='online-navigation'
>
<table align=
"center" width=
"100%" cellpadding=
"0" cellspacing=
"2">
<td class='online-navigation'
><a rel=
"prev" title=
"17.1 rexec "
href=
"module-rexec.html"><img src='../icons/previous.png'
border='
0' height='
32' alt='Previous Page' width='
32'
/></A></td>
<td class='online-navigation'
><a rel=
"parent" title=
"17.1 rexec "
href=
"module-rexec.html"><img src='../icons/up.png'
border='
0' height='
32' alt='Up One Level' width='
32'
/></A></td>
<td class='online-navigation'
><a rel=
"next" title=
"17.1.2 Defining restricted environments"
href=
"rexec-extension.html"><img src='../icons/next.png'
border='
0' height='
32' alt='Next Page' width='
32'
/></A></td>
<td align=
"center" width=
"100%">Python Library Reference
</td>
<td class='online-navigation'
><a rel=
"contents" title=
"Table of Contents"
href=
"contents.html"><img src='../icons/contents.png'
border='
0' height='
32' alt='Contents' width='
32'
/></A></td>
<td class='online-navigation'
><a href=
"modindex.html" title=
"Module Index"><img src='../icons/modules.png'
border='
0' height='
32' alt='Module Index' width='
32'
/></a></td>
<td class='online-navigation'
><a rel=
"index" title=
"Index"
href=
"genindex.html"><img src='../icons/index.png'
border='
0' height='
32' alt='Index' width='
32'
/></A></td>
<div class='online-navigation'
>
<b class=
"navlabel">Previous:
</b>
<a class=
"sectref" rel=
"prev" href=
"module-rexec.html">17.1 rexec
</A>
<b class=
"navlabel">Up:
</b>
<a class=
"sectref" rel=
"parent" href=
"module-rexec.html">17.1 rexec
</A>
<b class=
"navlabel">Next:
</b>
<a class=
"sectref" rel=
"next" href=
"rexec-extension.html">17.1.2 Defining restricted environments
</A>
<span class=
"release-info">Release
2.4.2, documentation updated on
28 September
2005.
</span>
<!--End of Navigation Panel-->
See
<i><a href=
"about.html">About this document...
</a></i> for information on suggesting changes.