restricted_module.py :  » Web-Frameworks » Zope » Zope-2.6.0 » lib » python » RestrictedPython » tests » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Web Frameworks » Zope 
Zope » Zope 2.6.0 » lib » python » RestrictedPython » tests » restricted_module.py
from __future__ import nested_scopes

def print0():
    print 'Hello, world!',
    return printed

def print1():
    print 'Hello,',
    print 'world!',
    return printed

def printStuff():
    print 'a', 'b', 'c',
    return printed

def printToNone():
    x = None
    print >>x, 'Hello, world!',
    return printed

def printLines():
    # This failed before Zope 2.4.0a2
    r = range(3)
    for n in r:
        for m in r:
            print m + n * len(r),
        print
    return printed

def primes():
    # Somewhat obfuscated code on purpose
    print filter(None,map(lambda y:y*reduce(lambda x,y:x*y!=0,
    map(lambda x,y=y:y%x,range(2,int(pow(y,0.5)+1))),1),range(2,20))),
    return printed

def allowed_read(ob):
    print ob.allowed
    print ob.s
    print ob[0]
    print ob[2]
    print ob[3:-1]
    print len(ob)
    return printed

def allowed_default_args(ob):
    def f(a=ob.allowed, s=ob.s):
        return a, s


def allowed_simple():
    q = {'x':'a'}
    q['y'] = 'b'
    q.update({'z': 'c'})
    r = ['a']
    r.append('b')
    r[2:2] = ['c']
    s = 'a'
    s = s[:100] + 'b'
    s += 'c'
    _ = q

    return q['x'] + q['y'] + q['z'] + r[0] + r[1] + r[2] + s

def allowed_write(ob):
    ob.writeable = 1
    #ob.writeable += 1
    [1 for ob.writeable in 1,2]
    ob['safe'] = 2
    #ob['safe'] += 2
    [1 for ob['safe'] in 1,2]

def denied_print(ob):
    print >> ob, 'Hello, world!',

def denied_getattr(ob):
    #ob.disallowed += 1
    ob.disallowed = 1
    return ob.disallowed

def denied_default_args(ob):
    def f(d=ob.disallowed):
        return d

def denied_setattr(ob):
    ob.allowed = -1

def denied_setattr2(ob):
    #ob.allowed += -1
    ob.allowed = -1

def denied_setattr3(ob):
    [1 for ob.allowed in 1,2]

def denied_getitem(ob):
    ob[1]

def denied_getitem2(ob):
    #ob[1] += 1
    ob[1]

def denied_setitem(ob):
    ob['x'] = 2

def denied_setitem2(ob):
    #ob[0] += 2
    ob['x'] = 2

def denied_setitem3(ob):
    [1 for ob['x'] in 1,2]

def denied_setslice(ob):
    ob[0:1] = 'a'

def denied_setslice2(ob):
    #ob[0:1] += 'a'
    ob[0:1] = 'a'

def denied_setslice3(ob):
    [1 for ob[0:1] in 1,2]

##def strange_attribute():
##    # If a guard has attributes with names that don't start with an
##    # underscore, those attributes appear to be an attribute of
##    # anything.
##    return [].attribute_of_anything

def order_of_operations():
    return 3 * 4 * -2 + 2 * 12

def rot13(ss):
    mapping = {}
    orda = ord('a')
    ordA = ord('A')
    for n in range(13):
        c1 = chr(orda + n)
        c2 = chr(orda + n + 13)
        c3 = chr(ordA + n)
        c4 = chr(ordA + n + 13)
        mapping[c1] = c2
        mapping[c2] = c1
        mapping[c3] = c4
        mapping[c4] = c3
    del c1, c2, c3, c4, orda, ordA
    res = ''
    for c in ss:
        res = res + mapping.get(c, c)
    return res

def nested_scopes_1():
    # Fails if 'a' is consumed by the first function.
    a = 1
    def f1():
        return a
    def f2():
        return a
    return f1() + f2()
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.