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

Avoid python3 error with no stdin #460

Merged
merged 1 commit into from
Oct 22, 2021
Merged

Conversation

mattaezell
Copy link
Contributor

Under python3, sys.stdin is None if there is no stdin attached to the process:

$ python3 -c 'import sys; assert not sys.stdin.isatty()' 0<&-
Traceback (most recent call last):
  File "<string>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'isatty'

Originally reported at https://bugs.schedmd.com/show_bug.cgi?id=11021

@thiell
Copy link
Collaborator

thiell commented Mar 5, 2021

Great finding @mattaezell! I will review your patch ASAP. We should include the fix in 1.8.4.

@thiell
Copy link
Collaborator

thiell commented Mar 5, 2021

It's interesting. I can't find any documentation to explain when sys.stdin can be None in the Python documentation. Looks like other developers are facing the same problem with Python 3: https://stackoverflow.com/questions/17458728/when-is-sys-stdin-none-in-python/17459258
I guess we have no choice but checking whether it is defined, like you're doing in your patch indeed. We'll need to find a way to make a test too.

@thiell
Copy link
Collaborator

thiell commented Mar 5, 2021

It's set to None here:
https://hg.python.org/cpython/file/dbceba88b96e/Python/pythonrun.c#l1201

    /* Under some conditions stdin, stdout and stderr may not be connected
     * and fileno() may point to an invalid file descriptor. For example
     * GUI apps don't have valid standard streams by default.
     */

@thiell
Copy link
Collaborator

thiell commented Mar 5, 2021

Ah! It is actually documented under sys.__stdin__ in the Python 3 documentation. It was not documented in Python 2.

Note: Under some conditions stdin, stdout and stderr as well as the original values stdin, stdout and stderr can be None. It is usually the case for Windows GUI apps that aren’t connected to a console and Python apps started with pythonw.

lib/ClusterShell/Gateway.py Outdated Show resolved Hide resolved
lib/ClusterShell/CLI/Clush.py Outdated Show resolved Hide resolved
@thiell thiell added this to the 1.8.4 milestone Mar 5, 2021
@mattaezell mattaezell force-pushed the stdin_null branch 2 times, most recently from 17602f5 to f96feed Compare March 5, 2021 20:20
@mattaezell mattaezell requested a review from thiell March 5, 2021 20:23
Copy link
Collaborator

@thiell thiell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks better :)

lib/ClusterShell/Gateway.py Outdated Show resolved Hide resolved
@thiell thiell merged commit f53a60a into cea-hpc:master Oct 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants