Skip to content

sys.displayhook is ignored #169

Open
@benburrill

Description

@benburrill

ptpython essentially reimplements the behavior of sys.displayhook. It would be nice if ptpython used sys.displayhook somehow, so that like the standard Python repl, users can define their own display hooks.
Defining display hooks is useful in a lot of ways. I use it in my PYTHONSTARTUP because, unlike __repr__, you don't have to worry about a hook that only affects a certain class getting called when you don't want it to (like when you do >>> globals()).

Simply calling sys.displayhook in the _execute method has a few drawbacks:

  • ptpython adds syntax highlighting to the output, which is a bit difficult to do because sys.displayhook simply prints to stdout instead of returning. You could redirect stdout or something, but that's gross.
  • The standard display hook sets _ on builtins instead of on your locals dict. This probably isn't a huge deal though, since you can always add to the locals dict in addition to whatever sys.displayhook wants to do.

I think the best solution might be to set both sys.__displayhook__ and sys.displayhook to a function that does the highlighted display output and inserts _ and _{n} into the currently running ptpython repl. Then you would call sys.displayhook in the _execute method.
This would work with my use case where I have a display hook which allows objects to implement a method which essentially overrides what sys.__displayhook__ does. This solution would also let users completely override what the ptpython display hook does if they wanted to.

It might also make sense to give a similar treatment to excepthook.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions