From bebfba0f08b3198cee2f1652393f032af4bf2016 Mon Sep 17 00:00:00 2001 From: Dulsara Date: Fri, 2 Feb 2024 22:35:46 +0530 Subject: [PATCH] Fix: fileno error google colab (#729) (#1156) Instead of using a devnull just made a dummy class with a 'write()' method that does nothing. --- llama_cpp/_utils.py | 38 ++++++++------------------------------ 1 file changed, 8 insertions(+), 30 deletions(-) diff --git a/llama_cpp/_utils.py b/llama_cpp/_utils.py index 4a10647..4990c11 100644 --- a/llama_cpp/_utils.py +++ b/llama_cpp/_utils.py @@ -4,9 +4,9 @@ import sys import sys from typing import Any, Dict -# Avoid "LookupError: unknown encoding: ascii" when open() called in a destructor -outnull_file = open(os.devnull, "w") -errnull_file = open(os.devnull, "w") +class NullDevice(): + def write(self, s): + pass class suppress_stdout_stderr(object): # NOTE: these must be "saved" here to avoid exceptions when using @@ -21,41 +21,19 @@ class suppress_stdout_stderr(object): def __enter__(self): if self.disable: return self - - # Check if sys.stdout and sys.stderr have fileno method - if not hasattr(self.sys.stdout, 'fileno') or not hasattr(self.sys.stderr, 'fileno'): - return self # Return the instance without making changes - - self.old_stdout_fileno_undup = self.sys.stdout.fileno() - self.old_stderr_fileno_undup = self.sys.stderr.fileno() - - self.old_stdout_fileno = self.os.dup(self.old_stdout_fileno_undup) - self.old_stderr_fileno = self.os.dup(self.old_stderr_fileno_undup) - self.old_stdout = self.sys.stdout self.old_stderr = self.sys.stderr - self.os.dup2(outnull_file.fileno(), self.old_stdout_fileno_undup) - self.os.dup2(errnull_file.fileno(), self.old_stderr_fileno_undup) - - self.sys.stdout = outnull_file - self.sys.stderr = errnull_file + self.sys.stdout = NullDevice() + self.sys.stderr = NullDevice() return self def __exit__(self, *_): if self.disable: return - - # Check if sys.stdout and sys.stderr have fileno method - if hasattr(self.sys.stdout, 'fileno') and hasattr(self.sys.stderr, 'fileno'): - self.sys.stdout = self.old_stdout - self.sys.stderr = self.old_stderr - - self.os.dup2(self.old_stdout_fileno, self.old_stdout_fileno_undup) - self.os.dup2(self.old_stderr_fileno, self.old_stderr_fileno_undup) - - self.os.close(self.old_stdout_fileno) - self.os.close(self.old_stderr_fileno) + + self.sys.stdout = self.old_stdout + self.sys.stderr = self.old_stderr class MetaSingleton(type):