File: VSEval.py

package info (click to toggle)
python-bobodtml 2.2.1-1
  • links: PTS
  • area: main
  • in suites: slink
  • size: 504 kB
  • ctags: 1,730
  • sloc: python: 2,903; makefile: 40; sh: 24
file content (177 lines) | stat: -rwxr-xr-x 5,594 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
##############################################################################
#
# Copyright (c) 1996-1998, Digital Creations, Fredericksburg, VA, USA.
# All rights reserved.
# 
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
# 
#   o Redistributions of source code must retain the above copyright
#     notice, this list of conditions, and the disclaimer that follows.
# 
#   o Redistributions in binary form must reproduce the above copyright
#     notice, this list of conditions, and the following disclaimer in
#     the documentation and/or other materials provided with the
#     distribution.
# 
#   o All advertising materials mentioning features or use of this
#     software must display the following acknowledgement:
# 
#       This product includes software developed by Digital Creations
#       and its contributors.
# 
#   o Neither the name of Digital Creations nor the names of its
#     contributors may be used to endorse or promote products derived
#     from this software without specific prior written permission.
# 
# 
# THIS SOFTWARE IS PROVIDED BY DIGITAL CREATIONS AND CONTRIBUTORS *AS IS*
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
# PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL DIGITAL
# CREATIONS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
# TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
# USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
# DAMAGE.
#
# 
# If you have questions regarding this software, contact:
#
#   Digital Creations, L.C.
#   910 Princess Ann Street
#   Fredericksburge, Virginia  22401
#
#   info@digicool.com
#
#   (540) 371-6909
#
##############################################################################

"""Very Safe Python Expressions
"""
__rcs_id__='$Id: VSEval.py,v 1.19 1998/09/14 22:03:34 jim Exp $'
__version__='$Revision: 1.19 $'[11:-2]

from string import translate
import string
gparse=None

nltosp=string.maketrans('\r\n','  ')

def default_slicer(env, s, *ind):
    l=len(ind)
    if l==2: return s[ind[0]:ind[1]]
    elif l==1: return s[ind[0]:]
    return s[:]

def careful_mul(env, *factors):
    s=None
    r=1
    for factor in factors:
        try:
            l=len(factor)
            s=1
        except: l=factor
        if s and (l*r) > 1000: raise TypeError, 'Illegal sequence repeat'
        r=r*factor

    return r

default_globals={
    '__builtins__':{},
    '__guarded_mul__':       careful_mul,
    '__guarded_getattr__':   lambda env, inst, name: getattr(inst, name),
    '__guarded_getitem__':   lambda env, coll, key:  coll[key],
    '__guarded_getslice__':  default_slicer,
    }



class Eval:
    """Provide a very-safe environment for evaluating expressions

    This class lets you overide operations, __power__, __mul__,
    __div__, __mod__, __add__, __sub__, __getitem__, __lshift__,
    __rshift__, __and__, __xor__, __or__,__pos__, __neg__, __not__,
    __repr__, __invert__, and __getattr__.

    For example, __mult__ might be overridden to prevent expressions like::

      'I like spam' * 100000000

    or to disallow or limit attribute access.

    """

    def __init__(self, expr, globals=default_globals):
        """Create a 'safe' expression

        where:

          expr -- a string containing the expression to be evaluated.

          globals -- A global namespace.
        """
        global gparse
        if gparse is None: import gparse
        
        self.__name__=expr
        expr=translate(expr,nltosp)
        self.expr=expr
        self.globals=globals

        co=compile(expr,'<string>','eval')

        names=list(co.co_names)

        # Check for valid names, disallowing names that begin with '_' or
        # 'manage'. This is a DC specific rule and probably needs to be
        # made customizable!
        for name in names:
            if name[:1]=='_' and name not in ('_', '_vars', '_getattr'):
                raise TypeError, 'illegal name used in expression'
                
        used={}

        i=0
        code=co.co_code
        l=len(code)
        LOAD_NAME=101   
        HAVE_ARGUMENT=90        
        def HAS_ARG(op): ((op) >= HAVE_ARGUMENT)
        while(i < l):
            c=ord(code[i])
            if c==LOAD_NAME:
                name=names[ord(code[i+1])+256*ord(code[i+2])]
                used[name]=1
                i=i+3           
            elif c >= HAVE_ARGUMENT: i=i+3
            else: i=i+1
        
        self.code=gparse.compile(expr,'<string>','eval')
        self.used=tuple(used.keys())

    def eval(self, mapping):
        d={'_vars': mapping}
        code=self.code
        globals=self.globals
        for name in self.used:
            try: d[name]=mapping.getitem(name,0)
            except KeyError:
                if name=='_getattr':
                    d['__builtins__']=globals
                    exec compiled_getattr in d

        return eval(code,globals,d)

    def __call__(self, **kw):
        return eval(self.code, self.globals, kw)

compiled_getattr=compile(
    'def _getattr(o,n): return __guarded_getattr__(_vars,o,n)',
    '<string>','exec')