Esempio n. 1
0
    def __init__(self, f):
        self._func = f

    def __call__(self, *args, **kw):
        return self._func(*args, **kw)


for name, f in list(safe_builtins.items()) + list(utility_builtins.items()):
    if type(f) is functype:
        f = NotBindable(f)
    setattr(TemplateDict, name, f)

if LIMITED_BUILTINS:
    # Replace certain builtins with limited versions.
    from RestrictedPython.Limits import limited_builtins
    for name, f in limited_builtins.items():
        if type(f) is functype:
            f = NotBindable(f)
        setattr(TemplateDict, name, f)


class StringModuleWrapper(object):
    # Wrap the string module so it can deal with TaintedString strings.

    def __getattr__(self, key):
        attr = getattr(string, key)
        if (isinstance(attr, FunctionType)
                or isinstance(attr, BuiltinFunctionType)):
            return StringFunctionWrapper(attr)
        else:
            return attr
Esempio n. 2
0
class NotBindable:
    # Used to prevent TemplateDict from trying to bind to functions.
    def __init__(self, f):
        self.__call__ = f

d = TemplateDict.__dict__
for name, f in safe_builtins.items() + utility_builtins.items():
    if type(f) is functype:
        d[name] = NotBindable(f)
    else:
        d[name] = f

if LIMITED_BUILTINS:
    # Replace certain builtins with limited versions.
    from RestrictedPython.Limits import limited_builtins
    for name, f in limited_builtins.items():
        if type(f) is functype:
            d[name] = NotBindable(f)
        else:
            d[name] = f

try:
    # Wrap the string module so it can deal with TaintedString strings.
    from ZPublisher.TaintedString import TaintedString
    from types import FunctionType, BuiltinFunctionType, StringType
    import string

    class StringModuleWrapper:
        def __getattr__(self, key):
            attr = getattr(string, key)
            if (isinstance(attr, FunctionType) or