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

help-method crashes if sys.stdin is None #55918

Closed
palmkevin mannequin opened this issue Mar 29, 2011 · 10 comments
Closed

help-method crashes if sys.stdin is None #55918

palmkevin mannequin opened this issue Mar 29, 2011 · 10 comments
Labels
topic-IO type-crash A hard crash of the interpreter, possibly with a core dump

Comments

@palmkevin
Copy link
Mannequin

palmkevin mannequin commented Mar 29, 2011

BPO 11709
Nosy @amauryfa, @benjaminp
Files
  • no-stdin.patch
  • issue11709.patch
  • issue11709.patch
  • issue11709.patch
  • issue11709.patch
  • 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:

    assignee = None
    closed_at = <Date 2014-06-08.03:17:43.858>
    created_at = <Date 2011-03-29.09:26:03.502>
    labels = ['expert-IO', 'type-crash']
    title = 'help-method crashes if sys.stdin is None'
    updated_at = <Date 2014-06-08.03:17:43.856>
    user = 'https://bugs.python.org/palmkevin'

    bugs.python.org fields:

    activity = <Date 2014-06-08.03:17:43.856>
    actor = 'python-dev'
    assignee = 'none'
    closed = True
    closed_date = <Date 2014-06-08.03:17:43.858>
    closer = 'python-dev'
    components = ['IO']
    creation = <Date 2011-03-29.09:26:03.502>
    creator = 'palm.kevin'
    dependencies = []
    files = ['21451', '35523', '35527', '35531', '35535']
    hgrepos = []
    issue_num = 11709
    keywords = ['patch']
    message_count = 10.0
    messages = ['132474', '132480', '132482', '217241', '219974', '219984', '219990', '219996', '220013', '220014']
    nosy_count = 7.0
    nosy_names = ['amaury.forgeotdarc', 'benjamin.peterson', 'jesstess', 'palm.kevin', 'python-dev', 'bdettmer', 'roxane']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'crash'
    url = 'https://bugs.python.org/issue11709'
    versions = ['Python 3.5']

    @palmkevin
    Copy link
    Mannequin Author

    palmkevin mannequin commented Mar 29, 2011

    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__)")

    @palmkevin palmkevin mannequin added topic-IO type-crash A hard crash of the interpreter, possibly with a core dump labels Mar 29, 2011
    @amauryfa
    Copy link
    Member

    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?

    @palmkevin
    Copy link
    Mannequin Author

    palmkevin mannequin commented Mar 29, 2011

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

    @jesstess
    Copy link
    Member

    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?

    @bdettmer
    Copy link
    Mannequin

    bdettmer mannequin commented Jun 7, 2014

    added unit test for this behavior with roxane. verified that the updated patch applies cleanly, passes make patch check, and unit tests all pass.

    @bdettmer
    Copy link
    Mannequin

    bdettmer mannequin commented Jun 7, 2014

    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.

    @bdettmer
    Copy link
    Mannequin

    bdettmer mannequin commented Jun 7, 2014

    removed comments.

    @benjaminp
    Copy link
    Contributor

    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.

    @bdettmer
    Copy link
    Mannequin

    bdettmer mannequin commented Jun 8, 2014

    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.

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Jun 8, 2014

    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 bpo-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 bpo-11709)
    http://hg.python.org/cpython/rev/1a9c07880a15

    New changeset 3bbb8cb45f58 by Benjamin Peterson in branch 'default':
    merge 3.4 (bpo-11709)
    http://hg.python.org/cpython/rev/3bbb8cb45f58

    @python-dev python-dev mannequin closed this as completed Jun 8, 2014
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    topic-IO type-crash A hard crash of the interpreter, possibly with a core dump
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants