annotate Lib/rexec.py @ 25838:8540887bcec6 2.2

Backport of 2.183: Fix memory leak in add_subclass() found by NealN with valgrind.
author Guido van Rossum <guido@python.org>
date Fri, 18 Oct 2002 13:42:21 +0000
parents fb00fd93188a
children e96575f7e96a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2886
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
1 """Restricted execution facilities.
2135
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
3637
6c0086f9270c Fix some obsolete names comments.
Guido van Rossum <guido@python.org>
parents: 2965
diff changeset
3 The class RExec exports methods r_exec(), r_eval(), r_execfile(), and
6c0086f9270c Fix some obsolete names comments.
Guido van Rossum <guido@python.org>
parents: 2965
diff changeset
4 r_import(), which correspond roughly to the built-in operations
2886
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
5 exec, eval(), execfile() and import, but executing the code in an
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
6 environment that only exposes those built-in operations that are
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
7 deemed safe. To this end, a modest collection of 'fake' modules is
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
8 created which mimics the standard modules by the same names. It is a
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
9 policy decision which built-in modules and operations are made
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
10 available; this module provides a reasonable default, but derived
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
11 classes can change the policies e.g. by overriding or extending class
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
12 variables like ok_builtin_modules or methods like make_sys().
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
13
2926
4a53483939c1 added sys.std files, read-only open, reload
Guido van Rossum <guido@python.org>
parents: 2886
diff changeset
14 XXX To do:
4a53483939c1 added sys.std files, read-only open, reload
Guido van Rossum <guido@python.org>
parents: 2886
diff changeset
15 - r_open should allow writing tmp dir
4a53483939c1 added sys.std files, read-only open, reload
Guido van Rossum <guido@python.org>
parents: 2886
diff changeset
16 - r_exec etc. with explicit globals/locals? (Use rexec("exec ... in ...")?)
4a53483939c1 added sys.std files, read-only open, reload
Guido van Rossum <guido@python.org>
parents: 2886
diff changeset
17
2886
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
18 """
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
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
22 import __builtin__
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
23604
ef8d456067fd Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23577
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
f68dbffff927 bunch more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 15871
diff changeset
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',
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
32 'readlines', 'seek', 'tell', 'write', 'writelines')
2926
4a53483939c1 added sys.std files, read-only open, reload
Guido van Rossum <guido@python.org>
parents: 2886
diff changeset
33
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 class FileWrapper(FileBase):
4a53483939c1 added sys.std files, read-only open, reload
Guido van Rossum <guido@python.org>
parents: 2886
diff changeset
36
7756
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
37 # 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
38
7756
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
39 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
40 self.f = 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
23604
ef8d456067fd Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23577
diff changeset
87 def get_suffixes(self):
ef8d456067fd Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23577
diff changeset
88 return self.rexec.get_suffixes()
ef8d456067fd Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23577
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
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
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):
23577
8f7b79cda0d3 Backport change to 1.35 adding doc strings
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
23577
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
123 Code executed in this restricted environment will only have access to
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
124 modules and functions that are deemed safe; you can subclass RExec to
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
125 add or remove capabilities as desired.
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
126
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
127 The RExec class can prevent code from performing unsafe operations like
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
128 reading or writing disk files, or using TCP/IP sockets. However, it does
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
129 not protect against code using extremely large amounts of memory or
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
130 processor time.
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
131
8f7b79cda0d3 Backport change to 1.35 adding doc strings
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',
18224
83c4748b9390 Add sha and _sre to the list of allowed built-in modules.
Fred Drake <fdrake@acm.org>
parents: 18142
diff changeset
140 'sha', '_sre', 'strop', 'struct', 'time')
2886
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
141
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
142 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
143 '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
144 '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
145
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
146 ok_sys_names = ('ps1', 'ps2', 'copyright', 'version',
7756
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
147 'platform', 'exit', 'maxint')
2886
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
148
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
149 nok_builtin_names = ('open', 'file', 'reload', '__import__')
2135
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
150
23604
ef8d456067fd Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23577
diff changeset
151 ok_file_types = (imp.C_EXTENSION, imp.PY_SOURCE)
ef8d456067fd Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23577
diff changeset
152
2886
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
153 def __init__(self, hooks = None, verbose = 0):
23577
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
154 """Returns an instance of the RExec class.
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
155
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
156 The hooks parameter is an instance of the RHooks class or a subclass
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
157 of it. If it is omitted or None, the default RHooks class is
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
158 instantiated.
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
159
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
160 Whenever the RExec module searches for a module (even a built-in one)
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
161 or reads a module's code, it doesn't actually go out to the file
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
162 system itself. Rather, it calls methods of an RHooks instance that
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
163 was passed to or created by its constructor. (Actually, the RExec
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
164 object doesn't make these calls --- they are made by a module loader
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
165 object that's part of the RExec object. This allows another level of
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
166 flexibility, which can be useful when changing the mechanics of
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
167 import within the restricted environment.)
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
168
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
169 By providing an alternate RHooks object, we can control the file
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
170 system accesses made to import a module, without changing the
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
171 actual algorithm that controls the order in which those accesses are
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
172 made. For instance, we could substitute an RHooks object that
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
173 passes all filesystem requests to a file server elsewhere, via some
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
174 RPC mechanism such as ILU. Grail's applet loader uses this to support
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
175 importing applets from a URL for a directory.
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
176
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
177 If the verbose parameter is true, additional debugging output may be
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
178 sent to standard output.
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
179
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
180 """
7756
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
181 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
182 # 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
183 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
184 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
185 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
186 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
187 list = []
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
188 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
189 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
190 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
191 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
192 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
193 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
194 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
195 # 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
196 # 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
197 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
198 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
199 self.importer = RModuleImporter(self.loader, verbose)
2135
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
200
3637
6c0086f9270c Fix some obsolete names comments.
Guido van Rossum <guido@python.org>
parents: 2965
diff changeset
201 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
202 # 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
203 # 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
204 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
205
6c0086f9270c Fix some obsolete names comments.
Guido van Rossum <guido@python.org>
parents: 2965
diff changeset
206 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
207 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
208 raise ImportError, "untrusted dynamic module: %s" % name
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
209 if sys.modules.has_key(name):
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
210 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
211 else:
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
212 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
213 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
214 return dst
3637
6c0086f9270c Fix some obsolete names comments.
Guido van Rossum <guido@python.org>
parents: 2965
diff changeset
215
2886
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
216 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
217 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
218 self.make_osname()
2886
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
219
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
220 # Helpers for RHooks
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
221
23604
ef8d456067fd Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23577
diff changeset
222 def get_suffixes(self):
ef8d456067fd Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23577
diff changeset
223 return [item # (suff, mode, type)
ef8d456067fd Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23577
diff changeset
224 for item in imp.get_suffixes()
ef8d456067fd Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23577
diff changeset
225 if item[2] in self.ok_file_types]
ef8d456067fd Backport to 2.2.x:
Guido van Rossum <guido@python.org>
parents: 23577
diff changeset
226
2886
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
227 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
228 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
229
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
230 # 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
231
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
232 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
233 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
234 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
235 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
236 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
237
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
238 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
239 m = self.add_module('__main__')
2135
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
240
2886
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
241 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
242 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
243 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
244 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
245 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
246 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
247 e[key] = value
2886
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
248
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
249 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
250 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
251 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
252 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
253 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
254 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
255 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
256 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
257 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
258 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
259
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
260 # The copy_* methods copy existing modules with some changes
2135
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
261
2886
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
262 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
263 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
264 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
265 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
266 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
267 try:
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
268 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
269 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
270 pass
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
271 return dst
2886
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
272
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
273 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
274 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
275 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
276 try:
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
277 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
278 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
279 continue
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
280 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
281 return dst
2886
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
282
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
283 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
284 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
285 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
286 return m
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 # 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
289
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
290 def add_module(self, mname):
25274
fb00fd93188a Backport (the relevant part of) rexec.py 1.41.
Guido van Rossum <guido@python.org>
parents: 23604
diff changeset
291 m = self.modules.get(mname)
fb00fd93188a Backport (the relevant part of) rexec.py 1.41.
Guido van Rossum <guido@python.org>
parents: 23604
diff changeset
292 if m is None:
fb00fd93188a Backport (the relevant part of) rexec.py 1.41.
Guido van Rossum <guido@python.org>
parents: 23604
diff changeset
293 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
294 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
295 return m
2135
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
296
2886
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
297 # The r* methods are public interfaces
2135
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
298
2886
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
299 def r_exec(self, code):
23577
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
300 """Execute code within a restricted environment.
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
301
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
302 The code parameter must either be a string containing one or more
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
303 lines of Python code, or a compiled code object, which will be
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
304 executed in the restricted environment's __main__ module.
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
305
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
306 """
7756
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
307 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
308 exec code in m.__dict__
2135
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
309
2886
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
310 def r_eval(self, code):
23577
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
311 """Evaluate code within a restricted environment.
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
312
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
313 The code parameter must either be a string containing a Python
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
314 expression, or a compiled code object, which will be evaluated in
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
315 the restricted environment's __main__ module. The value of the
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
316 expression or code object will be returned.
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
317
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
318 """
7756
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
319 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
320 return eval(code, m.__dict__)
2135
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
321
2886
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
322 def r_execfile(self, file):
23577
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
323 """Execute the Python code in the file in the restricted
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
324 environment's __main__ module.
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
325
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
326 """
7756
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
327 m = self.add_module('__main__')
20419
4f3d1240830e Ignore execfile() return value; reported by Neal Norwitz.
Fred Drake <fdrake@acm.org>
parents: 19901
diff changeset
328 execfile(file, m.__dict__)
2135
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
329
2886
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
330 def r_import(self, mname, globals={}, locals={}, fromlist=[]):
23577
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
331 """Import a module, raising an ImportError exception if the module
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
332 is considered unsafe.
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
333
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
334 This method is implicitly called by code executing in the
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
335 restricted environment. Overriding this method in a subclass is
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
336 used to change the policies enforced by a restricted environment.
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
337
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
338 """
7756
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
339 return self.importer.import_module(mname, globals, locals, fromlist)
2135
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
340
2926
4a53483939c1 added sys.std files, read-only open, reload
Guido van Rossum <guido@python.org>
parents: 2886
diff changeset
341 def r_reload(self, m):
23577
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
342 """Reload the module object, re-parsing and re-initializing it.
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
343
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
344 This method is implicitly called by code executing in the
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
345 restricted environment. Overriding this method in a subclass is
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
346 used to change the policies enforced by a restricted environment.
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
347
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
348 """
2926
4a53483939c1 added sys.std files, read-only open, reload
Guido van Rossum <guido@python.org>
parents: 2886
diff changeset
349 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
350
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
351 def r_unload(self, m):
23577
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
352 """Unload the module.
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
353
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
354 Removes it from the restricted environment's sys.modules dictionary.
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
355
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
356 This method is implicitly called by code executing in the
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
357 restricted environment. Overriding this method in a subclass is
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
358 used to change the policies enforced by a restricted environment.
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
359
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
360 """
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
361 return self.importer.unload(m)
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15039
diff changeset
362
2926
4a53483939c1 added sys.std files, read-only open, reload
Guido van Rossum <guido@python.org>
parents: 2886
diff changeset
363 # 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
364
3734
04c07538cac4 Fixed restore_files(); added reset_files(); made these more flexible.
Guido van Rossum <guido@python.org>
parents: 3648
diff changeset
365 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
366 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
367 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
368 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
369 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
370 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
371 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
372 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
373
2926
4a53483939c1 added sys.std files, read-only open, reload
Guido van Rossum <guido@python.org>
parents: 2886
diff changeset
374 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
375 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
376 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
377 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
378 self.make_delegate_files()
2926
4a53483939c1 added sys.std files, read-only open, reload
Guido van Rossum <guido@python.org>
parents: 2886
diff changeset
379 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
380 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
381 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
382 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
383 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
384 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
385 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
386
04c07538cac4 Fixed restore_files(); added reset_files(); made these more flexible.
Guido van Rossum <guido@python.org>
parents: 3648
diff changeset
387 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
388 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
389 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
390 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
391 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
392 self.restricted_stderr = s.stderr
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15039
diff changeset
393
2926
4a53483939c1 added sys.std files, read-only open, reload
Guido van Rossum <guido@python.org>
parents: 2886
diff changeset
394
4a53483939c1 added sys.std files, read-only open, reload
Guido van Rossum <guido@python.org>
parents: 2886
diff changeset
395 def save_files(self):
4a53483939c1 added sys.std files, read-only open, reload
Guido van Rossum <guido@python.org>
parents: 2886
diff changeset
396 self.save_stdin = sys.stdin
4a53483939c1 added sys.std files, read-only open, reload
Guido van Rossum <guido@python.org>
parents: 2886
diff changeset
397 self.save_stdout = sys.stdout
4a53483939c1 added sys.std files, read-only open, reload
Guido van Rossum <guido@python.org>
parents: 2886
diff changeset
398 self.save_stderr = sys.stderr
4a53483939c1 added sys.std files, read-only open, reload
Guido van Rossum <guido@python.org>
parents: 2886
diff changeset
399
3734
04c07538cac4 Fixed restore_files(); added reset_files(); made these more flexible.
Guido van Rossum <guido@python.org>
parents: 3648
diff changeset
400 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
401 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
402 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
403 sys.stderr = self.save_stderr
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15039
diff changeset
404
4334
f37cbfaa7328 1. Correct typo in FileWrapper.close() (fix by AMK).
Guido van Rossum <guido@python.org>
parents: 4075
diff changeset
405 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
406 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
407 try:
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
408 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
409 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
410 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
411 else:
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
412 r = apply(func, args)
2926
4a53483939c1 added sys.std files, read-only open, reload
Guido van Rossum <guido@python.org>
parents: 2886
diff changeset
413 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
414 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
415 return r
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15039
diff changeset
416
2926
4a53483939c1 added sys.std files, read-only open, reload
Guido van Rossum <guido@python.org>
parents: 2886
diff changeset
417 def s_exec(self, *args):
23577
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
418 """Execute code within a restricted environment.
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
419
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
420 Similar to the r_exec() method, but the code will be granted access
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
421 to restricted versions of the standard I/O streams sys.stdin,
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
422 sys.stderr, and sys.stdout.
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
423
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
424 The code parameter must either be a string containing one or more
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
425 lines of Python code, or a compiled code object, which will be
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
426 executed in the restricted environment's __main__ module.
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
427
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
428 """
18142
47c9d0cb6e31 Fix SF bug #433904 (Alex Martelli) - all s_* methods return None only.
Guido van Rossum <guido@python.org>
parents: 16613
diff changeset
429 return self.s_apply(self.r_exec, args)
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15039
diff changeset
430
2926
4a53483939c1 added sys.std files, read-only open, reload
Guido van Rossum <guido@python.org>
parents: 2886
diff changeset
431 def s_eval(self, *args):
23577
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
432 """Evaluate code within a restricted environment.
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
433
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
434 Similar to the r_eval() method, but the code will be granted access
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
435 to restricted versions of the standard I/O streams sys.stdin,
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
436 sys.stderr, and sys.stdout.
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
437
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
438 The code parameter must either be a string containing a Python
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
439 expression, or a compiled code object, which will be evaluated in
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
440 the restricted environment's __main__ module. The value of the
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
441 expression or code object will be returned.
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
442
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
443 """
18142
47c9d0cb6e31 Fix SF bug #433904 (Alex Martelli) - all s_* methods return None only.
Guido van Rossum <guido@python.org>
parents: 16613
diff changeset
444 return self.s_apply(self.r_eval, args)
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15039
diff changeset
445
2926
4a53483939c1 added sys.std files, read-only open, reload
Guido van Rossum <guido@python.org>
parents: 2886
diff changeset
446 def s_execfile(self, *args):
23577
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
447 """Execute the Python code in the file in the restricted
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
448 environment's __main__ module.
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
449
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
450 Similar to the r_execfile() method, but the code will be granted
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
451 access to restricted versions of the standard I/O streams sys.stdin,
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
452 sys.stderr, and sys.stdout.
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
453
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
454 """
18142
47c9d0cb6e31 Fix SF bug #433904 (Alex Martelli) - all s_* methods return None only.
Guido van Rossum <guido@python.org>
parents: 16613
diff changeset
455 return self.s_apply(self.r_execfile, args)
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15039
diff changeset
456
2926
4a53483939c1 added sys.std files, read-only open, reload
Guido van Rossum <guido@python.org>
parents: 2886
diff changeset
457 def s_import(self, *args):
23577
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
458 """Import a module, raising an ImportError exception if the module
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
459 is considered unsafe.
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
460
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
461 This method is implicitly called by code executing in the
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
462 restricted environment. Overriding this method in a subclass is
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
463 used to change the policies enforced by a restricted environment.
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
464
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
465 Similar to the r_import() method, but has access to restricted
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
466 versions of the standard I/O streams sys.stdin, sys.stderr, and
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
467 sys.stdout.
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
468
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
469 """
18142
47c9d0cb6e31 Fix SF bug #433904 (Alex Martelli) - all s_* methods return None only.
Guido van Rossum <guido@python.org>
parents: 16613
diff changeset
470 return self.s_apply(self.r_import, args)
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15039
diff changeset
471
2926
4a53483939c1 added sys.std files, read-only open, reload
Guido van Rossum <guido@python.org>
parents: 2886
diff changeset
472 def s_reload(self, *args):
23577
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
473 """Reload the module object, re-parsing and re-initializing it.
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
474
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
475 This method is implicitly called by code executing in the
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
476 restricted environment. Overriding this method in a subclass is
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
477 used to change the policies enforced by a restricted environment.
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
478
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
479 Similar to the r_reload() method, but has access to restricted
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
480 versions of the standard I/O streams sys.stdin, sys.stderr, and
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
481 sys.stdout.
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
482
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
483 """
18142
47c9d0cb6e31 Fix SF bug #433904 (Alex Martelli) - all s_* methods return None only.
Guido van Rossum <guido@python.org>
parents: 16613
diff changeset
484 return self.s_apply(self.r_reload, args)
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15039
diff changeset
485
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
486 def s_unload(self, *args):
23577
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
487 """Unload the module.
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
488
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
489 Removes it from the restricted environment's sys.modules dictionary.
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
490
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
491 This method is implicitly called by code executing in the
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
492 restricted environment. Overriding this method in a subclass is
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
493 used to change the policies enforced by a restricted environment.
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
494
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
495 Similar to the r_unload() method, but has access to restricted
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
496 versions of the standard I/O streams sys.stdin, sys.stderr, and
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
497 sys.stdout.
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
498
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
499 """
18142
47c9d0cb6e31 Fix SF bug #433904 (Alex Martelli) - all s_* methods return None only.
Guido van Rossum <guido@python.org>
parents: 16613
diff changeset
500 return self.s_apply(self.r_unload, args)
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15039
diff changeset
501
2926
4a53483939c1 added sys.std files, read-only open, reload
Guido van Rossum <guido@python.org>
parents: 2886
diff changeset
502 # Restricted open(...)
15871
62aeefa01fdd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15039
diff changeset
503
2926
4a53483939c1 added sys.std files, read-only open, reload
Guido van Rossum <guido@python.org>
parents: 2886
diff changeset
504 def r_open(self, file, mode='r', buf=-1):
23577
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
505 """Method called when open() is called in the restricted environment.
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
506
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
507 The arguments are identical to those of the open() function, and a
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
508 file object (or a class instance compatible with file objects)
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
509 should be returned. RExec's default behaviour is allow opening
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
510 any file for reading, but forbidding any attempt to write a file.
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
511
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
512 This method is implicitly called by code executing in the
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
513 restricted environment. Overriding this method in a subclass is
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
514 used to change the policies enforced by a restricted environment.
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
515
8f7b79cda0d3 Backport change to 1.35 adding doc strings
Raymond Hettinger <python@rcn.com>
parents: 20419
diff changeset
516 """
2926
4a53483939c1 added sys.std files, read-only open, reload
Guido van Rossum <guido@python.org>
parents: 2886
diff changeset
517 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
518 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
519 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
520
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
521 # 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
522
ed0a38cbc3e6 (1) Added a sys.exc_info() emulation. (It returns None for the traceback.)
Guido van Rossum <guido@python.org>
parents: 8587
diff changeset
523 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
524 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
525 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
526 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
527
2135
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
528
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
529 def test():
19231
e29ba8f1e7f8 Remove redundant import (PyChecker).
Guido van Rossum <guido@python.org>
parents: 19045
diff changeset
530 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
531 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
532 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
533 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
534 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
535 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
536 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
537 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
538 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
539 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
540 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
541 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
542 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
543 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
544 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
545 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
546 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
547 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
548 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
549 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
550 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
551 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
552 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
553 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
554 if fp.isatty():
ed0a38cbc3e6 (1) Added a sys.exc_info() emulation. (It returns None for the traceback.)
Guido van Rossum <guido@python.org>
parents: 8587
diff changeset
555 print "*** RESTRICTED *** Python", sys.version
19231
e29ba8f1e7f8 Remove redundant import (PyChecker).
Guido van Rossum <guido@python.org>
parents: 19045
diff changeset
556 print 'Type "help", "copyright", "credits" or "license" ' \
e29ba8f1e7f8 Remove redundant import (PyChecker).
Guido van Rossum <guido@python.org>
parents: 19045
diff changeset
557 'for more information.'
e29ba8f1e7f8 Remove redundant import (PyChecker).
Guido van Rossum <guido@python.org>
parents: 19045
diff changeset
558
8655
ed0a38cbc3e6 (1) Added a sys.exc_info() emulation. (It returns None for the traceback.)
Guido van Rossum <guido@python.org>
parents: 8587
diff changeset
559 while 1:
7756
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
560 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
561 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
562 s = raw_input('>>> ')
ed0a38cbc3e6 (1) Added a sys.exc_info() emulation. (It returns None for the traceback.)
Guido van Rossum <guido@python.org>
parents: 8587
diff changeset
563 except EOFError:
ed0a38cbc3e6 (1) Added a sys.exc_info() emulation. (It returns None for the traceback.)
Guido van Rossum <guido@python.org>
parents: 8587
diff changeset
564 print
ed0a38cbc3e6 (1) Added a sys.exc_info() emulation. (It returns None for the traceback.)
Guido van Rossum <guido@python.org>
parents: 8587
diff changeset
565 break
ed0a38cbc3e6 (1) Added a sys.exc_info() emulation. (It returns None for the traceback.)
Guido van Rossum <guido@python.org>
parents: 8587
diff changeset
566 if s and s[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
567 s = s + '\n'
ed0a38cbc3e6 (1) Added a sys.exc_info() emulation. (It returns None for the traceback.)
Guido van Rossum <guido@python.org>
parents: 8587
diff changeset
568 c = compile(s, '<stdin>', 'single')
ed0a38cbc3e6 (1) Added a sys.exc_info() emulation. (It returns None for the traceback.)
Guido van Rossum <guido@python.org>
parents: 8587
diff changeset
569 r.s_exec(c)
ed0a38cbc3e6 (1) Added a sys.exc_info() emulation. (It returns None for the traceback.)
Guido van Rossum <guido@python.org>
parents: 8587
diff changeset
570 except SystemExit, n:
ed0a38cbc3e6 (1) Added a sys.exc_info() emulation. (It returns None for the traceback.)
Guido van Rossum <guido@python.org>
parents: 8587
diff changeset
571 return n
ed0a38cbc3e6 (1) Added a sys.exc_info() emulation. (It returns None for the traceback.)
Guido van Rossum <guido@python.org>
parents: 8587
diff changeset
572 except:
ed0a38cbc3e6 (1) Added a sys.exc_info() emulation. (It returns None for the traceback.)
Guido van Rossum <guido@python.org>
parents: 8587
diff changeset
573 traceback.print_exc()
ed0a38cbc3e6 (1) Added a sys.exc_info() emulation. (It returns None for the traceback.)
Guido van Rossum <guido@python.org>
parents: 8587
diff changeset
574 else:
ed0a38cbc3e6 (1) Added a sys.exc_info() emulation. (It returns None for the traceback.)
Guido van Rossum <guido@python.org>
parents: 8587
diff changeset
575 text = fp.read()
ed0a38cbc3e6 (1) Added a sys.exc_info() emulation. (It returns None for the traceback.)
Guido van Rossum <guido@python.org>
parents: 8587
diff changeset
576 fp.close()
ed0a38cbc3e6 (1) Added a sys.exc_info() emulation. (It returns None for the traceback.)
Guido van Rossum <guido@python.org>
parents: 8587
diff changeset
577 c = compile(text, fp.name, 'exec')
ed0a38cbc3e6 (1) Added a sys.exc_info() emulation. (It returns None for the traceback.)
Guido van Rossum <guido@python.org>
parents: 8587
diff changeset
578 try:
ed0a38cbc3e6 (1) Added a sys.exc_info() emulation. (It returns None for the traceback.)
Guido van Rossum <guido@python.org>
parents: 8587
diff changeset
579 r.s_exec(c)
7756
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
580 except SystemExit, n:
8655
ed0a38cbc3e6 (1) Added a sys.exc_info() emulation. (It returns None for the traceback.)
Guido van Rossum <guido@python.org>
parents: 8587
diff changeset
581 return n
7756
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
582 except:
a419334f3b39 A few lines were indented using tabs instead of spaces -- fix them.
Guido van Rossum <guido@python.org>
parents: 6518
diff changeset
583 traceback.print_exc()
8655
ed0a38cbc3e6 (1) Added a sys.exc_info() emulation. (It returns None for the traceback.)
Guido van Rossum <guido@python.org>
parents: 8587
diff changeset
584 return 1
2886
21e105460a49 new package support, import hooks, restricted execution support
Guido van Rossum <guido@python.org>
parents: 2776
diff changeset
585
2135
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
586
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
587 if __name__ == '__main__':
8655
ed0a38cbc3e6 (1) Added a sys.exc_info() emulation. (It returns None for the traceback.)
Guido van Rossum <guido@python.org>
parents: 8587
diff changeset
588 sys.exit(test())