guydav commited on
Commit
42e1223
1 Parent(s): 7293ac9

Added another default.

Browse files
Files changed (2) hide show
  1. README.md +1 -1
  2. restrictedpython_code_eval.py +28 -0
README.md CHANGED
@@ -62,7 +62,7 @@ In addition, this metric supports three additional arguments, specifying which i
62
 
63
  As the new arguments are optional, this could be used as a drop-in replacement for `code_eval`.
64
 
65
- Additionally, this metric sets several different `globals` if they are not provided as additional globals. The full list of globals set is: `__metaclass__, __name__, _getiter_, _iter_unpack_sequence_, _getitem_, getattr, _write_, _inplacevar_`. See the code for additional details.
66
 
67
  ### Output Values
68
 
 
62
 
63
  As the new arguments are optional, this could be used as a drop-in replacement for `code_eval`.
64
 
65
+ Additionally, this metric sets several different `globals` if they are not provided as additional globals. The full list of globals set is: `__metaclass__, __name__, _getiter_, _iter_unpack_sequence_, _getitem_, getattr, _write_, _inplacevar_, _print_`. See the code for additional details.
66
 
67
  ### Output Values
68
 
restrictedpython_code_eval.py CHANGED
@@ -286,6 +286,31 @@ def _default_write_(obj):
286
  return obj
287
 
288
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
289
  def _unsafe_execute(check_program, result, timeout,
290
  use_safe_builtins: bool = True, use_limited_builtins: bool = True, use_utility_builtins: bool = True,
291
  additional_globals: Optional[Dict[str, Any]] = None, additional_locals: Optional[Dict[str, Any]] = None,
@@ -347,6 +372,9 @@ def _unsafe_execute(check_program, result, timeout,
347
  if '_inplacevar_' not in exec_globals:
348
  exec_globals['_inplacevar_'] = protected_inplacevar # type: ignore
349
 
 
 
 
350
  with swallow_io():
351
  with time_limit(timeout):
352
  byte_code = compile_restricted(check_program, filename="<model output>", mode="exec")
 
286
  return obj
287
 
288
 
289
+ class DefaultPrinter:
290
+ def __init__(self, _getattr_=None, *args, **kwargs):
291
+ self._getattr_ = _getattr_
292
+ self.txt = []
293
+
294
+ self.args = args
295
+ self.kwargs = kwargs
296
+
297
+ def write(self, text):
298
+ self.txt.append(text)
299
+ print(text)
300
+
301
+ def __call__(self):
302
+ return ''.join(self.txt)
303
+
304
+ def _call_print(self, *objects, **kwargs):
305
+ if kwargs.get('file', None) is None:
306
+ kwargs['file'] = self
307
+ else:
308
+ self._getattr_(kwargs['file'], 'write') # type: ignore
309
+
310
+ print(*objects, **kwargs)
311
+
312
+
313
+
314
  def _unsafe_execute(check_program, result, timeout,
315
  use_safe_builtins: bool = True, use_limited_builtins: bool = True, use_utility_builtins: bool = True,
316
  additional_globals: Optional[Dict[str, Any]] = None, additional_locals: Optional[Dict[str, Any]] = None,
 
372
  if '_inplacevar_' not in exec_globals:
373
  exec_globals['_inplacevar_'] = protected_inplacevar # type: ignore
374
 
375
+ if '_print_' not in exec_globals:
376
+ exec_globals['_print_'] = DefaultPrinter # type: ignore
377
+
378
  with swallow_io():
379
  with time_limit(timeout):
380
  byte_code = compile_restricted(check_program, filename="<model output>", mode="exec")