python: Make sure help() doesn't cause hexchat to hang (#2290)

* Make sure `help()` doesn't cause hexchat to hang

Replace `pydoc.help` with a copy of `pydoc.Helper` with an empty
`StringIO` instead of stdin

* Handle BytesIO vs StringIO on 2.7
This commit is contained in:
linuxdaemon 2018-12-27 13:46:02 -06:00 committed by TingPing
parent f7713a6a64
commit a5a727122b

View File

@ -1,6 +1,7 @@
from __future__ import print_function
import os
import pydoc
import sys
from contextlib import contextmanager
import importlib
@ -8,6 +9,12 @@ import signal
import site
import traceback
import weakref
if sys.version_info < (3, 0):
from io import BytesIO as HelpEater
else:
from io import StringIO as HelpEater
from _hexchat_embedded import ffi, lib
if not hasattr(sys, 'argv'):
@ -57,7 +64,6 @@ class Stdout:
self.buffer += string
def isatty(self):
# FIXME: help() locks app despite this?
return False
@ -474,6 +480,7 @@ def _on_plugin_init(plugin_name, plugin_desc, plugin_version, arg, libdir):
hexchat_stdout = Stdout()
sys.stdout = hexchat_stdout
sys.stderr = hexchat_stdout
pydoc.help = pydoc.Helper(HelpEater(), HelpEater())
lib.hexchat_hook_command(lib.ph, b'', 0, lib._on_say_command, ffi.NULL, ffi.NULL)
lib.hexchat_hook_command(lib.ph, b'LOAD', 0, lib._on_load_command, ffi.NULL, ffi.NULL)
@ -505,6 +512,7 @@ def _on_plugin_deinit():
hexchat_stdout = None
sys.stdout = sys.__stdout__
sys.stderr = sys.__stderr__
pydoc.help = pydoc.Helper()
for mod in ('_hexchat', 'hexchat', 'xchat', '_hexchat_embedded'):
try: