guydav commited on
Commit
563f5f1
1 Parent(s): 0aadf05

Added a range with a higher limit

Browse files
Files changed (1) hide show
  1. restrictedpython_code_eval.py +28 -0
restrictedpython_code_eval.py CHANGED
@@ -62,6 +62,34 @@ def limited_tuple(seq=None):
62
  limited_builtins['tuple'] = limited_tuple
63
 
64
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65
  def safer_getattr_allowing_string_format(object, name, default=None, getattr=getattr):
66
  """Getattr implementation allowing str.format(), but preventing access to
67
  private attributes.
 
62
  limited_builtins['tuple'] = limited_tuple
63
 
64
 
65
+ def limited_range(iFirst, *args):
66
+ # limited range function from Martijn Pieters
67
+ RANGELIMIT = 10000
68
+ if not len(args):
69
+ iStart, iEnd, iStep = 0, iFirst, 1
70
+ elif len(args) == 1:
71
+ iStart, iEnd, iStep = iFirst, args[0], 1
72
+ elif len(args) == 2:
73
+ iStart, iEnd, iStep = iFirst, args[0], args[1]
74
+ else:
75
+ raise AttributeError('range() requires 1-3 int arguments')
76
+ if iStep == 0:
77
+ raise ValueError('zero step for range()')
78
+ iLen = int((iEnd - iStart) / iStep)
79
+ if iLen < 0:
80
+ iLen = 0
81
+ if iLen >= RANGELIMIT:
82
+ raise ValueError(
83
+ 'To be created range() object would be too large, '
84
+ 'in RestrictedPython we only allow {limit} '
85
+ 'elements in a range.'.format(limit=str(RANGELIMIT)),
86
+ )
87
+ return range(iStart, iEnd, iStep)
88
+
89
+
90
+ limited_builtins['range'] = limited_range
91
+
92
+
93
  def safer_getattr_allowing_string_format(object, name, default=None, getattr=getattr):
94
  """Getattr implementation allowing str.format(), but preventing access to
95
  private attributes.