Python () online help redirection

I am working on an interactive python shell for an application using Qt. However, I cannot get the online help to redirect. I have this in my python code:

class OutputCatcher: def __init__(self): self.data = '' def write(self, stuff): self.data += stuff sys.stdout = OutputCatcher() 

However, when I run help (), it does not redirect the online help, but simply uploads it to the console where I ran the python script. If I press ctrl + c in the console, it will send it to my OutputCatcher object.

I tried google but couldn't find anything.

+4
source share
2 answers

No need to guess what help does; just read the source.

The built-in help is created on site.py, this is an instance of the _Helper class. When called, it simply passes the call through pydoc.help(...) source for which you will find in pydoc.py.

 class _Helper(object): """Define the built-in 'help'. This is a wrapper around pydoc.help (with a twist). """ def __repr__(self): return "Type help() for interactive help, " \ "or help(object) for help about object." def __call__(self, *args, **kwds): import pydoc return pydoc.help(*args, **kwds) 

pydoc.help is an instance of pydoc.Helper with I / O installed on sys.stdin , sys.stdout , but (and I suspect that this is exactly where you have the problem) it uses the value stdin / stdout for a while, when pydoc is imported, so re-linking them later will have no effect.

I suggest you replace the built-in help instance with your own _Helper class, which creates a new pydoc helper explicitly with any files you need.

+1
source

Help does not just reset to stdout, but interacts with the terminal. It is also never intended to be used outside the shell, so it will not be written to ensure that such things work.

What you are trying to do is implement a terminal, and this is not a trivial task, but perhaps there are existing terminal emulation libraries for Qt. Maybe even written in Python and, of course, with bindings.

+1
source

Source: https://habr.com/ru/post/1335541/


All Articles