Commit | Line | Data |
---|---|---|
920dae64 AT |
1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
2 | <html> | |
3 | <head> | |
4 | <link rel="STYLESHEET" href="lib.css" type='text/css' /> | |
5 | <link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" /> | |
6 | <link rel='start' href='../index.html' title='Python Documentation Index' /> | |
7 | <link rel="first" href="lib.html" title='Python Library Reference' /> | |
8 | <link rel='contents' href='contents.html' title="Contents" /> | |
9 | <link rel='index' href='genindex.html' title='Index' /> | |
10 | <link rel='last' href='about.html' title='About this document...' /> | |
11 | <link rel='help' href='about.html' title='About this document...' /> | |
12 | <link rel="next" href="rexec-extension.html" /> | |
13 | <link rel="prev" href="module-rexec.html" /> | |
14 | <link rel="parent" href="module-rexec.html" /> | |
15 | <link rel="next" href="rexec-extension.html" /> | |
16 | <meta name='aesop' content='information' /> | |
17 | <title>17.1.1 RExec Objects </title> | |
18 | </head> | |
19 | <body> | |
20 | <DIV CLASS="navigation"> | |
21 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> | |
22 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
23 | <tr> | |
24 | <td class='online-navigation'><a rel="prev" title="17.1 rexec " | |
25 | href="module-rexec.html"><img src='../icons/previous.png' | |
26 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
27 | <td class='online-navigation'><a rel="parent" title="17.1 rexec " | |
28 | href="module-rexec.html"><img src='../icons/up.png' | |
29 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
30 | <td class='online-navigation'><a rel="next" title="17.1.2 Defining restricted environments" | |
31 | href="rexec-extension.html"><img src='../icons/next.png' | |
32 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
33 | <td align="center" width="100%">Python Library Reference</td> | |
34 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
35 | href="contents.html"><img src='../icons/contents.png' | |
36 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
37 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' | |
38 | border='0' height='32' alt='Module Index' width='32' /></a></td> | |
39 | <td class='online-navigation'><a rel="index" title="Index" | |
40 | href="genindex.html"><img src='../icons/index.png' | |
41 | border='0' height='32' alt='Index' width='32' /></A></td> | |
42 | </tr></table> | |
43 | <div class='online-navigation'> | |
44 | <b class="navlabel">Previous:</b> | |
45 | <a class="sectref" rel="prev" href="module-rexec.html">17.1 rexec </A> | |
46 | <b class="navlabel">Up:</b> | |
47 | <a class="sectref" rel="parent" href="module-rexec.html">17.1 rexec </A> | |
48 | <b class="navlabel">Next:</b> | |
49 | <a class="sectref" rel="next" href="rexec-extension.html">17.1.2 Defining restricted environments</A> | |
50 | </div> | |
51 | <hr /></div> | |
52 | </DIV> | |
53 | <!--End of Navigation Panel--> | |
54 | ||
55 | <H2><A NAME="SECTION0019110000000000000000"></A><A NAME="rexec-objects"></A> | |
56 | <BR> | |
57 | 17.1.1 RExec Objects | |
58 | </H2> | |
59 | ||
60 | <P> | |
61 | <tt class="class">RExec</tt> instances support the following methods: | |
62 | ||
63 | <P> | |
64 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
65 | <td><nobr><b><tt id='l2h-4914' xml:id='l2h-4914' class="method">r_eval</tt></b>(</nobr></td> | |
66 | <td><var>code</var>)</td></tr></table></dt> | |
67 | <dd> | |
68 | <var>code</var> must either be a string containing a Python expression, or | |
69 | a compiled code object, which will be evaluated in the restricted | |
70 | environment's <tt class="module">__main__</tt> module. The value of the expression or | |
71 | code object will be returned. | |
72 | </dl> | |
73 | ||
74 | <P> | |
75 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
76 | <td><nobr><b><tt id='l2h-4915' xml:id='l2h-4915' class="method">r_exec</tt></b>(</nobr></td> | |
77 | <td><var>code</var>)</td></tr></table></dt> | |
78 | <dd> | |
79 | <var>code</var> must either be a string containing one or more lines of | |
80 | Python code, or a compiled code object, which will be executed in the | |
81 | restricted environment's <tt class="module">__main__</tt> module. | |
82 | </dl> | |
83 | ||
84 | <P> | |
85 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
86 | <td><nobr><b><tt id='l2h-4916' xml:id='l2h-4916' class="method">r_execfile</tt></b>(</nobr></td> | |
87 | <td><var>filename</var>)</td></tr></table></dt> | |
88 | <dd> | |
89 | Execute the Python code contained in the file <var>filename</var> in the | |
90 | restricted environment's <tt class="module">__main__</tt> module. | |
91 | </dl> | |
92 | ||
93 | <P> | |
94 | Methods whose names begin with "<tt class="samp">s_</tt>" are similar to the functions | |
95 | beginning with "<tt class="samp">r_</tt>", but the code will be granted access to | |
96 | restricted versions of the standard I/O streams <code>sys.stdin</code>, | |
97 | <code>sys.stderr</code>, and <code>sys.stdout</code>. | |
98 | ||
99 | <P> | |
100 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
101 | <td><nobr><b><tt id='l2h-4917' xml:id='l2h-4917' class="method">s_eval</tt></b>(</nobr></td> | |
102 | <td><var>code</var>)</td></tr></table></dt> | |
103 | <dd> | |
104 | <var>code</var> must be a string containing a Python expression, which will | |
105 | be evaluated in the restricted environment. | |
106 | </dl> | |
107 | ||
108 | <P> | |
109 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
110 | <td><nobr><b><tt id='l2h-4918' xml:id='l2h-4918' class="method">s_exec</tt></b>(</nobr></td> | |
111 | <td><var>code</var>)</td></tr></table></dt> | |
112 | <dd> | |
113 | <var>code</var> must be a string containing one or more lines of Python code, | |
114 | which will be executed in the restricted environment. | |
115 | </dl> | |
116 | ||
117 | <P> | |
118 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
119 | <td><nobr><b><tt id='l2h-4919' xml:id='l2h-4919' class="method">s_execfile</tt></b>(</nobr></td> | |
120 | <td><var>code</var>)</td></tr></table></dt> | |
121 | <dd> | |
122 | Execute the Python code contained in the file <var>filename</var> in the | |
123 | restricted environment. | |
124 | </dl> | |
125 | ||
126 | <P> | |
127 | <tt class="class">RExec</tt> objects must also support various methods which will be | |
128 | implicitly called by code executing in the restricted environment. | |
129 | Overriding these methods in a subclass is used to change the policies | |
130 | enforced by a restricted environment. | |
131 | ||
132 | <P> | |
133 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
134 | <td><nobr><b><tt id='l2h-4920' xml:id='l2h-4920' class="method">r_import</tt></b>(</nobr></td> | |
135 | <td><var>modulename</var><big>[</big><var>, globals</var><big>[</big><var>, | |
136 | locals</var><big>[</big><var>, fromlist</var><big>]</big><var></var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt> | |
137 | <dd> | |
138 | Import the module <var>modulename</var>, raising an <tt class="exception">ImportError</tt> | |
139 | exception if the module is considered unsafe. | |
140 | </dl> | |
141 | ||
142 | <P> | |
143 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
144 | <td><nobr><b><tt id='l2h-4921' xml:id='l2h-4921' class="method">r_open</tt></b>(</nobr></td> | |
145 | <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> | |
146 | <dd> | |
147 | Method called when <tt class="function">open()</tt> is called in the restricted | |
148 | environment. The arguments are identical to those of <tt class="function">open()</tt>, | |
149 | and a file object (or a class instance compatible with file objects) | |
150 | should be returned. <tt class="class">RExec</tt>'s default behaviour is allow opening | |
151 | any file for reading, but forbidding any attempt to write a file. See | |
152 | the example below for an implementation of a less restrictive | |
153 | <tt class="method">r_open()</tt>. | |
154 | </dl> | |
155 | ||
156 | <P> | |
157 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
158 | <td><nobr><b><tt id='l2h-4922' xml:id='l2h-4922' class="method">r_reload</tt></b>(</nobr></td> | |
159 | <td><var>module</var>)</td></tr></table></dt> | |
160 | <dd> | |
161 | Reload the module object <var>module</var>, re-parsing and re-initializing it. | |
162 | </dl> | |
163 | ||
164 | <P> | |
165 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
166 | <td><nobr><b><tt id='l2h-4923' xml:id='l2h-4923' class="method">r_unload</tt></b>(</nobr></td> | |
167 | <td><var>module</var>)</td></tr></table></dt> | |
168 | <dd> | |
169 | Unload the module object <var>module</var> (remove it from the | |
170 | restricted environment's <code>sys.modules</code> dictionary). | |
171 | </dl> | |
172 | ||
173 | <P> | |
174 | And their equivalents with access to restricted standard I/O streams: | |
175 | ||
176 | <P> | |
177 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
178 | <td><nobr><b><tt id='l2h-4924' xml:id='l2h-4924' class="method">s_import</tt></b>(</nobr></td> | |
179 | <td><var>modulename</var><big>[</big><var>, globals</var><big>[</big><var>, | |
180 | locals</var><big>[</big><var>, fromlist</var><big>]</big><var></var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt> | |
181 | <dd> | |
182 | Import the module <var>modulename</var>, raising an <tt class="exception">ImportError</tt> | |
183 | exception if the module is considered unsafe. | |
184 | </dl> | |
185 | ||
186 | <P> | |
187 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
188 | <td><nobr><b><tt id='l2h-4925' xml:id='l2h-4925' class="method">s_reload</tt></b>(</nobr></td> | |
189 | <td><var>module</var>)</td></tr></table></dt> | |
190 | <dd> | |
191 | Reload the module object <var>module</var>, re-parsing and re-initializing it. | |
192 | </dl> | |
193 | ||
194 | <P> | |
195 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
196 | <td><nobr><b><tt id='l2h-4926' xml:id='l2h-4926' class="method">s_unload</tt></b>(</nobr></td> | |
197 | <td><var>module</var>)</td></tr></table></dt> | |
198 | <dd> | |
199 | Unload the module object <var>module</var>. | |
200 | </dl> | |
201 | ||
202 | <P> | |
203 | ||
204 | <DIV CLASS="navigation"> | |
205 | <div class='online-navigation'> | |
206 | <p></p><hr /> | |
207 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
208 | <tr> | |
209 | <td class='online-navigation'><a rel="prev" title="17.1 rexec " | |
210 | href="module-rexec.html"><img src='../icons/previous.png' | |
211 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
212 | <td class='online-navigation'><a rel="parent" title="17.1 rexec " | |
213 | href="module-rexec.html"><img src='../icons/up.png' | |
214 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
215 | <td class='online-navigation'><a rel="next" title="17.1.2 Defining restricted environments" | |
216 | href="rexec-extension.html"><img src='../icons/next.png' | |
217 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
218 | <td align="center" width="100%">Python Library Reference</td> | |
219 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
220 | href="contents.html"><img src='../icons/contents.png' | |
221 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
222 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' | |
223 | border='0' height='32' alt='Module Index' width='32' /></a></td> | |
224 | <td class='online-navigation'><a rel="index" title="Index" | |
225 | href="genindex.html"><img src='../icons/index.png' | |
226 | border='0' height='32' alt='Index' width='32' /></A></td> | |
227 | </tr></table> | |
228 | <div class='online-navigation'> | |
229 | <b class="navlabel">Previous:</b> | |
230 | <a class="sectref" rel="prev" href="module-rexec.html">17.1 rexec </A> | |
231 | <b class="navlabel">Up:</b> | |
232 | <a class="sectref" rel="parent" href="module-rexec.html">17.1 rexec </A> | |
233 | <b class="navlabel">Next:</b> | |
234 | <a class="sectref" rel="next" href="rexec-extension.html">17.1.2 Defining restricted environments</A> | |
235 | </div> | |
236 | </div> | |
237 | <hr /> | |
238 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> | |
239 | </DIV> | |
240 | <!--End of Navigation Panel--> | |
241 | <ADDRESS> | |
242 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. | |
243 | </ADDRESS> | |
244 | </BODY> | |
245 | </HTML> |