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
help-method crashes if sys.stdin is None #55918
Comments
The interactive help-method provided by python crashes when no stdin-stream is available (sys.stdin == None).
Exception:
Traceback (most recent call last):
File "MyScript", line 4, in <module>
File "C:\Python32\lib\site.py", line 457, in __call__
return pydoc.help(*args, **kwds)
File "C:\Python32\lib\pydoc.py", line 1748, in __call__
self.help(request)
File "C:\Python32\lib\pydoc.py", line 1795, in help
else: doc(request, 'Help on %s:', output=self._output)
File "C:\Python32\lib\pydoc.py", line 1537, in doc
pager(render_doc(thing, title, forceload))
File "C:\Python32\lib\pydoc.py", line 1345, in pager
pager = getpager()
File "C:\Python32\lib\pydoc.py", line 1352, in getpager
if not sys.stdin.isatty() or not sys.stdout.isatty():
AttributeError: 'NoneType' object has no attribute 'isatty' I think that this situation should be handled:
|
The code fails precisely when checking that sys.stdin is a valid terminal. See the attached patch for a fix. Out of curiosity, why did you set sys.stdin to None? |
I embed Python into an existing, external C application. In addition to this I extend Python to access that app from Python. |
Thanks for reporting this, palm.kevin, and thanks for the patch, amaury.forgeotdarc. First, just to be explicit here's a short reproducer: import sys
sys.stdin = None
help(1) (Note that to get to the isatty check you need to provide an argument and it has to be something that has help, so Also, here is where sys.stdin can be set to None: http://hg.python.org/cpython/file/dbceba88b96e/Python/pythonrun.c#l1201 The provided patch fixes the above test case; instead of erroring out with the traceback in the original bug report, the plain pager is used and the help message is printed to stdout. Anyone on the nosy list interested in writing some tests? |
added unit test for this behavior with roxane. verified that the updated patch applies cleanly, passes make patch check, and unit tests all pass. |
added try finally as suggested by berkerpeksag. make patchcheck still works and all test cases still pass. did not use the test.support.swap_attr context manager because it may inhibit readability for those that are not familiar with it. |
removed comments. |
Unfortunately, the test doesn't fail without the fix in, probably because the pager() function replaces itself in the module and thus can only be called once. It might make more sense to just directly test the getpager function with sys.stdin = None. |
I've updated the unit test and have verified that it does fail when the original patch is not included. I also ran make patchcheck again and re-ran all of the tests. This should be good to go. Thanks for your insights, Benjamin. |
New changeset baca52bb5c74 by Benjamin Peterson in branch '3.4': New changeset 1a9c07880a15 by Benjamin Peterson in branch '2.7': New changeset 3bbb8cb45f58 by Benjamin Peterson in branch 'default': |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: