[Zope-Checkins] CVS: Zope3/lib/python/Zope/Security - RestrictedInterpreter.py:1.1.2.1 Proxy.py:1.1.2.6

Jeremy Hylton jeremy@zope.com
Wed, 17 Apr 2002 19:16:02 -0400


Update of /cvs-repository/Zope3/lib/python/Zope/Security
In directory cvs.zope.org:/tmp/cvs-serv32523

Modified Files:
      Tag: SecurityProxy-branch
	Proxy.py 
Added Files:
      Tag: SecurityProxy-branch
	RestrictedInterpreter.py 
Log Message:
A *very* minimal restricted interpreter that doesn't depend on rexec.

including another addition to Proxy to get a minimal test for
wrappedness working



=== Added File Zope3/lib/python/Zope/Security/RestrictedInterpreter.py ===
import sys

from Zope.Security.Proxy import Proxy

class RestrictedInterpreter:

    ok_builtin_modules = ('audioop', 'array', 'binascii',
                          'cmath', 'errno', 'imageop',
                          'marshal', 'math', 'md5', 'operator',
                          'parser', 'regex', 'pcre', 'rotor', 'select',
                          'sha', '_sre', 'strop', 'struct', 'time')

    ok_sys_names = ('ps1', 'ps2', 'copyright', 'version',
                    'platform', 'exit', 'maxint')

    nok_builtin_names = ('open', 'file', 'reload', '__import__')

    def __init__(self, checker):
        self.checker = checker
        self.builtins = {}
        self.globals = {'__builtins__' : self.builtins}
        self.create_builtins()

    def ri_exec(self, code):
        # what is the type of code?
        exec code in self.globals

    def create_builtins(self):
        import __builtin__
        for k, v in __builtin__.__dict__.iteritems():
            if k not in self.nok_builtin_names:
                self.builtins[k] = Proxy(v, self.checker)
        self.builtins['__import__'] = Proxy(self.ri_import, None)

    def ri_import(self, name, globals, locals, fromlist):
        # XXX handle fromlist
        return sys.modules[name]
                

    


=== Zope3/lib/python/Zope/Security/Proxy.py 1.1.2.5 => 1.1.2.6 ===
 
-class Proxy:
-    def __init__(self, object, checker=None):
+def Proxy(obj, checker=None):
+    if obj is None:
+        return None
+    elif isinstance(obj, int):
+        return obj
+    elif isinstance(obj, float):
+        return obj
+    else:
+        return _Proxy(obj, checker)
+
+class _Proxy:
+    def __init__(self, object, checker):
         self.object = object
 
     def __repr__(self):
@@ -19,3 +29,5 @@
     def __getitem__(self, i):
         return Proxy(self.object[i], None)
 
+    def __cmp__(self, object):
+        return cmp(self.object, object)