blob: 99990a2d7df662987e78597948b520778e628ff7 [file] [log] [blame]
Guido van Rossum601d3321996-06-11 20:12:49 +00001"""Bastionification utility.
2
3A bastion (for another object -- the 'original') is an object that has
4the same methods as the original but does not give access to its
5instance variables. Bastions have a number of uses, but the most
6obvious one is to provide code executing in restricted mode with a
7safe interface to an object implemented in unrestricted mode.
8
9The bastionification routine has an optional second argument which is
10a filter function. Only those methods for which the filter method
11(called with the method name as argument) returns true are accessible.
12The default filter method returns true unless the method name begins
13with an underscore.
14
15There are a number of possible implementations of bastions. We use a
16'lazy' approach where the bastion's __getattr__() discipline does all
17the work for a particular method the first time it is used. This is
18usually fastest, especially if the user doesn't call all available
19methods. The retrieved methods are stored as instance variables of
20the bastion, so the overhead is only occurred on the first use of each
21method.
22
23Detail: the bastion class has a __repr__() discipline which includes
24the repr() of the original object. This is precomputed when the
25bastion is created.
26
27"""
28
Skip Montanaroe99d5ea2001-01-20 19:54:20 +000029__all__ = ["BastionClass", "Bastion"]
Guido van Rossum601d3321996-06-11 20:12:49 +000030
31from types import MethodType
32
33
34class BastionClass:
35
36 """Helper class used by the Bastion() function.
37
38 You could subclass this and pass the subclass as the bastionclass
39 argument to the Bastion() function, as long as the constructor has
40 the same signature (a get() function and a name for the object).
41
42 """
43
44 def __init__(self, get, name):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000045 """Constructor.
Guido van Rossum601d3321996-06-11 20:12:49 +000046
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000047 Arguments:
Guido van Rossum601d3321996-06-11 20:12:49 +000048
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000049 get - a function that gets the attribute value (by name)
50 name - a human-readable name for the original object
51 (suggestion: use repr(object))
Guido van Rossum601d3321996-06-11 20:12:49 +000052
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000053 """
54 self._get_ = get
55 self._name_ = name
Guido van Rossum601d3321996-06-11 20:12:49 +000056
57 def __repr__(self):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000058 """Return a representation string.
Guido van Rossum601d3321996-06-11 20:12:49 +000059
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000060 This includes the name passed in to the constructor, so that
61 if you print the bastion during debugging, at least you have
62 some idea of what it is.
Guido van Rossum601d3321996-06-11 20:12:49 +000063
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000064 """
65 return "<Bastion for %s>" % self._name_
Guido van Rossum601d3321996-06-11 20:12:49 +000066
67 def __getattr__(self, name):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000068 """Get an as-yet undefined attribute value.
Guido van Rossum601d3321996-06-11 20:12:49 +000069
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000070 This calls the get() function that was passed to the
71 constructor. The result is stored as an instance variable so
72 that the next time the same attribute is requested,
73 __getattr__() won't be invoked.
Guido van Rossum601d3321996-06-11 20:12:49 +000074
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000075 If the get() function raises an exception, this is simply
76 passed on -- exceptions are not cached.
Guido van Rossum601d3321996-06-11 20:12:49 +000077
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000078 """
79 attribute = self._get_(name)
80 self.__dict__[name] = attribute
81 return attribute
Guido van Rossum601d3321996-06-11 20:12:49 +000082
83
84def Bastion(object, filter = lambda name: name[:1] != '_',
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000085 name=None, bastionclass=BastionClass):
Guido van Rossum601d3321996-06-11 20:12:49 +000086 """Create a bastion for an object, using an optional filter.
87
88 See the Bastion module's documentation for background.
89
90 Arguments:
91
92 object - the original object
93 filter - a predicate that decides whether a function name is OK;
94 by default all names are OK that don't start with '_'
95 name - the name of the object; default repr(object)
96 bastionclass - class used to create the bastion; default BastionClass
97
98 """
99
100 # Note: we define *two* ad-hoc functions here, get1 and get2.
101 # Both are intended to be called in the same way: get(name).
102 # It is clear that the real work (getting the attribute
103 # from the object and calling the filter) is done in get1.
104 # Why can't we pass get1 to the bastion? Because the user
105 # would be able to override the filter argument! With get2,
106 # overriding the default argument is no security loophole:
107 # all it does is call it.
108 # Also notice that we can't place the object and filter as
109 # instance variables on the bastion object itself, since
110 # the user has full access to all instance variables!
111
112 def get1(name, object=object, filter=filter):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000113 """Internal function for Bastion(). See source comments."""
114 if filter(name):
115 attribute = getattr(object, name)
116 if type(attribute) == MethodType:
117 return attribute
118 raise AttributeError, name
Guido van Rossum601d3321996-06-11 20:12:49 +0000119
120 def get2(name, get1=get1):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000121 """Internal function for Bastion(). See source comments."""
122 return get1(name)
Guido van Rossum601d3321996-06-11 20:12:49 +0000123
124 if name is None:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000125 name = `object`
Guido van Rossum601d3321996-06-11 20:12:49 +0000126 return bastionclass(get2, name)
127
128
129def _test():
130 """Test the Bastion() function."""
131 class Original:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000132 def __init__(self):
133 self.sum = 0
134 def add(self, n):
135 self._add(n)
136 def _add(self, n):
137 self.sum = self.sum + n
138 def total(self):
139 return self.sum
Guido van Rossum601d3321996-06-11 20:12:49 +0000140 o = Original()
141 b = Bastion(o)
Guido van Rossum6ba66d01996-08-20 20:21:52 +0000142 testcode = """if 1:
Guido van Rossum601d3321996-06-11 20:12:49 +0000143 b.add(81)
144 b.add(18)
145 print "b.total() =", b.total()
146 try:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000147 print "b.sum =", b.sum,
Guido van Rossum601d3321996-06-11 20:12:49 +0000148 except:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000149 print "inaccessible"
Guido van Rossum601d3321996-06-11 20:12:49 +0000150 else:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000151 print "accessible"
Guido van Rossum601d3321996-06-11 20:12:49 +0000152 try:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000153 print "b._add =", b._add,
Guido van Rossum601d3321996-06-11 20:12:49 +0000154 except:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000155 print "inaccessible"
Guido van Rossum601d3321996-06-11 20:12:49 +0000156 else:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000157 print "accessible"
Guido van Rossum6ba66d01996-08-20 20:21:52 +0000158 try:
Jeremy Hylton1a34c872001-01-19 03:30:22 +0000159 print "b._get_.func_defaults =", map(type, b._get_.func_defaults),
Guido van Rossum6ba66d01996-08-20 20:21:52 +0000160 except:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000161 print "inaccessible"
Guido van Rossum6ba66d01996-08-20 20:21:52 +0000162 else:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000163 print "accessible"
Guido van Rossum6ba66d01996-08-20 20:21:52 +0000164 \n"""
165 exec testcode
166 print '='*20, "Using rexec:", '='*20
167 import rexec
168 r = rexec.RExec()
169 m = r.add_module('__main__')
170 m.b = b
171 r.r_exec(testcode)
Guido van Rossum601d3321996-06-11 20:12:49 +0000172
173
174if __name__ == '__main__':
175 _test()