"""simple docstring"""






# 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 						a_		(      lowerCamelCase      ,    lowerCamelCase      ,    lowerCamelCase      ,    lowerCamelCase ):
				UpperCAmelCase__    =							multiprocessing.Manager()
				UpperCAmelCase__    =							manager.list()

				UpperCAmelCase__    =							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 						a_		(      lowerCamelCase      ,    lowerCamelCase      ,    lowerCamelCase ):
				with create_tempdir():
								# These system calls are needed when cleaning up tempdir.
								import os
								import shutil

								UpperCAmelCase__    =							shutil.rmtree
								UpperCAmelCase__    =							os.rmdir
								UpperCAmelCase__    =							os.chdir

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

								# Run program.
								try:
												UpperCAmelCase__    =							{}
												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__    =							rmtree
								UpperCAmelCase__    =							rmdir
								UpperCAmelCase__    =							chdir



@contextlib.contextmanager
def 						a_		(      lowerCamelCase ):
				def signal_handler(lowerCamelCase      ,    lowerCamelCase ):
								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 						a_		(      ):
				UpperCAmelCase__    =							WriteOnlyStringIO()
				with contextlib.redirect_stdout(lowerCamelCase ):
								with contextlib.redirect_stderr(lowerCamelCase ):
												with redirect_stdin(lowerCamelCase ):
																yield



@contextlib.contextmanager
def 						a_		(      ):
				with tempfile.TemporaryDirectory() as dirname:
								with chdir(lowerCamelCase ):
												yield dirname
class        snake_case (				__UpperCAmelCase       ):




					"""simple docstring"""

					pass






class        snake_case (				io.StringIO       ):




					"""simple docstring"""

					def 	__lowerCAmelCase		(       self     :			Union[str, Any]					,*lowerCamelCase__     :			int					,**lowerCamelCase__     :			Optional[int]    ):
									raise OSError







					def 	__lowerCAmelCase		(       self     :			Any					,*lowerCamelCase__     :			Optional[int]					,**lowerCamelCase__     :			Any    ):
									raise OSError







					def 	__lowerCAmelCase		(       self     :			Dict					,*lowerCamelCase__     :			Optional[Any]					,**lowerCamelCase__     :			List[Any]    ):
									raise OSError







					def 	__lowerCAmelCase		(       self     :			List[Any]					,*lowerCamelCase__     :			Tuple					,**lowerCamelCase__     :			List[str]    ):
									return False






class        snake_case (				contextlib._RedirectStream       ):  # type: ignore




					"""simple docstring"""

					snake_case__       					=				"stdin"






@contextlib.contextmanager
def 						a_		(      lowerCamelCase ):
				if root == ".":
								yield
								return
				UpperCAmelCase__    =							os.getcwd()
				os.chdir(lowerCamelCase )
				try:
								yield
				except BaseException as exc:
								raise exc
				finally:
								os.chdir(lowerCamelCase )







def 						a_		(      lowerCamelCase=None ):

				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__    =							None
				UpperCAmelCase__    =							None

				import os

				UpperCAmelCase__    =							'1'

				UpperCAmelCase__    =							None
				UpperCAmelCase__    =							None
				UpperCAmelCase__    =							None
				UpperCAmelCase__    =							None
				UpperCAmelCase__    =							None
				UpperCAmelCase__    =							None
				UpperCAmelCase__    =							None
				UpperCAmelCase__    =							None
				UpperCAmelCase__    =							None
				UpperCAmelCase__    =							None
				UpperCAmelCase__    =							None
				UpperCAmelCase__    =							None
				UpperCAmelCase__    =							None
				UpperCAmelCase__    =							None
				UpperCAmelCase__    =							None
				UpperCAmelCase__    =							None
				UpperCAmelCase__    =							None
				UpperCAmelCase__    =							None
				UpperCAmelCase__    =							None
				UpperCAmelCase__    =							None
				UpperCAmelCase__    =							None
				UpperCAmelCase__    =							None
				UpperCAmelCase__    =							None
				UpperCAmelCase__    =							None
				UpperCAmelCase__    =							None
				UpperCAmelCase__    =							None
				UpperCAmelCase__    =							None

				import shutil

				UpperCAmelCase__    =							None
				UpperCAmelCase__    =							None
				UpperCAmelCase__    =							None

				import subprocess

				UpperCAmelCase__    =							None  # type: ignore

				UpperCAmelCase__    =							None

				import sys

				UpperCAmelCase__    =							None
				UpperCAmelCase__    =							None
				UpperCAmelCase__    =							None
				UpperCAmelCase__    =							None
				UpperCAmelCase__    =							None




"""simple docstring"""






import os
import sys
from contextlib import contextmanager


# Windows only
if os.name == "nt":
						import ctypes
						import msvcrt  # noqa
						class        snake_case (				ctypes.Structure       ):




											"""simple docstring"""

											snake_case__       					=				[("size", ctypes.c_int), ("visible", ctypes.c_byte)]



def 						a_		(      ):
				if os.name == "nt":
								UpperCAmelCase__    =							CursorInfo()
								UpperCAmelCase__    =							ctypes.windll.kernelaa.GetStdHandle(-1_1 )
								ctypes.windll.kernelaa.GetConsoleCursorInfo(lowerCamelCase      ,    ctypes.byref(lowerCamelCase ) )
								UpperCAmelCase__    =							False
								ctypes.windll.kernelaa.SetConsoleCursorInfo(lowerCamelCase      ,    ctypes.byref(lowerCamelCase ) )
				elif os.name == "posix":
								sys.stdout.write('\033[?25l' )
								sys.stdout.flush()



def 						a_		(      ):
				if os.name == "nt":
								UpperCAmelCase__    =							CursorInfo()
								UpperCAmelCase__    =							ctypes.windll.kernelaa.GetStdHandle(-1_1 )
								ctypes.windll.kernelaa.GetConsoleCursorInfo(lowerCamelCase      ,    ctypes.byref(lowerCamelCase ) )
								UpperCAmelCase__    =							True
								ctypes.windll.kernelaa.SetConsoleCursorInfo(lowerCamelCase      ,    ctypes.byref(lowerCamelCase ) )
				elif os.name == "posix":
								sys.stdout.write('\033[?25h' )
								sys.stdout.flush()







@contextmanager
def 						a_		(      ):
				try:
								hide_cursor()
								yield
				finally:
								show_cursor()



