[Python-Dev] Re: Capabilities

Samuele Pedroni pedronis@bluewin.ch
Wed, 12 Mar 2003 17:25:58 +0100


This is a multi-part message in MIME format.

------=_NextPart_000_0077_01C2E8BC.71D0CC00
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

I posted

> <s.py>
[...]
>
> class MyExc(Exception): # !!! definition outside of resticted execution
>   def __init__(self,msg):
>     self.message = msg
>     Exception.__init__(self,msg)
>
>   def __str__(self):
>     return self.message
>
> def myfunc():
>   raise MyExc('foo')
>
> ri = RestrictedInterpreter()
>
> ri.globals['myfunc'] = ProxyFactory(myfunc)
>
> f = open('c:/Documenti/x.txt','r')
> code = f.read()
> f.close()
>
> ri.ri_exec(code)
>
> print "OK"
> </s.py>
>
> Anyway I have a _very baroque_ x.txt that  manages to call sys.exit.
>

attached is a modified version of s.py that takes a filename for the code to
run inside the RestrictedInterpreter. Also myfunc is now myexc_source . There
is also a new function candy, next mail on that.

Here is a run with xpl1 (was x.txt):

...>\usr\python22\python -i s.py xpl1
restricted execution
no exit
cannot access sys.exit directly
Got sys.exit

...>

no OK, no  Python prompt !

here is xpl1 code [warning: metaclasses, descriptors usage, functional
programming ahead :)]
[some things are artifacts of the non-deliberate limitations inside
RestrictedInterpreter]

#Object = ''.__class__.__base__
Type = ''.__class__.__class__

class Iter:
  __metaclass__ = Type
  def __init__(self,v):
    self.v = v
    self.i = 0
  def __iter__(self): return self
  def next(self):
    try:
      v = self.v[self.i]
      self.i += 1
      return v
    except IndexError:
      raise StopIteration

class consta:
  __metaclass__ = Type

  def __init__(self,o):
    self.o = o

  def __get__(self,obj,typ): return self.o

#

try:
  myexc_source()
except Exception,e:
  pass

MyExc = e.__class__
e__str__ = e.__str__

#

try:
  e__str__.func_globals
except:
  print "restricted execution"

try:
  exit(0)
except:
  print "no exit"

try:
  import sys
  sys.exit(0)
except:
  print "cannot access sys.exit directly"

#

class Y:
  class __metaclass__(Type):
    def __iter__(cls): return Iter(['func_globals'])

class X(Y,MyExc):

  message = None

  __call__ = consta(getattr)
  def __iter__(self): return Iter([e__str__])

  #def __get__(self,x,X):
  #  print self,x,y
  #  return map(self,x,X)

  __get__ = map

# x isinst MyExc
# x.message === x.__get__(x,X) === map(x,x,X)
# x(o,a) === getattr(o,a)
# map(None,x) === [e__str__]
# map(None,X) === ['func_globals']

x=X()
X.message = x

g = MyExc.__str__(x)

print "Got sys.exit"
g[0]['exit'](0)

------=_NextPart_000_0077_01C2E8BC.71D0CC00
Content-Type: text/plain;
	name="s.py"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment;
	filename="s.py"

import sys

from os.path import join as pathjoin,dirname

code_fname =3D pathjoin(dirname(sys.argv[0]),sys.argv[1])

from sys import exit # !!! same module as MyExc

sys.path.append('C:/transit/Zope3-3.0a1/src') # ! add Zope3 (alpha) to =
sys.path

from zope.security.interpreter import RestrictedInterpreter
from zope.security.checker import ProxyFactory

class MyExc(Exception): # !!! definition outside of resticted execution
  def __init__(self,msg):
    self.message =3D msg
    Exception.__init__(self,msg)

  def __str__(self):
    return self.message

def myexc_source():
  raise MyExc('foo')

def candy(s):
  if s =3D=3D "yes":
    return 'candy'
  else:
    return 'none'

ri =3D RestrictedInterpreter()

ri.globals['myexc_source'] =3D ProxyFactory(myexc_source)

ri.globals['candy'] =3D ProxyFactory(candy)

f =3D open(code_fname,'r')
code =3D f.read()
f.close()

ri.ri_exec(code)

print "OK"
------=_NextPart_000_0077_01C2E8BC.71D0CC00--