From 2f300db2529106970e3c3de0d7602e2d233d9620 Mon Sep 17 00:00:00 2001 From: Bob Farrell Date: Wed, 25 Jun 2008 17:49:47 +0100 Subject: [PATCH] help() now works just the same as vanilla help() --- CHANGELOG | 9 +++++++++ bpython/_internal.py | 40 +++++++++++++++++++++++++++++++++++++--- 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 3aefd78..66efa84 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,12 @@ +v0.6.2 +====== +The help() now works (as far as I can see) exactly the same +as the vanilla help() in the regular interpreter. I copied some +code from pydoc.py to make it handle the special cases, e.g. +help('keywords') +help('modules') +etc. + v0.6.1 ====== Somehow it escaped my attention that the list window was never diff --git a/bpython/_internal.py b/bpython/_internal.py index fa220da..8fdabd4 100644 --- a/bpython/_internal.py +++ b/bpython/_internal.py @@ -1,23 +1,57 @@ import pydoc import textwrap import sys +import cStringIO # window has to be a global so that the main bpython.py can load it and # alter its state and share it with the interpreter being used for the # actual user input, I couldn't think of another way of doing this. window = None -def _help( query ): +def _help( obj ): """Wrapper for the regular help() function but with a ghetto PAGER since curses + less = :( + As per the vanilla help(), this function special-cases for str, + so you can do help('isinstance') or help(isinstance) and get the + same result. """ + io = cStringIO.StringIO() doc = pydoc.TextDoc() + helper = pydoc.Helper( None, io ) + rows, columns = window.getmaxyx() rows -= 3 columns -= 1 - output = doc.document( query ) + output = None + +# Copied and pasted from Lib/pydoc.py and fiddled with +# so it works fine with bpython. As far as I can tell +# the bpython help is no compliant with the vanilla help. +# Please let me know if you find this to be untrue. + if type(obj) is type(''): + if obj == 'help': helper.intro() + elif obj == 'keywords': helper.listkeywords() + elif obj == 'topics': helper.listtopics() + elif obj == 'modules': helper.listmodules() + elif obj[:8] == 'modules ': + helper.listmodules(split(obj)[1]) + elif obj in helper.keywords: helper.showtopic(obj) + elif obj in helper.topics: helper.showtopic(obj) + elif obj: output = doc.document( eval(obj) ) +####################### + + else: + output = doc.document( obj ) + if not output: + output = "No help found for %s" % obj + return + + if output is None: + output = io.getvalue() + io.close() + if not output: - output = "No help found for %s" % query + return output = output.replace('\t', ' ')