Mercurial > cpython-lru_cache-weakref
annotate Lib/rexec.py @ 27998:e27f58a50737 legacy-trunk
SF bug #690377: Dud footnote reference in distutils docs
| author | Raymond Hettinger <python@rcn.com> |
|---|---|
| date | Fri, 21 Feb 2003 02:47:09 +0000 |
| parents | 0464acb5b3e4 |
| children | 14f65d661084 |
| 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 | 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 """ |
|
21e105460a49
new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents:
2776
diff
changeset
|
19 |
|
21e105460a49
new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents:
2776
diff
changeset
|
20 |
|
21e105460a49
new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents:
2776
diff
changeset
|
21 import sys |
| 2135 | 22 import __builtin__ |
| 23 import os | |
|
2886
21e105460a49
new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents:
2776
diff
changeset
|
24 import ihooks |
|
23603
cffd091f56d0
SF bug 533625 (Armin Rigo). rexec: potential security hole
Guido van Rossum <guido@python.org>
parents:
23578
diff
changeset
|
25 import imp |
|
2886
21e105460a49
new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents:
2776
diff
changeset
|
26 |
| 16613 | 27 __all__ = ["RExec"] |
|
2886
21e105460a49
new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents:
2776
diff
changeset
|
28 |
|
2926
4a53483939c1
added sys.std files, read-only open, reload
Guido van Rossum <guido@python.org>
parents:
2886
diff
changeset
|
29 class FileBase: |
|
4a53483939c1
added sys.std files, read-only open, reload
Guido van Rossum <guido@python.org>
parents:
2886
diff
changeset
|
30 |
|
7756
a419334f3b39
A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents:
6518
diff
changeset
|
31 ok_file_methods = ('fileno', 'flush', 'isatty', 'read', 'readline', |
| 26668 | 32 'readlines', 'seek', 'tell', 'write', 'writelines', 'xreadlines', |
| 33 '__iter__') | |
|
2926
4a53483939c1
added sys.std files, read-only open, reload
Guido van Rossum <guido@python.org>
parents:
2886
diff
changeset
|
34 |
|
4a53483939c1
added sys.std files, read-only open, reload
Guido van Rossum <guido@python.org>
parents:
2886
diff
changeset
|
35 |
|
4a53483939c1
added sys.std files, read-only open, reload
Guido van Rossum <guido@python.org>
parents:
2886
diff
changeset
|
36 class FileWrapper(FileBase): |
|
4a53483939c1
added sys.std files, read-only open, reload
Guido van Rossum <guido@python.org>
parents:
2886
diff
changeset
|
37 |
|
7756
a419334f3b39
A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents:
6518
diff
changeset
|
38 # 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
|
39 |
|
7756
a419334f3b39
A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents:
6518
diff
changeset
|
40 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
|
41 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
|
42 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
|
43 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
|
44 |
|
a419334f3b39
A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents:
6518
diff
changeset
|
45 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
|
46 self.flush() |
|
2926
4a53483939c1
added sys.std files, read-only open, reload
Guido van Rossum <guido@python.org>
parents:
2886
diff
changeset
|
47 |
|
4a53483939c1
added sys.std files, read-only open, reload
Guido van Rossum <guido@python.org>
parents:
2886
diff
changeset
|
48 |
|
4a53483939c1
added sys.std files, read-only open, reload
Guido van Rossum <guido@python.org>
parents:
2886
diff
changeset
|
49 TEMPLATE = """ |
|
4a53483939c1
added sys.std files, read-only open, reload
Guido van Rossum <guido@python.org>
parents:
2886
diff
changeset
|
50 def %s(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
|
51 return apply(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
|
52 """ |
|
4a53483939c1
added sys.std files, read-only open, reload
Guido van Rossum <guido@python.org>
parents:
2886
diff
changeset
|
53 |
|
4a53483939c1
added sys.std files, read-only open, reload
Guido van Rossum <guido@python.org>
parents:
2886
diff
changeset
|
54 class FileDelegate(FileBase): |
|
4a53483939c1
added sys.std files, read-only open, reload
Guido van Rossum <guido@python.org>
parents:
2886
diff
changeset
|
55 |
|
7756
a419334f3b39
A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents:
6518
diff
changeset
|
56 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
|
57 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
|
58 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
|
59 |
|
a419334f3b39
A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents:
6518
diff
changeset
|
60 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
|
61 exec TEMPLATE % (m, m) |
|
2926
4a53483939c1
added sys.std files, read-only open, reload
Guido van Rossum <guido@python.org>
parents:
2886
diff
changeset
|
62 |
|
4a53483939c1
added sys.std files, read-only open, reload
Guido van Rossum <guido@python.org>
parents:
2886
diff
changeset
|
63 |
|
2886
21e105460a49
new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents:
2776
diff
changeset
|
64 class RHooks(ihooks.Hooks): |
|
21e105460a49
new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents:
2776
diff
changeset
|
65 |
|
3637
6c0086f9270c
Fix some obsolete names comments.
Guido van Rossum <guido@python.org>
parents:
2965
diff
changeset
|
66 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
|
67 # 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
|
68 # 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
|
69 # 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
|
70 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
|
71 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
|
72 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
|
73 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
|
74 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
|
75 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
|
76 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
|
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: |
|
a419334f3b39
A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents:
6518
diff
changeset
|
79 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
|
80 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
|
81 self.rexec = rexec |
|
2886
21e105460a49
new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents:
2776
diff
changeset
|
82 |
|
3637
6c0086f9270c
Fix some obsolete names comments.
Guido van Rossum <guido@python.org>
parents:
2965
diff
changeset
|
83 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
|
84 # 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
|
85 self.rexec = rexec |
|
3637
6c0086f9270c
Fix some obsolete names comments.
Guido van Rossum <guido@python.org>
parents:
2965
diff
changeset
|
86 |
|
23603
cffd091f56d0
SF bug 533625 (Armin Rigo). rexec: potential security hole
Guido van Rossum <guido@python.org>
parents:
23578
diff
changeset
|
87 def get_suffixes(self): |
|
cffd091f56d0
SF bug 533625 (Armin Rigo). rexec: potential security hole
Guido van Rossum <guido@python.org>
parents:
23578
diff
changeset
|
88 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
|
89 |
|
2886
21e105460a49
new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents:
2776
diff
changeset
|
90 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
|
91 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
|
92 |
|
21e105460a49
new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents:
2776
diff
changeset
|
93 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
|
94 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
|
95 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
|
96 |
|
21e105460a49
new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents:
2776
diff
changeset
|
97 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
|
98 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
|
99 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
|
100 def load_package(self, *args): raise SystemError, "don't use this" |
| 2135 | 101 |
|
3637
6c0086f9270c
Fix some obsolete names comments.
Guido van Rossum <guido@python.org>
parents:
2965
diff
changeset
|
102 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
|
103 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
|
104 |
|
21e105460a49
new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents:
2776
diff
changeset
|
105 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
|
106 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
|
107 |
|
21e105460a49
new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents:
2776
diff
changeset
|
108 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
|
109 return self.rexec.modules |
|
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 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
|
112 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
|
113 |
|
21e105460a49
new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents:
2776
diff
changeset
|
114 |
|
9130
b0fa2fc12dfb
Get rid of the classes RModuleLoader and RModuleImporter -- these were
Guido van Rossum <guido@python.org>
parents:
8655
diff
changeset
|
115 # XXX Backwards compatibility |
|
b0fa2fc12dfb
Get rid of the classes RModuleLoader and RModuleImporter -- these were
Guido van Rossum <guido@python.org>
parents:
8655
diff
changeset
|
116 RModuleLoader = ihooks.FancyModuleLoader |
|
b0fa2fc12dfb
Get rid of the classes RModuleLoader and RModuleImporter -- these were
Guido van Rossum <guido@python.org>
parents:
8655
diff
changeset
|
117 RModuleImporter = ihooks.ModuleImporter |
|
2886
21e105460a49
new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents:
2776
diff
changeset
|
118 |
|
21e105460a49
new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents:
2776
diff
changeset
|
119 |
|
21e105460a49
new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents:
2776
diff
changeset
|
120 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
|
121 """Basic restricted execution framework. |
|
2886
21e105460a49
new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents:
2776
diff
changeset
|
122 |
|
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
|
123 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
|
124 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
|
125 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
|
126 |
|
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 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
|
128 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
|
129 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
|
130 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
|
131 |
|
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 """ |
|
2886
21e105460a49
new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents:
2776
diff
changeset
|
133 |
|
7756
a419334f3b39
A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents:
6518
diff
changeset
|
134 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
|
135 |
|
4334
f37cbfaa7328
1. Correct typo in FileWrapper.close() (fix by AMK).
Guido van Rossum <guido@python.org>
parents:
4075
diff
changeset
|
136 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
|
137 '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
|
138 'marshal', 'math', 'md5', 'operator', |
|
a419334f3b39
A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents:
6518
diff
changeset
|
139 'parser', 'regex', 'pcre', 'rotor', 'select', |
| 26668 | 140 'sha', '_sre', 'strop', 'struct', 'time', |
| 141 'xreadlines', '_weakref') | |
|
2886
21e105460a49
new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents:
2776
diff
changeset
|
142 |
|
21e105460a49
new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents:
2776
diff
changeset
|
143 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
|
144 '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
|
145 '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
|
146 |
|
25729
b59d1a119393
Allow restricted code to get byteorder, getdefaultencoding(),
Fred Drake <fdrake@acm.org>
parents:
25728
diff
changeset
|
147 ok_sys_names = ('byteorder', 'copyright', 'exit', 'getdefaultencoding', |
|
b59d1a119393
Allow restricted code to get byteorder, getdefaultencoding(),
Fred Drake <fdrake@acm.org>
parents:
25728
diff
changeset
|
148 'getrefcount', 'hexversion', 'maxint', 'maxunicode', |
|
b59d1a119393
Allow restricted code to get byteorder, getdefaultencoding(),
Fred Drake <fdrake@acm.org>
parents:
25728
diff
changeset
|
149 '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
|
150 |
|
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
|
151 nok_builtin_names = ('open', 'file', 'reload', '__import__') |
| 2135 | 152 |
|
23603
cffd091f56d0
SF bug 533625 (Armin Rigo). rexec: potential security hole
Guido van Rossum <guido@python.org>
parents:
23578
diff
changeset
|
153 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
|
154 |
|
2886
21e105460a49
new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents:
2776
diff
changeset
|
155 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
|
156 """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
|
157 |
|
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
|
158 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
|
159 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
|
160 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
|
161 |
|
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 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
|
163 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
|
164 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
|
165 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
|
166 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
|
167 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
|
168 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
|
169 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
|
170 |
|
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 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
|
172 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
|
173 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
|
174 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
|
175 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
|
176 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
|
177 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
|
178 |
|
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 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
|
180 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
|
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 """ |
|
27109
0464acb5b3e4
Sabotage rexec.py. It is not safe since the new-style classes.
Guido van Rossum <guido@python.org>
parents:
26668
diff
changeset
|
183 |
|
0464acb5b3e4
Sabotage rexec.py. It is not safe since the new-style classes.
Guido van Rossum <guido@python.org>
parents:
26668
diff
changeset
|
184 raise RuntimeError, "This code is not secure in Python 2.2 and 2.3" |
|
0464acb5b3e4
Sabotage rexec.py. It is not safe since the new-style classes.
Guido van Rossum <guido@python.org>
parents:
26668
diff
changeset
|
185 |
|
7756
a419334f3b39
A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents:
6518
diff
changeset
|
186 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
|
187 # 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
|
188 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
|
189 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
|
190 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
|
191 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
|
192 list = [] |
|
a419334f3b39
A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents:
6518
diff
changeset
|
193 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
|
194 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
|
195 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
|
196 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
|
197 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
|
198 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
|
199 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
|
200 # 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
|
201 # 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
|
202 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
|
203 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
|
204 self.importer = RModuleImporter(self.loader, verbose) |
| 2135 | 205 |
|
3637
6c0086f9270c
Fix some obsolete names comments.
Guido van Rossum <guido@python.org>
parents:
2965
diff
changeset
|
206 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
|
207 # 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
|
208 # 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
|
209 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
|
210 |
|
6c0086f9270c
Fix some obsolete names comments.
Guido van Rossum <guido@python.org>
parents:
2965
diff
changeset
|
211 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
|
212 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
|
213 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
|
214 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
|
215 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
|
216 else: |
|
a419334f3b39
A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents:
6518
diff
changeset
|
217 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
|
218 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
|
219 return dst |
|
3637
6c0086f9270c
Fix some obsolete names comments.
Guido van Rossum <guido@python.org>
parents:
2965
diff
changeset
|
220 |
|
2886
21e105460a49
new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents:
2776
diff
changeset
|
221 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
|
222 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
|
223 self.make_osname() |
|
2886
21e105460a49
new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents:
2776
diff
changeset
|
224 |
|
21e105460a49
new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents:
2776
diff
changeset
|
225 # Helpers for RHooks |
|
21e105460a49
new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents:
2776
diff
changeset
|
226 |
|
23603
cffd091f56d0
SF bug 533625 (Armin Rigo). rexec: potential security hole
Guido van Rossum <guido@python.org>
parents:
23578
diff
changeset
|
227 def get_suffixes(self): |
|
cffd091f56d0
SF bug 533625 (Armin Rigo). rexec: potential security hole
Guido van Rossum <guido@python.org>
parents:
23578
diff
changeset
|
228 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
|
229 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
|
230 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
|
231 |
|
2886
21e105460a49
new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents:
2776
diff
changeset
|
232 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
|
233 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
|
234 |
|
21e105460a49
new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents:
2776
diff
changeset
|
235 # 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
|
236 |
|
21e105460a49
new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents:
2776
diff
changeset
|
237 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
|
238 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
|
239 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
|
240 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
|
241 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
|
242 |
|
21e105460a49
new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents:
2776
diff
changeset
|
243 def make_main(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
|
244 m = self.add_module('__main__') |
| 2135 | 245 |
|
2886
21e105460a49
new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents:
2776
diff
changeset
|
246 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
|
247 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
|
248 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
|
249 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
|
250 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
|
251 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
|
252 e[key] = value |
|
2886
21e105460a49
new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents:
2776
diff
changeset
|
253 |
|
21e105460a49
new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents:
2776
diff
changeset
|
254 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
|
255 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
|
256 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
|
257 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
|
258 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
|
259 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
|
260 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
|
261 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
|
262 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
|
263 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
|
264 |
|
21e105460a49
new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents:
2776
diff
changeset
|
265 # The copy_* methods copy existing modules with some changes |
| 2135 | 266 |
|
2886
21e105460a49
new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents:
2776
diff
changeset
|
267 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
|
268 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
|
269 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
|
270 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
|
271 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
|
272 try: |
|
a419334f3b39
A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents:
6518
diff
changeset
|
273 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
|
274 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
|
275 pass |
|
a419334f3b39
A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents:
6518
diff
changeset
|
276 return dst |
|
2886
21e105460a49
new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents:
2776
diff
changeset
|
277 |
|
21e105460a49
new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents:
2776
diff
changeset
|
278 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
|
279 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
|
280 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
|
281 try: |
|
a419334f3b39
A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents:
6518
diff
changeset
|
282 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
|
283 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
|
284 continue |
|
a419334f3b39
A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents:
6518
diff
changeset
|
285 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
|
286 return dst |
|
2886
21e105460a49
new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents:
2776
diff
changeset
|
287 |
|
21e105460a49
new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents:
2776
diff
changeset
|
288 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
|
289 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
|
290 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
|
291 return m |
|
2886
21e105460a49
new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents:
2776
diff
changeset
|
292 |
|
21e105460a49
new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents:
2776
diff
changeset
|
293 # 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
|
294 |
|
21e105460a49
new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents:
2776
diff
changeset
|
295 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
|
296 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
|
297 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
|
298 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
|
299 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
|
300 return m |
| 2135 | 301 |
|
2886
21e105460a49
new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents:
2776
diff
changeset
|
302 # The r* methods are public interfaces |
| 2135 | 303 |
|
2886
21e105460a49
new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents:
2776
diff
changeset
|
304 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
|
305 """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
|
306 |
|
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
|
307 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
|
308 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
|
309 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
|
310 |
|
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 """ |
|
7756
a419334f3b39
A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents:
6518
diff
changeset
|
312 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
|
313 exec code in m.__dict__ |
| 2135 | 314 |
|
2886
21e105460a49
new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents:
2776
diff
changeset
|
315 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
|
316 """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
|
317 |
|
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
|
318 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
|
319 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
|
320 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
|
321 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
|
322 |
|
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 """ |
|
7756
a419334f3b39
A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents:
6518
diff
changeset
|
324 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
|
325 return eval(code, m.__dict__) |
| 2135 | 326 |
|
2886
21e105460a49
new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents:
2776
diff
changeset
|
327 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
|
328 """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
|
329 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
|
330 |
|
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 """ |
|
7756
a419334f3b39
A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents:
6518
diff
changeset
|
332 m = self.add_module('__main__') |
|
20419
4f3d1240830e
Ignore execfile() return value; reported by Neal Norwitz.
Fred Drake <fdrake@acm.org>
parents:
19901
diff
changeset
|
333 execfile(file, m.__dict__) |
| 2135 | 334 |
|
2886
21e105460a49
new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents:
2776
diff
changeset
|
335 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
|
336 """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
|
337 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
|
338 |
|
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 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
|
340 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
|
341 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
|
342 |
|
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 """ |
|
7756
a419334f3b39
A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents:
6518
diff
changeset
|
344 return self.importer.import_module(mname, globals, locals, fromlist) |
| 2135 | 345 |
|
2926
4a53483939c1
added sys.std files, read-only open, reload
Guido van Rossum <guido@python.org>
parents:
2886
diff
changeset
|
346 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
|
347 """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
|
348 |
|
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
|
349 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
|
350 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
|
351 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
|
352 |
|
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 """ |
|
2926
4a53483939c1
added sys.std files, read-only open, reload
Guido van Rossum <guido@python.org>
parents:
2886
diff
changeset
|
354 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
|
355 |
|
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
|
356 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
|
357 """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
|
358 |
|
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
|
359 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
|
360 |
|
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 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
|
362 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
|
363 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
|
364 |
|
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 """ |
|
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
|
366 return self.importer.unload(m) |
|
15871
62aeefa01fdd
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15039
diff
changeset
|
367 |
|
2926
4a53483939c1
added sys.std files, read-only open, reload
Guido van Rossum <guido@python.org>
parents:
2886
diff
changeset
|
368 # 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
|
369 |
|
3734
04c07538cac4
Fixed restore_files(); added reset_files(); made these more flexible.
Guido van Rossum <guido@python.org>
parents:
3648
diff
changeset
|
370 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
|
371 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
|
372 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
|
373 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
|
374 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
|
375 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
|
376 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
|
377 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
|
378 |
|
2926
4a53483939c1
added sys.std files, read-only open, reload
Guido van Rossum <guido@python.org>
parents:
2886
diff
changeset
|
379 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
|
380 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
|
381 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
|
382 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
|
383 self.make_delegate_files() |
|
2926
4a53483939c1
added sys.std files, read-only open, reload
Guido van Rossum <guido@python.org>
parents:
2886
diff
changeset
|
384 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
|
385 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
|
386 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
|
387 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
|
388 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
|
389 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
|
390 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
|
391 |
|
04c07538cac4
Fixed restore_files(); added reset_files(); made these more flexible.
Guido van Rossum <guido@python.org>
parents:
3648
diff
changeset
|
392 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
|
393 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
|
394 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
|
395 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
|
396 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
|
397 self.restricted_stderr = s.stderr |
|
15871
62aeefa01fdd
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15039
diff
changeset
|
398 |
|
2926
4a53483939c1
added sys.std files, read-only open, reload
Guido van Rossum <guido@python.org>
parents:
2886
diff
changeset
|
399 |
|
4a53483939c1
added sys.std files, read-only open, reload
Guido van Rossum <guido@python.org>
parents:
2886
diff
changeset
|
400 def save_files(self): |
|
4a53483939c1
added sys.std files, read-only open, reload
Guido van Rossum <guido@python.org>
parents:
2886
diff
changeset
|
401 self.save_stdin = sys.stdin |
|
4a53483939c1
added sys.std files, read-only open, reload
Guido van Rossum <guido@python.org>
parents:
2886
diff
changeset
|
402 self.save_stdout = sys.stdout |
|
4a53483939c1
added sys.std files, read-only open, reload
Guido van Rossum <guido@python.org>
parents:
2886
diff
changeset
|
403 self.save_stderr = sys.stderr |
|
4a53483939c1
added sys.std files, read-only open, reload
Guido van Rossum <guido@python.org>
parents:
2886
diff
changeset
|
404 |
|
3734
04c07538cac4
Fixed restore_files(); added reset_files(); made these more flexible.
Guido van Rossum <guido@python.org>
parents:
3648
diff
changeset
|
405 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
|
406 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
|
407 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
|
408 sys.stderr = self.save_stderr |
|
15871
62aeefa01fdd
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15039
diff
changeset
|
409 |
|
4334
f37cbfaa7328
1. Correct typo in FileWrapper.close() (fix by AMK).
Guido van Rossum <guido@python.org>
parents:
4075
diff
changeset
|
410 def s_apply(self, func, args=(), kw=None): |
|
7756
a419334f3b39
A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents:
6518
diff
changeset
|
411 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
|
412 try: |
|
a419334f3b39
A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents:
6518
diff
changeset
|
413 self.set_files() |
|
a419334f3b39
A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents:
6518
diff
changeset
|
414 if kw: |
|
a419334f3b39
A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents:
6518
diff
changeset
|
415 r = apply(func, args, kw) |
|
a419334f3b39
A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents:
6518
diff
changeset
|
416 else: |
|
a419334f3b39
A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents:
6518
diff
changeset
|
417 r = apply(func, args) |
|
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 | 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 | 534 |
| 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: |
|
ed0a38cbc3e6
(1) Added a sys.exc_info() emulation. (It returns None for the traceback.)
Guido van Rossum <guido@python.org>
parents:
8587
diff
changeset
|
558 print "%s: can't open file %s" % (sys.argv[0], `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
|
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 | 586 |
| 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()) |
