[Zope-Checkins] CVS: Zope2 - RestrictionMutator.py:1.1.2.11
shane@digicool.com
shane@digicool.com
Fri, 27 Apr 2001 11:20:26 -0400 (EDT)
Update of /cvs-repository/Zope2/lib/python/RestrictedPython
In directory korak:/tmp/cvs-serv20197
Modified Files:
Tag: RestrictedPythonBranch
RestrictionMutator.py
Log Message:
Tidied the code
--- Updated File RestrictionMutator.py in package Zope2 --
--- RestrictionMutator.py 2001/04/26 19:13:39 1.1.2.10
+++ RestrictionMutator.py 2001/04/27 15:20:25 1.1.2.11
@@ -82,10 +82,14 @@
# attributions are listed in the accompanying credits file.
#
##############################################################################
-
+'''
+RestrictionMutator modifies a tree produced by
+compiler.transformer.Transformer, restricting and enhancing the
+code in various ways before sending it to pycodegen.
+'''
__version__='$Revision$'[11:-2]
-from compiler import ast, visitor, pycodegen
+from compiler import ast
from compiler.transformer import parse
from compiler.consts import OP_ASSIGN, OP_DELETE, OP_APPLY
@@ -271,44 +275,47 @@
return node
-class Noisy:
- '''Test guard class that babbles about accesses'''
- def __init__(self, _ob):
- self.__dict__['_ob'] = _ob
- # Read guard methods
- def __len__(self):
- # This is called by the interpreter before __getslice__().
- _ob = self.__dict__['_ob']
- print '__len__', `_ob`
- return len(_ob)
- def __getattr__(self, name):
- _ob = self.__dict__['_ob']
- print '__getattr__', `_ob`, name
- return getattr(_ob, name)
- def __getitem__(self, index):
- # Can receive an Ellipsis or "slice" instance.
- _ob = self.__dict__['_ob']
- print '__getitem__', `_ob`, index
- return _ob[index]
- def __getslice__(self, lo, hi):
- _ob = self.__dict__['_ob']
- print '__getslice__', `_ob`, lo, hi
- return _ob[lo:hi]
- # Write guard methods
- def __setattr__(self, name, value):
- _ob = self.__dict__['_ob']
- print '__setattr__', `_ob`, name, value
- setattr(_ob, name, value)
- def __setitem__(self, index, value):
- _ob = self.__dict__['_ob']
- print '__setitem__', `_ob`, index, value
- _ob[index] = value
- def __setslice__(self, lo, hi, value):
- _ob = self.__dict__['_ob']
- print '__setslice__', `_ob`, lo, hi, value
- _ob[lo:hi] = value
-
if __name__ == '__main__':
+ # A minimal test.
+ from compiler import visitor, pycodegen
+
+ class Noisy:
+ '''Test guard class that babbles about accesses'''
+ def __init__(self, _ob):
+ self.__dict__['_ob'] = _ob
+ # Read guard methods
+ def __len__(self):
+ # This is called by the interpreter before __getslice__().
+ _ob = self.__dict__['_ob']
+ print '__len__', `_ob`
+ return len(_ob)
+ def __getattr__(self, name):
+ _ob = self.__dict__['_ob']
+ print '__getattr__', `_ob`, name
+ return getattr(_ob, name)
+ def __getitem__(self, index):
+ # Can receive an Ellipsis or "slice" instance.
+ _ob = self.__dict__['_ob']
+ print '__getitem__', `_ob`, index
+ return _ob[index]
+ def __getslice__(self, lo, hi):
+ _ob = self.__dict__['_ob']
+ print '__getslice__', `_ob`, lo, hi
+ return _ob[lo:hi]
+ # Write guard methods
+ def __setattr__(self, name, value):
+ _ob = self.__dict__['_ob']
+ print '__setattr__', `_ob`, name, value
+ setattr(_ob, name, value)
+ def __setitem__(self, index, value):
+ _ob = self.__dict__['_ob']
+ print '__setitem__', `_ob`, index, value
+ _ob[index] = value
+ def __setslice__(self, lo, hi, value):
+ _ob = self.__dict__['_ob']
+ print '__setslice__', `_ob`, lo, hi, value
+ _ob[lo:hi] = value
+
tree = parse('''
def f():
print "Hello",
@@ -336,22 +343,3 @@
print f()
#import dis
#dis.dis(f.func_code)
-
- # Test: f can't access f.func_globals (otherwise it can override
- # _*_guard)
- # Test: no access to builtin getattr. getattr(guarded_ob, '_ob')
- # Test: no access to globals()
-
-
-
-if __name__ == '__main__2':
- c1 = compile_restricted('2+2', 'sdh', 'eval')
- c2 = compile('2+2', 'sdh', 'eval')
- for k in dir(c1):
- v1 = getattr(c1, k)
- v2 = getattr(c2, k)
- if v1 != v2:
- print k, `v1`, `v2`
- import dis
- dis.dis(c1)
- dis.dis(c2)