Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NullWriter has no attribute 'closed' #3503

Closed
kristjanvalur opened this issue May 9, 2018 · 2 comments · Fixed by #7217
Closed

NullWriter has no attribute 'closed' #3503

kristjanvalur opened this issue May 9, 2018 · 2 comments · Fixed by #7217

Comments

@kristjanvalur
Copy link

This is similar to issue #1883
It is triggered when using the "click" library in a PyInstaller --noconsole application.

A workaround is as follows:

# 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.

@bersbersbers
Copy link
Contributor

Came across basically the same issue with reconfigure:

AttributeError: 'NullWriter' object has no attribute 'reconfigure'

Honestly, I find the practice of overwriting sys.stdout/sys.stderr in

# 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()

quite questionable. The Python docs say that the two are "file objects" and "regular text files" (source), the APIs of which are defined in the io module (source). Replacing these objects with a class not derived from TextIOWrapper, TextIOBase or at least IOBase is a recipe for incompatibilities such as #1883 or the ones reported here.

Regarding the above workaround, I'd probably prefer a more focused approach using

if not hasattr(sys.stdout, "closed"):
    ...

@rokm
Copy link
Member

rokm commented Oct 26, 2022

Honestly, I find the practice of overwriting sys.stdout/sys.stderr in

# 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()

quite questionable.

I second that sentiment. Judging by the surrounding comments, this was added as a work-around for an issue from 15 years ago (http://bugs.python.org/issue1415), which has been addressed in python 3.0. So I think it is time to get rid of the NullWriter. I'm quite sure this will trip some user code that fails to check if sys.stderr/sys.stdout are None before accessing it, but that's the behavior they would get in pythonw.exe, as well, and should handle it themselves however they see it fit (e.g., by installing their own equivalent file-like object).

@rokm rokm closed this as completed in #7217 Nov 7, 2022
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 6, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants