prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  NullWriter has no attribute 'closed'
  This is similar to issue #1883
  It is triggered when using the "click" library in a PyInstaller --noconsole application.
  
  A workaround is as follows:
  ```python
  # fixup somw problems from pyinstaller
  if "NullWriter" in str(type(sys.stdout)):
      sys.stdout.closed = sys.stderr.closed = False
  ```
  I suggest adding a class attribute, closed=False to fix this.
  You may want to add the "errors" and "newlines" attributes as well, see the python docs.
  
  
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `PyInstaller/loader/pyiboot01_bootstrap.py`
  Content:
  ```
  1 #-----------------------------------------------------------------------------
  2 # Copyright (c) 2005-2022, PyInstaller Development Team.
  3 #
  4 # Distributed under the terms of the GNU General Public License (version 2
  5 # or later) with exception for distributing the bootloader.
  6 #
  7 # The full license is in the file COPYING.txt, distributed with this software.
  8 #
  9 # SPDX-License-Identifier: (GPL-2.0-or-later WITH Bootloader-exception)
  10 #-----------------------------------------------------------------------------
  11 
  12 #-- Start bootstrap process
  13 # Only python built-in modules can be used.
  14 
  15 import sys
  16 
  17 import pyimod02_importers
  18 
  19 # Extend Python import machinery by adding PEP302 importers to sys.meta_path.
  20 pyimod02_importers.install()
  21 
  22 #-- Bootstrap process is complete.
  23 # We can use other python modules (e.g. os)
  24 
  25 import os  # noqa: E402
  26 
  27 # Let other python modules know that the code is running in frozen mode.
  28 if not hasattr(sys, 'frozen'):
  29     sys.frozen = True
  30 
  31 # sys._MEIPASS is now set in the bootloader. Hooray.
  32 
  33 # Python 3 C-API function Py_SetPath() resets sys.prefix to empty string. Python 2 was using PYTHONHOME for sys.prefix.
  34 # Let's do the same for Python 3.
  35 sys.prefix = sys._MEIPASS
  36 sys.exec_prefix = sys.prefix
  37 
  38 # Python 3.3+ defines also sys.base_prefix. Let's set them too.
  39 sys.base_prefix = sys.prefix
  40 sys.base_exec_prefix = sys.exec_prefix
  41 
  42 # Some packages behave differently when running inside virtual environment. E.g., IPython tries to append path
  43 # VIRTUAL_ENV to sys.path. For the frozen app we want to prevent this behavior.
  44 VIRTENV = 'VIRTUAL_ENV'
  45 if VIRTENV in os.environ:
  46     # On some platforms (e.g., AIX) 'os.unsetenv()' is unavailable and deleting the var from os.environ does not
  47     # delete it from the environment.
  48     os.environ[VIRTENV] = ''
  49     del os.environ[VIRTENV]
  50 
  51 # Ensure sys.path contains absolute paths. Otherwise, import of other python modules will fail when current working
  52 # directory is changed by the frozen application.
  53 python_path = []
  54 for pth in sys.path:
  55     python_path.append(os.path.abspath(pth))
  56     sys.path = python_path
  57 
  58 
  59 # Implement workaround for prints in non-console mode. In non-console mode (with "pythonw"), print randomly fails with
  60 # "[errno 9] Bad file descriptor" when the printed text is flushed (e.g., buffer full); this is because the sys.stdout
  61 # object is bound to an invalid file descriptor.
  62 # Python 3000 has a fix for it (http://bugs.python.org/issue1415), but we feel that a workaround in PyInstaller is a
  63 # good thing, because most people first encounter this problem with PyInstaller as they do not usually run their code
  64 # with "pythonw" (and it is difficult to debug, anyway).
  65 class NullWriter:
  66     softspace = 0
  67     encoding = 'UTF-8'
  68 
  69     def write(*args):
  70         pass
  71 
  72     def flush(*args):
  73         pass
  74 
  75     # Some packages are checking if stdout/stderr is available (e.g., youtube-dl). For details, see #1883.
  76     def isatty(self):
  77         return False
  78 
  79 
  80 # sys.stdout/err is None in GUI mode on Windows.
  81 if sys.stdout is None:
  82     sys.stdout = NullWriter()
  83 if sys.stderr is None:
  84     sys.stderr = NullWriter()
  85 
  86 # At least on Windows, Python seems to hook up the codecs on this import, so it is not enough to just package up all
  87 # the encodings.
  88 #
  89 # It was also reported that without 'encodings' module, the frozen executable fails to load in some configurations:
  90 # http://www.pyinstaller.org/ticket/651
  91 #
  92 # Importing 'encodings' module in a run-time hook is not enough, since some run-time hooks require this module, and the
  93 # order of running the code from the run-time hooks is not defined.
  94 try:
  95     import encodings  # noqa: F401
  96 except ImportError:
  97     pass
  98 
  99 # In the Python interpreter 'warnings' module is imported when 'sys.warnoptions' is not empty. Mimic this behavior.
  100 if sys.warnoptions:
  101     import warnings  # noqa: F401
  102 
  103 # Install the hooks for ctypes
  104 import pyimod03_ctypes  # noqa: E402
  105 
  106 pyimod03_ctypes.install()
  107 
  108 # Install the hooks for pywin32 (Windows only)
  109 if sys.platform.startswith('win'):
  110     import pyimod04_pywin32
  111     pyimod04_pywin32.install()
  112 
  113 # Make .eggs and zipfiles available at runtime
  114 d = "eggs"
  115 d = os.path.join(sys._MEIPASS, d)
  116 # Test if the 'eggs' directory exists. This allows us to opportunistically include this script into the packaged exe,
  117 # even if no eggs were found when packaging the program. (Which may be a use-case, see issue #653).
  118 if os.path.isdir(d):
  119     for fn in os.listdir(d):
  120         sys.path.append(os.path.join(d, fn))
  121
  ```
  --- END FILES ---
  
  Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.
  
  Here is an example:
  
  ```diff
  diff --git a/examples/server_async.py b/examples/server_async.py
  --- a/examples/server_async.py
  +++ b/examples/server_async.py
  @@ -313,4 +313,4 @@
   
   
   if __name__ == "__main__":
  -    asyncio.run(run_async_server("."), debug=True)
  +    asyncio.run(run_async_server(), debug=True)
  diff --git a/examples/server_sync.py b/examples/server_sync.py
  --- a/examples/server_sync.py
  +++ b/examples/server_sync.py
  @@ -313,5 +313,5 @@
   
   
   if __name__ == "__main__":
  -    server = run_sync_server(".")
  +    server = run_sync_server()
       server.shutdown()
  
  ```
  
golden_diff: diff --git a/PyInstaller/loader/pyiboot01_bootstrap.py b/PyInstaller/loader/pyiboot01_bootstrap.py
  --- a/PyInstaller/loader/pyiboot01_bootstrap.py
  +++ b/PyInstaller/loader/pyiboot01_bootstrap.py
  @@ -55,34 +55,6 @@
       python_path.append(os.path.abspath(pth))
       sys.path = python_path
   
  -
  -# Implement workaround for prints in non-console mode. In non-console mode (with "pythonw"), print randomly fails with
  -# "[errno 9] Bad file descriptor" when the printed text is flushed (e.g., buffer full); this is because the sys.stdout
  -# object is bound to an invalid file descriptor.
  -# Python 3000 has a fix for it (http://bugs.python.org/issue1415), but we feel that a workaround in PyInstaller is a
  -# good thing, because most people first encounter this problem with PyInstaller as they do not usually run their code
  -# with "pythonw" (and it is difficult to debug, anyway).
  -class NullWriter:
  -    softspace = 0
  -    encoding = 'UTF-8'
  -
  -    def write(*args):
  -        pass
  -
  -    def flush(*args):
  -        pass
  -
  -    # Some packages are checking if stdout/stderr is available (e.g., youtube-dl). For details, see #1883.
  -    def isatty(self):
  -        return False
  -
  -
  -# sys.stdout/err is None in GUI mode on Windows.
  -if sys.stdout is None:
  -    sys.stdout = NullWriter()
  -if sys.stderr is None:
  -    sys.stderr = NullWriter()
  -
   # At least on Windows, Python seems to hook up the codecs on this import, so it is not enough to just package up all
   # the encodings.
   #
  
verification_info: {"golden_diff": "diff --git a/PyInstaller/loader/pyiboot01_bootstrap.py b/PyInstaller/loader/pyiboot01_bootstrap.py\n--- a/PyInstaller/loader/pyiboot01_bootstrap.py\n+++ b/PyInstaller/loader/pyiboot01_bootstrap.py\n@@ -55,34 +55,6 @@\n     python_path.append(os.path.abspath(pth))\n     sys.path = python_path\n \n-\n-# Implement workaround for prints in non-console mode. In non-console mode (with \"pythonw\"), print randomly fails with\n-# \"[errno 9] Bad file descriptor\" when the printed text is flushed (e.g., buffer full); this is because the sys.stdout\n-# object is bound to an invalid file descriptor.\n-# Python 3000 has a fix for it (http://bugs.python.org/issue1415), but we feel that a workaround in PyInstaller is a\n-# good thing, because most people first encounter this problem with PyInstaller as they do not usually run their code\n-# with \"pythonw\" (and it is difficult to debug, anyway).\n-class NullWriter:\n-    softspace = 0\n-    encoding = 'UTF-8'\n-\n-    def write(*args):\n-        pass\n-\n-    def flush(*args):\n-        pass\n-\n-    # Some packages are checking if stdout/stderr is available (e.g., youtube-dl). For details, see #1883.\n-    def isatty(self):\n-        return False\n-\n-\n-# sys.stdout/err is None in GUI mode on Windows.\n-if sys.stdout is None:\n-    sys.stdout = NullWriter()\n-if sys.stderr is None:\n-    sys.stderr = NullWriter()\n-\n # At least on Windows, Python seems to hook up the codecs on this import, so it is not enough to just package up all\n # the encodings.\n #\n", "issue": "NullWriter has no attribute 'closed'\nThis is similar to issue #1883\r\nIt is triggered when using the \"click\" library in a PyInstaller --noconsole application.\r\n\r\nA workaround is as follows:\r\n```python\r\n# fixup somw problems from pyinstaller\r\nif \"NullWriter\" in str(type(sys.stdout)):\r\n    sys.stdout.closed = sys.stderr.closed = False\r\n```\r\nI suggest adding a class attribute, closed=False to fix this.\r\nYou may want to add the \"errors\" and \"newlines\" attributes as well, see the python docs.\r\n\r\n\n", "before_files": [{"content": "#-----------------------------------------------------------------------------\n# Copyright (c) 2005-2022, PyInstaller Development Team.\n#\n# Distributed under the terms of the GNU General Public License (version 2\n# or later) with exception for distributing the bootloader.\n#\n# The full license is in the file COPYING.txt, distributed with this software.\n#\n# SPDX-License-Identifier: (GPL-2.0-or-later WITH Bootloader-exception)\n#-----------------------------------------------------------------------------\n\n#-- Start bootstrap process\n# Only python built-in modules can be used.\n\nimport sys\n\nimport pyimod02_importers\n\n# Extend Python import machinery by adding PEP302 importers to sys.meta_path.\npyimod02_importers.install()\n\n#-- Bootstrap process is complete.\n# We can use other python modules (e.g. os)\n\nimport os  # noqa: E402\n\n# Let other python modules know that the code is running in frozen mode.\nif not hasattr(sys, 'frozen'):\n    sys.frozen = True\n\n# sys._MEIPASS is now set in the bootloader. Hooray.\n\n# Python 3 C-API function Py_SetPath() resets sys.prefix to empty string. Python 2 was using PYTHONHOME for sys.prefix.\n# Let's do the same for Python 3.\nsys.prefix = sys._MEIPASS\nsys.exec_prefix = sys.prefix\n\n# Python 3.3+ defines also sys.base_prefix. Let's set them too.\nsys.base_prefix = sys.prefix\nsys.base_exec_prefix = sys.exec_prefix\n\n# Some packages behave differently when running inside virtual environment. E.g., IPython tries to append path\n# VIRTUAL_ENV to sys.path. For the frozen app we want to prevent this behavior.\nVIRTENV = 'VIRTUAL_ENV'\nif VIRTENV in os.environ:\n    # On some platforms (e.g., AIX) 'os.unsetenv()' is unavailable and deleting the var from os.environ does not\n    # delete it from the environment.\n    os.environ[VIRTENV] = ''\n    del os.environ[VIRTENV]\n\n# Ensure sys.path contains absolute paths. Otherwise, import of other python modules will fail when current working\n# directory is changed by the frozen application.\npython_path = []\nfor pth in sys.path:\n    python_path.append(os.path.abspath(pth))\n    sys.path = python_path\n\n\n# Implement workaround for prints in non-console mode. In non-console mode (with \"pythonw\"), print randomly fails with\n# \"[errno 9] Bad file descriptor\" when the printed text is flushed (e.g., buffer full); this is because the sys.stdout\n# object is bound to an invalid file descriptor.\n# Python 3000 has a fix for it (http://bugs.python.org/issue1415), but we feel that a workaround in PyInstaller is a\n# good thing, because most people first encounter this problem with PyInstaller as they do not usually run their code\n# with \"pythonw\" (and it is difficult to debug, anyway).\nclass NullWriter:\n    softspace = 0\n    encoding = 'UTF-8'\n\n    def write(*args):\n        pass\n\n    def flush(*args):\n        pass\n\n    # Some packages are checking if stdout/stderr is available (e.g., youtube-dl). For details, see #1883.\n    def isatty(self):\n        return False\n\n\n# sys.stdout/err is None in GUI mode on Windows.\nif sys.stdout is None:\n    sys.stdout = NullWriter()\nif sys.stderr is None:\n    sys.stderr = NullWriter()\n\n# At least on Windows, Python seems to hook up the codecs on this import, so it is not enough to just package up all\n# the encodings.\n#\n# It was also reported that without 'encodings' module, the frozen executable fails to load in some configurations:\n# http://www.pyinstaller.org/ticket/651\n#\n# Importing 'encodings' module in a run-time hook is not enough, since some run-time hooks require this module, and the\n# order of running the code from the run-time hooks is not defined.\ntry:\n    import encodings  # noqa: F401\nexcept ImportError:\n    pass\n\n# In the Python interpreter 'warnings' module is imported when 'sys.warnoptions' is not empty. Mimic this behavior.\nif sys.warnoptions:\n    import warnings  # noqa: F401\n\n# Install the hooks for ctypes\nimport pyimod03_ctypes  # noqa: E402\n\npyimod03_ctypes.install()\n\n# Install the hooks for pywin32 (Windows only)\nif sys.platform.startswith('win'):\n    import pyimod04_pywin32\n    pyimod04_pywin32.install()\n\n# Make .eggs and zipfiles available at runtime\nd = \"eggs\"\nd = os.path.join(sys._MEIPASS, d)\n# Test if the 'eggs' directory exists. This allows us to opportunistically include this script into the packaged exe,\n# even if no eggs were found when packaging the program. (Which may be a use-case, see issue #653).\nif os.path.isdir(d):\n    for fn in os.listdir(d):\n        sys.path.append(os.path.join(d, fn))\n", "path": "PyInstaller/loader/pyiboot01_bootstrap.py"}], "after_files": [{"content": "#-----------------------------------------------------------------------------\n# Copyright (c) 2005-2022, PyInstaller Development Team.\n#\n# Distributed under the terms of the GNU General Public License (version 2\n# or later) with exception for distributing the bootloader.\n#\n# The full license is in the file COPYING.txt, distributed with this software.\n#\n# SPDX-License-Identifier: (GPL-2.0-or-later WITH Bootloader-exception)\n#-----------------------------------------------------------------------------\n\n#-- Start bootstrap process\n# Only python built-in modules can be used.\n\nimport sys\n\nimport pyimod02_importers\n\n# Extend Python import machinery by adding PEP302 importers to sys.meta_path.\npyimod02_importers.install()\n\n#-- Bootstrap process is complete.\n# We can use other python modules (e.g. os)\n\nimport os  # noqa: E402\n\n# Let other python modules know that the code is running in frozen mode.\nif not hasattr(sys, 'frozen'):\n    sys.frozen = True\n\n# sys._MEIPASS is now set in the bootloader. Hooray.\n\n# Python 3 C-API function Py_SetPath() resets sys.prefix to empty string. Python 2 was using PYTHONHOME for sys.prefix.\n# Let's do the same for Python 3.\nsys.prefix = sys._MEIPASS\nsys.exec_prefix = sys.prefix\n\n# Python 3.3+ defines also sys.base_prefix. Let's set them too.\nsys.base_prefix = sys.prefix\nsys.base_exec_prefix = sys.exec_prefix\n\n# Some packages behave differently when running inside virtual environment. E.g., IPython tries to append path\n# VIRTUAL_ENV to sys.path. For the frozen app we want to prevent this behavior.\nVIRTENV = 'VIRTUAL_ENV'\nif VIRTENV in os.environ:\n    # On some platforms (e.g., AIX) 'os.unsetenv()' is unavailable and deleting the var from os.environ does not\n    # delete it from the environment.\n    os.environ[VIRTENV] = ''\n    del os.environ[VIRTENV]\n\n# Ensure sys.path contains absolute paths. Otherwise, import of other python modules will fail when current working\n# directory is changed by the frozen application.\npython_path = []\nfor pth in sys.path:\n    python_path.append(os.path.abspath(pth))\n    sys.path = python_path\n\n# At least on Windows, Python seems to hook up the codecs on this import, so it is not enough to just package up all\n# the encodings.\n#\n# It was also reported that without 'encodings' module, the frozen executable fails to load in some configurations:\n# http://www.pyinstaller.org/ticket/651\n#\n# Importing 'encodings' module in a run-time hook is not enough, since some run-time hooks require this module, and the\n# order of running the code from the run-time hooks is not defined.\ntry:\n    import encodings  # noqa: F401\nexcept ImportError:\n    pass\n\n# In the Python interpreter 'warnings' module is imported when 'sys.warnoptions' is not empty. Mimic this behavior.\nif sys.warnoptions:\n    import warnings  # noqa: F401\n\n# Install the hooks for ctypes\nimport pyimod03_ctypes  # noqa: E402\n\npyimod03_ctypes.install()\n\n# Install the hooks for pywin32 (Windows only)\nif sys.platform.startswith('win'):\n    import pyimod04_pywin32\n    pyimod04_pywin32.install()\n\n# Make .eggs and zipfiles available at runtime\nd = \"eggs\"\nd = os.path.join(sys._MEIPASS, d)\n# Test if the 'eggs' directory exists. This allows us to opportunistically include this script into the packaged exe,\n# even if no eggs were found when packaging the program. (Which may be a use-case, see issue #653).\nif os.path.isdir(d):\n    for fn in os.listdir(d):\n        sys.path.append(os.path.join(d, fn))\n", "path": "PyInstaller/loader/pyiboot01_bootstrap.py"}]}