[Zope-Checkins] CVS: Zope2 - RestrictionMutator.py:1.1.2.2
shane@digicool.com
shane@digicool.com
Wed, 18 Apr 2001 17:02:19 -0400 (EDT)
Update of /cvs-repository/Zope2/lib/python/RestrictedPython
In directory korak:/tmp/cvs-serv9014
Modified Files:
Tag: RestrictedPythonBranch
RestrictionMutator.py
Log Message:
Collected guards into one variable and tidied up here and there
--- Updated File RestrictionMutator.py in package Zope2 --
--- RestrictionMutator.py 2001/04/18 18:45:42 1.1.2.1
+++ RestrictionMutator.py 2001/04/18 21:02:18 1.1.2.2
@@ -2,6 +2,7 @@
import string
from compiler import ast
from compiler.transformer import parse
+from compiler.consts import OP_ASSIGN, OP_DELETE, OP_APPLY
def rmLineno(node):
'''Strip lineno attributes from a code tree'''
@@ -21,8 +22,8 @@
'''Make a "clean" expression node'''
return stmtNode(txt).expr
-_decl_globals = ast.Global(['_print_target_class', '_read_guard',
- '_write_guard'])
+_decl_globals_code = ast.Global(['_print_target_class', '_guards'])
+_prep_guards_code = stmtNode('_read_guard, _write_guard = _guards')
_print_code = stmtNode('_print_target = _print_target_class()')
_printed_expr = exprNode('_print_target()')
_print_target_name = ast.Name('_print_target')
@@ -30,7 +31,7 @@
_write_guard_name = ast.Name('_write_guard')
-class PrintRedirector:
+class PrintCollector:
'''Collect written text, and return it when called.'''
def __init__(self):
self.txt = []
@@ -90,7 +91,7 @@
elif not self._print_used:
self.warnings.append(
"Doesn't print, but reads 'printed' variable.")
- node.code.nodes.insert(0, _decl_globals)
+ node.code.nodes[0:0] = [_decl_globals_code, _prep_guards_code]
self._print_used, self._printed_used = self._print_stack.pop()
return node
@@ -125,9 +126,11 @@
def visitSubscript(self, node, walker):
node = walker.defaultVisitNode(node)
- if node.flags == 'OP_APPLY':
+ if node.flags == OP_APPLY:
+ # get subscript or slice
node.expr = ast.CallFunc(_read_guard_name, [node.expr])
- elif node.flags in ('OP_DELETE', 'OP_ASSIGN'):
+ elif node.flags in (OP_DELETE, OP_ASSIGN):
+ # set or remove subscript or slice
node.expr = ast.CallFunc(_write_guard_name, [node.expr])
return node
@@ -148,18 +151,21 @@
class Noisy:
- '''Test class that babbles about accesses'''
+ '''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]
@@ -196,6 +202,7 @@
from compiler.pycodegen import NestedScopeModuleCodeGenerator
from compiler import visitor
+ print tree
MutatingWalker.walk(tree, RestrictionMutator())
print tree
gen = NestedScopeModuleCodeGenerator('some_python_script')
@@ -205,9 +212,8 @@
exec code in dict
f = dict['f']
f.func_globals.update({
- '_print_target_class': PrintRedirector,
- '_read_guard': Noisy,
- '_write_guard': Noisy,
+ '_print_target_class': PrintCollector,
+ '_guards': (Noisy, Noisy),
})
print f()
#import dis