Skip to content

Commit 0394002

Browse files
author
Bob Farrell
committed
help() now works just the same as vanilla help()
committer: Bob Farrell <bob@keys.(none)>
1 parent d56215b commit 0394002

2 files changed

Lines changed: 46 additions & 3 deletions

File tree

‎CHANGELOG‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
v0.6.2
2+
======
3+
The help() now works (as far as I can see) exactly the same
4+
as the vanilla help() in the regular interpreter. I copied some
5+
code from pydoc.py to make it handle the special cases, e.g.
6+
help('keywords')
7+
help('modules')
8+
etc.
9+
110
v0.6.1
211
======
312
Somehow it escaped my attention that the list window was never

‎bpython/_internal.py‎

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,57 @@
11
import pydoc
22
import textwrap
33
import sys
4+
import cStringIO
45

56
# window has to be a global so that the main bpython.py can load it and
67
# alter its state and share it with the interpreter being used for the
78
# actual user input, I couldn't think of another way of doing this.
89
window = None
910

10-
def _help( query ):
11+
def _help( obj ):
1112
"""Wrapper for the regular help() function but with a ghetto
1213
PAGER since curses + less = :(
14+
As per the vanilla help(), this function special-cases for str,
15+
so you can do help('isinstance') or help(isinstance) and get the
16+
same result.
1317
"""
18+
io = cStringIO.StringIO()
1419
doc = pydoc.TextDoc()
20+
helper = pydoc.Helper( None, io )
21+
1522
rows, columns = window.getmaxyx()
1623
rows -= 3
1724
columns -= 1
18-
output = doc.document( query )
25+
output = None
26+
27+
# Copied and pasted from Lib/pydoc.py and fiddled with
28+
# so it works fine with bpython. As far as I can tell
29+
# the bpython help is no compliant with the vanilla help.
30+
# Please let me know if you find this to be untrue.
31+
if type(obj) is type(''):
32+
if obj == 'help': helper.intro()
33+
elif obj == 'keywords': helper.listkeywords()
34+
elif obj == 'topics': helper.listtopics()
35+
elif obj == 'modules': helper.listmodules()
36+
elif obj[:8] == 'modules ':
37+
helper.listmodules(split(obj)[1])
38+
elif obj in helper.keywords: helper.showtopic(obj)
39+
elif obj in helper.topics: helper.showtopic(obj)
40+
elif obj: output = doc.document( eval(obj) )
41+
#######################
42+
43+
else:
44+
output = doc.document( obj )
45+
if not output:
46+
output = "No help found for %s" % obj
47+
return
48+
49+
if output is None:
50+
output = io.getvalue()
51+
io.close()
52+
1953
if not output:
20-
output = "No help found for %s" % query
54+
return
2155

2256
output = output.replace('\t', ' ')
2357

0 commit comments

Comments
 (0)