

# This code is adapted from OpenAI's release
# https://github.com/openai/human-eval/blob/master/human_eval/execution.py

import contextlib
import faulthandler
import io
import multiprocessing
import os
import platform
import signal
import tempfile

def 							__SCREAMING_SNAKE_CASE							(				lowerCAmelCase:  str  ,      lowerCAmelCase:  Tuple  ,      lowerCAmelCase:  Optional[Any]  ,      lowerCAmelCase:  Optional[Any] )   ->       Union[str, Any]:
				_UpperCAmelCase       :		List[str] 		=    multiprocessing.Manager()
				_UpperCAmelCase       :		Optional[Any] 		=    manager.list()

				_UpperCAmelCase       :		int 		=    multiprocessing.Process(target=lowerCAmelCase  ,      args=(check_program, result, timeout) )
				p.start()
				p.join(timeout=timeout + 1 )
				if p.is_alive():
								p.kill()

				if not result:
								result.append("timed out" )

				return {
				    "task_id": task_id,
				    "passed": result[0] == "passed",
				    "result": result[0],
				    "completion_id": completion_id,
				}

def 							__SCREAMING_SNAKE_CASE							(				lowerCAmelCase:  Optional[int]  ,      lowerCAmelCase:  int  ,      lowerCAmelCase:  str )   ->       Tuple:
				with create_tempdir():
								# These system calls are needed when cleaning up tempdir.
								import os
								import shutil

								_UpperCAmelCase       :		Dict 		=    shutil.rmtree
								_UpperCAmelCase       :		Tuple 		=    os.rmdir
								_UpperCAmelCase       :		int 		=    os.chdir

								# Disable functionalities that can make destructive changes to the test.
								reliability_guard()

								# Run program.
								try:
												_UpperCAmelCase       :		Union[str, Any] 		=    {}
												with swallow_io():
																with time_limit(lowerCAmelCase ):
																				exec(lowerCAmelCase  ,      lowerCAmelCase )
												result.append("passed" )
								except TimeoutException:
												result.append("timed out" )
								except BaseException as e:
												result.append(F'failed: {e}' )

								# Needed for cleaning up.
								_UpperCAmelCase       :		str 		=    rmtree
								_UpperCAmelCase       :		int 		=    rmdir
								_UpperCAmelCase       :		List[str] 		=    chdir

@contextlib.contextmanager
def 							__SCREAMING_SNAKE_CASE							(				lowerCAmelCase:  str )   ->       List[str]:
				def signal_handler(lowerCAmelCase:  Union[str, Any]  ,      lowerCAmelCase:  Union[str, Any] ):
								raise TimeoutException("Timed out!" )

				signal.setitimer(signal.ITIMER_REAL  ,      lowerCAmelCase )
				signal.signal(signal.SIGALRM  ,      lowerCAmelCase )
				try:
								yield
				finally:
								signal.setitimer(signal.ITIMER_REAL  ,      0 )

@contextlib.contextmanager
def 							__SCREAMING_SNAKE_CASE							(				)   ->       Optional[Any]:
				_UpperCAmelCase       :		List[str] 		=    WriteOnlyStringIO()
				with contextlib.redirect_stdout(lowerCAmelCase ):
								with contextlib.redirect_stderr(lowerCAmelCase ):
												with redirect_stdin(lowerCAmelCase ):
																yield

@contextlib.contextmanager
def 							__SCREAMING_SNAKE_CASE							(				)   ->       Optional[int]:
				with tempfile.TemporaryDirectory() as dirname:
								with chdir(lowerCAmelCase ):
												yield dirname

class 				a  (	UpperCAmelCase				):
						pass







class 				a  (	io.StringIO				):


						def        _UpperCAmelCase				(       self						,					*A_						,					**A_	):







										'''simple docstring'''


										raise OSError


						def        _UpperCAmelCase				(       self						,					*A_						,					**A_	):







										'''simple docstring'''


										raise OSError


						def        _UpperCAmelCase				(       self						,					*A_						,					**A_	):







										'''simple docstring'''


										raise OSError





						def        _UpperCAmelCase				(       self						,					*A_						,					**A_	):







										'''simple docstring'''


										return False







class 				a  (	contextlib._RedirectStream				):  # type: ignore
						_lowercase    						=			"stdin"







@contextlib.contextmanager
def 							__SCREAMING_SNAKE_CASE							(				lowerCAmelCase:  Union[str, Any] )   ->       int:
				if root == ".":
								yield
								return
				_UpperCAmelCase       :		Union[str, Any] 		=    os.getcwd()
				os.chdir(lowerCAmelCase )
				try:
								yield
				except BaseException as exc:
								raise exc
				finally:
								os.chdir(lowerCAmelCase )





def 							__SCREAMING_SNAKE_CASE							(				lowerCAmelCase:  List[Any]=None )   ->       Union[str, Any]:

				if maximum_memory_bytes is not None:
								import resource

								resource.setrlimit(resource.RLIMIT_AS  ,      (maximum_memory_bytes, maximum_memory_bytes) )
								resource.setrlimit(resource.RLIMIT_DATA  ,      (maximum_memory_bytes, maximum_memory_bytes) )
								if not platform.uname().system == "Darwin":
												resource.setrlimit(resource.RLIMIT_STACK  ,      (maximum_memory_bytes, maximum_memory_bytes) )

				faulthandler.disable()

				import builtins

				_UpperCAmelCase       :		Union[str, Any] 		=    None
				_UpperCAmelCase       :		Tuple 		=    None

				import os

				_UpperCAmelCase       :		Optional[int] 		=    "1"

				_UpperCAmelCase       :		List[str] 		=    None
				_UpperCAmelCase       :		int 		=    None
				_UpperCAmelCase       :		Tuple 		=    None
				_UpperCAmelCase       :		Tuple 		=    None
				_UpperCAmelCase       :		Optional[int] 		=    None
				_UpperCAmelCase       :		List[str] 		=    None
				_UpperCAmelCase       :		str 		=    None
				_UpperCAmelCase       :		Optional[Any] 		=    None
				_UpperCAmelCase       :		str 		=    None
				_UpperCAmelCase       :		Optional[int] 		=    None
				_UpperCAmelCase       :		Optional[int] 		=    None
				_UpperCAmelCase       :		List[str] 		=    None
				_UpperCAmelCase       :		Any 		=    None
				_UpperCAmelCase       :		Union[str, Any] 		=    None
				_UpperCAmelCase       :		Tuple 		=    None
				_UpperCAmelCase       :		Any 		=    None
				_UpperCAmelCase       :		str 		=    None
				_UpperCAmelCase       :		Optional[Any] 		=    None
				_UpperCAmelCase       :		Optional[Any] 		=    None
				_UpperCAmelCase       :		List[str] 		=    None
				_UpperCAmelCase       :		Dict 		=    None
				_UpperCAmelCase       :		int 		=    None
				_UpperCAmelCase       :		Dict 		=    None
				_UpperCAmelCase       :		Optional[Any] 		=    None
				_UpperCAmelCase       :		Tuple 		=    None
				_UpperCAmelCase       :		Tuple 		=    None
				_UpperCAmelCase       :		List[str] 		=    None

				import shutil

				_UpperCAmelCase       :		Optional[int] 		=    None
				_UpperCAmelCase       :		Any 		=    None
				_UpperCAmelCase       :		Optional[Any] 		=    None

				import subprocess

				_UpperCAmelCase       :		Dict 		=    None  # type: ignore

				_UpperCAmelCase       :		Optional[Any] 		=    None

				import sys

				_UpperCAmelCase       :		List[str] 		=    None
				_UpperCAmelCase       :		Optional[Any] 		=    None
				_UpperCAmelCase       :		str 		=    None
				_UpperCAmelCase       :		str 		=    None
				_UpperCAmelCase       :		Tuple 		=    None





import logging
import os

from .state import PartialState

class 				a  (	logging.LoggerAdapter				):


						@staticmethod
						def        _UpperCAmelCase				(       A_	):







										'''simple docstring'''


										_UpperCAmelCase       :		Tuple 		=    PartialState()
										return not main_process_only or (main_process_only and state.is_main_process)





						def        _UpperCAmelCase				(       self						,					A_						,					A_						,					*A_						,					**A_	):







										'''simple docstring'''


										if PartialState._shared_state == {}:
														raise RuntimeError(
														    "You must initialize the accelerate state by calling either `PartialState()` or `Accelerator()` before using the logging utility."	)
										_UpperCAmelCase       :		Tuple 		=    kwargs.pop("main_process_only"						,					A_	)
										_UpperCAmelCase       :		int 		=    kwargs.pop("in_order"						,					A_	)

										if self.isEnabledFor(A_	):
														if self._should_log(A_	):
																		_UpperCAmelCase		,							_UpperCAmelCase       :		Optional[int] 		=    self.process(A_						,					A_	)
																		self.logger.log(A_						,					A_						,					*A_						,					**A_	)

														elif in_order:
																		_UpperCAmelCase       :		Dict 		=    PartialState()
																		for i in range(state.num_processes	):
																						if i == state.process_index:
																										_UpperCAmelCase		,							_UpperCAmelCase       :		Union[str, Any] 		=    self.process(A_						,					A_	)
																										self.logger.log(A_						,					A_						,					*A_						,					**A_	)
																						state.wait_for_everyone()







def 							__SCREAMING_SNAKE_CASE							(				lowerCAmelCase:  str  ,      lowerCAmelCase:  str = None )   ->       List[Any]:
				if log_level is None:
								_UpperCAmelCase       :		List[str] 		=    os.environ.get("ACCELERATE_LOG_LEVEL"  ,      lowerCAmelCase )
				_UpperCAmelCase       :		str 		=    logging.getLogger(lowerCAmelCase )
				if log_level is not None:
								logger.setLevel(log_level.upper() )
								logger.root.setLevel(log_level.upper() )
				return MultiProcessAdapter(lowerCAmelCase  ,      {} )


