Skip to content

Commit

Permalink
reformat
Browse files Browse the repository at this point in the history
  • Loading branch information
loechel committed Mar 21, 2018
1 parent 28d0ca8 commit 3014a28
Show file tree
Hide file tree
Showing 39 changed files with 582 additions and 534 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,4 @@ pyvenv.cfg
coverage.xml
docs/doctrees
docs/html
.ropeproject/
7 changes: 3 additions & 4 deletions src/RestrictedPython/Eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,15 @@ def prepRestrictedCode(self):
result = compile_restricted_eval(self.expr, '<string>')
if result.errors:
raise SyntaxError(result.errors[0])

self.used = tuple(result.used_names)
self.rcode = result.code

def prepUnrestrictedCode(self):
if self.ucode is None:
exp_node = compile(
self.expr,
'<string>',
'eval',
ast.PyCF_ONLY_AST)
self.expr, '<string>', 'eval', ast.PyCF_ONLY_AST
)

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

Expand Down
47 changes: 25 additions & 22 deletions src/RestrictedPython/Guards.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
'slice',
'str',
'tuple',
'zip'
'zip',
]

_safe_exceptions = [
Expand Down Expand Up @@ -108,21 +108,12 @@
]

if _compat.IS_PY2:
_safe_names.extend([
'basestring',
'cmp',
'long',
'unichr',
'unicode',
'xrange',
])
_safe_exceptions.extend([
'StandardError',
])
_safe_names.extend(
['basestring', 'cmp', 'long', 'unichr', 'unicode', 'xrange']
)
_safe_exceptions.extend(['StandardError'])
else:
_safe_names.extend([
'__build_class__', # needed to define new classes
])
_safe_names.extend(['__build_class__']) # needed to define new classes

for name in _safe_names:
safe_builtins[name] = getattr(builtins, name)
Expand Down Expand Up @@ -191,35 +182,43 @@

def _write_wrapper():
# Construct the write wrapper class

def _handler(secattr, error_msg):
# Make a class method.

def handler(self, *args):
try:
f = getattr(self.ob, secattr)
except AttributeError:
raise TypeError(error_msg)

f(*args)

return handler

class Wrapper(object):

def __init__(self, ob):
self.__dict__['ob'] = ob

__setitem__ = _handler(
'__guarded_setitem__',
'object does not support item or slice assignment')
'object does not support item or slice assignment',
)

__delitem__ = _handler(
'__guarded_delitem__',
'object does not support item or slice assignment')
'object does not support item or slice assignment',
)

__setattr__ = _handler(
'__guarded_setattr__',
'attribute-less object (assign or del)')
'__guarded_setattr__', 'attribute-less object (assign or del)'
)

__delattr__ = _handler(
'__guarded_delattr__',
'attribute-less object (assign or del)')
'__guarded_delattr__', 'attribute-less object (assign or del)'
)

return Wrapper


Expand All @@ -234,8 +233,10 @@ def guard(ob):
# handle their own write security.
if type(ob) in safetypes or hasattr(ob, '_guarded_writes'):
return ob

# Hand the object to the Wrapper instance, then return the instance.
return Wrapper(ob)

return guard


Expand Down Expand Up @@ -265,7 +266,9 @@ def safer_getattr(object, name, getattr=getattr):
"""
if isinstance(object, _compat.basestring) and name == 'format':
raise NotImplementedError(
'Using format() on a %s is not safe.' % object.__class__.__name__)
'Using format() on a %s is not safe.' % object.__class__.__name__
)

return getattr(object, name)


Expand Down
5 changes: 5 additions & 0 deletions src/RestrictedPython/Limits.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,16 @@ def limited_range(iFirst, *args):
iStart, iEnd, iStep = iFirst, args[0], args[1]
else:
raise AttributeError('range() requires 1-3 int arguments')

if iStep == 0:
raise ValueError('zero step for range()')

iLen = int((iEnd - iStart) / iStep)
if iLen < 0:
iLen = 0
if iLen >= RANGELIMIT:
raise ValueError('range() too large')

return range(iStart, iEnd, iStep)


Expand All @@ -41,6 +44,7 @@ def limited_range(iFirst, *args):
def limited_list(seq):
if isinstance(seq, str):
raise TypeError('cannot convert string to list')

return list(seq)


Expand All @@ -50,6 +54,7 @@ def limited_list(seq):
def limited_tuple(seq):
if isinstance(seq, str):
raise TypeError('cannot convert string to tuple')

return tuple(seq)


Expand Down
2 changes: 2 additions & 0 deletions src/RestrictedPython/Utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

try:
import DateTime

utility_builtins['DateTime'] = DateTime.DateTime # pragma: no cover
except ImportError:
pass
Expand All @@ -38,6 +39,7 @@ def same_type(arg1, *args):
for arg in args:
if getattr(arg, '__class__', type(arg)) is not t:
return 0

return 1


Expand Down
Loading

0 comments on commit 3014a28

Please sign in to comment.