home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 5 / DATAFILE_PDCD5.iso / utilities / p / python / pyhtmldoc / r / rexec < prev    next >
Encoding:
Text File  |  1996-11-14  |  9.2 KB  |  170 lines

  1. <TITLE>rexec -- Python library reference</TITLE>
  2. Next: <A HREF="../b/bastion" TYPE="Next">Bastion</A>  
  3. Prev: <A HREF="../r/restricted_execution" TYPE="Prev">Restricted Execution</A>  
  4. Up: <A HREF="../r/restricted_execution" TYPE="Up">Restricted Execution</A>  
  5. Top: <A HREF="../t/top" TYPE="Top">Top</A>  
  6. <H1>11.1. Standard Module <CODE>rexec</CODE></H1>
  7. This module contains the <CODE>RExec</CODE> class, which supports
  8. <CODE>r_exec()</CODE>, <CODE>r_eval()</CODE>, <CODE>r_execfile()</CODE>, and
  9. <CODE>r_import()</CODE> methods, which are restricted versions of the standard
  10. Python functions <CODE>exec()</CODE>, <CODE>eval()</CODE>, <CODE>execfile()</CODE>, and
  11. the <CODE>import</CODE> statement.
  12. Code executed in this restricted environment will
  13. only have access to modules and functions that are deemed safe; you
  14. can subclass <CODE>RExec</CODE> to add or remove capabilities as desired.
  15. <P>
  16. <I>Note:</I> The <CODE>RExec</CODE> class can prevent code from performing
  17. unsafe operations like reading or writing disk files, or using TCP/IP
  18. sockets.  However, it does not protect against code using extremely
  19. large amounts of memory or CPU time.  
  20. <P>
  21. <DL><DT><B>RExec</B> ([<VAR>hooks</VAR>[, <VAR>verbose</VAR>]]) -- function of module rexec<DD>
  22. Returns an instance of the <CODE>RExec</CODE> class.  
  23. <P>
  24. <VAR>hooks</VAR> is an instance of the <CODE>RHooks</CODE> class or a subclass of it.
  25. If it is omitted or <CODE>None</CODE>, the default <CODE>RHooks</CODE> class is
  26. instantiated.
  27. Whenever the RExec module searches for a module (even a built-in one)
  28. or reads a module's code, it doesn't actually go out to the file
  29. system itself.  Rather, it calls methods of an RHooks instance that
  30. was passed to or created by its constructor.  (Actually, the RExec
  31. object doesn't make these calls---they are made by a module loader
  32. object that's part of the RExec object.  This allows another level of
  33. flexibility, e.g. using packages.)
  34. <P>
  35. By providing an alternate RHooks object, we can control the
  36. file system accesses made to import a module, without changing the
  37. actual algorithm that controls the order in which those accesses are
  38. made.  For instance, we could substitute an RHooks object that passes
  39. all filesystem requests to a file server elsewhere, via some RPC
  40. mechanism such as ILU.  Grail's applet loader uses this to support
  41. importing applets from a URL for a directory.
  42. <P>
  43. If <VAR>verbose</VAR> is true, additional debugging output may be sent to
  44. standard output.
  45. </DL>
  46. The RExec class has the following class attributes, which are used by the
  47. <CODE>__init__</CODE> method.  Changing them on an existing instance won't
  48. have any effect; instead, create a subclass of <CODE>RExec</CODE> and assign
  49. them new values in the class definition.  Instances of the new class
  50. will then use those new values.  All these attributes are tuples of
  51. strings.
  52. <P>
  53. <DL><DT><B>nok_builtin_names</B> -- attribute of RExec object<DD>
  54. Contains the names of built-in functions which will <I>not</I> be
  55. available to programs running in the restricted environment.  The
  56. value for <CODE>RExec</CODE> is <CODE>('open',</CODE> <CODE>'reload',</CODE>
  57. <CODE>'__import__')</CODE>.  (This gives the exceptions, because by far the
  58. majority of built-in functions are harmless.  A subclass that wants to
  59. override this variable should probably start with the value from the
  60. base class and concatenate additional forbidden functions --- when new
  61. dangerous built-in functions are added to Python, they will also be
  62. added to this module.)
  63. </DL>
  64. <DL><DT><B>ok_builtin_modules</B> -- attribute of RExec object<DD>
  65. Contains the names of built-in modules which can be safely imported.
  66. The value for <CODE>RExec</CODE> is <CODE>('audioop',</CODE> <CODE>'array',</CODE>
  67. <CODE>'binascii',</CODE> <CODE>'cmath',</CODE> <CODE>'errno',</CODE> <CODE>'imageop',</CODE>
  68. <CODE>'marshal',</CODE> <CODE>'math',</CODE> <CODE>'md5',</CODE> <CODE>'operator',</CODE>
  69. <CODE>'parser',</CODE> <CODE>'regex',</CODE> <CODE>'rotor',</CODE> <CODE>'select',</CODE>
  70. <CODE>'strop',</CODE> <CODE>'struct',</CODE> <CODE>'time')</CODE>.  A similar remark
  71. about overriding this variable applies --- use the value from the base
  72. class as a starting point.
  73. </DL>
  74. <DL><DT><B>ok_path</B> -- attribute of RExec object<DD>
  75. Contains the directories which will be searched when an <CODE>import</CODE>
  76. is performed in the restricted environment.  
  77. The value for <CODE>RExec</CODE> is the same as <CODE>sys.path</CODE> (at the time
  78. the module is loaded) for unrestricted code.
  79. </DL>
  80. <DL><DT><B>ok_posix_names</B> -- attribute of RExec object<DD>
  81. Contains the names of the functions in the <CODE>os</CODE> module which will be
  82. available to programs running in the restricted environment.  The
  83. value for <CODE>RExec</CODE> is <CODE>('error',</CODE> <CODE>'fstat',</CODE>
  84. <CODE>'listdir',</CODE> <CODE>'lstat',</CODE> <CODE>'readlink',</CODE> <CODE>'stat',</CODE>
  85. <CODE>'times',</CODE> <CODE>'uname',</CODE> <CODE>'getpid',</CODE> <CODE>'getppid',</CODE>
  86. <CODE>'getcwd',</CODE> <CODE>'getuid',</CODE> <CODE>'getgid',</CODE> <CODE>'geteuid',</CODE>
  87. <CODE>'getegid')</CODE>.
  88. </DL>
  89. <DL><DT><B>ok_sys_names</B> -- attribute of RExec object<DD>
  90. Contains the names of the functions and variables in the <CODE>sys</CODE>
  91. module which will be available to programs running in the restricted
  92. environment.  The value for <CODE>RExec</CODE> is <CODE>('ps1',</CODE>
  93. <CODE>'ps2',</CODE> <CODE>'copyright',</CODE> <CODE>'version',</CODE> <CODE>'platform',</CODE>
  94. <CODE>'exit',</CODE> <CODE>'maxint')</CODE>.
  95. </DL>
  96. RExec instances support the following methods:
  97. <P>
  98. <DL><DT><B>r_eval</B> (<VAR>code</VAR>) -- Method on RExec object<DD>
  99. <VAR>code</VAR> must either be a string containing a Python expression, or
  100. a compiled code object, which will be evaluated in the restricted
  101. environment's <CODE>__main__</CODE> module.  The value of the expression or
  102. code object will be returned.
  103. </DL>
  104. <DL><DT><B>r_exec</B> (<VAR>code</VAR>) -- Method on RExec object<DD>
  105. <VAR>code</VAR> must either be a string containing one or more lines of
  106. Python code, or a compiled code object, which will be executed in the
  107. restricted environment's <CODE>__main__</CODE> module.
  108. </DL>
  109. <DL><DT><B>r_execfile</B> (<VAR>filename</VAR>) -- Method on RExec object<DD>
  110. Execute the Python code contained in the file <VAR>filename</VAR> in the
  111. restricted environment's <CODE>__main__</CODE> module.
  112. </DL>
  113. Methods whose names begin with <CODE>s_</CODE> are similar to the functions
  114. beginning with <CODE>r_</CODE>, but the code will be granted access to
  115. restricted versions of the standard I/O streans <CODE>sys.stdin</CODE>,
  116. <CODE>sys.stderr</CODE>, and <CODE>sys.stdout</CODE>.  
  117. <P>
  118. <DL><DT><B>s_eval</B> (<VAR>code</VAR>) -- Method on RExec object<DD>
  119. <VAR>code</VAR> must be a string containing a Python expression, which will
  120. be evaluated in the restricted environment.  
  121. </DL>
  122. <DL><DT><B>s_exec</B> (<VAR>code</VAR>) -- Method on RExec object<DD>
  123. <VAR>code</VAR> must be a string containing one or more lines of Python code,
  124. which will be executed in the restricted environment.  
  125. </DL>
  126. <DL><DT><B>s_execfile</B> (<VAR>code</VAR>) -- Method on RExec object<DD>
  127. Execute the Python code contained in the file <VAR>filename</VAR> in the
  128. restricted environment.
  129. </DL>
  130. <CODE>RExec</CODE> objects must also support various methods which will be
  131. implicitly called by code executing in the restricted environment.
  132. Overriding these methods in a subclass is used to change the policies
  133. enforced by a restricted environment.
  134. <P>
  135. <DL><DT><B>r_import</B> (<VAR>modulename</VAR>[, <VAR>globals</VAR>, <VAR>locals</VAR>, <VAR>fromlist</VAR>]) -- Method on RExec object<DD>
  136. Import the module <VAR>modulename</VAR>, raising an <CODE>ImportError</CODE>
  137. exception if the module is considered unsafe.
  138. </DL>
  139. <DL><DT><B>r_open</B> (<VAR>filename</VAR>[, <VAR>mode</VAR>[, <VAR>bufsize</VAR>]]) -- Method on RExec object<DD>
  140. Method called when <CODE>open()</CODE> is called in the restricted
  141. environment.  The arguments are identical to those of <CODE>open()</CODE>,
  142. and a file object (or a class instance compatible with file objects)
  143. should be returned.  <CODE>RExec</CODE>'s default behaviour is allow opening
  144. any file for reading, but forbidding any attempt to write a file.  See
  145. the example below for an implementation of a less restrictive
  146. <CODE>r_open()</CODE>.
  147. </DL>
  148. <DL><DT><B>r_reload</B> (<VAR>module</VAR>) -- Method on RExec object<DD>
  149. Reload the module object <VAR>module</VAR>, re-parsing and re-initializing it.  
  150. </DL>
  151. <DL><DT><B>r_unload</B> (<VAR>module</VAR>) -- Method on RExec object<DD>
  152. Unload the module object <VAR>module</VAR> (i.e., remove it from the
  153. restricted environment's <CODE>sys.modules</CODE> dictionary).
  154. </DL>
  155. And their equivalents with access to restricted standard I/O streams:
  156. <P>
  157. <DL><DT><B>s_import</B> (<VAR>modulename</VAR>[, <VAR>globals</VAR>, <VAR>locals</VAR>, <VAR>fromlist</VAR>]) -- Method on RExec object<DD>
  158. Import the module <VAR>modulename</VAR>, raising an <CODE>ImportError</CODE>
  159. exception if the module is considered unsafe.
  160. </DL>
  161. <DL><DT><B>s_reload</B> (<VAR>module</VAR>) -- Method on RExec object<DD>
  162. Reload the module object <VAR>module</VAR>, re-parsing and re-initializing it.  
  163. </DL>
  164. <DL><DT><B>s_unload</B> (<VAR>module</VAR>) -- Method on RExec object<DD>
  165. Unload the module object <VAR>module</VAR>.   
  166. </DL>
  167. <H2>Menu</H2><DL COMPACT>
  168. <DT><A HREF="../a/an_example" TYPE=Menu>An example</A>
  169. <DD></DL>
  170.