hymenjj/llama-cpp-python-prebuilt
0
1import os2import sys3 4from typing import Any, Dict5 6# Avoid "LookupError: unknown encoding: ascii" when open() called in a destructor7outnull_file = open(os.devnull, "w")8errnull_file = open(os.devnull, "w")9 10STDOUT_FILENO = 111STDERR_FILENO = 212 13 14class suppress_stdout_stderr(object):15 # NOTE: these must be "saved" here to avoid exceptions when using16 # this context manager inside of a __del__ method17 sys = sys18 os = os19 20 def __init__(self, disable: bool = True):21 self.disable = disable22 23 # Oddly enough this works better than the contextlib version24 def __enter__(self):25 if self.disable:26 return self27 28 self.old_stdout_fileno_undup = STDOUT_FILENO29 self.old_stderr_fileno_undup = STDERR_FILENO30 31 self.old_stdout_fileno = self.os.dup(self.old_stdout_fileno_undup)32 self.old_stderr_fileno = self.os.dup(self.old_stderr_fileno_undup)33 34 self.old_stdout = self.sys.stdout35 self.old_stderr = self.sys.stderr36 37 self.os.dup2(outnull_file.fileno(), self.old_stdout_fileno_undup)38 self.os.dup2(errnull_file.fileno(), self.old_stderr_fileno_undup)39 40 self.sys.stdout = outnull_file41 self.sys.stderr = errnull_file42 return self43 44 def __exit__(self, *_):45 if self.disable:46 return47 48 # Check if sys.stdout and sys.stderr have fileno method49 self.sys.stdout = self.old_stdout50 self.sys.stderr = self.old_stderr51 52 self.os.dup2(self.old_stdout_fileno, self.old_stdout_fileno_undup)53 self.os.dup2(self.old_stderr_fileno, self.old_stderr_fileno_undup)54 55 self.os.close(self.old_stdout_fileno)56 self.os.close(self.old_stderr_fileno)57 58 59class MetaSingleton(type):60 """61 Metaclass for implementing the Singleton pattern.62 """63 64 _instances: Dict[type, Any] = {}65 66 def __call__(cls, *args: Any, **kwargs: Any) -> Any:67 if cls not in cls._instances:68 cls._instances[cls] = super(MetaSingleton, cls).__call__(*args, **kwargs)69 return cls._instances[cls]70 71 72class Singleton(object, metaclass=MetaSingleton):73 """74 Base class for implementing the Singleton pattern.75 """76 77 def __init__(self):78 super(Singleton, self).__init__()79 