Spaces:
Runtime error
Runtime error
Allowing capturing and returning output
Browse filesFiles changed (2)
63 unmodified lines64656667686963 unmodified lines**`allow_underscore_variable_names`**: (`bool`): Whether or not to allow the use of variable names starting with an underscore. Defaults to False, as it's considered [harmful](https://stackoverflow.com/questions/1301346/what-is-the-meaning-of-a-single-and-a-double-underscore-before-an-object-name).As the new arguments are optional, this could be used as a drop-in replacement for `code_eval`.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.63 unmodified lines6465666768697071727363 unmodified lines**`allow_underscore_variable_names`**: (`bool`): Whether or not to allow the use of variable names starting with an underscore. Defaults to False, as it's considered [harmful](https://stackoverflow.com/questions/1301346/what-is-the-meaning-of-a-single-and-a-double-underscore-before-an-object-name).**`return_output`**: (`bool`): Whether or not to return the output of the code. Defaults to False.**`output_variable`**: (`str`): The name of the variable to return the output of. Defaults to `'output'`.As the new arguments are optional, this could be used as a drop-in replacement for `code_eval`.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.
262 unmodified lines26326426526626726881 unmodified lines35035135235335435535615 unmodified lines37237337437537637737842 unmodified lines4214224234244254264279 unmodified lines43743843944044144272 unmodified lines5155165175185195205215225235245255268 unmodified lines53553653753853954063 unmodified lines604605606607608609610611612613614615262 unmodified linesallowed_imports: an optional list of string, modules the tested code is allowed to importallow_str_format: a bool indicating whether to allow the use of str.format() in the tested codeallow_underscore_variable_names: a bool indicating whether to allow the use of underscore variable names in the tested codeReturns:pass_at_k: dict with pass rates for each k81 unmodified linesuse_safe_builtins: bool = True, use_limited_builtins: bool = True, use_utility_builtins: bool = True,additional_globals: Optional[Dict[str, Any]] = None, additional_locals: Optional[Dict[str, Any]] = None,allowed_imports: Optional[List[str]] = None, allow_str_format: bool = False,allow_underscore_variable_names: bool = False):"""Returns the scores"""if os.getenv("HF_ALLOW_CODE_EVAL", 0) != "1":15 unmodified linestest_program, timeout, task_id, completion_id[task_id],use_safe_builtins, use_limited_builtins, use_utility_builtins,additional_globals, additional_locals,allowed_imports, allow_str_format, allow_underscore_variable_names)future = executor.submit(_check_correctness, *args)futures.append(future)42 unmodified linesuse_safe_builtins: bool = True, use_limited_builtins: bool = True, use_utility_builtins: bool = True,additional_globals: Optional[Dict[str, Any]] = None, additional_locals: Optional[Dict[str, Any]] = None,allowed_imports: Optional[List[str]] = None, allow_str_format: bool = False,allow_underscore_variable_names: bool = False):"""Evaluates the functional correctness of a completion by running the testsuite provided in the problem.9 unmodified linesuse_safe_builtins, use_limited_builtins, use_utility_builtins,additional_globals, additional_locals,allowed_imports, allow_str_format, allow_underscore_variable_names,)p = multiprocessing.Process(target=_unsafe_execute, args=args)p.start()72 unmodified linesprint(*objects, **kwargs)def _unsafe_execute(check_program, result, timeout,use_safe_builtins: bool = True, use_limited_builtins: bool = True, use_utility_builtins: bool = True,additional_globals: Optional[Dict[str, Any]] = None, additional_locals: Optional[Dict[str, Any]] = None,allowed_imports: Optional[List[str]] = None, allow_str_format: bool = False,allow_underscore_variable_names: bool = False):with create_tempdir():8 unmodified lines# Disable functionalities that can make destructive changes to the test.reliability_guard()# Run program.try:builtins = {}63 unmodified lineswith swallow_io():policy_class = AllowAugmentedAssignAndUnderscoreVariableNamesRestrictingTransformer if allow_underscore_variable_names else AllowAugmentedAssignRestrictingTransformerwith time_limit(timeout):byte_code = compile_restricted(check_program, filename="<model output>", mode="exec", policy=policy_class)exec(byte_code, exec_globals, additional_locals)result.append("passed")except EOFError:result.append("EOF error")except TimeoutException:262 unmodified lines26326426526626726826927081 unmodified lines35235335435535635735815 unmodified lines37437537637737837938038142 unmodified lines4244254264274284294309 unmodified lines44044144244344444544672 unmodified lines5195205215225235245255265275285298 unmodified lines53853954054154254354454554663 unmodified lines610611612613614615616617618619620621622623624262 unmodified linesallowed_imports: an optional list of string, modules the tested code is allowed to importallow_str_format: a bool indicating whether to allow the use of str.format() in the tested codeallow_underscore_variable_names: a bool indicating whether to allow the use of underscore variable names in the tested codereturn_output: a bool indicating whether to return the output of the tested codeoutput_variable: a string indicating the name of the variable to return if return_output is TrueReturns:pass_at_k: dict with pass rates for each k81 unmodified linesuse_safe_builtins: bool = True, use_limited_builtins: bool = True, use_utility_builtins: bool = True,additional_globals: Optional[Dict[str, Any]] = None, additional_locals: Optional[Dict[str, Any]] = None,allowed_imports: Optional[List[str]] = None, allow_str_format: bool = False,allow_underscore_variable_names: bool = False, return_output: bool = False, output_variable: str = "output"):"""Returns the scores"""if os.getenv("HF_ALLOW_CODE_EVAL", 0) != "1":15 unmodified linestest_program, timeout, task_id, completion_id[task_id],use_safe_builtins, use_limited_builtins, use_utility_builtins,additional_globals, additional_locals,allowed_imports, allow_str_format, allow_underscore_variable_names,return_output, output_variable,)future = executor.submit(_check_correctness, *args)futures.append(future)42 unmodified linesuse_safe_builtins: bool = True, use_limited_builtins: bool = True, use_utility_builtins: bool = True,additional_globals: Optional[Dict[str, Any]] = None, additional_locals: Optional[Dict[str, Any]] = None,allowed_imports: Optional[List[str]] = None, allow_str_format: bool = False,allow_underscore_variable_names: bool = False, return_output: bool = False, output_variable: str = "output"):"""Evaluates the functional correctness of a completion by running the testsuite provided in the problem.9 unmodified linesuse_safe_builtins, use_limited_builtins, use_utility_builtins,additional_globals, additional_locals,allowed_imports, allow_str_format, allow_underscore_variable_names,return_output, output_variable)p = multiprocessing.Process(target=_unsafe_execute, args=args)p.start()72 unmodified linesprint(*objects, **kwargs)def _unsafe_execute(check_program, result, timeout,use_safe_builtins: bool = True, use_limited_builtins: bool = True, use_utility_builtins: bool = True,additional_globals: Optional[Dict[str, Any]] = None, additional_locals: Optional[Dict[str, Any]] = None,allowed_imports: Optional[List[str]] = None, allow_str_format: bool = False,allow_underscore_variable_names: bool = False, return_output: bool = False, output_variable: str = "output"):with create_tempdir():8 unmodified lines# Disable functionalities that can make destructive changes to the test.reliability_guard()if return_output and additional_locals is None:additional_locals = {}# Run program.try:builtins = {}63 unmodified lineswith swallow_io():policy_class = AllowAugmentedAssignAndUnderscoreVariableNamesRestrictingTransformer if allow_underscore_variable_names else AllowAugmentedAssignRestrictingTransformerwith time_limit(timeout):byte_code = compile_restricted(check_program, filename="<model output>", mode="exec", policy=policy_class)exec(byte_code, exec_globals, additional_locals)if return_output:result.append(additional_locals[output_variable])else:result.append("passed")except EOFError:result.append("EOF error")except TimeoutException:
README.md+4
restrictedpython_code_eval.py+17-8