annotate Lib/rexec.py @ 80661:94de77bd0b4b 2.7

Issue #16194: document imp.load_dynamic problems when read several extension modules from the same binary object file.
author Andrew Svetlov <andrew.svetlov@gmail.com>
date Thu, 29 Nov 2012 19:51:28 +0200
parents cc367c077245
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2886
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
1 """Restricted execution facilities.
2135
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
3637
6c0086f9270c Fix some obsolete names comments.
Guido van Rossum <guido@python.org>
parents: 2965
diff changeset
3 The class RExec exports methods r_exec(), r_eval(), r_execfile(), and
6c0086f9270c Fix some obsolete names comments.
Guido van Rossum <guido@python.org>
parents: 2965
diff changeset
4 r_import(), which correspond roughly to the built-in operations
2886
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
5 exec, eval(), execfile() and import, but executing the code in an
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
6 environment that only exposes those built-in operations that are
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
7 deemed safe. To this end, a modest collection of 'fake' modules is
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
8 created which mimics the standard modules by the same names. It is a
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
9 policy decision which built-in modules and operations are made
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
10 available; this module provides a reasonable default, but derived
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
11 classes can change the policies e.g. by overriding or extending class
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
12 variables like ok_builtin_modules or methods like make_sys().
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
13
2926
4a53483939c1 added sys.std files, read-only open, reload
Guido van Rossum <guido@python.org>
parents: 2886
diff changeset
14 XXX To do:
4a53483939c1 added sys.std files, read-only open, reload
Guido van Rossum <guido@python.org>
parents: 2886
diff changeset
15 - r_open should allow writing tmp dir
4a53483939c1 added sys.std files, read-only open, reload
Guido van Rossum <guido@python.org>
parents: 2886
diff changeset
16 - r_exec etc. with explicit globals/locals? (Use rexec("exec ... in ...")?)
4a53483939c1 added sys.std files, read-only open, reload
Guido van Rossum <guido@python.org>
parents: 2886
diff changeset
17
2886
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
18 """
46811
5679ce66e74b Deprecate Bastion and rexec for 3.0.
Brett Cannon <bcannon@gmail.com>
parents: 41443
diff changeset
19 from warnings import warnpy3k
5679ce66e74b Deprecate Bastion and rexec for 3.0.
Brett Cannon <bcannon@gmail.com>
parents: 41443
diff changeset
20 warnpy3k("the rexec module has been removed in Python 3.0", stacklevel=2)
5679ce66e74b Deprecate Bastion and rexec for 3.0.
Brett Cannon <bcannon@gmail.com>
parents: 41443
diff changeset
21 del warnpy3k
2886
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
22
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
23
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
24 import sys
2135
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 import __builtin__
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
26 import os
2886
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
27 import ihooks
23603
cffd091f56d0 SF bug 533625 (Armin Rigo). rexec: potential security hole
Guido van Rossum <guido@python.org>
parents: 23578
diff changeset
28 import imp
2886
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
29
16613
f68dbffff927 bunch more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 15871
diff changeset
30 __all__ = ["RExec"]
2886
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
31
2926
4a53483939c1 added sys.std files, read-only open, reload
Guido van Rossum <guido@python.org>
parents: 2886
diff changeset
32 class FileBase:
4a53483939c1 added sys.std files, read-only open, reload
Guido van Rossum <guido@python.org>
parents: 2886
diff changeset
33
7756
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
34 ok_file_methods = ('fileno', 'flush', 'isatty', 'read', 'readline',
26668
373d0a8ed6be Applying patch
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 25729
diff changeset
35 'readlines', 'seek', 'tell', 'write', 'writelines', 'xreadlines',
373d0a8ed6be Applying patch
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 25729
diff changeset
36 '__iter__')
2926
4a53483939c1 added sys.std files, read-only open, reload
Guido van Rossum <guido@python.org>
parents: 2886
diff changeset
37
4a53483939c1 added sys.std files, read-only open, reload
Guido van Rossum <guido@python.org>
parents: 2886
diff changeset
38
4a53483939c1 added sys.std files, read-only open, reload
Guido van Rossum <guido@python.org>
parents: 2886
diff changeset
39 class FileWrapper(FileBase):
4a53483939c1 added sys.std files, read-only open, reload
Guido van Rossum <guido@python.org>
parents: 2886
diff changeset
40
7756
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
41 # XXX This is just like a Bastion -- should use that!
3734
04c07538cac4 Fixed restore_files(); added reset_files(); made these more flexible.
Guido van Rossum <guido@python.org>
parents: 3648
diff changeset
42
7756
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
43 def __init__(self, f):
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
44 for m in self.ok_file_methods:
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
45 if not hasattr(self, m) and hasattr(f, m):
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
46 setattr(self, m, getattr(f, m))
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
47
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
48 def close(self):
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
49 self.flush()
2926
4a53483939c1 added sys.std files, read-only open, reload
Guido van Rossum <guido@python.org>
parents: 2886
diff changeset
50
4a53483939c1 added sys.std files, read-only open, reload
Guido van Rossum <guido@python.org>
parents: 2886
diff changeset
51
4a53483939c1 added sys.std files, read-only open, reload
Guido van Rossum <guido@python.org>
parents: 2886
diff changeset
52 TEMPLATE = """
4a53483939c1 added sys.std files, read-only open, reload
Guido van Rossum <guido@python.org>
parents: 2886
diff changeset
53 def %s(self, *args):
28089
14f65d661084 Get rid of many apply() calls.
Guido van Rossum <guido@python.org>
parents: 27109
diff changeset
54 return getattr(self.mod, self.name).%s(*args)
2926
4a53483939c1 added sys.std files, read-only open, reload
Guido van Rossum <guido@python.org>
parents: 2886
diff changeset
55 """
4a53483939c1 added sys.std files, read-only open, reload
Guido van Rossum <guido@python.org>
parents: 2886
diff changeset
56
4a53483939c1 added sys.std files, read-only open, reload
Guido van Rossum <guido@python.org>
parents: 2886
diff changeset
57 class FileDelegate(FileBase):
4a53483939c1 added sys.std files, read-only open, reload
Guido van Rossum <guido@python.org>
parents: 2886
diff changeset
58
7756
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
59 def __init__(self, mod, name):
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
60 self.mod = mod
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
61 self.name = name
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
62
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
63 for m in FileBase.ok_file_methods + ('close',):
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
64 exec TEMPLATE % (m, m)
2926
4a53483939c1 added sys.std files, read-only open, reload
Guido van Rossum <guido@python.org>
parents: 2886
diff changeset
65
4a53483939c1 added sys.std files, read-only open, reload
Guido van Rossum <guido@python.org>
parents: 2886
diff changeset
66
2886
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
67 class RHooks(ihooks.Hooks):
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
68
3637
6c0086f9270c Fix some obsolete names comments.
Guido van Rossum <guido@python.org>
parents: 2965
diff changeset
69 def __init__(self, *args):
7756
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
70 # Hacks to support both old and new interfaces:
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
71 # old interface was RHooks(rexec[, verbose])
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
72 # new interface is RHooks([verbose])
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
73 verbose = 0
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
74 rexec = None
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
75 if args and type(args[-1]) == type(0):
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
76 verbose = args[-1]
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
77 args = args[:-1]
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
78 if args and hasattr(args[0], '__class__'):
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
79 rexec = args[0]
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
80 args = args[1:]
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
81 if args:
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
82 raise TypeError, "too many arguments"
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
83 ihooks.Hooks.__init__(self, verbose)
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
84 self.rexec = rexec
2886
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
85
3637
6c0086f9270c Fix some obsolete names comments.
Guido van Rossum <guido@python.org>
parents: 2965
diff changeset
86 def set_rexec(self, rexec):
7756
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
87 # Called by RExec instance to complete initialization
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
88 self.rexec = rexec
3637
6c0086f9270c Fix some obsolete names comments.
Guido van Rossum <guido@python.org>
parents: 2965
diff changeset
89
23603
cffd091f56d0 SF bug 533625 (Armin Rigo). rexec: potential security hole
Guido van Rossum <guido@python.org>
parents: 23578
diff changeset
90 def get_suffixes(self):
cffd091f56d0 SF bug 533625 (Armin Rigo). rexec: potential security hole
Guido van Rossum <guido@python.org>
parents: 23578
diff changeset
91 return self.rexec.get_suffixes()
cffd091f56d0 SF bug 533625 (Armin Rigo). rexec: potential security hole
Guido van Rossum <guido@python.org>
parents: 23578
diff changeset
92
2886
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
93 def is_builtin(self, name):
7756
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
94 return self.rexec.is_builtin(name)
2886
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
95
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
96 def init_builtin(self, name):
7756
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
97 m = __import__(name)
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
98 return self.rexec.copy_except(m, ())
2886
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
99
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
100 def init_frozen(self, name): raise SystemError, "don't use this"
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
101 def load_source(self, *args): raise SystemError, "don't use this"
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
102 def load_compiled(self, *args): raise SystemError, "don't use this"
8587
1581c931cb35 For completeness, add a dummy load_package() method to RHooks. It
Guido van Rossum <guido@python.org>
parents: 8479
diff changeset
103 def load_package(self, *args): raise SystemError, "don't use this"
2135
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
104
3637
6c0086f9270c Fix some obsolete names comments.
Guido van Rossum <guido@python.org>
parents: 2965
diff changeset
105 def load_dynamic(self, name, filename, file):
7756
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
106 return self.rexec.load_dynamic(name, filename, file)
2886
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
107
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
108 def add_module(self, name):
7756
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
109 return self.rexec.add_module(name)
2886
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
110
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
111 def modules_dict(self):
7756
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
112 return self.rexec.modules
2886
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
113
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
114 def default_path(self):
7756
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
115 return self.rexec.modules['sys'].path
2886
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
116
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
117
9130
b0fa2fc12dfb Get rid of the classes RModuleLoader and RModuleImporter -- these were
Guido van Rossum <guido@python.org>
parents: 8655
diff changeset
118 # XXX Backwards compatibility
b0fa2fc12dfb Get rid of the classes RModuleLoader and RModuleImporter -- these were
Guido van Rossum <guido@python.org>
parents: 8655
diff changeset
119 RModuleLoader = ihooks.FancyModuleLoader
b0fa2fc12dfb Get rid of the classes RModuleLoader and RModuleImporter -- these were
Guido van Rossum <guido@python.org>
parents: 8655
diff changeset
120 RModuleImporter = ihooks.ModuleImporter
2886
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
121
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
122
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
123 class RExec(ihooks._Verbose):
23565
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
124 """Basic restricted execution framework.
2886
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
125
23565
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
126 Code executed in this restricted environment will only have access to
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
127 modules and functions that are deemed safe; you can subclass RExec to
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
128 add or remove capabilities as desired.
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
129
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
130 The RExec class can prevent code from performing unsafe operations like
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
131 reading or writing disk files, or using TCP/IP sockets. However, it does
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
132 not protect against code using extremely large amounts of memory or
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
133 processor time.
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
134
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
135 """
2886
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
136
7756
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
137 ok_path = tuple(sys.path) # That's a policy decision
2886
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
138
4334
f37cbfaa7328 1. Correct typo in FileWrapper.close() (fix by AMK).
Guido van Rossum <guido@python.org>
parents: 4075
diff changeset
139 ok_builtin_modules = ('audioop', 'array', 'binascii',
7756
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
140 'cmath', 'errno', 'imageop',
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
141 'marshal', 'math', 'md5', 'operator',
37021
4727f260f6f8 Remove regsub, reconvert, regex, regex_syntax and everything under lib-old.
Neal Norwitz <nnorwitz@gmail.com>
parents: 33369
diff changeset
142 'parser', 'select',
26668
373d0a8ed6be Applying patch
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 25729
diff changeset
143 'sha', '_sre', 'strop', 'struct', 'time',
33369
5f0c7f5ca6f9 Remove rotor, xreadlines modules
Andrew M. Kuchling <amk@amk.ca>
parents: 32200
diff changeset
144 '_weakref')
2886
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
145
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
146 ok_posix_names = ('error', 'fstat', 'listdir', 'lstat', 'readlink',
7756
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
147 'stat', 'times', 'uname', 'getpid', 'getppid',
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
148 'getcwd', 'getuid', 'getgid', 'geteuid', 'getegid')
2886
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
149
25729
b59d1a119393 Allow restricted code to get byteorder, getdefaultencoding(),
Fred Drake <fdrake@acm.org>
parents: 25728
diff changeset
150 ok_sys_names = ('byteorder', 'copyright', 'exit', 'getdefaultencoding',
b59d1a119393 Allow restricted code to get byteorder, getdefaultencoding(),
Fred Drake <fdrake@acm.org>
parents: 25728
diff changeset
151 'getrefcount', 'hexversion', 'maxint', 'maxunicode',
b59d1a119393 Allow restricted code to get byteorder, getdefaultencoding(),
Fred Drake <fdrake@acm.org>
parents: 25728
diff changeset
152 'platform', 'ps1', 'ps2', 'version', 'version_info')
2886
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
153
19901
07818cbcd302 Now that file objects are subclassable, you can get at the file constructor
Tim Peters <tim.peters@gmail.com>
parents: 19231
diff changeset
154 nok_builtin_names = ('open', 'file', 'reload', '__import__')
2135
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
155
23603
cffd091f56d0 SF bug 533625 (Armin Rigo). rexec: potential security hole
Guido van Rossum <guido@python.org>
parents: 23578
diff changeset
156 ok_file_types = (imp.C_EXTENSION, imp.PY_SOURCE)
cffd091f56d0 SF bug 533625 (Armin Rigo). rexec: potential security hole
Guido van Rossum <guido@python.org>
parents: 23578
diff changeset
157
2886
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
158 def __init__(self, hooks = None, verbose = 0):
23565
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
159 """Returns an instance of the RExec class.
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
160
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
161 The hooks parameter is an instance of the RHooks class or a subclass
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
162 of it. If it is omitted or None, the default RHooks class is
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
163 instantiated.
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
164
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
165 Whenever the RExec module searches for a module (even a built-in one)
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
166 or reads a module's code, it doesn't actually go out to the file
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
167 system itself. Rather, it calls methods of an RHooks instance that
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
168 was passed to or created by its constructor. (Actually, the RExec
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
169 object doesn't make these calls --- they are made by a module loader
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
170 object that's part of the RExec object. This allows another level of
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
171 flexibility, which can be useful when changing the mechanics of
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
172 import within the restricted environment.)
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
173
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
174 By providing an alternate RHooks object, we can control the file
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
175 system accesses made to import a module, without changing the
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
176 actual algorithm that controls the order in which those accesses are
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
177 made. For instance, we could substitute an RHooks object that
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
178 passes all filesystem requests to a file server elsewhere, via some
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
179 RPC mechanism such as ILU. Grail's applet loader uses this to support
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
180 importing applets from a URL for a directory.
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
181
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
182 If the verbose parameter is true, additional debugging output may be
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
183 sent to standard output.
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
184
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
185 """
27109
0464acb5b3e4 Sabotage rexec.py. It is not safe since the new-style classes.
Guido van Rossum <guido@python.org>
parents: 26668
diff changeset
186
41443
c34d705b884e Patch #1698951: clarify deprecation message in rexec and Bastion
Georg Brandl <georg@python.org>
parents: 37021
diff changeset
187 raise RuntimeError, "This code is not secure in Python 2.2 and later"
27109
0464acb5b3e4 Sabotage rexec.py. It is not safe since the new-style classes.
Guido van Rossum <guido@python.org>
parents: 26668
diff changeset
188
7756
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
189 ihooks._Verbose.__init__(self, verbose)
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
190 # XXX There's a circular reference here:
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
191 self.hooks = hooks or RHooks(verbose)
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
192 self.hooks.set_rexec(self)
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
193 self.modules = {}
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
194 self.ok_dynamic_modules = self.ok_builtin_modules
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
195 list = []
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
196 for mname in self.ok_builtin_modules:
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
197 if mname in sys.builtin_module_names:
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
198 list.append(mname)
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
199 self.ok_builtin_modules = tuple(list)
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
200 self.set_trusted_path()
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
201 self.make_builtin()
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
202 self.make_initial_modules()
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
203 # make_sys must be last because it adds the already created
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
204 # modules to its builtin_module_names
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
205 self.make_sys()
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
206 self.loader = RModuleLoader(self.hooks, verbose)
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
207 self.importer = RModuleImporter(self.loader, verbose)
2135
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
208
3637
6c0086f9270c Fix some obsolete names comments.
Guido van Rossum <guido@python.org>
parents: 2965
diff changeset
209 def set_trusted_path(self):
7756
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
210 # Set the path from which dynamic modules may be loaded.
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
211 # Those dynamic modules must also occur in ok_builtin_modules
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
212 self.trusted_path = filter(os.path.isabs, sys.path)
3637
6c0086f9270c Fix some obsolete names comments.
Guido van Rossum <guido@python.org>
parents: 2965
diff changeset
213
6c0086f9270c Fix some obsolete names comments.
Guido van Rossum <guido@python.org>
parents: 2965
diff changeset
214 def load_dynamic(self, name, filename, file):
7756
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
215 if name not in self.ok_dynamic_modules:
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
216 raise ImportError, "untrusted dynamic module: %s" % name
23620
a1de0bab5475 SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents: 23603
diff changeset
217 if name in sys.modules:
7756
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
218 src = sys.modules[name]
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
219 else:
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
220 src = imp.load_dynamic(name, filename, file)
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
221 dst = self.copy_except(src, [])
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
222 return dst
3637
6c0086f9270c Fix some obsolete names comments.
Guido van Rossum <guido@python.org>
parents: 2965
diff changeset
223
2886
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
224 def make_initial_modules(self):
7756
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
225 self.make_main()
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
226 self.make_osname()
2886
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
227
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
228 # Helpers for RHooks
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
229
23603
cffd091f56d0 SF bug 533625 (Armin Rigo). rexec: potential security hole
Guido van Rossum <guido@python.org>
parents: 23578
diff changeset
230 def get_suffixes(self):
cffd091f56d0 SF bug 533625 (Armin Rigo). rexec: potential security hole
Guido van Rossum <guido@python.org>
parents: 23578
diff changeset
231 return [item # (suff, mode, type)
cffd091f56d0 SF bug 533625 (Armin Rigo). rexec: potential security hole
Guido van Rossum <guido@python.org>
parents: 23578
diff changeset
232 for item in imp.get_suffixes()
cffd091f56d0 SF bug 533625 (Armin Rigo). rexec: potential security hole
Guido van Rossum <guido@python.org>
parents: 23578
diff changeset
233 if item[2] in self.ok_file_types]
cffd091f56d0 SF bug 533625 (Armin Rigo). rexec: potential security hole
Guido van Rossum <guido@python.org>
parents: 23578
diff changeset
234
2886
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
235 def is_builtin(self, mname):
7756
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
236 return mname in self.ok_builtin_modules
2886
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
237
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
238 # The make_* methods create specific built-in modules
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
239
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
240 def make_builtin(self):
7756
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
241 m = self.copy_except(__builtin__, self.nok_builtin_names)
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
242 m.__import__ = self.r_import
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
243 m.reload = self.r_reload
19901
07818cbcd302 Now that file objects are subclassable, you can get at the file constructor
Tim Peters <tim.peters@gmail.com>
parents: 19231
diff changeset
244 m.open = m.file = self.r_open
2886
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
245
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
246 def make_main(self):
58761
cc367c077245 No need to assign the results of expressions used only for side effects.
Georg Brandl <georg@python.org>
parents: 46811
diff changeset
247 self.add_module('__main__')
2135
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
248
2886
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
249 def make_osname(self):
7756
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
250 osname = os.name
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
251 src = __import__(osname)
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
252 dst = self.copy_only(src, self.ok_posix_names)
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
253 dst.environ = e = {}
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
254 for key, value in os.environ.items():
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
255 e[key] = value
2886
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
256
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
257 def make_sys(self):
7756
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
258 m = self.copy_only(sys, self.ok_sys_names)
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
259 m.modules = self.modules
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
260 m.argv = ['RESTRICTED']
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
261 m.path = map(None, self.ok_path)
8655
ed0a38cbc3e6 (1) Added a sys.exc_info() emulation. (It returns None for the traceback.)
Guido van Rossum <guido@python.org>
parents: 8587
diff changeset
262 m.exc_info = self.r_exc_info
7756
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
263 m = self.modules['sys']
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
264 l = self.modules.keys() + list(self.ok_builtin_modules)
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
265 l.sort()
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
266 m.builtin_module_names = tuple(l)
2886
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
267
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
268 # The copy_* methods copy existing modules with some changes
2135
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
269
2886
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
270 def copy_except(self, src, exceptions):
7756
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
271 dst = self.copy_none(src)
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
272 for name in dir(src):
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
273 setattr(dst, name, getattr(src, name))
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
274 for name in exceptions:
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
275 try:
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
276 delattr(dst, name)
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
277 except AttributeError:
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
278 pass
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
279 return dst
2886
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
280
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
281 def copy_only(self, src, names):
7756
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
282 dst = self.copy_none(src)
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
283 for name in names:
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
284 try:
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
285 value = getattr(src, name)
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
286 except AttributeError:
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
287 continue
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
288 setattr(dst, name, value)
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
289 return dst
2886
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
290
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
291 def copy_none(self, src):
8479
e281d5036725 Add the __doc__ string from the original module on copy_none().
Guido van Rossum <guido@python.org>
parents: 7756
diff changeset
292 m = self.add_module(src.__name__)
e281d5036725 Add the __doc__ string from the original module on copy_none().
Guido van Rossum <guido@python.org>
parents: 7756
diff changeset
293 m.__doc__ = src.__doc__
e281d5036725 Add the __doc__ string from the original module on copy_none().
Guido van Rossum <guido@python.org>
parents: 7756
diff changeset
294 return m
2886
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
295
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
296 # Add a module -- return an existing module or create one
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
297
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
298 def add_module(self, mname):
25273
98e12aa8c547 Address SF bug #577530: del __builtins__ breaks out of rexec
Guido van Rossum <guido@python.org>
parents: 23864
diff changeset
299 m = self.modules.get(mname)
98e12aa8c547 Address SF bug #577530: del __builtins__ breaks out of rexec
Guido van Rossum <guido@python.org>
parents: 23864
diff changeset
300 if m is None:
98e12aa8c547 Address SF bug #577530: del __builtins__ breaks out of rexec
Guido van Rossum <guido@python.org>
parents: 23864
diff changeset
301 self.modules[mname] = m = self.hooks.new_module(mname)
7756
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
302 m.__builtins__ = self.modules['__builtin__']
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
303 return m
2135
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
304
2886
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
305 # The r* methods are public interfaces
2135
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
306
2886
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
307 def r_exec(self, code):
23565
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
308 """Execute code within a restricted environment.
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
309
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
310 The code parameter must either be a string containing one or more
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
311 lines of Python code, or a compiled code object, which will be
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
312 executed in the restricted environment's __main__ module.
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
313
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
314 """
7756
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
315 m = self.add_module('__main__')
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
316 exec code in m.__dict__
2135
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
317
2886
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
318 def r_eval(self, code):
23565
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
319 """Evaluate code within a restricted environment.
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
320
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
321 The code parameter must either be a string containing a Python
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
322 expression, or a compiled code object, which will be evaluated in
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
323 the restricted environment's __main__ module. The value of the
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
324 expression or code object will be returned.
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
325
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
326 """
7756
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
327 m = self.add_module('__main__')
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
328 return eval(code, m.__dict__)
2135
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
329
2886
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
330 def r_execfile(self, file):
23565
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
331 """Execute the Python code in the file in the restricted
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
332 environment's __main__ module.
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
333
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
334 """
7756
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
335 m = self.add_module('__main__')
20419
4f3d1240830e Ignore execfile() return value; reported by Neal Norwitz.
Fred Drake <fdrake@acm.org>
parents: 19901
diff changeset
336 execfile(file, m.__dict__)
2135
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
337
2886
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
338 def r_import(self, mname, globals={}, locals={}, fromlist=[]):
23565
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
339 """Import a module, raising an ImportError exception if the module
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
340 is considered unsafe.
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
341
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
342 This method is implicitly called by code executing in the
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
343 restricted environment. Overriding this method in a subclass is
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
344 used to change the policies enforced by a restricted environment.
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
345
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
346 """
7756
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
347 return self.importer.import_module(mname, globals, locals, fromlist)
2135
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
348
2926
4a53483939c1 added sys.std files, read-only open, reload
Guido van Rossum <guido@python.org>
parents: 2886
diff changeset
349 def r_reload(self, m):
23565
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
350 """Reload the module object, re-parsing and re-initializing it.
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
351
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
352 This method is implicitly called by code executing in the
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
353 restricted environment. Overriding this method in a subclass is
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
354 used to change the policies enforced by a restricted environment.
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
355
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
356 """
2926
4a53483939c1 added sys.std files, read-only open, reload
Guido van Rossum <guido@python.org>
parents: 2886
diff changeset
357 return self.importer.reload(m)
2934
cb5ccbba1856 add module binascii; add r_unload/s_unload; don't change 'rb' to 'r' in open
Guido van Rossum <guido@python.org>
parents: 2926
diff changeset
358
cb5ccbba1856 add module binascii; add r_unload/s_unload; don't change 'rb' to 'r' in open
Guido van Rossum <guido@python.org>
parents: 2926
diff changeset
359 def r_unload(self, m):
23565
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
360 """Unload the module.
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
361
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
362 Removes it from the restricted environment's sys.modules dictionary.
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
363
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
364 This method is implicitly called by code executing in the
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
365 restricted environment. Overriding this method in a subclass is
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
366 used to change the policies enforced by a restricted environment.
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
367
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
368 """
2934
cb5ccbba1856 add module binascii; add r_unload/s_unload; don't change 'rb' to 'r' in open
Guido van Rossum <guido@python.org>
parents: 2926
diff changeset
369 return self.importer.unload(m)
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15039
diff changeset
370
2926
4a53483939c1 added sys.std files, read-only open, reload
Guido van Rossum <guido@python.org>
parents: 2886
diff changeset
371 # The s_* methods are similar but also swap std{in,out,err}
4a53483939c1 added sys.std files, read-only open, reload
Guido van Rossum <guido@python.org>
parents: 2886
diff changeset
372
3734
04c07538cac4 Fixed restore_files(); added reset_files(); made these more flexible.
Guido van Rossum <guido@python.org>
parents: 3648
diff changeset
373 def make_delegate_files(self):
04c07538cac4 Fixed restore_files(); added reset_files(); made these more flexible.
Guido van Rossum <guido@python.org>
parents: 3648
diff changeset
374 s = self.modules['sys']
7756
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
375 self.delegate_stdin = FileDelegate(s, 'stdin')
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
376 self.delegate_stdout = FileDelegate(s, 'stdout')
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
377 self.delegate_stderr = FileDelegate(s, 'stderr')
3734
04c07538cac4 Fixed restore_files(); added reset_files(); made these more flexible.
Guido van Rossum <guido@python.org>
parents: 3648
diff changeset
378 self.restricted_stdin = FileWrapper(sys.stdin)
04c07538cac4 Fixed restore_files(); added reset_files(); made these more flexible.
Guido van Rossum <guido@python.org>
parents: 3648
diff changeset
379 self.restricted_stdout = FileWrapper(sys.stdout)
04c07538cac4 Fixed restore_files(); added reset_files(); made these more flexible.
Guido van Rossum <guido@python.org>
parents: 3648
diff changeset
380 self.restricted_stderr = FileWrapper(sys.stderr)
04c07538cac4 Fixed restore_files(); added reset_files(); made these more flexible.
Guido van Rossum <guido@python.org>
parents: 3648
diff changeset
381
2926
4a53483939c1 added sys.std files, read-only open, reload
Guido van Rossum <guido@python.org>
parents: 2886
diff changeset
382 def set_files(self):
7756
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
383 if not hasattr(self, 'save_stdin'):
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
384 self.save_files()
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
385 if not hasattr(self, 'delegate_stdin'):
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
386 self.make_delegate_files()
2926
4a53483939c1 added sys.std files, read-only open, reload
Guido van Rossum <guido@python.org>
parents: 2886
diff changeset
387 s = self.modules['sys']
7756
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
388 s.stdin = self.restricted_stdin
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
389 s.stdout = self.restricted_stdout
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
390 s.stderr = self.restricted_stderr
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
391 sys.stdin = self.delegate_stdin
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
392 sys.stdout = self.delegate_stdout
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
393 sys.stderr = self.delegate_stderr
3734
04c07538cac4 Fixed restore_files(); added reset_files(); made these more flexible.
Guido van Rossum <guido@python.org>
parents: 3648
diff changeset
394
04c07538cac4 Fixed restore_files(); added reset_files(); made these more flexible.
Guido van Rossum <guido@python.org>
parents: 3648
diff changeset
395 def reset_files(self):
7756
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
396 self.restore_files()
3734
04c07538cac4 Fixed restore_files(); added reset_files(); made these more flexible.
Guido van Rossum <guido@python.org>
parents: 3648
diff changeset
397 s = self.modules['sys']
7756
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
398 self.restricted_stdin = s.stdin
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
399 self.restricted_stdout = s.stdout
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
400 self.restricted_stderr = s.stderr
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15039
diff changeset
401
2926
4a53483939c1 added sys.std files, read-only open, reload
Guido van Rossum <guido@python.org>
parents: 2886
diff changeset
402
4a53483939c1 added sys.std files, read-only open, reload
Guido van Rossum <guido@python.org>
parents: 2886
diff changeset
403 def save_files(self):
4a53483939c1 added sys.std files, read-only open, reload
Guido van Rossum <guido@python.org>
parents: 2886
diff changeset
404 self.save_stdin = sys.stdin
4a53483939c1 added sys.std files, read-only open, reload
Guido van Rossum <guido@python.org>
parents: 2886
diff changeset
405 self.save_stdout = sys.stdout
4a53483939c1 added sys.std files, read-only open, reload
Guido van Rossum <guido@python.org>
parents: 2886
diff changeset
406 self.save_stderr = sys.stderr
4a53483939c1 added sys.std files, read-only open, reload
Guido van Rossum <guido@python.org>
parents: 2886
diff changeset
407
3734
04c07538cac4 Fixed restore_files(); added reset_files(); made these more flexible.
Guido van Rossum <guido@python.org>
parents: 3648
diff changeset
408 def restore_files(self):
7756
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
409 sys.stdin = self.save_stdin
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
410 sys.stdout = self.save_stdout
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
411 sys.stderr = self.save_stderr
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15039
diff changeset
412
28089
14f65d661084 Get rid of many apply() calls.
Guido van Rossum <guido@python.org>
parents: 27109
diff changeset
413 def s_apply(self, func, args=(), kw={}):
7756
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
414 self.save_files()
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
415 try:
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
416 self.set_files()
28089
14f65d661084 Get rid of many apply() calls.
Guido van Rossum <guido@python.org>
parents: 27109
diff changeset
417 r = func(*args, **kw)
2926
4a53483939c1 added sys.std files, read-only open, reload
Guido van Rossum <guido@python.org>
parents: 2886
diff changeset
418 finally:
7756
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
419 self.restore_files()
18142
47c9d0cb6e31 Fix SF bug #433904 (Alex Martelli) - all s_* methods return None only.
Guido van Rossum <guido@python.org>
parents: 16613
diff changeset
420 return r
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15039
diff changeset
421
2926
4a53483939c1 added sys.std files, read-only open, reload
Guido van Rossum <guido@python.org>
parents: 2886
diff changeset
422 def s_exec(self, *args):
23565
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
423 """Execute code within a restricted environment.
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
424
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
425 Similar to the r_exec() method, but the code will be granted access
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
426 to restricted versions of the standard I/O streams sys.stdin,
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
427 sys.stderr, and sys.stdout.
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
428
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
429 The code parameter must either be a string containing one or more
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
430 lines of Python code, or a compiled code object, which will be
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
431 executed in the restricted environment's __main__ module.
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
432
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
433 """
18142
47c9d0cb6e31 Fix SF bug #433904 (Alex Martelli) - all s_* methods return None only.
Guido van Rossum <guido@python.org>
parents: 16613
diff changeset
434 return self.s_apply(self.r_exec, args)
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15039
diff changeset
435
2926
4a53483939c1 added sys.std files, read-only open, reload
Guido van Rossum <guido@python.org>
parents: 2886
diff changeset
436 def s_eval(self, *args):
23565
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
437 """Evaluate code within a restricted environment.
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
438
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
439 Similar to the r_eval() method, but the code will be granted access
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
440 to restricted versions of the standard I/O streams sys.stdin,
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
441 sys.stderr, and sys.stdout.
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
442
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
443 The code parameter must either be a string containing a Python
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
444 expression, or a compiled code object, which will be evaluated in
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
445 the restricted environment's __main__ module. The value of the
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
446 expression or code object will be returned.
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15039
diff changeset
447
23565
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
448 """
23578
c367607bb5f7 Move statement out of comment block
Raymond Hettinger <python@rcn.com>
parents: 23565
diff changeset
449 return self.s_apply(self.r_eval, args)
23565
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
450
2926
4a53483939c1 added sys.std files, read-only open, reload
Guido van Rossum <guido@python.org>
parents: 2886
diff changeset
451 def s_execfile(self, *args):
23565
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
452 """Execute the Python code in the file in the restricted
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
453 environment's __main__ module.
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
454
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
455 Similar to the r_execfile() method, but the code will be granted
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
456 access to restricted versions of the standard I/O streams sys.stdin,
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
457 sys.stderr, and sys.stdout.
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
458
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
459 """
18142
47c9d0cb6e31 Fix SF bug #433904 (Alex Martelli) - all s_* methods return None only.
Guido van Rossum <guido@python.org>
parents: 16613
diff changeset
460 return self.s_apply(self.r_execfile, args)
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15039
diff changeset
461
2926
4a53483939c1 added sys.std files, read-only open, reload
Guido van Rossum <guido@python.org>
parents: 2886
diff changeset
462 def s_import(self, *args):
23565
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
463 """Import a module, raising an ImportError exception if the module
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
464 is considered unsafe.
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
465
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
466 This method is implicitly called by code executing in the
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
467 restricted environment. Overriding this method in a subclass is
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
468 used to change the policies enforced by a restricted environment.
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
469
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
470 Similar to the r_import() method, but has access to restricted
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
471 versions of the standard I/O streams sys.stdin, sys.stderr, and
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
472 sys.stdout.
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
473
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
474 """
18142
47c9d0cb6e31 Fix SF bug #433904 (Alex Martelli) - all s_* methods return None only.
Guido van Rossum <guido@python.org>
parents: 16613
diff changeset
475 return self.s_apply(self.r_import, args)
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15039
diff changeset
476
2926
4a53483939c1 added sys.std files, read-only open, reload
Guido van Rossum <guido@python.org>
parents: 2886
diff changeset
477 def s_reload(self, *args):
23565
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
478 """Reload the module object, re-parsing and re-initializing it.
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
479
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
480 This method is implicitly called by code executing in the
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
481 restricted environment. Overriding this method in a subclass is
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
482 used to change the policies enforced by a restricted environment.
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
483
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
484 Similar to the r_reload() method, but has access to restricted
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
485 versions of the standard I/O streams sys.stdin, sys.stderr, and
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
486 sys.stdout.
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
487
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
488 """
18142
47c9d0cb6e31 Fix SF bug #433904 (Alex Martelli) - all s_* methods return None only.
Guido van Rossum <guido@python.org>
parents: 16613
diff changeset
489 return self.s_apply(self.r_reload, args)
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15039
diff changeset
490
2934
cb5ccbba1856 add module binascii; add r_unload/s_unload; don't change 'rb' to 'r' in open
Guido van Rossum <guido@python.org>
parents: 2926
diff changeset
491 def s_unload(self, *args):
23565
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
492 """Unload the module.
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
493
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
494 Removes it from the restricted environment's sys.modules dictionary.
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
495
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
496 This method is implicitly called by code executing in the
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
497 restricted environment. Overriding this method in a subclass is
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
498 used to change the policies enforced by a restricted environment.
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
499
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
500 Similar to the r_unload() method, but has access to restricted
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
501 versions of the standard I/O streams sys.stdin, sys.stderr, and
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
502 sys.stdout.
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
503
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
504 """
18142
47c9d0cb6e31 Fix SF bug #433904 (Alex Martelli) - all s_* methods return None only.
Guido van Rossum <guido@python.org>
parents: 16613
diff changeset
505 return self.s_apply(self.r_unload, args)
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15039
diff changeset
506
2926
4a53483939c1 added sys.std files, read-only open, reload
Guido van Rossum <guido@python.org>
parents: 2886
diff changeset
507 # Restricted open(...)
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15039
diff changeset
508
2926
4a53483939c1 added sys.std files, read-only open, reload
Guido van Rossum <guido@python.org>
parents: 2886
diff changeset
509 def r_open(self, file, mode='r', buf=-1):
23565
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
510 """Method called when open() is called in the restricted environment.
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
511
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
512 The arguments are identical to those of the open() function, and a
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
513 file object (or a class instance compatible with file objects)
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
514 should be returned. RExec's default behaviour is allow opening
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
515 any file for reading, but forbidding any attempt to write a file.
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
516
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
517 This method is implicitly called by code executing in the
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
518 restricted environment. Overriding this method in a subclass is
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
519 used to change the policies enforced by a restricted environment.
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
520
1a67fbc7a066 Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
521 """
26668
373d0a8ed6be Applying patch
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 25729
diff changeset
522 mode = str(mode)
2926
4a53483939c1 added sys.std files, read-only open, reload
Guido van Rossum <guido@python.org>
parents: 2886
diff changeset
523 if mode not in ('r', 'rb'):
4a53483939c1 added sys.std files, read-only open, reload
Guido van Rossum <guido@python.org>
parents: 2886
diff changeset
524 raise IOError, "can't open files for writing in restricted mode"
2934
cb5ccbba1856 add module binascii; add r_unload/s_unload; don't change 'rb' to 'r' in open
Guido van Rossum <guido@python.org>
parents: 2926
diff changeset
525 return open(file, mode, buf)
2926
4a53483939c1 added sys.std files, read-only open, reload
Guido van Rossum <guido@python.org>
parents: 2886
diff changeset
526
8655
ed0a38cbc3e6 (1) Added a sys.exc_info() emulation. (It returns None for the traceback.)
Guido van Rossum <guido@python.org>
parents: 8587
diff changeset
527 # Restricted version of sys.exc_info()
ed0a38cbc3e6 (1) Added a sys.exc_info() emulation. (It returns None for the traceback.)
Guido van Rossum <guido@python.org>
parents: 8587
diff changeset
528
ed0a38cbc3e6 (1) Added a sys.exc_info() emulation. (It returns None for the traceback.)
Guido van Rossum <guido@python.org>
parents: 8587
diff changeset
529 def r_exc_info(self):
ed0a38cbc3e6 (1) Added a sys.exc_info() emulation. (It returns None for the traceback.)
Guido van Rossum <guido@python.org>
parents: 8587
diff changeset
530 ty, va, tr = sys.exc_info()
ed0a38cbc3e6 (1) Added a sys.exc_info() emulation. (It returns None for the traceback.)
Guido van Rossum <guido@python.org>
parents: 8587
diff changeset
531 tr = None
ed0a38cbc3e6 (1) Added a sys.exc_info() emulation. (It returns None for the traceback.)
Guido van Rossum <guido@python.org>
parents: 8587
diff changeset
532 return ty, va, tr
ed0a38cbc3e6 (1) Added a sys.exc_info() emulation. (It returns None for the traceback.)
Guido van Rossum <guido@python.org>
parents: 8587
diff changeset
533
2135
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
534
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
535 def test():
19231
e29ba8f1e7f8 Remove redundant import (PyChecker).
Guido van Rossum <guido@python.org>
parents: 19045
diff changeset
536 import getopt, traceback
8655
ed0a38cbc3e6 (1) Added a sys.exc_info() emulation. (It returns None for the traceback.)
Guido van Rossum <guido@python.org>
parents: 8587
diff changeset
537 opts, args = getopt.getopt(sys.argv[1:], 'vt:')
ed0a38cbc3e6 (1) Added a sys.exc_info() emulation. (It returns None for the traceback.)
Guido van Rossum <guido@python.org>
parents: 8587
diff changeset
538 verbose = 0
ed0a38cbc3e6 (1) Added a sys.exc_info() emulation. (It returns None for the traceback.)
Guido van Rossum <guido@python.org>
parents: 8587
diff changeset
539 trusted = []
ed0a38cbc3e6 (1) Added a sys.exc_info() emulation. (It returns None for the traceback.)
Guido van Rossum <guido@python.org>
parents: 8587
diff changeset
540 for o, a in opts:
ed0a38cbc3e6 (1) Added a sys.exc_info() emulation. (It returns None for the traceback.)
Guido van Rossum <guido@python.org>
parents: 8587
diff changeset
541 if o == '-v':
ed0a38cbc3e6 (1) Added a sys.exc_info() emulation. (It returns None for the traceback.)
Guido van Rossum <guido@python.org>
parents: 8587
diff changeset
542 verbose = verbose+1
ed0a38cbc3e6 (1) Added a sys.exc_info() emulation. (It returns None for the traceback.)
Guido van Rossum <guido@python.org>
parents: 8587
diff changeset
543 if o == '-t':
ed0a38cbc3e6 (1) Added a sys.exc_info() emulation. (It returns None for the traceback.)
Guido van Rossum <guido@python.org>
parents: 8587
diff changeset
544 trusted.append(a)
ed0a38cbc3e6 (1) Added a sys.exc_info() emulation. (It returns None for the traceback.)
Guido van Rossum <guido@python.org>
parents: 8587
diff changeset
545 r = RExec(verbose=verbose)
ed0a38cbc3e6 (1) Added a sys.exc_info() emulation. (It returns None for the traceback.)
Guido van Rossum <guido@python.org>
parents: 8587
diff changeset
546 if trusted:
ed0a38cbc3e6 (1) Added a sys.exc_info() emulation. (It returns None for the traceback.)
Guido van Rossum <guido@python.org>
parents: 8587
diff changeset
547 r.ok_builtin_modules = r.ok_builtin_modules + tuple(trusted)
ed0a38cbc3e6 (1) Added a sys.exc_info() emulation. (It returns None for the traceback.)
Guido van Rossum <guido@python.org>
parents: 8587
diff changeset
548 if args:
ed0a38cbc3e6 (1) Added a sys.exc_info() emulation. (It returns None for the traceback.)
Guido van Rossum <guido@python.org>
parents: 8587
diff changeset
549 r.modules['sys'].argv = args
ed0a38cbc3e6 (1) Added a sys.exc_info() emulation. (It returns None for the traceback.)
Guido van Rossum <guido@python.org>
parents: 8587
diff changeset
550 r.modules['sys'].path.insert(0, os.path.dirname(args[0]))
ed0a38cbc3e6 (1) Added a sys.exc_info() emulation. (It returns None for the traceback.)
Guido van Rossum <guido@python.org>
parents: 8587
diff changeset
551 else:
ed0a38cbc3e6 (1) Added a sys.exc_info() emulation. (It returns None for the traceback.)
Guido van Rossum <guido@python.org>
parents: 8587
diff changeset
552 r.modules['sys'].path.insert(0, "")
ed0a38cbc3e6 (1) Added a sys.exc_info() emulation. (It returns None for the traceback.)
Guido van Rossum <guido@python.org>
parents: 8587
diff changeset
553 fp = sys.stdin
ed0a38cbc3e6 (1) Added a sys.exc_info() emulation. (It returns None for the traceback.)
Guido van Rossum <guido@python.org>
parents: 8587
diff changeset
554 if args and args[0] != '-':
7756
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
555 try:
8655
ed0a38cbc3e6 (1) Added a sys.exc_info() emulation. (It returns None for the traceback.)
Guido van Rossum <guido@python.org>
parents: 8587
diff changeset
556 fp = open(args[0])
ed0a38cbc3e6 (1) Added a sys.exc_info() emulation. (It returns None for the traceback.)
Guido van Rossum <guido@python.org>
parents: 8587
diff changeset
557 except IOError, msg:
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 28089
diff changeset
558 print "%s: can't open file %r" % (sys.argv[0], args[0])
8655
ed0a38cbc3e6 (1) Added a sys.exc_info() emulation. (It returns None for the traceback.)
Guido van Rossum <guido@python.org>
parents: 8587
diff changeset
559 return 1
ed0a38cbc3e6 (1) Added a sys.exc_info() emulation. (It returns None for the traceback.)
Guido van Rossum <guido@python.org>
parents: 8587
diff changeset
560 if fp.isatty():
23862
b79d43305c72 Don't poorly emulate the interactive interpreter, use
Guido van Rossum <guido@python.org>
parents: 23620
diff changeset
561 try:
25273
98e12aa8c547 Address SF bug #577530: del __builtins__ breaks out of rexec
Guido van Rossum <guido@python.org>
parents: 23864
diff changeset
562 import readline
98e12aa8c547 Address SF bug #577530: del __builtins__ breaks out of rexec
Guido van Rossum <guido@python.org>
parents: 23864
diff changeset
563 except ImportError:
98e12aa8c547 Address SF bug #577530: del __builtins__ breaks out of rexec
Guido van Rossum <guido@python.org>
parents: 23864
diff changeset
564 pass
98e12aa8c547 Address SF bug #577530: del __builtins__ breaks out of rexec
Guido van Rossum <guido@python.org>
parents: 23864
diff changeset
565 import code
98e12aa8c547 Address SF bug #577530: del __builtins__ breaks out of rexec
Guido van Rossum <guido@python.org>
parents: 23864
diff changeset
566 class RestrictedConsole(code.InteractiveConsole):
98e12aa8c547 Address SF bug #577530: del __builtins__ breaks out of rexec
Guido van Rossum <guido@python.org>
parents: 23864
diff changeset
567 def runcode(self, co):
98e12aa8c547 Address SF bug #577530: del __builtins__ breaks out of rexec
Guido van Rossum <guido@python.org>
parents: 23864
diff changeset
568 self.locals['__builtins__'] = r.modules['__builtin__']
98e12aa8c547 Address SF bug #577530: del __builtins__ breaks out of rexec
Guido van Rossum <guido@python.org>
parents: 23864
diff changeset
569 r.s_apply(code.InteractiveConsole.runcode, (self, co))
98e12aa8c547 Address SF bug #577530: del __builtins__ breaks out of rexec
Guido van Rossum <guido@python.org>
parents: 23864
diff changeset
570 try:
98e12aa8c547 Address SF bug #577530: del __builtins__ breaks out of rexec
Guido van Rossum <guido@python.org>
parents: 23864
diff changeset
571 RestrictedConsole(r.modules['__main__'].__dict__).interact()
23862
b79d43305c72 Don't poorly emulate the interactive interpreter, use
Guido van Rossum <guido@python.org>
parents: 23620
diff changeset
572 except SystemExit, n:
b79d43305c72 Don't poorly emulate the interactive interpreter, use
Guido van Rossum <guido@python.org>
parents: 23620
diff changeset
573 return n
8655
ed0a38cbc3e6 (1) Added a sys.exc_info() emulation. (It returns None for the traceback.)
Guido van Rossum <guido@python.org>
parents: 8587
diff changeset
574 else:
ed0a38cbc3e6 (1) Added a sys.exc_info() emulation. (It returns None for the traceback.)
Guido van Rossum <guido@python.org>
parents: 8587
diff changeset
575 text = fp.read()
ed0a38cbc3e6 (1) Added a sys.exc_info() emulation. (It returns None for the traceback.)
Guido van Rossum <guido@python.org>
parents: 8587
diff changeset
576 fp.close()
ed0a38cbc3e6 (1) Added a sys.exc_info() emulation. (It returns None for the traceback.)
Guido van Rossum <guido@python.org>
parents: 8587
diff changeset
577 c = compile(text, fp.name, 'exec')
ed0a38cbc3e6 (1) Added a sys.exc_info() emulation. (It returns None for the traceback.)
Guido van Rossum <guido@python.org>
parents: 8587
diff changeset
578 try:
ed0a38cbc3e6 (1) Added a sys.exc_info() emulation. (It returns None for the traceback.)
Guido van Rossum <guido@python.org>
parents: 8587
diff changeset
579 r.s_exec(c)
7756
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
580 except SystemExit, n:
8655
ed0a38cbc3e6 (1) Added a sys.exc_info() emulation. (It returns None for the traceback.)
Guido van Rossum <guido@python.org>
parents: 8587
diff changeset
581 return n
7756
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
582 except:
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
583 traceback.print_exc()
8655
ed0a38cbc3e6 (1) Added a sys.exc_info() emulation. (It returns None for the traceback.)
Guido van Rossum <guido@python.org>
parents: 8587
diff changeset
584 return 1
2886
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
585
2135
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
586
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
587 if __name__ == '__main__':
8655
ed0a38cbc3e6 (1) Added a sys.exc_info() emulation. (It returns None for the traceback.)
Guido van Rossum <guido@python.org>
parents: 8587
diff changeset
588 sys.exit(test())