annotate Lib/Bastion.py @ 79675:c0a2294de787 2.7

Issue #16199: Backport 2to3 typo fix from 3.2.
author Chris Jerdonek <chris.jerdonek@gmail.com>
date Thu, 11 Oct 2012 16:15:46 -0700
parents 5679ce66e74b
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3671
9a8a40f2218c Bastionification utility (useful for rexec clients)
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 """Bastionification utility.
9a8a40f2218c Bastionification utility (useful for rexec clients)
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
9a8a40f2218c Bastionification utility (useful for rexec clients)
Guido van Rossum <guido@python.org>
parents:
diff changeset
3 A bastion (for another object -- the 'original') is an object that has
9a8a40f2218c Bastionification utility (useful for rexec clients)
Guido van Rossum <guido@python.org>
parents:
diff changeset
4 the same methods as the original but does not give access to its
9a8a40f2218c Bastionification utility (useful for rexec clients)
Guido van Rossum <guido@python.org>
parents:
diff changeset
5 instance variables. Bastions have a number of uses, but the most
9a8a40f2218c Bastionification utility (useful for rexec clients)
Guido van Rossum <guido@python.org>
parents:
diff changeset
6 obvious one is to provide code executing in restricted mode with a
9a8a40f2218c Bastionification utility (useful for rexec clients)
Guido van Rossum <guido@python.org>
parents:
diff changeset
7 safe interface to an object implemented in unrestricted mode.
9a8a40f2218c Bastionification utility (useful for rexec clients)
Guido van Rossum <guido@python.org>
parents:
diff changeset
8
9a8a40f2218c Bastionification utility (useful for rexec clients)
Guido van Rossum <guido@python.org>
parents:
diff changeset
9 The bastionification routine has an optional second argument which is
9a8a40f2218c Bastionification utility (useful for rexec clients)
Guido van Rossum <guido@python.org>
parents:
diff changeset
10 a filter function. Only those methods for which the filter method
9a8a40f2218c Bastionification utility (useful for rexec clients)
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 (called with the method name as argument) returns true are accessible.
9a8a40f2218c Bastionification utility (useful for rexec clients)
Guido van Rossum <guido@python.org>
parents:
diff changeset
12 The default filter method returns true unless the method name begins
9a8a40f2218c Bastionification utility (useful for rexec clients)
Guido van Rossum <guido@python.org>
parents:
diff changeset
13 with an underscore.
9a8a40f2218c Bastionification utility (useful for rexec clients)
Guido van Rossum <guido@python.org>
parents:
diff changeset
14
9a8a40f2218c Bastionification utility (useful for rexec clients)
Guido van Rossum <guido@python.org>
parents:
diff changeset
15 There are a number of possible implementations of bastions. We use a
9a8a40f2218c Bastionification utility (useful for rexec clients)
Guido van Rossum <guido@python.org>
parents:
diff changeset
16 'lazy' approach where the bastion's __getattr__() discipline does all
9a8a40f2218c Bastionification utility (useful for rexec clients)
Guido van Rossum <guido@python.org>
parents:
diff changeset
17 the work for a particular method the first time it is used. This is
9a8a40f2218c Bastionification utility (useful for rexec clients)
Guido van Rossum <guido@python.org>
parents:
diff changeset
18 usually fastest, especially if the user doesn't call all available
9a8a40f2218c Bastionification utility (useful for rexec clients)
Guido van Rossum <guido@python.org>
parents:
diff changeset
19 methods. The retrieved methods are stored as instance variables of
9a8a40f2218c Bastionification utility (useful for rexec clients)
Guido van Rossum <guido@python.org>
parents:
diff changeset
20 the bastion, so the overhead is only occurred on the first use of each
9a8a40f2218c Bastionification utility (useful for rexec clients)
Guido van Rossum <guido@python.org>
parents:
diff changeset
21 method.
9a8a40f2218c Bastionification utility (useful for rexec clients)
Guido van Rossum <guido@python.org>
parents:
diff changeset
22
9a8a40f2218c Bastionification utility (useful for rexec clients)
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 Detail: the bastion class has a __repr__() discipline which includes
9a8a40f2218c Bastionification utility (useful for rexec clients)
Guido van Rossum <guido@python.org>
parents:
diff changeset
24 the repr() of the original object. This is precomputed when the
9a8a40f2218c Bastionification utility (useful for rexec clients)
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 bastion is created.
9a8a40f2218c Bastionification utility (useful for rexec clients)
Guido van Rossum <guido@python.org>
parents:
diff changeset
26
9a8a40f2218c Bastionification utility (useful for rexec clients)
Guido van Rossum <guido@python.org>
parents:
diff changeset
27 """
46811
5679ce66e74b Deprecate Bastion and rexec for 3.0.
Brett Cannon <bcannon@gmail.com>
parents: 41443
diff changeset
28 from warnings import warnpy3k
5679ce66e74b Deprecate Bastion and rexec for 3.0.
Brett Cannon <bcannon@gmail.com>
parents: 41443
diff changeset
29 warnpy3k("the Bastion module has been removed in Python 3.0", stacklevel=2)
5679ce66e74b Deprecate Bastion and rexec for 3.0.
Brett Cannon <bcannon@gmail.com>
parents: 41443
diff changeset
30 del warnpy3k
3671
9a8a40f2218c Bastionification utility (useful for rexec clients)
Guido van Rossum <guido@python.org>
parents:
diff changeset
31
16070
b5345e99596b added __all__ lists to a number of Python modules
Skip Montanaro <skip@pobox.com>
parents: 16021
diff changeset
32 __all__ = ["BastionClass", "Bastion"]
3671
9a8a40f2218c Bastionification utility (useful for rexec clients)
Guido van Rossum <guido@python.org>
parents:
diff changeset
33
9a8a40f2218c Bastionification utility (useful for rexec clients)
Guido van Rossum <guido@python.org>
parents:
diff changeset
34 from types import MethodType
9a8a40f2218c Bastionification utility (useful for rexec clients)
Guido van Rossum <guido@python.org>
parents:
diff changeset
35
9a8a40f2218c Bastionification utility (useful for rexec clients)
Guido van Rossum <guido@python.org>
parents:
diff changeset
36
9a8a40f2218c Bastionification utility (useful for rexec clients)
Guido van Rossum <guido@python.org>
parents:
diff changeset
37 class BastionClass:
9a8a40f2218c Bastionification utility (useful for rexec clients)
Guido van Rossum <guido@python.org>
parents:
diff changeset
38
9a8a40f2218c Bastionification utility (useful for rexec clients)
Guido van Rossum <guido@python.org>
parents:
diff changeset
39 """Helper class used by the Bastion() function.
9a8a40f2218c Bastionification utility (useful for rexec clients)
Guido van Rossum <guido@python.org>
parents:
diff changeset
40
9a8a40f2218c Bastionification utility (useful for rexec clients)
Guido van Rossum <guido@python.org>
parents:
diff changeset
41 You could subclass this and pass the subclass as the bastionclass
9a8a40f2218c Bastionification utility (useful for rexec clients)
Guido van Rossum <guido@python.org>
parents:
diff changeset
42 argument to the Bastion() function, as long as the constructor has
9a8a40f2218c Bastionification utility (useful for rexec clients)
Guido van Rossum <guido@python.org>
parents:
diff changeset
43 the same signature (a get() function and a name for the object).
9a8a40f2218c Bastionification utility (useful for rexec clients)
Guido van Rossum <guido@python.org>
parents:
diff changeset
44
9a8a40f2218c Bastionification utility (useful for rexec clients)
Guido van Rossum <guido@python.org>
parents:
diff changeset
45 """
9a8a40f2218c Bastionification utility (useful for rexec clients)
Guido van Rossum <guido@python.org>
parents:
diff changeset
46
9a8a40f2218c Bastionification utility (useful for rexec clients)
Guido van Rossum <guido@python.org>
parents:
diff changeset
47 def __init__(self, get, name):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4369
diff changeset
48 """Constructor.
3671
9a8a40f2218c Bastionification utility (useful for rexec clients)
Guido van Rossum <guido@python.org>
parents:
diff changeset
49
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4369
diff changeset
50 Arguments:
3671
9a8a40f2218c Bastionification utility (useful for rexec clients)
Guido van Rossum <guido@python.org>
parents:
diff changeset
51
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4369
diff changeset
52 get - a function that gets the attribute value (by name)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4369
diff changeset
53 name - a human-readable name for the original object
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4369
diff changeset
54 (suggestion: use repr(object))
3671
9a8a40f2218c Bastionification utility (useful for rexec clients)
Guido van Rossum <guido@python.org>
parents:
diff changeset
55
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4369
diff changeset
56 """
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4369
diff changeset
57 self._get_ = get
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4369
diff changeset
58 self._name_ = name
3671
9a8a40f2218c Bastionification utility (useful for rexec clients)
Guido van Rossum <guido@python.org>
parents:
diff changeset
59
9a8a40f2218c Bastionification utility (useful for rexec clients)
Guido van Rossum <guido@python.org>
parents:
diff changeset
60 def __repr__(self):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4369
diff changeset
61 """Return a representation string.
3671
9a8a40f2218c Bastionification utility (useful for rexec clients)
Guido van Rossum <guido@python.org>
parents:
diff changeset
62
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4369
diff changeset
63 This includes the name passed in to the constructor, so that
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4369
diff changeset
64 if you print the bastion during debugging, at least you have
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4369
diff changeset
65 some idea of what it is.
3671
9a8a40f2218c Bastionification utility (useful for rexec clients)
Guido van Rossum <guido@python.org>
parents:
diff changeset
66
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4369
diff changeset
67 """
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4369
diff changeset
68 return "<Bastion for %s>" % self._name_
3671
9a8a40f2218c Bastionification utility (useful for rexec clients)
Guido van Rossum <guido@python.org>
parents:
diff changeset
69
9a8a40f2218c Bastionification utility (useful for rexec clients)
Guido van Rossum <guido@python.org>
parents:
diff changeset
70 def __getattr__(self, name):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4369
diff changeset
71 """Get an as-yet undefined attribute value.
3671
9a8a40f2218c Bastionification utility (useful for rexec clients)
Guido van Rossum <guido@python.org>
parents:
diff changeset
72
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4369
diff changeset
73 This calls the get() function that was passed to the
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4369
diff changeset
74 constructor. The result is stored as an instance variable so
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4369
diff changeset
75 that the next time the same attribute is requested,
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4369
diff changeset
76 __getattr__() won't be invoked.
3671
9a8a40f2218c Bastionification utility (useful for rexec clients)
Guido van Rossum <guido@python.org>
parents:
diff changeset
77
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4369
diff changeset
78 If the get() function raises an exception, this is simply
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4369
diff changeset
79 passed on -- exceptions are not cached.
3671
9a8a40f2218c Bastionification utility (useful for rexec clients)
Guido van Rossum <guido@python.org>
parents:
diff changeset
80
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4369
diff changeset
81 """
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4369
diff changeset
82 attribute = self._get_(name)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4369
diff changeset
83 self.__dict__[name] = attribute
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4369
diff changeset
84 return attribute
3671
9a8a40f2218c Bastionification utility (useful for rexec clients)
Guido van Rossum <guido@python.org>
parents:
diff changeset
85
9a8a40f2218c Bastionification utility (useful for rexec clients)
Guido van Rossum <guido@python.org>
parents:
diff changeset
86
9a8a40f2218c Bastionification utility (useful for rexec clients)
Guido van Rossum <guido@python.org>
parents:
diff changeset
87 def Bastion(object, filter = lambda name: name[:1] != '_',
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4369
diff changeset
88 name=None, bastionclass=BastionClass):
3671
9a8a40f2218c Bastionification utility (useful for rexec clients)
Guido van Rossum <guido@python.org>
parents:
diff changeset
89 """Create a bastion for an object, using an optional filter.
9a8a40f2218c Bastionification utility (useful for rexec clients)
Guido van Rossum <guido@python.org>
parents:
diff changeset
90
9a8a40f2218c Bastionification utility (useful for rexec clients)
Guido van Rossum <guido@python.org>
parents:
diff changeset
91 See the Bastion module's documentation for background.
9a8a40f2218c Bastionification utility (useful for rexec clients)
Guido van Rossum <guido@python.org>
parents:
diff changeset
92
9a8a40f2218c Bastionification utility (useful for rexec clients)
Guido van Rossum <guido@python.org>
parents:
diff changeset
93 Arguments:
9a8a40f2218c Bastionification utility (useful for rexec clients)
Guido van Rossum <guido@python.org>
parents:
diff changeset
94
9a8a40f2218c Bastionification utility (useful for rexec clients)
Guido van Rossum <guido@python.org>
parents:
diff changeset
95 object - the original object
9a8a40f2218c Bastionification utility (useful for rexec clients)
Guido van Rossum <guido@python.org>
parents:
diff changeset
96 filter - a predicate that decides whether a function name is OK;
9a8a40f2218c Bastionification utility (useful for rexec clients)
Guido van Rossum <guido@python.org>
parents:
diff changeset
97 by default all names are OK that don't start with '_'
9a8a40f2218c Bastionification utility (useful for rexec clients)
Guido van Rossum <guido@python.org>
parents:
diff changeset
98 name - the name of the object; default repr(object)
9a8a40f2218c Bastionification utility (useful for rexec clients)
Guido van Rossum <guido@python.org>
parents:
diff changeset
99 bastionclass - class used to create the bastion; default BastionClass
9a8a40f2218c Bastionification utility (useful for rexec clients)
Guido van Rossum <guido@python.org>
parents:
diff changeset
100
9a8a40f2218c Bastionification utility (useful for rexec clients)
Guido van Rossum <guido@python.org>
parents:
diff changeset
101 """
9a8a40f2218c Bastionification utility (useful for rexec clients)
Guido van Rossum <guido@python.org>
parents:
diff changeset
102
41443
c34d705b884e Patch #1698951: clarify deprecation message in rexec and Bastion
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
103 raise RuntimeError, "This code is not secure in Python 2.2 and later"
27109
0464acb5b3e4 Sabotage rexec.py. It is not safe since the new-style classes.
Guido van Rossum <guido@python.org>
parents: 16070
diff changeset
104
3671
9a8a40f2218c Bastionification utility (useful for rexec clients)
Guido van Rossum <guido@python.org>
parents:
diff changeset
105 # Note: we define *two* ad-hoc functions here, get1 and get2.
9a8a40f2218c Bastionification utility (useful for rexec clients)
Guido van Rossum <guido@python.org>
parents:
diff changeset
106 # Both are intended to be called in the same way: get(name).
9a8a40f2218c Bastionification utility (useful for rexec clients)
Guido van Rossum <guido@python.org>
parents:
diff changeset
107 # It is clear that the real work (getting the attribute
9a8a40f2218c Bastionification utility (useful for rexec clients)
Guido van Rossum <guido@python.org>
parents:
diff changeset
108 # from the object and calling the filter) is done in get1.
9a8a40f2218c Bastionification utility (useful for rexec clients)
Guido van Rossum <guido@python.org>
parents:
diff changeset
109 # Why can't we pass get1 to the bastion? Because the user
9a8a40f2218c Bastionification utility (useful for rexec clients)
Guido van Rossum <guido@python.org>
parents:
diff changeset
110 # would be able to override the filter argument! With get2,
9a8a40f2218c Bastionification utility (useful for rexec clients)
Guido van Rossum <guido@python.org>
parents:
diff changeset
111 # overriding the default argument is no security loophole:
9a8a40f2218c Bastionification utility (useful for rexec clients)
Guido van Rossum <guido@python.org>
parents:
diff changeset
112 # all it does is call it.
9a8a40f2218c Bastionification utility (useful for rexec clients)
Guido van Rossum <guido@python.org>
parents:
diff changeset
113 # Also notice that we can't place the object and filter as
9a8a40f2218c Bastionification utility (useful for rexec clients)
Guido van Rossum <guido@python.org>
parents:
diff changeset
114 # instance variables on the bastion object itself, since
9a8a40f2218c Bastionification utility (useful for rexec clients)
Guido van Rossum <guido@python.org>
parents:
diff changeset
115 # the user has full access to all instance variables!
9a8a40f2218c Bastionification utility (useful for rexec clients)
Guido van Rossum <guido@python.org>
parents:
diff changeset
116
9a8a40f2218c Bastionification utility (useful for rexec clients)
Guido van Rossum <guido@python.org>
parents:
diff changeset
117 def get1(name, object=object, filter=filter):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4369
diff changeset
118 """Internal function for Bastion(). See source comments."""
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4369
diff changeset
119 if filter(name):
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4369
diff changeset
120 attribute = getattr(object, name)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4369
diff changeset
121 if type(attribute) == MethodType:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4369
diff changeset
122 return attribute
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4369
diff changeset
123 raise AttributeError, name
3671
9a8a40f2218c Bastionification utility (useful for rexec clients)
Guido van Rossum <guido@python.org>
parents:
diff changeset
124
9a8a40f2218c Bastionification utility (useful for rexec clients)
Guido van Rossum <guido@python.org>
parents:
diff changeset
125 def get2(name, get1=get1):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4369
diff changeset
126 """Internal function for Bastion(). See source comments."""
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4369
diff changeset
127 return get1(name)
3671
9a8a40f2218c Bastionification utility (useful for rexec clients)
Guido van Rossum <guido@python.org>
parents:
diff changeset
128
9a8a40f2218c Bastionification utility (useful for rexec clients)
Guido van Rossum <guido@python.org>
parents:
diff changeset
129 if name is None:
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 27109
diff changeset
130 name = repr(object)
3671
9a8a40f2218c Bastionification utility (useful for rexec clients)
Guido van Rossum <guido@python.org>
parents:
diff changeset
131 return bastionclass(get2, name)
9a8a40f2218c Bastionification utility (useful for rexec clients)
Guido van Rossum <guido@python.org>
parents:
diff changeset
132
9a8a40f2218c Bastionification utility (useful for rexec clients)
Guido van Rossum <guido@python.org>
parents:
diff changeset
133
9a8a40f2218c Bastionification utility (useful for rexec clients)
Guido van Rossum <guido@python.org>
parents:
diff changeset
134 def _test():
9a8a40f2218c Bastionification utility (useful for rexec clients)
Guido van Rossum <guido@python.org>
parents:
diff changeset
135 """Test the Bastion() function."""
9a8a40f2218c Bastionification utility (useful for rexec clients)
Guido van Rossum <guido@python.org>
parents:
diff changeset
136 class Original:
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4369
diff changeset
137 def __init__(self):
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4369
diff changeset
138 self.sum = 0
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4369
diff changeset
139 def add(self, n):
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4369
diff changeset
140 self._add(n)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4369
diff changeset
141 def _add(self, n):
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4369
diff changeset
142 self.sum = self.sum + n
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4369
diff changeset
143 def total(self):
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4369
diff changeset
144 return self.sum
3671
9a8a40f2218c Bastionification utility (useful for rexec clients)
Guido van Rossum <guido@python.org>
parents:
diff changeset
145 o = Original()
9a8a40f2218c Bastionification utility (useful for rexec clients)
Guido van Rossum <guido@python.org>
parents:
diff changeset
146 b = Bastion(o)
4071
c575a920e4a1 Improved tests; run twice, onece in normal mode, once in restricted mode.
Guido van Rossum <guido@python.org>
parents: 3671
diff changeset
147 testcode = """if 1:
3671
9a8a40f2218c Bastionification utility (useful for rexec clients)
Guido van Rossum <guido@python.org>
parents:
diff changeset
148 b.add(81)
9a8a40f2218c Bastionification utility (useful for rexec clients)
Guido van Rossum <guido@python.org>
parents:
diff changeset
149 b.add(18)
9a8a40f2218c Bastionification utility (useful for rexec clients)
Guido van Rossum <guido@python.org>
parents:
diff changeset
150 print "b.total() =", b.total()
9a8a40f2218c Bastionification utility (useful for rexec clients)
Guido van Rossum <guido@python.org>
parents:
diff changeset
151 try:
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4369
diff changeset
152 print "b.sum =", b.sum,
3671
9a8a40f2218c Bastionification utility (useful for rexec clients)
Guido van Rossum <guido@python.org>
parents:
diff changeset
153 except:
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4369
diff changeset
154 print "inaccessible"
3671
9a8a40f2218c Bastionification utility (useful for rexec clients)
Guido van Rossum <guido@python.org>
parents:
diff changeset
155 else:
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4369
diff changeset
156 print "accessible"
3671
9a8a40f2218c Bastionification utility (useful for rexec clients)
Guido van Rossum <guido@python.org>
parents:
diff changeset
157 try:
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4369
diff changeset
158 print "b._add =", b._add,
3671
9a8a40f2218c Bastionification utility (useful for rexec clients)
Guido van Rossum <guido@python.org>
parents:
diff changeset
159 except:
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4369
diff changeset
160 print "inaccessible"
3671
9a8a40f2218c Bastionification utility (useful for rexec clients)
Guido van Rossum <guido@python.org>
parents:
diff changeset
161 else:
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4369
diff changeset
162 print "accessible"
4071
c575a920e4a1 Improved tests; run twice, onece in normal mode, once in restricted mode.
Guido van Rossum <guido@python.org>
parents: 3671
diff changeset
163 try:
16021
cfac2245a2c7 make the output of _test() suitable for use in the regression test
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 7755
diff changeset
164 print "b._get_.func_defaults =", map(type, b._get_.func_defaults),
4071
c575a920e4a1 Improved tests; run twice, onece in normal mode, once in restricted mode.
Guido van Rossum <guido@python.org>
parents: 3671
diff changeset
165 except:
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4369
diff changeset
166 print "inaccessible"
4071
c575a920e4a1 Improved tests; run twice, onece in normal mode, once in restricted mode.
Guido van Rossum <guido@python.org>
parents: 3671
diff changeset
167 else:
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4369
diff changeset
168 print "accessible"
4071
c575a920e4a1 Improved tests; run twice, onece in normal mode, once in restricted mode.
Guido van Rossum <guido@python.org>
parents: 3671
diff changeset
169 \n"""
c575a920e4a1 Improved tests; run twice, onece in normal mode, once in restricted mode.
Guido van Rossum <guido@python.org>
parents: 3671
diff changeset
170 exec testcode
c575a920e4a1 Improved tests; run twice, onece in normal mode, once in restricted mode.
Guido van Rossum <guido@python.org>
parents: 3671
diff changeset
171 print '='*20, "Using rexec:", '='*20
c575a920e4a1 Improved tests; run twice, onece in normal mode, once in restricted mode.
Guido van Rossum <guido@python.org>
parents: 3671
diff changeset
172 import rexec
c575a920e4a1 Improved tests; run twice, onece in normal mode, once in restricted mode.
Guido van Rossum <guido@python.org>
parents: 3671
diff changeset
173 r = rexec.RExec()
c575a920e4a1 Improved tests; run twice, onece in normal mode, once in restricted mode.
Guido van Rossum <guido@python.org>
parents: 3671
diff changeset
174 m = r.add_module('__main__')
c575a920e4a1 Improved tests; run twice, onece in normal mode, once in restricted mode.
Guido van Rossum <guido@python.org>
parents: 3671
diff changeset
175 m.b = b
c575a920e4a1 Improved tests; run twice, onece in normal mode, once in restricted mode.
Guido van Rossum <guido@python.org>
parents: 3671
diff changeset
176 r.r_exec(testcode)
3671
9a8a40f2218c Bastionification utility (useful for rexec clients)
Guido van Rossum <guido@python.org>
parents:
diff changeset
177
9a8a40f2218c Bastionification utility (useful for rexec clients)
Guido van Rossum <guido@python.org>
parents:
diff changeset
178
9a8a40f2218c Bastionification utility (useful for rexec clients)
Guido van Rossum <guido@python.org>
parents:
diff changeset
179 if __name__ == '__main__':
9a8a40f2218c Bastionification utility (useful for rexec clients)
Guido van Rossum <guido@python.org>
parents:
diff changeset
180 _test()