This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: help-method crashes if sys.stdin is None
Type: crash Stage: resolved
Components: IO Versions: Python 3.5
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: amaury.forgeotdarc, bdettmer, benjamin.peterson, jesstess, palm.kevin, python-dev, roxane
Priority: normal Keywords: patch

Created on 2011-03-29 09:26 by palm.kevin, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
no-stdin.patch amaury.forgeotdarc, 2011-03-29 12:27 review
issue11709.patch bdettmer, 2014-06-07 21:10 review
issue11709.patch bdettmer, 2014-06-07 21:51 review
issue11709.patch bdettmer, 2014-06-07 22:32 review
issue11709.patch bdettmer, 2014-06-08 03:09 review
Messages (10)
msg132474 - (view) Author: Palm Kevin (palm.kevin) Date: 2011-03-29 09:26
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:
 - either by raising a clear error message indicating that help cannot be displayed because Python is executing in a non-interactive mode
 - either by simply printing documentation to stdout (like this: "print(sys.__doc__)")
msg132480 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2011-03-29 12:27
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?
msg132482 - (view) Author: Palm Kevin (palm.kevin) Date: 2011-03-29 12:36
I embed Python into an existing, external C application. In addition to this I extend Python to access that app from Python.
I do never set explicitly set the standard input to NULL. I guess that the external C application does this...
msg217241 - (view) Author: Jessica McKellar (jesstess) * (Python triager) Date: 2014-04-27 06:41
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 `help()` and `help("a")` don't exercise this code path)

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?
msg219974 - (view) Author: B D (bdettmer) * Date: 2014-06-07 21:10
added unit test for this behavior with roxane. verified that the updated patch applies cleanly, passes make patch check, and unit tests all pass.
msg219984 - (view) Author: B D (bdettmer) * Date: 2014-06-07 21:51
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.
msg219990 - (view) Author: B D (bdettmer) * Date: 2014-06-07 22:32
removed comments.
msg219996 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2014-06-07 23:07
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.
msg220013 - (view) Author: B D (bdettmer) * Date: 2014-06-08 03:09
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.
msg220014 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2014-06-08 03:17
New changeset baca52bb5c74 by Benjamin Peterson in branch '3.4':
make sure the builtin help function doesn't fail when sys.stdin is not a valid file (closes #11709)
http://hg.python.org/cpython/rev/baca52bb5c74

New changeset 1a9c07880a15 by Benjamin Peterson in branch '2.7':
make sure the builtin help function doesn't fail when sys.stdin is not a valid file (closes #11709)
http://hg.python.org/cpython/rev/1a9c07880a15

New changeset 3bbb8cb45f58 by Benjamin Peterson in branch 'default':
merge 3.4 (#11709)
http://hg.python.org/cpython/rev/3bbb8cb45f58
History
Date User Action Args
2022-04-11 14:57:15adminsetgithub: 55918
2014-06-08 03:17:43python-devsetstatus: open -> closed

nosy: + python-dev
messages: + msg220014

resolution: fixed
stage: test needed -> resolved
2014-06-08 03:09:08bdettmersetfiles: + issue11709.patch

messages: + msg220013
2014-06-07 23:07:34benjamin.petersonsetnosy: + benjamin.peterson
messages: + msg219996
2014-06-07 22:32:14bdettmersetfiles: + issue11709.patch

messages: + msg219990
2014-06-07 21:51:31bdettmersetfiles: + issue11709.patch

messages: + msg219984
2014-06-07 21:10:34bdettmersetfiles: + issue11709.patch
nosy: + bdettmer, roxane
messages: + msg219974

2014-04-27 06:41:54jesstesssetversions: + Python 3.5, - Python 3.2
nosy: + jesstess

messages: + msg217241

stage: patch review -> test needed
2011-03-29 12:36:19palm.kevinsetmessages: + msg132482
2011-03-29 12:27:36amaury.forgeotdarcsetfiles: + no-stdin.patch

nosy: + amaury.forgeotdarc
messages: + msg132480

keywords: + patch
stage: patch review
2011-03-29 09:26:41palm.kevinsettype: crash
components: + IO
versions: + Python 3.2
2011-03-29 09:26:03palm.kevincreate