Skip to content

Mention sys.displayhook in code module docs and the compile builtin docs #56612

Open

Description

@tebeka
mannequin
BPO 12403
Nosy @tebeka, @bitdancer, @skirpichev, @normanlorrain, @iritkatriel
PRs
  • gh-56612: Mention sys.displayhook in code module docs and the compile builtin docs #26217
  • Files
  • p1346072775.diff
  • 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 = None
    created_at = <Date 2011-06-24.23:55:44.415>
    labels = ['easy', 'type-bug', '3.9', '3.10', '3.11', 'docs']
    title = 'Mention sys.displayhook in code module docs and the compile builtin docs'
    updated_at = <Date 2021-05-18.20:21:10.279>
    user = 'https://github.com/tebeka'

    bugs.python.org fields:

    activity = <Date 2021-05-18.20:21:10.279>
    actor = 'Norman Lorrain'
    assignee = 'docs@python'
    closed = False
    closed_date = None
    closer = None
    components = ['Documentation']
    creation = <Date 2011-06-24.23:55:44.415>
    creator = 'tebeka'
    dependencies = []
    files = ['27014']
    hgrepos = []
    issue_num = 12403
    keywords = ['patch', 'easy']
    message_count = 6.0
    messages = ['139004', '139074', '139077', '139097', '169199', '391566']
    nosy_count = 8.0
    nosy_names = ['tebeka', 'r.david.murray', 'santoso.wijaya', 'docs@python', 'aliles', 'Sergey.Kirpichev', 'Norman Lorrain', 'iritkatriel']
    pr_nums = ['26217']
    priority = 'normal'
    resolution = None
    stage = 'patch review'
    status = 'open'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue12403'
    versions = ['Python 3.9', 'Python 3.10', 'Python 3.11']

    Linked PRs

    Activity

    1. tebeka commented on Jun 24, 2011

      tebekamannequin
      MannequinAuthor

      Currently, code.InteractiveConsole lets your override "write" which is the error stream. Allow overriding of "normal" (stdout) writing as well.
      This will enable to spawn interpreters into custom stdin/stdout.

      See twisted.manhole for example, very useful.

    2. added
      type-feature
      on Jun 24, 2011
    3. bitdancer commented on Jun 25, 2011

      @bitdancer
      Member

      Well, the code is being executed by an exec call on a code object that was compiled with the 'single' flag, which is what causes non-None values to get "printed". The compile docs aren't clear on how "printed" is implemented, but the answer is that it calls sys.displayhook. So the way to control that part of the output is to assign your own function to sys.displayhook. None of this is documented, and it should be (it took me a while to figure it out, and I had a suspicions about how it worked), so I'm changing this to a documentation bug.

      However, you mention in the other ticket that are trying to do the interactive interpreter trick through a socket. For that application I think you want to ignore both the write method and sys.displayhook, and instead directly patch sys.stdout and sys.stderr. Otherwise you'd also miss output sent to those destinations via print statements or writing directly to the sys objects, which would confuse the user of your interface since the in the normal interactive prompt those show up on the console.

    4. added
      docs
      on Jun 25, 2011
    5. changed the title [-]Allow overriding of writing to stdout in code.InteractiveConsole[/-] [+]Mention sys.displayhook in code module docs and the compile builtin docs[/+] on Jun 25, 2011
    6. added
      type-bug
      and removed
      type-feature
      on Jun 25, 2011
    7. tebeka commented on Jun 25, 2011

      tebekamannequin
      MannequinAuthor

      Yeah, I though about using dup2 from stdout/stderr to the socket. However this means I can connect only one client at a time. Which was an issue I was trying to avoid. Didn't think about print statements though ...

      Thanks.

    8. bitdancer commented on Jun 25, 2011

      @bitdancer
      Member

      Well, sys.stdout and sys.stderr don't have to be real file objects. You could substitute a file-like object (one that implements the methods that get called during the interpretation loop) that does your multiplexing. That kind of trick is one of the things that makes Python so much fun to work with :)

    9. aliles commented on Aug 27, 2012

      alilesmannequin
      Mannequin

      I've submitted a patch which adds a section to the code module's documentation on overriding console output. It attempts to catalogue when sys.stderr, sys.excepthook and sys.displayhook are used to print console output.

    10. iritkatriel commented on Apr 22, 2021

      @iritkatriel
      Member

      The patch for the code module doc needs to be converted into a github PR.

      A new patch is needed for the compile builtin documentation: https://docs.python.org/3/library/functions.html#compile
      This could be added at the end of the third paragraph, where it says that in 'single' mode non-None results are printed - clarify that sys.displayhook is used for that.

      (See these unit tests for examples:

      class DisplayHookTest(unittest.TestCase):
      )

    11. added
      3.9 (EOL)
      3.10 (EOL)
      3.11
      on Apr 22, 2021
    12. transferred this issue fromon Apr 10, 2022
    13. removed
      3.11
      3.10 (EOL)
      3.9 (EOL)
      on Dec 3, 2026
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      docstype-bug

      Projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions